use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class VmwareStorageManagerImpl method execute.
@Override
public RevertToVMSnapshotAnswer execute(VmwareHostService hostService, RevertToVMSnapshotCommand cmd) {
String snapshotName = cmd.getTarget().getSnapshotName();
String vmName = cmd.getVmName();
Boolean snapshotMemory = cmd.getTarget().getType() == VMSnapshot.Type.DiskAndMemory;
List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
VirtualMachine.PowerState vmState = VirtualMachine.PowerState.PowerOn;
VirtualMachineMO vmMo = null;
VmwareContext context = hostService.getServiceContext(cmd);
try {
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
// wait if there are already VM revert task running
ManagedObjectReference taskmgr = context.getServiceContent().getTaskManager();
List<ManagedObjectReference> tasks = context.getVimClient().getDynamicProperty(taskmgr, "recentTask");
for (ManagedObjectReference taskMor : tasks) {
TaskInfo info = (TaskInfo) (context.getVimClient().getDynamicProperty(taskMor, "info"));
if (info.getEntityName().equals(cmd.getVmName()) && StringUtils.isNotBlank(info.getName()) && info.getName().equalsIgnoreCase("RevertToSnapshot_Task")) {
s_logger.debug("There is already a VM snapshot task running, wait for it");
context.getVimClient().waitForTask(taskMor);
}
}
HostMO hostMo = (HostMO) hyperHost;
vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
}
if (vmMo == null) {
String msg = "Unable to find VM for RevertToVMSnapshotCommand";
s_logger.debug(msg);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} else {
if (cmd.isReloadVm()) {
vmMo.reload();
}
boolean result = false;
if (snapshotName != null) {
ManagedObjectReference morSnapshot = vmMo.getSnapshotMor(snapshotName);
result = hostMo.revertToSnapshot(morSnapshot);
} else {
return new RevertToVMSnapshotAnswer(cmd, false, "Unable to find the snapshot by name " + snapshotName);
}
if (result) {
Map<String, String> mapNewDisk = getNewDiskMap(vmMo);
setVolumeToPathAndSize(listVolumeTo, mapNewDisk, context, hyperHost, cmd.getVmName());
if (!snapshotMemory) {
vmState = VirtualMachine.PowerState.PowerOff;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} else {
return new RevertToVMSnapshotAnswer(cmd, false, "Error while reverting to snapshot due to execute in ESXi");
}
}
} catch (Exception e) {
String msg = "revert vm " + vmName + " to snapshot " + snapshotName + " failed due to " + e.getMessage();
s_logger.error(msg);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
}
}
use of org.apache.cloudstack.storage.to.VolumeObjectTO 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.VolumeObjectTO 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.VolumeObjectTO in project cloudstack by apache.
the class CitrixResourceBase method createWorkingVM.
public VM createWorkingVM(final Connection conn, final String vmName, final String guestOSType, final String platformEmulator, final List<VolumeObjectTO> listVolumeTo) throws BadServerResponse, Types.VmBadPowerState, Types.SrFull, Types.OperationNotAllowed, XenAPIException, XmlRpcException {
// below is redundant but keeping for consistency and code readabilty
final String guestOsTypeName = platformEmulator;
if (guestOsTypeName == null) {
final String msg = " Hypervisor " + this.getClass().getName() + " doesn't support guest OS type " + guestOSType + ". you can choose 'Other install media' to run it as HVM";
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
final VM template = getVM(conn, guestOsTypeName);
final VM vm = template.createClone(conn, vmName);
vm.setIsATemplate(conn, false);
final Map<VDI, VolumeObjectTO> vdiMap = new HashMap<VDI, VolumeObjectTO>();
for (final VolumeObjectTO volume : listVolumeTo) {
final String vdiUuid = volume.getPath();
try {
final VDI vdi = VDI.getByUuid(conn, vdiUuid);
vdiMap.put(vdi, volume);
} catch (final Types.UuidInvalid e) {
s_logger.warn("Unable to find vdi by uuid: " + vdiUuid + ", skip it");
}
}
for (final Map.Entry<VDI, VolumeObjectTO> entry : vdiMap.entrySet()) {
final VDI vdi = entry.getKey();
final VolumeObjectTO volumeTO = entry.getValue();
final VBD.Record vbdr = new VBD.Record();
vbdr.VM = vm;
vbdr.VDI = vdi;
if (volumeTO.getVolumeType() == Volume.Type.ROOT) {
vbdr.bootable = true;
vbdr.unpluggable = false;
} else {
vbdr.bootable = false;
vbdr.unpluggable = true;
}
vbdr.userdevice = "autodetect";
vbdr.mode = Types.VbdMode.RW;
vbdr.type = Types.VbdType.DISK;
Long deviceId = volumeTO.getDeviceId();
if (deviceId != null && (!isDeviceUsed(conn, vm, deviceId) || deviceId > 3)) {
vbdr.userdevice = deviceId.toString();
}
VBD.create(conn, vbdr);
}
return vm;
}
use of org.apache.cloudstack.storage.to.VolumeObjectTO in project cloudstack by apache.
the class CitrixResourceBase method mount.
protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
final DataTO data = volume.getData();
final Volume.Type type = volume.getType();
if (type == Volume.Type.ISO) {
final TemplateObjectTO iso = (TemplateObjectTO) data;
final DataStoreTO store = iso.getDataStore();
if (store == null) {
// It's a fake iso
return null;
}
// corer case, xenserver pv driver iso
final String templateName = iso.getName();
if (templateName.startsWith("xs-tools")) {
try {
final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
if (vdis.isEmpty()) {
throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
}
return vdis.iterator().next();
} catch (final XenAPIException e) {
throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
} catch (final Exception e) {
throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
}
}
if (!(store instanceof NfsTO)) {
throw new CloudRuntimeException("only support mount iso on nfs");
}
final NfsTO nfsStore = (NfsTO) store;
final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
final int index = isoPath.lastIndexOf("/");
final String mountpoint = isoPath.substring(0, index);
URI uri;
try {
uri = new URI(mountpoint);
} catch (final URISyntaxException e) {
throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
}
final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
final String isoname = isoPath.substring(index + 1);
final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
if (isoVdi == null) {
throw new CloudRuntimeException("Unable to find ISO " + isoPath);
}
return isoVdi;
} else {
final VolumeObjectTO vol = (VolumeObjectTO) data;
return VDI.getByUuid(conn, vol.getPath());
}
}
Aggregations