use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class KVMStorageProcessor method deleteVolume.
@Override
public Answer deleteVolume(final DeleteCommand cmd) {
final VolumeObjectTO vol = (VolumeObjectTO) cmd.getData();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
try {
final KVMStoragePool pool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
try {
pool.getPhysicalDisk(vol.getPath());
} catch (final Exception e) {
s_logger.debug("can't find volume: " + vol.getPath() + ", return true");
return new Answer(null);
}
pool.deletePhysicalDisk(vol.getPath(), vol.getFormat());
return new Answer(null);
} catch (final CloudRuntimeException e) {
s_logger.debug("Failed to delete volume: ", e);
return new Answer(null, false, e.toString());
}
}
use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class KVMStorageProcessor method createTemplateFromVolume.
@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final int wait = cmd.getWaitInMillSeconds();
final TemplateObjectTO template = (TemplateObjectTO) destData;
final DataStoreTO imageStore = template.getDataStore();
final VolumeObjectTO volume = (VolumeObjectTO) srcData;
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
KVMStoragePool secondaryStorage = null;
KVMStoragePool primary = null;
try {
final String templateFolder = template.getPath();
secondaryStorage = storagePoolMgr.getStoragePoolByURI(nfsImageStore.getUrl());
primary = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
final KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
storageLayer.mkdirs(tmpltPath);
final String templateName = UUID.randomUUID().toString();
if (primary.getType() != StoragePoolType.RBD) {
final Script command = new Script(_createTmplPath, wait, s_logger);
command.add("-f", disk.getPath());
command.add("-t", tmpltPath);
command.add("-n", templateName + ".qcow2");
final String result = command.execute();
if (result != null) {
s_logger.debug("failed to create template: " + result);
return new CopyCmdAnswer(result);
}
} else {
s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + templateName);
final QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(), primary.getSourcePort(), primary.getAuthUserName(), primary.getAuthSecret(), disk.getPath()));
srcFile.setFormat(PhysicalDiskFormat.RAW);
final QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + templateName + ".qcow2");
destFile.setFormat(PhysicalDiskFormat.QCOW2);
final QemuImg q = new QemuImg(cmd.getWaitInMillSeconds());
try {
q.convert(srcFile, destFile);
} catch (final QemuImgException e) {
final String message = "Failed to create new template while converting " + srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage();
throw new QemuImgException(message);
}
final File templateProp = new File(tmpltPath + "/template.properties");
if (!templateProp.exists()) {
templateProp.createNewFile();
}
String templateContent = "filename=" + templateName + ".qcow2" + System.getProperty("line.separator");
final DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy");
final Date date = new Date();
templateContent += "snapshot.name=" + dateFormat.format(date) + System.getProperty("line.separator");
try (FileOutputStream templFo = new FileOutputStream(templateProp)) {
templFo.write(templateContent.getBytes());
templFo.flush();
} catch (final IOException e) {
throw e;
}
}
final Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, storageLayer);
final Processor qcow2Processor = new QCOW2Processor();
qcow2Processor.configure("QCOW2 Processor", params);
final FormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
final TemplateLocation loc = new TemplateLocation(storageLayer, tmpltPath);
loc.create(1, true, templateName);
loc.addFormat(info);
loc.save();
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(templateFolder + File.separator + templateName + ".qcow2");
newTemplate.setSize(info.virtualSize);
newTemplate.setPhysicalSize(info.size);
newTemplate.setFormat(ImageFormat.QCOW2);
newTemplate.setName(templateName);
return new CopyCmdAnswer(newTemplate);
} catch (final QemuImgException e) {
s_logger.error(e.getMessage());
return new CopyCmdAnswer(e.toString());
} catch (final IOException e) {
s_logger.debug("Failed to createTemplateFromVolume: ", e);
return new CopyCmdAnswer(e.toString());
} catch (final Exception e) {
s_logger.debug("Failed to createTemplateFromVolume: ", e);
return new CopyCmdAnswer(e.toString());
} finally {
if (secondaryStorage != null) {
secondaryStorage.delete();
}
}
}
use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class LibvirtRestoreVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
String vmName = cmd.getVmName();
List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
}
String xmlDesc = dm.getXMLDesc(0);
List<VMSnapshotTO> snapshots = cmd.getSnapshots();
Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
for (VMSnapshotTO snapshot : snapshots) {
VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
try {
// VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
int flags = 1;
if (snapshot.getCurrent()) {
// VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
flags += 2;
}
dm.snapshotCreateXML(vmSnapshotXML, flags);
} catch (LibvirtException e) {
s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
}
}
return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (LibvirtException e) {
String msg = " Restore snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RestoreVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class LibvirtRevertToVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
String vmName = cmd.getVmName();
List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
VirtualMachine.PowerState vmState = null;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
}
DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
if (snapshot == null)
return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
dm.revertToSnapshot(snapshot);
snapshot.free();
if (!snapshotMemory) {
dm.destroy();
if (dm.isPersistent() == 1)
dm.undefine();
vmState = VirtualMachine.PowerState.PowerOff;
} else {
vmState = VirtualMachine.PowerState.PowerOn;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (LibvirtException e) {
String msg = " Revert to VM snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class KVMStoragePoolManager method connectPhysicalDisksViaVmSpec.
public boolean connectPhysicalDisksViaVmSpec(VirtualMachineTO vmSpec) {
boolean result = false;
final String vmName = vmSpec.getName();
List<DiskTO> disks = Arrays.asList(vmSpec.getDisks());
for (DiskTO disk : disks) {
if (disk.getType() != Volume.Type.ISO) {
VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
PrimaryDataStoreTO store = (PrimaryDataStoreTO) vol.getDataStore();
KVMStoragePool pool = getStoragePool(store.getPoolType(), store.getUuid());
StorageAdaptor adaptor = getStorageAdaptor(pool.getType());
result = adaptor.connectPhysicalDisk(vol.getPath(), pool, disk.getDetails());
if (!result) {
s_logger.error("Failed to connect disks via vm spec for vm: " + vmName + " volume:" + vol.toString());
return result;
}
}
}
return result;
}
Aggregations