use of com.cloud.storage.to.SnapshotObjectTO in project cosmic by MissionCriticalCloud.
the class SnapshotObject method processEvent.
@Override
public void processEvent(final ObjectInDataStoreStateMachine.Event event, final Answer answer) {
try {
final SnapshotDataStoreVO snapshotStore = snapshotStoreDao.findByStoreSnapshot(getDataStore().getRole(), getDataStore().getId(), getId());
if (answer instanceof CreateObjectAnswer) {
final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CreateObjectAnswer) answer).getData();
snapshotStore.setInstallPath(snapshotTO.getPath());
snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
} else if (answer instanceof CopyCmdAnswer) {
final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) ((CopyCmdAnswer) answer).getNewData();
snapshotStore.setInstallPath(snapshotTO.getPath());
if (snapshotTO.getPhysicalSize() != null) {
// For S3 delta snapshot, physical size is currently not set
snapshotStore.setPhysicalSize(snapshotTO.getPhysicalSize());
}
if (snapshotTO.getParentSnapshotPath() == null) {
snapshotStore.setParentSnapshotId(0L);
}
snapshotStoreDao.update(snapshotStore.getId(), snapshotStore);
// update side-effect of snapshot operation
if (snapshotTO.getVolume() != null && snapshotTO.getVolume().getPath() != null) {
final VolumeVO vol = volumeDao.findByUuid(snapshotTO.getVolume().getUuid());
if (vol != null) {
s_logger.info("Update volume path change due to snapshot operation, volume " + vol.getId() + " path: " + vol.getPath() + "->" + snapshotTO.getVolume().getPath());
vol.setPath(snapshotTO.getVolume().getPath());
volumeDao.update(vol.getId(), vol);
} else {
s_logger.error("Cound't find the original volume with uuid: " + snapshotTO.getVolume().getUuid());
}
}
} else {
throw new CloudRuntimeException("Unknown answer: " + answer.getClass());
}
} catch (final RuntimeException ex) {
if (event == ObjectInDataStoreStateMachine.Event.OperationFailed) {
objectInStoreMgr.deleteIfNotReady(this);
}
throw ex;
}
this.processEvent(event);
}
use of com.cloud.storage.to.SnapshotObjectTO in project cosmic by MissionCriticalCloud.
the class KvmStorageProcessor method createSnapshot.
@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
final SnapshotObjectTO snapshotTo = (SnapshotObjectTO) cmd.getData();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) snapshotTo.getDataStore();
final VolumeObjectTO volume = snapshotTo.getVolume();
final String snapshotName = UUID.randomUUID().toString();
final String vmName = volume.getVmName();
try {
final Connect conn = LibvirtConnection.getConnectionByVmName(vmName);
DomainState state = null;
Domain vm = null;
if (vmName != null) {
try {
vm = resource.getDomain(conn, vmName);
state = vm.getInfo().state;
} catch (final LibvirtException e) {
logger.trace("Ignoring libvirt error.", e);
}
}
final KvmStoragePool primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
final KvmPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
if (state == DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
final String vmUuid = vm.getUUIDString();
final Object[] args = new Object[] { snapshotName, vmUuid };
final String snapshot = SnapshotXML.format(args);
final long start = System.currentTimeMillis();
vm.snapshotCreateXML(snapshot);
final long total = (System.currentTimeMillis() - start) / 1000;
logger.debug("snapshot takes " + total + " seconds to finish");
/*
* libvirt on RHEL6 doesn't handle resume event emitted from qemu
*/
vm = resource.getDomain(conn, vmName);
state = vm.getInfo().state;
if (state == DomainState.VIR_DOMAIN_PAUSED) {
vm.resume();
}
} else {
if (primaryPool.getType() == StoragePoolType.RBD) {
try {
final Rados r = new Rados(primaryPool.getAuthUserName());
r.confSet("mon_host", primaryPool.getSourceHost() + ":" + primaryPool.getSourcePort());
r.confSet("key", primaryPool.getAuthSecret());
r.confSet("client_mount_timeout", "30");
r.connect();
logger.debug("Succesfully connected to Ceph cluster at " + r.confGet("mon_host"));
final IoCTX io = r.ioCtxCreate(primaryPool.getSourceDir());
final Rbd rbd = new Rbd(io);
final RbdImage image = rbd.open(disk.getName());
logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
image.snapCreate(snapshotName);
rbd.close(image);
r.ioCtxDestroy(io);
} catch (final Exception e) {
logger.error("A RBD snapshot operation on " + disk.getName() + " failed. The error was: " + e.getMessage());
}
} else {
/* VM is not running, create a snapshot by ourself */
final Script command = new Script(manageSnapshotPath, cmdsTimeout, logger);
command.add("-c", disk.getPath());
command.add("-n", snapshotName);
final String result = command.execute();
if (result != null) {
logger.debug("Failed to manage snapshot: " + result);
return new CreateObjectAnswer("Failed to manage snapshot: " + result);
}
}
}
final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
// NOTE: sort of hack, we'd better just put snapshtoName
newSnapshot.setPath(disk.getPath() + File.separator + snapshotName);
return new CreateObjectAnswer(newSnapshot);
} catch (final LibvirtException e) {
logger.debug("Failed to manage snapshot: ", e);
return new CreateObjectAnswer("Failed to manage snapshot: " + e.toString());
}
}
use of com.cloud.storage.to.SnapshotObjectTO in project cosmic by MissionCriticalCloud.
the class LibvirtRevertSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
final SnapshotObjectTO snapshot = command.getData();
final VolumeObjectTO volume = snapshot.getVolume();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
final DataStoreTO snapshotImageStore = snapshot.getDataStore();
if (!(snapshotImageStore instanceof NfsTO)) {
return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
}
final NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
final String volumePath = volume.getPath();
String snapshotPath = null;
String snapshotRelPath = null;
KvmStoragePool secondaryStoragePool = null;
try {
final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
secondaryStoragePool = storagePoolMgr.getStoragePoolByUri(secondaryStoragePoolUrl);
final String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotRelPath = snapshot.getPath();
snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
final KvmPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
final KvmStoragePool primaryPool = snapshotDisk.getPool();
if (primaryPool.getType() == StoragePoolType.RBD) {
return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
} else {
final Script cmd = new Script(libvirtComputingResource.manageSnapshotPath(), libvirtComputingResource.getCmdsTimeout(), s_logger);
cmd.add("-v", snapshotPath);
cmd.add("-n", snapshotDisk.getName());
cmd.add("-p", snapshotDisk.getPath());
final 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 (final CloudRuntimeException e) {
return new Answer(command, false, e.toString());
}
}
use of com.cloud.storage.to.SnapshotObjectTO 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.to.SnapshotObjectTO 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);
}
Aggregations