use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.
the class VmwareStorageManagerImpl method createTemplateFromVolume.
private Ternary<String, Long, Long> createTemplateFromVolume(VirtualMachineMO vmMo, long accountId, long templateId, String templateUniqueName, String secStorageUrl, String volumePath, String workerVmName, Integer nfsVersion) throws Exception {
String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
String installPath = getTemplateRelativeDirInSecStorage(accountId, templateId);
String installFullPath = secondaryMountPoint + "/" + installPath;
synchronized (installPath.intern()) {
Script command = new Script(false, "mkdir", _timeout, s_logger);
command.add("-p");
command.add(installFullPath);
String result = command.execute();
if (result != null) {
String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
VirtualMachineMO clonedVm = null;
try {
Pair<VirtualDisk, String> volumeDeviceInfo = vmMo.getDiskDevice(volumePath);
if (volumeDeviceInfo == null) {
String msg = "Unable to find related disk device for volume. volume path: " + volumePath;
s_logger.error(msg);
throw new Exception(msg);
}
if (!vmMo.createSnapshot(templateUniqueName, "Temporary snapshot for template creation", false, false)) {
String msg = "Unable to take snapshot for creating template from volume. volume path: " + volumePath;
s_logger.error(msg);
throw new Exception(msg);
}
// 4 MB is the minimum requirement for VM memory in VMware
vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, volumeDeviceInfo.second(), VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()));
clonedVm = vmMo.getRunningHost().findVmOnHyperHost(workerVmName);
if (clonedVm == null) {
String msg = "Unable to create dummy VM to export volume. volume path: " + volumePath;
s_logger.error(msg);
throw new Exception(msg);
}
clonedVm.exportVm(secondaryMountPoint + "/" + installPath, templateUniqueName, true, false);
long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
OVAProcessor processor = new OVAProcessor();
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("OVA Processor", params);
long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
} finally {
if (clonedVm != null) {
clonedVm.detachAllDisks();
clonedVm.destroy();
}
vmMo.removeSnapshot(templateUniqueName, false);
}
}
use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.
the class VmwareStorageProcessor method copyTemplateFromSecondaryToPrimary.
private Pair<VirtualMachineMO, Long> copyTemplateFromSecondaryToPrimary(VmwareHypervisorHost hyperHost, DatastoreMO datastoreMo, String secondaryStorageUrl, String templatePathAtSecondaryStorage, String templateName, String templateUuid, boolean createSnapshot, Integer nfsVersion) throws Exception {
s_logger.info("Executing copyTemplateFromSecondaryToPrimary. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName);
String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, nfsVersion);
s_logger.info("Secondary storage mount point: " + secondaryMountPoint);
String srcOVAFileName = VmwareStorageLayoutHelper.getTemplateOnSecStorageFilePath(secondaryMountPoint, templatePathAtSecondaryStorage, templateName, ImageFormat.OVA.getFileExtension());
String srcFileName = getOVFFilePath(srcOVAFileName);
if (srcFileName == null) {
Script command = new Script("tar", 0, s_logger);
command.add("--no-same-owner");
command.add("-xf", srcOVAFileName);
command.setWorkDir(secondaryMountPoint + "/" + templatePathAtSecondaryStorage);
s_logger.info("Executing command: " + command.toString());
String result = command.execute();
if (result != null) {
String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
s_logger.error(msg);
throw new Exception(msg);
}
}
srcFileName = getOVFFilePath(srcOVAFileName);
if (srcFileName == null) {
String msg = "Unable to locate OVF file in template package directory: " + srcOVAFileName;
s_logger.error(msg);
throw new Exception(msg);
}
String vmName = templateUuid;
hyperHost.importVmFromOVF(srcFileName, vmName, datastoreMo, "thin");
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
String msg = "Failed to import OVA template. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName + ", templateUuid: " + templateUuid;
s_logger.error(msg);
throw new Exception(msg);
}
OVAProcessor processor = new OVAProcessor();
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("OVA Processor", params);
long virtualSize = processor.getTemplateVirtualSize(secondaryMountPoint + "/" + templatePathAtSecondaryStorage, templateName);
if (createSnapshot) {
if (vmMo.createSnapshot("cloud.template.base", "Base snapshot", false, false)) {
// the same template may be deployed with multiple copies at per-datastore per-host basis,
// save the original template name from CloudStack DB as the UUID to associate them.
vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, templateName);
vmMo.markAsTemplate();
} else {
vmMo.destroy();
String msg = "Unable to create base snapshot for template, templateName: " + templateName + ", templateUuid: " + templateUuid;
s_logger.error(msg);
throw new Exception(msg);
}
}
return new Pair<VirtualMachineMO, Long>(vmMo, new Long(virtualSize));
}
use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.
the class VmwareStorageProcessor method createTemplateFromSnapshot.
private Ternary<String, Long, Long> createTemplateFromSnapshot(String installPath, String templateUniqueName, String secStorageUrl, String snapshotPath, Long templateId, long wait, Integer nfsVersion) throws Exception {
//Snapshot path is decoded in this form: /snapshots/account/volumeId/uuid/uuid
String backupSSUuid;
String snapshotFolder;
if (snapshotPath.endsWith(".ova")) {
int index = snapshotPath.lastIndexOf(File.separator);
backupSSUuid = snapshotPath.substring(index + 1).replace(".ova", "");
snapshotFolder = snapshotPath.substring(0, index);
} else {
String[] tokens = snapshotPath.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
backupSSUuid = tokens[tokens.length - 1];
snapshotFolder = StringUtils.join(tokens, File.separator, 0, tokens.length - 1);
}
String secondaryMountPoint = mountService.getMountPoint(secStorageUrl, nfsVersion);
String installFullPath = secondaryMountPoint + "/" + installPath;
//Note: volss for tmpl
String installFullOVAName = installFullPath + "/" + templateUniqueName + ".ova";
String snapshotRoot = secondaryMountPoint + "/" + snapshotFolder;
String snapshotFullOVAName = snapshotRoot + "/" + backupSSUuid + ".ova";
String snapshotFullOvfName = snapshotRoot + "/" + backupSSUuid + ".ovf";
String result;
Script command;
String templateVMDKName = "";
String snapshotFullVMDKName = snapshotRoot + "/" + backupSSUuid + "/";
synchronized (installPath.intern()) {
command = new Script(false, "mkdir", _timeout, s_logger);
command.add("-p");
command.add(installFullPath);
result = command.execute();
if (result != null) {
String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
try {
if (new File(snapshotFullOVAName).exists()) {
command = new Script(false, "cp", wait, s_logger);
command.add(snapshotFullOVAName);
command.add(installFullOVAName);
result = command.execute();
if (result != null) {
String msg = "unable to copy snapshot " + snapshotFullOVAName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
// untar OVA file at template directory
command = new Script("tar", wait, s_logger);
command.add("--no-same-owner");
command.add("-xf", installFullOVAName);
command.setWorkDir(installFullPath);
s_logger.info("Executing command: " + command.toString());
result = command.execute();
if (result != null) {
String msg = "unable to untar snapshot " + snapshotFullOVAName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
// there is no ova file, only ovf originally;
if (new File(snapshotFullOvfName).exists()) {
command = new Script(false, "cp", wait, s_logger);
command.add(snapshotFullOvfName);
//command.add(installFullOvfName);
command.add(installFullPath);
result = command.execute();
if (result != null) {
String msg = "unable to copy snapshot " + snapshotFullOvfName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
s_logger.info("vmdkfile parent dir: " + snapshotRoot);
File snapshotdir = new File(snapshotRoot);
File[] ssfiles = snapshotdir.listFiles();
if (ssfiles == null) {
String msg = "unable to find snapshot vmdk files in " + snapshotRoot;
s_logger.error(msg);
throw new Exception(msg);
}
// List<String> filenames = new ArrayList<String>();
for (int i = 0; i < ssfiles.length; i++) {
String vmdkfile = ssfiles[i].getName();
s_logger.info("vmdk file name: " + vmdkfile);
if (vmdkfile.toLowerCase().startsWith(backupSSUuid) && vmdkfile.toLowerCase().endsWith(".vmdk")) {
snapshotFullVMDKName = snapshotRoot + File.separator + vmdkfile;
templateVMDKName += vmdkfile;
break;
}
}
if (snapshotFullVMDKName != null) {
command = new Script(false, "cp", wait, s_logger);
command.add(snapshotFullVMDKName);
command.add(installFullPath);
result = command.execute();
s_logger.info("Copy VMDK file: " + snapshotFullVMDKName);
if (result != null) {
String msg = "unable to copy snapshot vmdk file " + snapshotFullVMDKName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
}
} else {
String msg = "unable to find any snapshot ova/ovf files" + snapshotFullOVAName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
}
long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
OVAProcessor processor = new OVAProcessor();
// long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("OVA Processor", params);
long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
writeMetaOvaForTemplate(installFullPath, backupSSUuid + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
} finally {
// TODO, clean up left over files
}
}
use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.
the class VmwareStorageProcessor method copyTemplateFromSecondaryToPrimary.
private Pair<VirtualMachineMO, Long> copyTemplateFromSecondaryToPrimary(VmwareHypervisorHost hyperHost, DatastoreMO datastoreMo, String secondaryStorageUrl, String templatePathAtSecondaryStorage, String templateName, String templateUuid, boolean createSnapshot, String nfsVersion, String configuration) throws Exception {
String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, nfsVersion);
s_logger.info(String.format("Init copy of template [name: %s, path in secondary storage: %s, configuration: %s] in secondary storage [url: %s, mount point: %s] to primary storage.", templateName, templatePathAtSecondaryStorage, configuration, secondaryStorageUrl, secondaryMountPoint));
String srcOVAFileName = VmwareStorageLayoutHelper.getTemplateOnSecStorageFilePath(secondaryMountPoint, templatePathAtSecondaryStorage, templateName, ImageFormat.OVA.getFileExtension());
String srcFileName = getOVFFilePath(srcOVAFileName);
if (srcFileName == null) {
Script command = new Script("tar", 0, s_logger);
command.add("--no-same-owner");
command.add("-xf", srcOVAFileName);
command.setWorkDir(secondaryMountPoint + "/" + templatePathAtSecondaryStorage);
s_logger.info("Executing command: " + command.toString());
String result = command.execute();
if (result != null) {
String msg = "Unable to unpack snapshot OVA file at: " + srcOVAFileName;
s_logger.error(msg);
throw new Exception(msg);
}
}
srcFileName = getOVFFilePath(srcOVAFileName);
if (srcFileName == null) {
String msg = "Unable to locate OVF file in template package directory: " + srcOVAFileName;
s_logger.error(msg);
throw new Exception(msg);
}
if (datastoreMo.getDatastoreType().equalsIgnoreCase("VVOL")) {
templateUuid = CustomFieldConstants.CLOUD_UUID + "-" + templateUuid;
}
VmConfigInfo vAppConfig;
s_logger.debug(String.format("Deploying OVF template %s with configuration %s.", templateName, configuration));
hyperHost.importVmFromOVF(srcFileName, templateUuid, datastoreMo, "thin", configuration);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(templateUuid);
if (vmMo == null) {
String msg = "Failed to import OVA template. secondaryStorage: " + secondaryStorageUrl + ", templatePathAtSecondaryStorage: " + templatePathAtSecondaryStorage + ", templateName: " + templateName + ", templateUuid: " + templateUuid;
s_logger.error(msg);
throw new Exception(msg);
} else {
vAppConfig = vmMo.getConfigInfo().getVAppConfig();
if (vAppConfig != null) {
s_logger.info("Found vApp configuration");
}
}
OVAProcessor processor = new OVAProcessor();
Map<String, Object> params = new HashMap<>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("OVA Processor", params);
long virtualSize = processor.getTemplateVirtualSize(secondaryMountPoint + "/" + templatePathAtSecondaryStorage, templateName);
if (createSnapshot) {
if (vmMo.createSnapshot("cloud.template.base", "Base snapshot", false, false)) {
// the same template may be deployed with multiple copies at per-datastore per-host basis,
// save the original template name from CloudStack DB as the UUID to associate them.
vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, templateName);
if (vAppConfig == null || (vAppConfig.getProperty().size() == 0)) {
vmMo.markAsTemplate();
}
} else {
vmMo.destroy();
String msg = "Unable to create base snapshot for template, templateName: " + templateName + ", templateUuid: " + templateUuid;
s_logger.error(msg);
throw new Exception(msg);
}
}
return new Pair<>(vmMo, virtualSize);
}
use of com.cloud.storage.template.OVAProcessor in project cloudstack by apache.
the class VmwareStorageManagerImpl method createTemplateFromSnapshot.
private Ternary<String, Long, Long> createTemplateFromSnapshot(long accountId, long templateId, String templateUniqueName, String secStorageUrl, long volumeId, String backedUpSnapshotUuid, String nfsVersion) throws Exception {
String secondaryMountPoint = _mountService.getMountPoint(secStorageUrl, nfsVersion);
String installPath = getTemplateRelativeDirInSecStorage(accountId, templateId);
String installFullPath = secondaryMountPoint + "/" + installPath;
// Note: volss for tmpl
String installFullOVAName = installFullPath + "/" + templateUniqueName + ".ova";
String snapshotRoot = secondaryMountPoint + "/" + getSnapshotRelativeDirInSecStorage(accountId, volumeId);
String snapshotFullOVAName = snapshotRoot + "/" + backedUpSnapshotUuid + ".ova";
String snapshotFullOvfName = snapshotRoot + "/" + backedUpSnapshotUuid + ".ovf";
String result;
Script command;
String templateVMDKName = "";
// String snapshotFullVMDKName = snapshotRoot + "/";
// the backedUpSnapshotUuid field currently has the format: uuid/uuid. so we need to extract the uuid out
String backupSSUuid = backedUpSnapshotUuid.substring(0, backedUpSnapshotUuid.indexOf('/'));
String snapshotFullVMDKName = snapshotRoot + "/" + backupSSUuid + "/";
synchronized (installPath.intern()) {
command = new Script(false, "mkdir", _timeout, s_logger);
command.add("-p");
command.add(installFullPath);
result = command.execute();
if (result != null) {
String msg = "unable to prepare template directory: " + installPath + ", storage: " + secStorageUrl + ", error msg: " + result;
s_logger.error(msg);
throw new Exception(msg);
}
}
try {
if (new File(snapshotFullOVAName).exists()) {
command = new Script(false, "cp", _timeout, s_logger);
command.add(snapshotFullOVAName);
command.add(installFullOVAName);
result = command.execute();
if (result != null) {
String msg = "unable to copy snapshot " + snapshotFullOVAName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
// untar OVA file at template directory
command = new Script("tar", 0, s_logger);
command.add("--no-same-owner");
command.add("-xf", installFullOVAName);
command.setWorkDir(installFullPath);
s_logger.info("Executing command: " + command.toString());
result = command.execute();
if (result != null) {
String msg = "unable to untar snapshot " + snapshotFullOVAName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
} else {
// there is no ova file, only ovf originally;
if (new File(snapshotFullOvfName).exists()) {
command = new Script(false, "cp", _timeout, s_logger);
command.add(snapshotFullOvfName);
// command.add(installFullOvfName);
command.add(installFullPath);
result = command.execute();
if (result != null) {
String msg = "unable to copy snapshot " + snapshotFullOvfName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
s_logger.info("vmdkfile parent dir: " + snapshotFullVMDKName);
File snapshotdir = new File(snapshotFullVMDKName);
// File snapshotdir = new File(snapshotRoot);
File[] ssfiles = snapshotdir.listFiles();
// List<String> filenames = new ArrayList<String>();
for (int i = 0; i < ssfiles.length; i++) {
String vmdkfile = ssfiles[i].getName();
s_logger.info("vmdk file name: " + vmdkfile);
if (vmdkfile.toLowerCase().startsWith(backupSSUuid) && vmdkfile.toLowerCase().endsWith(".vmdk")) {
snapshotFullVMDKName += vmdkfile;
templateVMDKName += vmdkfile;
break;
}
}
if (snapshotFullVMDKName != null) {
command = new Script(false, "cp", _timeout, s_logger);
command.add(snapshotFullVMDKName);
command.add(installFullPath);
result = command.execute();
s_logger.info("Copy VMDK file: " + snapshotFullVMDKName);
if (result != null) {
String msg = "unable to copy snapshot vmdk file " + snapshotFullVMDKName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
}
} else {
String msg = "unable to find any snapshot ova/ovf files" + snapshotFullOVAName + " to " + installFullPath;
s_logger.error(msg);
throw new Exception(msg);
}
}
long physicalSize = new File(installFullPath + "/" + templateVMDKName).length();
OVAProcessor processor = new OVAProcessor();
// long physicalSize = new File(installFullPath + "/" + templateUniqueName + ".ova").length();
Map<String, Object> params = new HashMap<String, Object>();
params.put(StorageLayer.InstanceConfigKey, _storage);
processor.configure("OVA Processor", params);
long virtualSize = processor.getTemplateVirtualSize(installFullPath, templateUniqueName);
postCreatePrivateTemplate(installFullPath, templateId, templateUniqueName, physicalSize, virtualSize);
writeMetaOvaForTemplate(installFullPath, backedUpSnapshotUuid + ".ovf", templateVMDKName, templateUniqueName, physicalSize);
return new Ternary<String, Long, Long>(installPath + "/" + templateUniqueName + ".ova", physicalSize, virtualSize);
} catch (Exception e) {
// TODO, clean up left over files
throw e;
}
}
Aggregations