use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.
the class TemplateManagerImpl method attachISOToVM.
private boolean attachISOToVM(final long vmId, final long isoId, final boolean attach) {
final UserVmVO vm = this._userVmDao.findById(vmId);
if (vm == null) {
return false;
} else if (vm.getState() != State.Running) {
return true;
}
// prepare ISO ready to mount on hypervisor resource level
final TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId());
final String vmName = vm.getInstanceName();
final HostVO host = this._hostDao.findById(vm.getHostId());
if (host == null) {
s_logger.warn("Host: " + vm.getHostId() + " does not exist");
return false;
}
final DataTO isoTO = tmplt.getTO();
final DiskTO disk = new DiskTO(isoTO, null, null, VolumeType.ISO);
final Command cmd;
if (attach) {
cmd = new AttachCommand(disk, vmName);
} else {
cmd = new DettachCommand(disk, vmName);
}
final Answer a = this._agentMgr.easySend(vm.getHostId(), cmd);
return a != null && a.getResult();
}
use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.
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;
final KvmStoragePool primary;
try {
final String templateFolder = template.getPath();
secondaryStorage = this.storagePoolMgr.getStoragePoolByUri(nfsImageStore.getUrl());
primary = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
final KvmPhysicalDisk disk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
final String tmpltPath = secondaryStorage.getLocalPath() + File.separator + templateFolder;
this.storageLayer.mkdirs(tmpltPath);
final String templateName = UUID.randomUUID().toString();
if (primary.getType() != StoragePoolType.RBD) {
final Script command = new Script(this.createTmplPath, wait, this.logger);
command.add("-f", disk.getPath());
command.add("-t", tmpltPath);
command.add("-n", templateName + ".qcow2");
final String result = command.execute();
if (result != null) {
this.logger.debug("failed to create template: " + result);
return new CopyCmdAnswer(result);
}
} else {
this.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 (final FileOutputStream templFo = new FileOutputStream(templateProp)) {
templFo.write(templateContent.getBytes());
templFo.flush();
} catch (final IOException e) {
throw e;
}
}
final Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, this.storageLayer);
final Processor qcow2Processor = new QCOW2Processor();
qcow2Processor.configure("QCOW2 Processor", params);
final TemplateFormatInfo info = qcow2Processor.process(tmpltPath, null, templateName);
final TemplateLocation loc = new TemplateLocation(this.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) {
this.logger.error(e.getMessage());
return new CopyCmdAnswer(e.toString());
} catch (final Exception e) {
this.logger.debug("Failed to createTemplateFromVolume: ", e);
return new CopyCmdAnswer(e.toString());
} finally {
if (secondaryStorage != null) {
secondaryStorage.delete();
}
}
}
use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method copyVolumeFromImageCacheToPrimary.
@Override
public Answer copyVolumeFromImageCacheToPrimary(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final DataStoreTO srcStore = srcData.getDataStore();
final DataStoreTO destStore = destData.getDataStore();
final VolumeObjectTO srcVol = (VolumeObjectTO) srcData;
final ImageFormat srcFormat = srcVol.getFormat();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destStore;
if (!(srcStore instanceof NfsTO)) {
return new CopyCmdAnswer("can only handle nfs storage");
}
final NfsTO nfsStore = (NfsTO) srcStore;
final String srcVolumePath = srcData.getPath();
final String secondaryStorageUrl = nfsStore.getUrl();
KvmStoragePool secondaryStoragePool = null;
KvmStoragePool primaryPool;
try {
try {
primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
} catch (final CloudRuntimeException e) {
if (e.getMessage().contains("not found")) {
primaryPool = this.storagePoolMgr.createStoragePool(primaryStore.getUuid(), primaryStore.getHost(), primaryStore.getPort(), primaryStore.getPath(), null, primaryStore.getPoolType());
} else {
return new CopyCmdAnswer(e.getMessage());
}
}
final String volumeName = UUID.randomUUID().toString();
final int index = srcVolumePath.lastIndexOf(File.separator);
final String volumeDir = srcVolumePath.substring(0, index);
String srcVolumeName = srcVolumePath.substring(index + 1);
secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + File.separator + volumeDir);
if (!srcVolumeName.endsWith(".qcow2") && srcFormat == ImageFormat.QCOW2) {
srcVolumeName = srcVolumeName + ".qcow2";
}
final KvmPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(srcVolumeName);
volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
final KvmPhysicalDisk newDisk = this.storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, cmd.getWaitInMillSeconds());
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setFormat(ImageFormat.valueOf(newDisk.getFormat().toString().toUpperCase()));
newVol.setPath(volumeName);
return new CopyCmdAnswer(newVol);
} catch (final CloudRuntimeException e) {
this.logger.debug("Failed to ccopyVolumeFromImageCacheToPrimary: ", e);
return new CopyCmdAnswer(e.toString());
} finally {
if (secondaryStoragePool != null) {
this.storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
}
}
}
use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method cloneVolumeFromBaseTemplate.
@Override
public Answer cloneVolumeFromBaseTemplate(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final TemplateObjectTO template = (TemplateObjectTO) srcData;
final DataStoreTO imageStore = template.getDataStore();
final VolumeObjectTO volume = (VolumeObjectTO) destData;
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
final KvmPhysicalDisk baseVolume;
final KvmStoragePool primaryPool;
final KvmPhysicalDisk vol;
try {
primaryPool = this.storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
String templatePath = template.getPath();
if (primaryPool.getType() == StoragePoolType.CLVM) {
templatePath = imageStore.getUrl() + File.separator + templatePath;
vol = templateToPrimaryDownload(templatePath, primaryPool, volume.getUuid(), volume.getSize(), cmd.getWaitInMillSeconds());
} else if (primaryPool.getType() == StoragePoolType.LVM) {
templatePath = imageStore.getUrl() + File.separator + templatePath;
vol = templateToPrimaryDownload(templatePath, primaryPool, volume.getUuid(), volume.getSize(), cmd.getWaitInMillSeconds());
} else {
if (templatePath.contains("/mnt")) {
// upgrade issue, if the path contains path, need to extract the volume uuid from path
templatePath = templatePath.substring(templatePath.lastIndexOf(File.separator) + 1);
}
baseVolume = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), templatePath);
vol = this.storagePoolMgr.createDiskFromTemplate(baseVolume, volume.getUuid(), volume.getProvisioningType(), baseVolume.getPool(), volume.getSize(), cmd.getWaitInMillSeconds());
}
if (vol == null) {
return new CopyCmdAnswer(" Can't create storage volume on storage pool");
}
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(vol.getName());
newVol.setSize(volume.getSize());
if (vol.getFormat() == PhysicalDiskFormat.RAW) {
newVol.setFormat(ImageFormat.RAW);
} else if (vol.getFormat() == PhysicalDiskFormat.QCOW2) {
newVol.setFormat(ImageFormat.QCOW2);
} else if (vol.getFormat() == PhysicalDiskFormat.DIR) {
newVol.setFormat(ImageFormat.DIR);
}
return new CopyCmdAnswer(newVol);
} catch (final CloudRuntimeException e) {
this.logger.debug("Failed to create volume: ", e);
return new CopyCmdAnswer(e.toString());
}
}
use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method copyVolumeFromPrimaryToSecondary.
@Override
public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final VolumeObjectTO srcVol = (VolumeObjectTO) srcData;
final VolumeObjectTO destVol = (VolumeObjectTO) destData;
final ImageFormat srcFormat = srcVol.getFormat();
final ImageFormat destFormat = destVol.getFormat();
final DataStoreTO srcStore = srcData.getDataStore();
final DataStoreTO destStore = destData.getDataStore();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) srcStore;
if (!(destStore instanceof NfsTO)) {
return new CopyCmdAnswer("can only handle nfs storage");
}
final NfsTO nfsStore = (NfsTO) destStore;
final String srcVolumePath = srcData.getPath();
final String destVolumePath = destData.getPath();
final String secondaryStorageUrl = nfsStore.getUrl();
KvmStoragePool secondaryStoragePool = null;
try {
final String volumeName = UUID.randomUUID().toString();
final String destVolumeName = volumeName + "." + destFormat.toString().toLowerCase();
final KvmPhysicalDisk volume = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), srcVolumePath);
volume.setFormat(PhysicalDiskFormat.valueOf(srcFormat.toString()));
secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl);
secondaryStoragePool.createFolder(destVolumePath);
this.storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
secondaryStoragePool = this.storagePoolMgr.getStoragePoolByUri(secondaryStorageUrl + File.separator + destVolumePath);
this.storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, cmd.getWaitInMillSeconds());
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(destVolumePath + File.separator + destVolumeName);
newVol.setFormat(destFormat);
return new CopyCmdAnswer(newVol);
} catch (final CloudRuntimeException e) {
this.logger.debug("Failed to copyVolumeFromPrimaryToSecondary: ", e);
return new CopyCmdAnswer(e.toString());
} finally {
if (secondaryStoragePool != null) {
this.storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
}
}
}
Aggregations