use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method dettachIso.
@Override
public Answer dettachIso(final DettachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final DataTO data = disk.getData();
final DataStoreTO store = data.getDataStore();
String isoURL = null;
if (store == null) {
final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
isoURL = iso.getName();
} else {
if (!(store instanceof NfsTO)) {
s_logger.debug("Can't attach a iso which is not created on nfs: ");
return new AttachAnswer("Can't attach a iso which is not created on nfs: ");
}
final NfsTO nfsStore = (NfsTO) store;
isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
}
try {
final Connection conn = hypervisorResource.getConnection();
// Find the VM
final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
final String vmUUID = vm.getUuid(conn);
// Find the ISO VDI
final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
final SR sr = isoVDI.getSR(conn);
// Look up all VBDs for this VDI
final Set<VBD> vbds = isoVDI.getVBDs(conn);
// the ISO from it
for (final VBD vbd : vbds) {
final VM vbdVM = vbd.getVM(conn);
final String vbdVmUUID = vbdVM.getUuid(conn);
if (vbdVmUUID.equals(vmUUID)) {
// If an ISO is already inserted, eject it
if (!vbd.getEmpty(conn)) {
vbd.eject(conn);
}
break;
}
}
if (!sr.getNameLabel(conn).startsWith("XenServer Tools")) {
hypervisorResource.removeSR(conn, sr);
}
return new DettachAnswer(disk);
} catch (final XenAPIException e) {
final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + " due to " + e.toString();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
} catch (final Exception e) {
final String msg = "Failed to dettach volume" + " for uuid: " + data.getPath() + " due to " + e.getMessage();
s_logger.warn(msg, e);
return new DettachAnswer(msg);
}
}
use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method createTemplateFromVolume.
@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
final int wait = cmd.getWait();
final String secondaryStoragePoolURL = destStore.getUrl();
final String volumeUUID = volume.getPath();
final String userSpecifiedName = template.getName();
String details = null;
SR tmpltSR = null;
boolean result = false;
String secondaryStorageMountPath = null;
String installPath = null;
Task task = null;
try {
final URI uri = new URI(secondaryStoragePoolURL);
secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
installPath = template.getPath();
if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
details = " Filed to create folder " + installPath + " in secondary storage";
s_logger.warn(details);
return new CopyCmdAnswer(details);
}
final VDI vol = getVDIbyUuid(conn, volumeUUID);
// create template SR
tmpltSR = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), installPath);
// copy volume to template SR
task = vol.copyAsync(conn, tmpltSR, null, null);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
final VDI tmpltVDI = Types.toVDI(task, conn);
// scan makes XenServer pick up VDI physicalSize
tmpltSR.scan(conn);
if (userSpecifiedName != null) {
tmpltVDI.setNameLabel(conn, userSpecifiedName);
}
final String tmpltUUID = tmpltVDI.getUuid(conn);
final String tmpltFilename = tmpltUUID + ".vhd";
final long virtualSize = tmpltVDI.getVirtualSize(conn);
final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
// create the template.properties file
final String templatePath = secondaryStorageMountPath + "/" + installPath;
result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
if (!result) {
throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir");
}
installPath = installPath + "/" + tmpltFilename;
hypervisorResource.removeSR(conn, tmpltSR);
tmpltSR = null;
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(installPath);
newTemplate.setFormat(ImageFormat.VHD);
newTemplate.setSize(virtualSize);
newTemplate.setPhysicalSize(physicalSize);
newTemplate.setName(tmpltUUID);
final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
return answer;
} catch (final Exception e) {
if (tmpltSR != null) {
hypervisorResource.removeSR(conn, tmpltSR);
}
if (secondaryStorageMountPath != null) {
hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
}
details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
s_logger.error(details, e);
} finally {
if (task != null) {
try {
task.destroy(conn);
} catch (final Exception e) {
s_logger.warn("unable to destroy task(" + task.toWireString() + ") due to " + e.toString());
}
}
}
return new CopyCmdAnswer(details);
}
use of com.cloud.legacymodel.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class Xenserver625StorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final int wait = cmd.getWait();
final DataStoreTO srcStore = srcData.getDataStore();
final Connection conn = hypervisorResource.getConnection();
SR srcSr = null;
Task task = null;
try {
if (srcStore instanceof NfsTO && srcData.getObjectType() == DataObjectType.TEMPLATE) {
final NfsTO srcImageStore = (NfsTO) srcStore;
final TemplateObjectTO srcTemplate = (TemplateObjectTO) srcData;
final String storeUrl = srcImageStore.getUrl();
final URI uri = new URI(storeUrl);
String volumePath = srcData.getPath();
volumePath = StringUtils.stripEnd(volumePath, "/");
final String[] splits = volumePath.split("/");
String volumeDirectory = volumePath;
if (splits.length > 4) {
// "template/tmpl/dcid/templateId/templatename"
final int index = volumePath.lastIndexOf("/");
volumeDirectory = volumePath.substring(0, index);
}
srcSr = createFileSr(conn, uri.getHost() + ":" + uri.getPath(), volumeDirectory);
final Set<VDI> setVdis = srcSr.getVDIs(conn);
if (setVdis.size() != 1) {
return new CopyCmdAnswer("Expected 1 VDI template but found " + setVdis.size() + " VDI template(s) on: " + uri.getHost() + ":" + uri.getPath() + "/" + volumeDirectory);
}
final VDI srcVdi = setVdis.iterator().next();
boolean managed = false;
String storageHost = null;
String managedStoragePoolName = null;
String managedStoragePoolRootVolumeName = null;
String managedStoragePoolRootVolumeSize = null;
String chapInitiatorUsername = null;
String chapInitiatorSecret = null;
final PrimaryDataStoreTO destStore = (PrimaryDataStoreTO) destData.getDataStore();
Map<String, String> details = destStore.getDetails();
if (details != null) {
managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
if (managed) {
storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
managedStoragePoolRootVolumeSize = details.get(PrimaryDataStoreTO.VOLUME_SIZE);
chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
}
}
final SR destSr;
if (managed) {
details = new HashMap<>();
details.put(DiskTO.STORAGE_HOST, storageHost);
details.put(DiskTO.IQN, managedStoragePoolName);
details.put(DiskTO.VOLUME_SIZE, managedStoragePoolRootVolumeSize);
details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInitiatorUsername);
details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInitiatorSecret);
destSr = hypervisorResource.prepareManagedSr(conn, details);
} else {
final String srName = destStore.getUuid();
final Set<SR> srs = SR.getByNameLabel(conn, srName);
if (srs.size() != 1) {
final String msg = "There are " + srs.size() + " SRs with same name: " + srName;
s_logger.warn(msg);
return new CopyCmdAnswer(msg);
} else {
destSr = srs.iterator().next();
}
}
task = srcVdi.copyAsync(conn, destSr, null, null);
// poll every 1 seconds ,
hypervisorResource.waitForTask(conn, task, 1000, wait * 1000);
hypervisorResource.checkForSuccess(conn, task);
final VDI tmplVdi = Types.toVDI(task, conn);
final String uuidToReturn;
final Long physicalSize = tmplVdi.getPhysicalUtilisation(conn);
if (managed) {
uuidToReturn = tmplVdi.getUuid(conn);
tmplVdi.setNameLabel(conn, managedStoragePoolRootVolumeName);
} else {
final VDI snapshotVdi = tmplVdi.snapshot(conn, new HashMap<>());
uuidToReturn = snapshotVdi.getUuid(conn);
snapshotVdi.setNameLabel(conn, "Template " + srcTemplate.getName());
tmplVdi.destroy(conn);
}
destSr.scan(conn);
try {
Thread.sleep(5000);
} catch (final Exception e) {
}
final TemplateObjectTO newVol = new TemplateObjectTO();
newVol.setUuid(uuidToReturn);
newVol.setPath(uuidToReturn);
if (physicalSize != null) {
newVol.setSize(physicalSize);
}
newVol.setFormat(ImageFormat.VHD);
return new CopyCmdAnswer(newVol);
}
} catch (final Exception e) {
final String msg = "Catch Exception " + e.getClass().getName() + " for template due to " + e.toString();
s_logger.warn(msg, e);
return new CopyCmdAnswer(msg);
} finally {
if (task != null) {
try {
task.destroy(conn);
} catch (final Exception e) {
s_logger.debug("unable to destroy task (" + task.toWireString() + ") due to " + e.toString());
}
}
if (srcSr != null) {
hypervisorResource.removeSR(conn, srcSr);
}
}
return new CopyCmdAnswer("not implemented yet");
}
use of com.cloud.legacymodel.to.TemplateObjectTO 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.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method dettachIso.
@Override
public Answer dettachIso(final DettachCommand cmd) {
final DiskTO disk = cmd.getDisk();
final TemplateObjectTO isoTo = (TemplateObjectTO) disk.getData();
final DataStoreTO store = isoTo.getDataStore();
if (!(store instanceof NfsTO)) {
return new AttachAnswer("unsupported protocol");
}
final NfsTO nfsStore = (NfsTO) store;
try {
final Connect conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
attachOrDetachIso(conn, cmd.getVmName(), nfsStore.getUrl() + File.separator + isoTo.getPath(), false);
} catch (final LibvirtException e) {
return new Answer(cmd, false, e.toString());
}
return new Answer(cmd);
}
Aggregations