use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.
the class LibvirtRevertSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
SnapshotObjectTO snapshot = command.getData();
VolumeObjectTO volume = snapshot.getVolume();
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
DataStoreTO snapshotImageStore = snapshot.getDataStore();
if (!(snapshotImageStore instanceof NfsTO) && primaryStore.getPoolType() != StoragePoolType.RBD) {
return new Answer(command, false, String.format("Revert snapshot does not support storage pool of type [%s]. Revert snapshot is supported by storage pools of type 'NFS' or 'RBD'", primaryStore.getPoolType()));
}
String volumePath = volume.getPath();
String snapshotPath = null;
String snapshotRelPath = null;
KVMStoragePool secondaryStoragePool = null;
try {
snapshotRelPath = snapshot.getPath();
KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
KVMStoragePool primaryPool = snapshotDisk.getPool();
if (primaryPool.getType() == StoragePoolType.RBD) {
Rados rados = new Rados(primaryPool.getAuthUserName());
rados.confSet(MON_HOST, primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
rados.confSet(KEY, primaryPool.getAuthSecret());
rados.confSet(CLIENT_MOUNT_TIMEOUT, RADOS_CONNECTION_TIMEOUT);
rados.connect();
String[] rbdPoolAndVolumeAndSnapshot = snapshotRelPath.split("/");
int snapshotIndex = rbdPoolAndVolumeAndSnapshot.length - 1;
String rbdSnapshotId = rbdPoolAndVolumeAndSnapshot[snapshotIndex];
IoCTX io = rados.ioCtxCreate(primaryPool.getSourceDir());
Rbd rbd = new Rbd(io);
s_logger.debug(String.format("Attempting to rollback RBD snapshot [name:%s], [volumeid:%s], [snapshotid:%s]", snapshot.getName(), volumePath, rbdSnapshotId));
RbdImage image = rbd.open(volumePath);
image.snapRollBack(rbdSnapshotId);
rbd.close(image);
rados.ioCtxDestroy(io);
} else {
NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
String secondaryStoragePoolUrl = nfsImageStore.getUrl();
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
cmd.add("-v", snapshotPath);
cmd.add("-n", snapshotDisk.getName());
cmd.add("-p", snapshotDisk.getPath());
String result = cmd.execute();
if (result != null) {
s_logger.debug("Failed to revert snaptshot: " + result);
return new Answer(command, false, result);
}
}
return new Answer(command, true, "RevertSnapshotCommand executes successfully");
} catch (CloudRuntimeException e) {
return new Answer(command, false, e.toString());
} catch (RadosException e) {
s_logger.error("Failed to connect to Rados pool while trying to revert snapshot. Exception: ", e);
return new Answer(command, false, e.toString());
} catch (RbdException e) {
s_logger.error("Failed to connect to revert snapshot due to RBD exception: ", e);
return new Answer(command, false, e.toString());
}
}
use of org.apache.cloudstack.storage.to.PrimaryDataStoreTO in project cloudstack by apache.
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 */
s_logger.debug("Copying template to primary storage, template format is " + tmplVol.getFormat());
final KVMStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
KVMPhysicalDisk primaryVol = null;
if (destData instanceof VolumeObjectTO) {
final VolumeObjectTO volume = (VolumeObjectTO) destData;
// pass along volume's target size if it's bigger than template's size, for storage types that copy template rather than cloning on deploy
if (volume.getSize() != null && volume.getSize() > tmplVol.getVirtualSize()) {
s_logger.debug("Using configured size of " + toHumanReadableSize(volume.getSize()));
tmplVol.setSize(volume.getSize());
tmplVol.setVirtualSize(volume.getSize());
} else {
s_logger.debug("Using template's size of " + toHumanReadableSize(tmplVol.getVirtualSize()));
}
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, volume.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
} else if (destData instanceof TemplateObjectTO) {
TemplateObjectTO destTempl = (TemplateObjectTO) destData;
Map<String, String> details = primaryStore.getDetails();
String path = details != null ? details.get("managedStoreTarget") : null;
if (!storagePoolMgr.connectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path, details)) {
s_logger.warn("Failed to connect physical disk at path: " + path + ", in storage pool id: " + primaryStore.getUuid());
}
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, path != null ? path : destTempl.getUuid(), primaryPool, cmd.getWaitInMillSeconds());
if (!storagePoolMgr.disconnectPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), path)) {
s_logger.warn("Failed to disconnect physical disk at path: " + path + ", in storage pool id: " + primaryStore.getUuid());
}
} else {
primaryVol = storagePoolMgr.copyPhysicalDisk(tmplVol, UUID.randomUUID().toString(), primaryPool, cmd.getWaitInMillSeconds());
}
DataTO data = null;
/**
* Force the ImageFormat for RBD templates to RAW
*/
if (destData.getObjectType() == DataObjectType.TEMPLATE) {
final TemplateObjectTO newTemplate = new TemplateObjectTO();
newTemplate.setPath(primaryVol.getName());
newTemplate.setSize(primaryVol.getSize());
if (primaryPool.getType() == StoragePoolType.RBD || primaryPool.getType() == StoragePoolType.PowerFlex || primaryPool.getType() == StoragePoolType.Linstor) {
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) {
s_logger.debug("Failed to clean up secondary storage", e);
}
}
}
Aggregations