use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class LibvirtRevertSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
final SnapshotObjectTO snapshot = command.getData();
final VolumeObjectTO volume = snapshot.getVolume();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
final DataStoreTO snapshotImageStore = snapshot.getDataStore();
if (!(snapshotImageStore instanceof NfsTO)) {
return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
}
final NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
final String volumePath = volume.getPath();
String snapshotPath = null;
String snapshotRelPath = null;
KvmStoragePool secondaryStoragePool = null;
try {
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolUrl);
final String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotRelPath = snapshot.getPath();
snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
final KvmPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
final KvmStoragePool primaryPool = snapshotDisk.getPool();
if (primaryPool.getType() == StoragePoolType.RBD) {
return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
} else {
final Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
cmd.add("-v", snapshotPath);
cmd.add("-n", snapshotDisk.getName());
cmd.add("-p", snapshotDisk.getPath());
final String result = cmd.execute();
if (result != null) {
s_logger.debug("Failed to revert snaptshot: " + result);
return new Answer(command, false, result);
}
}
return new Answer(command, true, "RevertSnapshotCommand executes successfully");
} catch (final CloudRuntimeException e) {
return new Answer(command, false, e.toString());
}
}
use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class LibvirtDeleteVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final DeleteVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
Domain dm = null;
DomainSnapshot snapshot = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
// only remove this snapshot, not children
snapshot.delete(0);
return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
} catch (final LibvirtException e) {
final String msg = " Delete VM snapshot failed due to " + e.toString();
if (dm == null) {
s_logger.debug("Can not find running vm: " + vmName + ", now we are trying to delete the vm snapshot using qemu-img if the format of root volume is QCOW2");
VolumeObjectTO rootVolume = null;
for (VolumeObjectTO volume : cmd.getVolumeTOs()) {
if (volume.getVolumeType() == Volume.Type.ROOT) {
rootVolume = volume;
break;
}
}
if (rootVolume != null && ImageFormat.QCOW2.equals(rootVolume.getFormat())) {
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) rootVolume.getDataStore();
final KvmPhysicalDisk rootDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), rootVolume.getPath());
final String command = "qemu-img snapshot -l " + rootDisk.getPath() + " | tail -n +3 | awk -F ' ' '{print $2}' | grep ^" + cmd.getTarget().getSnapshotName() + "$";
final String qemu_img_snapshot = Script.runSimpleBashScript(command);
if (qemu_img_snapshot == null) {
s_logger.info("Cannot find snapshot " + cmd.getTarget().getSnapshotName() + " in file " + rootDisk.getPath() + ", return true");
return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
}
int result = Script.runSimpleBashScriptForExitValue("qemu-img snapshot -d " + cmd.getTarget().getSnapshotName() + " " + rootDisk.getPath());
if (result != 0) {
return new DeleteVMSnapshotAnswer(cmd, false, "Delete VM Snapshot Failed due to can not remove snapshot from image file " + rootDisk.getPath() + " : " + result);
} else {
return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
}
}
} else if (snapshot == null) {
s_logger.debug("Can not find vm snapshot " + cmd.getTarget().getSnapshotName() + " on vm: " + vmName + ", return true");
return new DeleteVMSnapshotAnswer(cmd, cmd.getVolumeTOs());
}
s_logger.warn(msg, e);
return new DeleteVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method mount.
protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
final DataTO data = volume.getData();
final Volume.Type type = volume.getType();
if (type == Volume.Type.ISO) {
final TemplateObjectTO iso = (TemplateObjectTO) data;
final DataStoreTO store = iso.getDataStore();
if (store == null) {
// It's a fake iso
return null;
}
// corer case, xenserver pv driver iso
final String templateName = iso.getName();
if (templateName.startsWith("xs-tools")) {
try {
final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
if (vdis.isEmpty()) {
throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
}
return vdis.iterator().next();
} catch (final XenAPIException e) {
throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
}
}
if (!(store instanceof NfsTO)) {
throw new CloudRuntimeException("only support mount iso on nfs");
}
final NfsTO nfsStore = (NfsTO) store;
final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
final int index = isoPath.lastIndexOf("/");
final String mountpoint = isoPath.substring(0, index);
final URI uri;
try {
uri = new URI(mountpoint);
} catch (final URISyntaxException e) {
throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
}
final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
final String isoname = isoPath.substring(index + 1);
final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
if (isoVdi == null) {
throw new CloudRuntimeException("Unable to find ISO " + isoPath);
}
return isoVdi;
} else {
final VolumeObjectTO vol = (VolumeObjectTO) data;
return VDI.getByUuid(conn, vol.getPath());
}
}
use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method createVolume.
@Override
public Answer createVolume(final CreateObjectCommand cmd) {
final DataTO data = cmd.getData();
final VolumeObjectTO volume = (VolumeObjectTO) data;
try {
final Connection conn = hypervisorResource.getConnection();
final SR poolSr = hypervisorResource.getStorageRepository(conn, data.getDataStore().getUuid());
VDI.Record vdir = new VDI.Record();
vdir.nameLabel = volume.getName();
vdir.SR = poolSr;
vdir.type = Types.VdiType.USER;
vdir.virtualSize = volume.getSize();
final VDI vdi;
vdi = VDI.create(conn, vdir);
vdir = vdi.getRecord(conn);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setName(vdir.nameLabel);
newVol.setSize(vdir.virtualSize);
newVol.setPath(vdir.uuid);
return new CreateObjectAnswer(newVol);
} catch (final Exception e) {
s_logger.debug("create volume failed: " + e.toString());
return new CreateObjectAnswer(e.toString());
}
}
use of com.cloud.storage.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method createVolumeFromSnapshot2.
protected Answer createVolumeFromSnapshot2(final CopyCommand cmd) {
try {
final Connection conn = hypervisorResource.getConnection();
final Map<String, String> srcOptions = cmd.getOptions();
final String src_iScsiName = srcOptions.get(DiskTO.IQN);
final String srcStorageHost = srcOptions.get(DiskTO.STORAGE_HOST);
final String srcChapInitiatorUsername = srcOptions.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String srcChapInitiatorSecret = srcOptions.get(DiskTO.CHAP_INITIATOR_SECRET);
final SR srcSr = hypervisorResource.getIscsiSR(conn, src_iScsiName, srcStorageHost, src_iScsiName, srcChapInitiatorUsername, srcChapInitiatorSecret, false);
final Map<String, String> destOptions = cmd.getOptions2();
final String dest_iScsiName = destOptions.get(DiskTO.IQN);
final String destStorageHost = destOptions.get(DiskTO.STORAGE_HOST);
final String destChapInitiatorUsername = destOptions.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String destChapInitiatorSecret = destOptions.get(DiskTO.CHAP_INITIATOR_SECRET);
final SR destSr = hypervisorResource.getIscsiSR(conn, dest_iScsiName, destStorageHost, dest_iScsiName, destChapInitiatorUsername, destChapInitiatorSecret, false);
// there should only be one VDI in this SR
final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
final VDI vdiCopy = srcVdi.copy(conn, destSr);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setSize(vdiCopy.getVirtualSize(conn));
newVol.setPath(vdiCopy.getUuid(conn));
newVol.setFormat(ImageFormat.VHD);
hypervisorResource.removeSR(conn, srcSr);
hypervisorResource.removeSR(conn, destSr);
return new CopyCmdAnswer(newVol);
} catch (final Exception ex) {
s_logger.warn("Failed to copy snapshot to volume: " + ex.toString(), ex);
return new CopyCmdAnswer(ex.getMessage());
}
}
Aggregations