use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.
the class Ovm3StorageProcessorTest method createSnapshotObjectCommandTest.
@Test
public void createSnapshotObjectCommandTest() throws ConfigurationException {
con = prepare();
String snapuuid = ovmObject.newUuid();
SnapshotObjectTO snap = snapshot(snapuuid, linux.getRepoId(), "", linux.getVirtualDisksDir());
String response = storageplugin.getFileCreateXml().replace(storageplugin.getFileName(), snapuuid + ".raw");
response = response.replace(storageplugin.getPoolUuid(), ovmObject.deDash(linux.getRepoId()));
con.setMethodResponse("storage_plugin_create", results.simpleResponseWrapWrapper(response));
CreateObjectCommand create = new CreateObjectCommand(snap);
Answer ra = hypervisor.executeRequest(create);
results.basicBooleanTest(ra.getResult(), false);
}
use of org.apache.cloudstack.storage.to.SnapshotObjectTO 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)) {
return new Answer(command, false, "revert snapshot on object storage is not implemented yet");
}
NfsTO nfsImageStore = (NfsTO) snapshotImageStore;
String secondaryStoragePoolUrl = nfsImageStore.getUrl();
String volumePath = volume.getPath();
String snapshotPath = null;
String snapshotRelPath = null;
KVMStoragePool secondaryStoragePool = null;
try {
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotRelPath = snapshot.getPath();
snapshotPath = ssPmountPath + File.separator + snapshotRelPath;
KVMPhysicalDisk snapshotDisk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volumePath);
KVMStoragePool primaryPool = snapshotDisk.getPool();
if (primaryPool.getType() == StoragePoolType.RBD) {
return new Answer(command, false, "revert snapshot to RBD is not implemented yet");
} else {
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());
}
}
use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.
the class Ovm3StorageProcessor method createSnapshot.
/**
* Creates a snapshot from a volume, but only if the VM is stopped.
* This due qemu not being able to snap raw volumes.
*
* if stopped yes, if running ... no, unless we have ocfs2 when
* using raw partitions (file:) if using tap:aio we cloud...
* The "ancient" way:
* We do however follow the "two stage" approach, of "snap"
* on primary first, with the create object... and then
* backup the snapshot with the copycmd....
* (should transfer to createSnapshot, backupSnapshot)
*/
@Override
public Answer createSnapshot(CreateObjectCommand cmd) {
LOGGER.debug("execute createSnapshot: " + cmd.getClass());
DataTO data = cmd.getData();
Xen xen = new Xen(c);
SnapshotObjectTO snap = (SnapshotObjectTO) data;
VolumeObjectTO vol = snap.getVolume();
try {
Xen.Vm vm = xen.getVmConfig(snap.getVmName());
if (vm != null) {
return new CreateObjectAnswer("Snapshot object creation not supported for running VMs." + snap.getVmName());
}
Linux host = new Linux(c);
String uuid = host.newUuid();
/* for root volumes this works... */
String src = vol.getPath() + "/" + vol.getUuid() + ".raw";
String dest = vol.getPath() + "/" + uuid + ".raw";
/* seems that sometimes the path is already contains a file
* in case, we just replace it.... (Seems to happen if not ROOT)
*/
if (vol.getPath().contains(vol.getUuid())) {
src = getVirtualDiskPath(vol.getUuid(), data.getDataStore().getUuid());
dest = src.replace(vol.getUuid(), uuid);
}
LOGGER.debug("Snapshot " + src + " to " + dest);
host.copyFile(src, dest);
SnapshotObjectTO nsnap = new SnapshotObjectTO();
// nsnap.setPath(dest);
// move to something that looks the same as xenserver.
nsnap.setPath(uuid);
return new CreateObjectAnswer(nsnap);
} catch (Ovm3ResourceException e) {
return new CreateObjectAnswer("Snapshot object creation failed. " + e.getMessage());
}
}
use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.
the class SimulatorStorageProcessor method createVolumeFromSnapshot.
@Override
public Answer createVolumeFromSnapshot(CopyCommand cmd) {
DataTO srcData = cmd.getSrcTO();
SnapshotObjectTO snapshot = (SnapshotObjectTO) srcData;
String snapshotPath = snapshot.getPath();
int index = snapshotPath.lastIndexOf("/");
String snapshotName = snapshotPath.substring(index + 1);
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(snapshotName);
return new CopyCmdAnswer(newVol);
}
use of org.apache.cloudstack.storage.to.SnapshotObjectTO in project cloudstack by apache.
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);
DomainInfo.DomainState state = null;
Domain vm = null;
if (vmName != null) {
try {
vm = resource.getDomain(conn, vmName);
state = vm.getInfo().state;
} catch (final LibvirtException e) {
s_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 == DomainInfo.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;
s_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 == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
vm.resume();
}
} else {
/**
* For RBD we can't use libvirt to do our snapshotting or any Bash scripts.
* libvirt also wants to store the memory contents of the Virtual Machine,
* but that's not possible with RBD since there is no way to store the memory
* contents in RBD.
*
* So we rely on the Java bindings for RBD to create our snapshot
*
* This snapshot might not be 100% consistent due to writes still being in the
* memory of the Virtual Machine, but if the VM runs a kernel which supports
* barriers properly (>2.6.32) this won't be any different then pulling the power
* cord out of a running machine.
*/
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();
s_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());
s_logger.debug("Attempting to create RBD snapshot " + disk.getName() + "@" + snapshotName);
image.snapCreate(snapshotName);
rbd.close(image);
r.ioCtxDestroy(io);
} catch (final Exception e) {
s_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, s_logger);
command.add("-c", disk.getPath());
command.add("-n", snapshotName);
final String result = command.execute();
if (result != null) {
s_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) {
s_logger.debug("Failed to manage snapshot: ", e);
return new CreateObjectAnswer("Failed to manage snapshot: " + e.toString());
}
}
Aggregations