use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method attachIso.
@Override
public Answer attachIso(final AttachCommand 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(), true);
} catch (final LibvirtException | URISyntaxException | InternalErrorException e) {
return new Answer(cmd, false, e.toString());
}
return new Answer(cmd);
}
use of com.cloud.storage.to.TemplateObjectTO 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();
KvmPhysicalDisk baseVolume;
KvmStoragePool primaryPool;
KvmPhysicalDisk vol;
try {
primaryPool = 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 (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 = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), templatePath);
vol = 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) {
logger.debug("Failed to create volume: ", e);
return new CopyCmdAnswer(e.toString());
}
}
use of com.cloud.storage.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;
KvmStoragePool primary;
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, logger);
command.add("-f", disk.getPath());
command.add("-t", tmpltPath);
command.add("-n", templateName + ".qcow2");
final String result = command.execute();
if (result != null) {
logger.debug("failed to create template: " + result);
return new CopyCmdAnswer(result);
}
} else {
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<>();
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) {
logger.error(e.getMessage());
return new CopyCmdAnswer(e.toString());
} catch (final Exception e) {
logger.debug("Failed to createTemplateFromVolume: ", e);
return new CopyCmdAnswer(e.toString());
} finally {
if (secondaryStorage != null) {
secondaryStorage.delete();
}
}
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method createVmFromTemplate.
public VM createVmFromTemplate(final Connection conn, final VirtualMachineTO vmSpec, final Host host) throws XenAPIException, XmlRpcException {
final String guestOsTypeName = getGuestOsType(vmSpec.getOs(), vmSpec.getPlatformEmulator(), vmSpec.getBootloader() == BootloaderType.CD);
final Set<VM> templates = VM.getByNameLabel(conn, guestOsTypeName);
if (templates == null || templates.isEmpty()) {
throw new CloudRuntimeException("Cannot find template " + guestOsTypeName + " on XenServer host");
}
assert templates.size() == 1 : "Should only have 1 template but found " + templates.size();
final VM template = templates.iterator().next();
final VM.Record vmr = template.getRecord(conn);
vmr.affinity = host;
vmr.otherConfig.remove("disks");
vmr.otherConfig.remove("default_template");
vmr.otherConfig.remove("mac_seed");
vmr.isATemplate = false;
vmr.nameLabel = vmSpec.getName();
vmr.actionsAfterCrash = Types.OnCrashBehaviour.DESTROY;
vmr.actionsAfterShutdown = Types.OnNormalExit.DESTROY;
vmr.otherConfig.put("vm_uuid", vmSpec.getUuid());
// FIX ME: In case of dynamic
vmr.VCPUsMax = (long) vmSpec.getCpus();
if (isDmcEnabled(conn, host) && vmSpec.isEnableDynamicallyScaleVm()) {
// scaling is allowed
vmr.memoryStaticMin = getStaticMin(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
vmr.memoryStaticMax = getStaticMax(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD, vmSpec.getMinRam(), vmSpec.getMaxRam());
vmr.memoryDynamicMin = vmSpec.getMinRam();
vmr.memoryDynamicMax = vmSpec.getMaxRam();
if (guestOsTypeName.toLowerCase().contains("windows")) {
vmr.VCPUsMax = (long) vmSpec.getCpus();
} else {
if (vmSpec.getVcpuMaxLimit() != null) {
vmr.VCPUsMax = (long) vmSpec.getVcpuMaxLimit();
}
}
} else {
// scaling disallowed, set static memory target
if (vmSpec.isEnableDynamicallyScaleVm() && !isDmcEnabled(conn, host)) {
s_logger.warn("Host " + host.getHostname(conn) + " does not support dynamic scaling, so the vm " + vmSpec.getName() + " is not dynamically scalable");
}
vmr.memoryStaticMin = vmSpec.getMinRam();
vmr.memoryStaticMax = vmSpec.getMaxRam();
vmr.memoryDynamicMin = vmSpec.getMinRam();
vmr.memoryDynamicMax = vmSpec.getMaxRam();
vmr.VCPUsMax = (long) vmSpec.getCpus();
}
vmr.VCPUsAtStartup = (long) vmSpec.getCpus();
vmr.consoles.clear();
final VM vm = VM.create(conn, vmr);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Created VM " + vm.getUuid(conn) + " for " + vmSpec.getName());
}
final String bootArgs = vmSpec.getBootArgs();
if (bootArgs != null && bootArgs.length() > 0) {
String pvargs = vm.getPVArgs(conn);
pvargs = pvargs + vmSpec.getBootArgs().replaceAll(" ", "%");
if (s_logger.isDebugEnabled()) {
s_logger.debug("PV args are " + pvargs);
}
vm.setPVArgs(conn, pvargs);
}
if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) {
if (vmSpec.getBootloader() == BootloaderType.CD) {
final DiskTO[] disks = vmSpec.getDisks();
for (final DiskTO disk : disks) {
if (disk.getType() == Volume.Type.ISO) {
final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
final String osType = iso.getGuestOsType();
if (osType != null) {
final String isoGuestOsName = getGuestOsType(osType, vmSpec.getPlatformEmulator(), vmSpec.getBootloader() == BootloaderType.CD);
if (!isoGuestOsName.equals(guestOsTypeName)) {
vmSpec.setBootloader(BootloaderType.PyGrub);
}
}
}
}
}
if (vmSpec.getBootloader() == BootloaderType.CD) {
vm.setPVBootloader(conn, "eliloader");
if (!vm.getOtherConfig(conn).containsKey("install-repository")) {
vm.addToOtherConfig(conn, "install-repository", "cdrom");
}
} else if (vmSpec.getBootloader() == BootloaderType.PyGrub) {
vm.setPVBootloader(conn, "pygrub");
vm.setPVBootloaderArgs(conn, CitrixHelper.getPVbootloaderArgs(guestOsTypeName));
} else {
vm.destroy(conn);
throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
}
}
try {
finalizeVmMetaData(vm, conn, vmSpec);
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to finalize VM MetaData: " + vmSpec);
}
return vm;
}
use of com.cloud.storage.to.TemplateObjectTO in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor 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;
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
final URI tmpltURI = new URI(secondaryStoragePoolURL + "/" + installPath);
tmpltSR = hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
// copy volume to template SR
final VDI tmpltVDI = hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
// 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: " + tmpltURI);
}
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);
}
return new CopyCmdAnswer(details);
}
Aggregations