use of com.cloud.legacymodel.to.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class VMSnapshotHelperImpl method getVolumeTOList.
@Override
public List<VolumeObjectTO> getVolumeTOList(final Long vmId) {
final List<VolumeObjectTO> volumeTOs = new ArrayList<>();
final List<VolumeVO> volumeVOs = volumeDao.findByInstance(vmId);
for (final VolumeVO volume : volumeVOs) {
final VolumeInfo volumeInfo = volumeDataFactory.getVolume(volume.getId());
volumeTOs.add((VolumeObjectTO) volumeInfo.getTO());
}
return volumeTOs;
}
use of com.cloud.legacymodel.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 VolumeType type = volume.getType();
if (type == VolumeType.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.legacymodel.to.VolumeObjectTO 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());
}
}
}
use of com.cloud.legacymodel.to.VolumeObjectTO 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.VolumeObjectTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method dettachVolume.
@Override
public Answer dettachVolume(final DettachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) vol.getDataStore();
final String vmName = cmd.getVmName();
final String serial = this.resource.diskUuidToSerial(vol.getUuid());
try {
final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
final KvmPhysicalDisk phyDisk = this.storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
attachOrDetachDisk(conn, false, vmName, phyDisk, disk.getDiskSeq().intValue(), disk.getDiskController(), disk.getDiskFormat(), serial, vol);
this.storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), vol.getPath());
return new DettachAnswer(disk);
} catch (final LibvirtException | InternalErrorException e) {
this.logger.debug("Failed to attach volume: " + vol.getPath() + ", due to ", e);
return new DettachAnswer(e.toString());
}
}
Aggregations