use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
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 = null;
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, s_logger);
command.add("-f", disk.getPath());
command.add("-t", tmpltPath);
command.add("-n", templateName + ".qcow2");
final String result = command.execute();
if (result != null) {
s_logger.debug("failed to create template: " + result);
return new CopyCmdAnswer(result);
}
} else {
s_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<String, Object>();
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) {
s_logger.error(e.getMessage());
return new CopyCmdAnswer(e.toString());
} catch (final IOException e) {
s_logger.debug("Failed to createTemplateFromVolume: ", e);
return new CopyCmdAnswer(e.toString());
} catch (final Exception e) {
s_logger.debug("Failed to createTemplateFromVolume: ", e);
return new CopyCmdAnswer(e.toString());
} finally {
if (secondaryStorage != null) {
secondaryStorage.delete();
}
}
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
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 e) {
return new Answer(cmd, false, e.toString());
} catch (final URISyntaxException e) {
return new Answer(cmd, false, e.toString());
} catch (final InternalErrorException e) {
return new Answer(cmd, false, e.toString());
}
return new Answer(cmd);
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class Ovm3VmSupport method createVbds.
/*
* Add rootdisk, datadisk and iso's
*/
public Boolean createVbds(Xen.Vm vm, VirtualMachineTO spec) {
if (spec.getDisks() == null) {
LOGGER.info("No disks defined for " + vm.getVmName());
return false;
}
for (DiskTO disk : spec.getDisks()) {
try {
if (disk.getType() == Volume.Type.ROOT) {
VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
String diskFile = processor.getVirtualDiskPath(vol.getUuid(), vol.getDataStore().getUuid());
vm.addRootDisk(diskFile);
vm.setPrimaryPoolUuid(vol.getDataStore().getUuid());
LOGGER.debug("Adding root disk: " + diskFile);
} else if (disk.getType() == Volume.Type.ISO) {
DataTO isoTO = disk.getData();
if (isoTO.getPath() != null) {
TemplateObjectTO template = (TemplateObjectTO) isoTO;
DataStoreTO store = template.getDataStore();
if (!(store instanceof NfsTO)) {
throw new CloudRuntimeException("unsupported protocol");
}
NfsTO nfsStore = (NfsTO) store;
String secPoolUuid = pool.setupSecondaryStorage(nfsStore.getUrl());
String isoPath = config.getAgentSecStoragePath() + "/" + secPoolUuid + "/" + template.getPath();
vm.addIso(isoPath);
/* check if secondary storage is mounted */
LOGGER.debug("Adding ISO: " + isoPath);
}
} else if (disk.getType() == Volume.Type.DATADISK) {
VolumeObjectTO vol = (VolumeObjectTO) disk.getData();
String diskFile = processor.getVirtualDiskPath(vol.getUuid(), vol.getDataStore().getUuid());
vm.addDataDisk(diskFile);
LOGGER.debug("Adding data disk: " + diskFile);
} else {
throw new CloudRuntimeException("Unknown disk type: " + disk.getType());
}
} catch (Exception e) {
LOGGER.debug("CreateVbds failed", e);
throw new CloudRuntimeException("Exception" + e.getMessage(), e);
}
}
return true;
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class VmwareResource method syncDiskChain.
// return the finalized disk chain for startup, from top to bottom
private String[] syncDiskChain(DatacenterMO dcMo, VirtualMachineMO vmMo, VirtualMachineTO vmSpec, DiskTO vol, VirtualMachineDiskInfo diskInfo, HashMap<String, Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails) throws Exception {
VolumeObjectTO volumeTO = (VolumeObjectTO) vol.getData();
DataStoreTO primaryStore = volumeTO.getDataStore();
Map<String, String> details = vol.getDetails();
boolean isManaged = false;
String iScsiName = null;
if (details != null) {
isManaged = Boolean.parseBoolean(details.get(DiskTO.MANAGED));
iScsiName = details.get(DiskTO.IQN);
}
// if the storage is managed, iScsiName should not be null
String datastoreName = isManaged ? VmwareResource.getDatastoreName(iScsiName) : primaryStore.getUuid();
Pair<ManagedObjectReference, DatastoreMO> volumeDsDetails = dataStoresDetails.get(datastoreName);
if (volumeDsDetails == null) {
throw new Exception("Primary datastore " + primaryStore.getUuid() + " is not mounted on host.");
}
DatastoreMO dsMo = volumeDsDetails.second();
// we will honor vCenter's meta if it exists
if (diskInfo != null) {
// to deal with run-time upgrade to maintain the new datastore folder structure
String[] disks = diskInfo.getDiskChain();
for (int i = 0; i < disks.length; i++) {
DatastoreFile file = new DatastoreFile(disks[i]);
if (!isManaged && file.getDir() != null && file.getDir().isEmpty()) {
s_logger.info("Perform run-time datastore folder upgrade. sync " + disks[i] + " to VM folder");
disks[i] = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmMo.getName(), dsMo, file.getFileBaseName());
}
}
return disks;
}
final String datastoreDiskPath;
if (isManaged) {
if (volumeTO.getVolumeType() == Volume.Type.ROOT) {
datastoreDiskPath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmMo.getName(), dsMo, volumeTO.getName());
} else {
datastoreDiskPath = dsMo.getDatastorePath(dsMo.getName() + ".vmdk");
}
} else {
datastoreDiskPath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(dcMo, vmMo.getName(), dsMo, volumeTO.getPath());
}
if (!dsMo.fileExists(datastoreDiskPath)) {
s_logger.warn("Volume " + volumeTO.getId() + " does not seem to exist on datastore, out of sync? path: " + datastoreDiskPath);
}
return new String[] { datastoreDiskPath };
}
use of com.cloud.agent.api.to.DataStoreTO in project cloudstack by apache.
the class VmwareResource method examineStorageSubSystemCommandNfsVersion.
/**
* Examine StorageSubSystem command to get storage NFS version, if provided
* @param cmd command to execute
* @param params params
*/
protected void examineStorageSubSystemCommandNfsVersion(CopyCommand cmd, EnumMap<VmwareStorageProcessorConfigurableFields, Object> params) {
DataStoreTO srcDataStore = cmd.getSrcTO().getDataStore();
boolean nfsVersionFound = false;
if (srcDataStore instanceof NfsTO) {
nfsVersionFound = getStorageNfsVersionFromNfsTO((NfsTO) srcDataStore);
}
if (nfsVersionFound) {
params.put(VmwareStorageProcessorConfigurableFields.NFS_VERSION, storageNfsVersion);
}
}
Aggregations