use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method copyTemplateToPrimaryStorage.
@Override
public Answer copyTemplateToPrimaryStorage(final CopyCommand cmd) {
final DataTO srcData = cmd.getSrcTO();
final DataTO destData = cmd.getDestTO();
final TemplateObjectTO template = (TemplateObjectTO) srcData;
final DataStoreTO imageStore = template.getDataStore();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) destData.getDataStore();
if (!(imageStore instanceof NfsTO)) {
return new CopyCmdAnswer("unsupported protocol");
}
final NfsTO nfsImageStore = (NfsTO) imageStore;
final String tmplturl = nfsImageStore.getUrl() + File.separator + template.getPath();
final int index = tmplturl.lastIndexOf("/");
final String mountpoint = tmplturl.substring(0, index);
String tmpltname = null;
if (index < tmplturl.length() - 1) {
tmpltname = tmplturl.substring(index + 1);
}
KvmPhysicalDisk tmplVol = null;
KvmStoragePool secondaryPool = null;
try {
secondaryPool = storagePoolMgr.getStoragePoolByUri(mountpoint);
/* Get template vol */
if (tmpltname == null) {
secondaryPool.refresh();
final List<KvmPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
if (disks == null || disks.isEmpty()) {
return new PrimaryStorageDownloadAnswer("Failed to get volumes from pool: " + secondaryPool.getUuid());
}
for (final KvmPhysicalDisk disk : disks) {
if (disk.getName().endsWith("qcow2")) {
tmplVol = disk;
break;
}
}
} else {
tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
}
if (tmplVol == null) {
return new PrimaryStorageDownloadAnswer("Failed to get template from pool: " + secondaryPool.getUuid());
}
/* Copy volume to primary storage */
logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
final KvmStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
KvmPhysicalDisk primaryVol;
if (destData instanceof VolumeObjectTO) {
final VolumeObjectTO volume = (VolumeObjectTO) destData;
// rather than cloning on deploy
if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
logger.debug("Using configured size of " + volume.getSize());
tmplVol.setSize(volume.getSize());
tmplVol.setVirtualSize(volume.getSize());
} else {
logger.debug("Using template's size of " + tmplVol.getVirtualSize());
}
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else if (destData instanceof TemplateObjectTO) {
final TemplateObjectTO destTempl = (TemplateObjectTO) destData;
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else {
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
}
DataTO data = null;
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(primaryVol.getName());
newTemplate.setSize(primaryVol.getSize());
if (primaryPool.getType() == StoragePoolType.RBD) {
newTemplate.setFormat(ImageFormat.RAW);
} else {
newTemplate.setFormat(ImageFormat.QCOW2);
}
data = newTemplate;
} else if (destData.getObjectType() == DataObjectType.VOLUME) {
final VolumeObjectTO volumeObjectTo = new VolumeObjectTO();
volumeObjectTo.setPath(primaryVol.getName());
volumeObjectTo.setSize(primaryVol.getSize());
if (primaryVol.getFormat() == PhysicalDiskFormat.RAW) {
volumeObjectTo.setFormat(ImageFormat.RAW);
} else if (primaryVol.getFormat() == PhysicalDiskFormat.QCOW2) {
volumeObjectTo.setFormat(ImageFormat.QCOW2);
}
data = volumeObjectTo;
}
return new CopyCmdAnswer(data);
} catch (final CloudRuntimeException e) {
return new CopyCmdAnswer(e.toString());
} finally {
try {
if (secondaryPool != null) {
secondaryPool.delete();
}
} catch (final Exception e) {
logger.debug("Failed to clean up secondary storage", e);
}
}
}
use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method createTemplateFromSnapshot.
@Override
public Answer createTemplateFromSnapshot(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final SnapshotObjectTO snapshotObjTO = (SnapshotObjectTO) cmd.getSrcTO();
final TemplateObjectTO templateObjTO = (TemplateObjectTO) cmd.getDestTO();
if (!(snapshotObjTO.getDataStore() instanceof PrimaryDataStoreTO) || !(templateObjTO.getDataStore() instanceof NfsTO)) {
return null;
}
final String userSpecifiedTemplateName = templateObjTO.getName();
NfsTO destStore = null;
URI destUri = null;
try {
destStore = (NfsTO) templateObjTO.getDataStore();
destUri = new URI(destStore.getUrl());
} catch (final Exception ex) {
s_logger.debug("Invalid URI", ex);
return new CopyCmdAnswer("Invalid URI: " + ex.toString());
}
SR srcSr = null;
SR destSr = null;
final String destDir = templateObjTO.getPath();
VDI destVdi = null;
boolean result = false;
try {
final Map<String, String> srcDetails = cmd.getOptions();
final String iScsiName = srcDetails.get(DiskTO.IQN);
final String storageHost = srcDetails.get(DiskTO.STORAGE_HOST);
final String chapInitiatorUsername = srcDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String chapInitiatorSecret = srcDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
srcSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
if (!hypervisorResource.createSecondaryStorageFolder(conn, destNfsPath, destDir)) {
final String details = " Failed to create folder " + destDir + " in secondary storage";
s_logger.warn(details);
return new CopyCmdAnswer(details);
}
final URI templateUri = new URI(destStore.getUrl() + "/" + destDir);
destSr = hypervisorResource.createNfsSRbyURI(conn, templateUri, false);
// there should only be one VDI in this SR
final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
destVdi = srcVdi.copy(conn, destSr);
// scan makes XenServer pick up VDI physicalSize
destSr.scan(conn);
if (userSpecifiedTemplateName != null) {
destVdi.setNameLabel(conn, userSpecifiedTemplateName);
}
final String templateUuid = destVdi.getUuid(conn);
final String templateFilename = templateUuid + ".vhd";
final long virtualSize = destVdi.getVirtualSize(conn);
final long physicalSize = destVdi.getPhysicalUtilisation(conn);
// create the template.properties file
String templatePath = destNfsPath + "/" + destDir;
templatePath = templatePath.replaceAll("//", "/");
result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, userSpecifiedTemplateName, null, physicalSize, virtualSize, templateObjTO.getId());
if (!result) {
throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + templateUri);
}
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(destDir + "/" + templateFilename);
newTemplate.setFormat(Storage.ImageFormat.VHD);
newTemplate.setHypervisorType(HypervisorType.XenServer);
newTemplate.setSize(virtualSize);
newTemplate.setPhysicalSize(physicalSize);
newTemplate.setName(templateUuid);
result = true;
return new CopyCmdAnswer(newTemplate);
} catch (final Exception ex) {
s_logger.error("Failed to create a template from a snapshot", ex);
return new CopyCmdAnswer("Failed to create a template from a snapshot: " + ex.toString());
} finally {
if (!result) {
if (destVdi != null) {
try {
destVdi.destroy(conn);
} catch (final Exception e) {
s_logger.debug("Cleaned up leftover VDI on destination storage due to failure: ", e);
}
}
}
if (srcSr != null) {
hypervisorResource.removeSR(conn, srcSr);
}
if (destSr != null) {
hypervisorResource.removeSR(conn, destSr);
}
}
}
use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method directDownloadHttpTemplate.
protected Answer directDownloadHttpTemplate(final CopyCommand cmd, final DecodedDataObject srcObj, final DecodedDataObject destObj) {
final Connection conn = hypervisorResource.getConnection();
SR poolsr = null;
VDI vdi = null;
boolean result = false;
try {
if (destObj.getPath() == null) {
// need to create volume at first
}
vdi = VDI.getByUuid(conn, destObj.getPath());
if (vdi == null) {
throw new CloudRuntimeException("can't find volume: " + destObj.getPath());
}
final String destStoreUuid = destObj.getStore().getUuid();
final Set<SR> srs = SR.getByNameLabel(conn, destStoreUuid);
if (srs.size() != 1) {
throw new CloudRuntimeException("storage uuid: " + destStoreUuid + " is not unique");
}
poolsr = srs.iterator().next();
final VDI.Record vdir = vdi.getRecord(conn);
final String vdiLocation = vdir.location;
String pbdLocation = null;
if (destObj.getStore().getScheme().equalsIgnoreCase(DataStoreProtocol.NFS.toString())) {
pbdLocation = "/run/sr-mount/" + poolsr.getUuid(conn);
} else {
final Set<PBD> pbds = poolsr.getPBDs(conn);
if (pbds.size() != 1) {
throw new CloudRuntimeException("Don't how to handle multiple pbds:" + pbds.size() + " for sr: " + poolsr.getUuid(conn));
}
final PBD pbd = pbds.iterator().next();
final Map<String, String> deviceCfg = pbd.getDeviceConfig(conn);
pbdLocation = deviceCfg.get("location");
}
if (pbdLocation == null) {
throw new CloudRuntimeException("Can't get pbd location");
}
final String vdiPath = pbdLocation + "/" + vdiLocation + ".vhd";
// download a url into vdipath
// downloadHttpToLocalFile(vdiPath, template.getPath());
hypervisorResource.callHostPlugin(conn, "storagePlugin", "downloadTemplateFromUrl", "destPath", vdiPath, "srcUrl", srcObj.getPath());
result = true;
// return new CopyCmdAnswer(cmd, vdi.getUuid(conn));
} catch (final BadServerResponse e) {
s_logger.debug("Failed to download template", e);
} catch (final XenAPIException e) {
s_logger.debug("Failed to download template", e);
} catch (final XmlRpcException e) {
s_logger.debug("Failed to download template", e);
} catch (final Exception e) {
s_logger.debug("Failed to download template", e);
} finally {
if (!result && vdi != null) {
try {
vdi.destroy(conn);
} catch (final BadServerResponse e) {
s_logger.debug("Failed to cleanup newly created vdi");
} catch (final XenAPIException e) {
s_logger.debug("Failed to cleanup newly created vdi");
} catch (final XmlRpcException e) {
s_logger.debug("Failed to cleanup newly created vdi");
}
}
}
return new Answer(cmd, false, "Failed to download template");
}
use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method backupSnapshot.
@Override
public Answer backupSnapshot(final CopyCommand cmd) {
final Connection conn = hypervisorResource.getConnection();
final DataTO srcData = cmd.getSrcTO();
final DataTO cacheData = cmd.getCacheTO();
final DataTO destData = cmd.getDestTO();
final int wait = cmd.getWait();
final String primaryStorageNameLabel = srcData.getDataStore().getUuid();
String secondaryStorageUrl = null;
NfsTO cacheStore = null;
String destPath = null;
if (cacheData != null) {
cacheStore = (NfsTO) cacheData.getDataStore();
secondaryStorageUrl = cacheStore.getUrl();
destPath = cacheData.getPath();
} else {
cacheStore = (NfsTO) destData.getDataStore();
secondaryStorageUrl = cacheStore.getUrl();
destPath = destData.getPath();
}
final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) srcData;
final SnapshotObjectTO snapshotOnImage = (SnapshotObjectTO) destData;
final String snapshotUuid = snapshotTO.getPath();
final String volumeUuid = snapshotTO.getVolume().getPath();
final String prevBackupUuid = snapshotOnImage.getParentSnapshotPath();
final String prevSnapshotUuid = snapshotTO.getParentSnapshotPath();
// By default assume failure
String details = null;
String snapshotBackupUuid = null;
Long physicalSize = null;
final Map<String, String> options = cmd.getOptions();
boolean fullbackup = Boolean.parseBoolean(options.get("fullSnapshot"));
boolean result = false;
try {
final SR primaryStorageSR = hypervisorResource.getSRByNameLabelandHost(conn, primaryStorageNameLabel);
if (primaryStorageSR == null) {
throw new InternalErrorException("Could not backup snapshot because the primary Storage SR could not be created from the name label: " + primaryStorageNameLabel);
}
final String psUuid = primaryStorageSR.getUuid(conn);
final Boolean isISCSI = IsISCSI(primaryStorageSR.getType(conn));
final VDI snapshotVdi = getVDIbyUuid(conn, snapshotUuid);
String snapshotPaUuid = null;
if (prevSnapshotUuid != null && !fullbackup) {
try {
snapshotPaUuid = getVhdParent(conn, psUuid, snapshotUuid, isISCSI);
if (snapshotPaUuid != null) {
final String snashotPaPaPaUuid = getVhdParent(conn, psUuid, snapshotPaUuid, isISCSI);
final String prevSnashotPaUuid = getVhdParent(conn, psUuid, prevSnapshotUuid, isISCSI);
if (snashotPaPaPaUuid != null && prevSnashotPaUuid != null && prevSnashotPaUuid.equals(snashotPaPaPaUuid)) {
fullbackup = false;
} else {
fullbackup = true;
}
}
} catch (final Exception e) {
s_logger.debug("Failed to get parent snapshots, take full snapshot", e);
fullbackup = true;
}
}
final URI uri = new URI(secondaryStorageUrl);
final String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
final DataStoreTO destStore = destData.getDataStore();
final String folder = destPath;
String finalPath = null;
final String localMountPoint = BaseMountPointOnHost + File.separator + UUID.nameUUIDFromBytes(secondaryStorageUrl.getBytes()).toString();
if (fullbackup) {
if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, folder)) {
details = " Filed to create folder " + folder + " in secondary storage";
s_logger.warn(details);
return new CopyCmdAnswer(details);
}
final String snapshotMountpoint = secondaryStorageUrl + "/" + folder;
SR snapshotSr = null;
try {
snapshotSr = hypervisorResource.createNfsSRbyURI(conn, new URI(snapshotMountpoint), false);
final VDI backedVdi = hypervisorResource.cloudVDIcopy(conn, snapshotVdi, snapshotSr, wait);
snapshotBackupUuid = backedVdi.getUuid(conn);
final String primarySRuuid = snapshotSr.getUuid(conn);
physicalSize = getSnapshotSize(conn, primarySRuuid, snapshotBackupUuid, isISCSI, wait);
finalPath = folder + cacheStore.getPathSeparator() + snapshotBackupUuid;
} finally {
if (snapshotSr != null) {
hypervisorResource.removeSR(conn, snapshotSr);
}
}
} else {
final String primaryStorageSRUuid = primaryStorageSR.getUuid(conn);
final String results = backupSnapshot(conn, primaryStorageSRUuid, localMountPoint, folder, secondaryStorageMountPath, snapshotUuid, prevBackupUuid, isISCSI, wait);
final String[] tmp = results.split("#");
snapshotBackupUuid = tmp[1];
physicalSize = Long.parseLong(tmp[2]);
finalPath = folder + cacheStore.getPathSeparator() + snapshotBackupUuid;
}
// delete primary snapshots with only the last one left
destroySnapshotOnPrimaryStorageExceptThis(conn, volumeUuid, snapshotUuid);
final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
newSnapshot.setPath(finalPath);
newSnapshot.setPhysicalSize(physicalSize);
if (fullbackup) {
newSnapshot.setParentSnapshotPath(null);
} else {
newSnapshot.setParentSnapshotPath(prevBackupUuid);
}
result = true;
return new CopyCmdAnswer(newSnapshot);
} catch (final XenAPIException e) {
details = "BackupSnapshot Failed due to " + e.toString();
s_logger.warn(details, e);
} catch (final Exception e) {
details = "BackupSnapshot Failed due to " + e.getMessage();
s_logger.warn(details, e);
} finally {
if (!result) {
// remove last bad primary snapshot when exception happens
try {
destroySnapshotOnPrimaryStorage(conn, snapshotUuid);
} catch (final Exception e) {
s_logger.debug("clean up snapshot failed", e);
}
}
}
return new CopyCmdAnswer(details);
}
use of com.cloud.storage.command.CopyCmdAnswer in project cosmic by MissionCriticalCloud.
the class XenServerStorageProcessor method createVolumeFromSnapshot2.
protected Answer createVolumeFromSnapshot2(final CopyCommand cmd) {
try {
final Connection conn = hypervisorResource.getConnection();
final Map<String, String> srcOptions = cmd.getOptions();
final String src_iScsiName = srcOptions.get(DiskTO.IQN);
final String srcStorageHost = srcOptions.get(DiskTO.STORAGE_HOST);
final String srcChapInitiatorUsername = srcOptions.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String srcChapInitiatorSecret = srcOptions.get(DiskTO.CHAP_INITIATOR_SECRET);
final SR srcSr = hypervisorResource.getIscsiSR(conn, src_iScsiName, srcStorageHost, src_iScsiName, srcChapInitiatorUsername, srcChapInitiatorSecret, false);
final Map<String, String> destOptions = cmd.getOptions2();
final String dest_iScsiName = destOptions.get(DiskTO.IQN);
final String destStorageHost = destOptions.get(DiskTO.STORAGE_HOST);
final String destChapInitiatorUsername = destOptions.get(DiskTO.CHAP_INITIATOR_USERNAME);
final String destChapInitiatorSecret = destOptions.get(DiskTO.CHAP_INITIATOR_SECRET);
final SR destSr = hypervisorResource.getIscsiSR(conn, dest_iScsiName, destStorageHost, dest_iScsiName, destChapInitiatorUsername, destChapInitiatorSecret, false);
// there should only be one VDI in this SR
final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
final VDI vdiCopy = srcVdi.copy(conn, destSr);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setSize(vdiCopy.getVirtualSize(conn));
newVol.setPath(vdiCopy.getUuid(conn));
newVol.setFormat(ImageFormat.VHD);
hypervisorResource.removeSR(conn, srcSr);
hypervisorResource.removeSR(conn, destSr);
return new CopyCmdAnswer(newVol);
} catch (final Exception ex) {
s_logger.warn("Failed to copy snapshot to volume: " + ex.toString(), ex);
return new CopyCmdAnswer(ex.getMessage());
}
}
Aggregations