use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.
the class KVMStorageProcessor method createVolume.
@Override
public Answer createVolume(final CreateObjectCommand cmd) {
final VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
final PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO) volume.getDataStore();
KVMStoragePool primaryPool = null;
KVMPhysicalDisk vol = null;
long disksize;
try {
primaryPool = storagePoolMgr.getStoragePool(primaryStore.getPoolType(), primaryStore.getUuid());
disksize = volume.getSize();
PhysicalDiskFormat format;
if (volume.getFormat() == null) {
format = primaryPool.getDefaultFormat();
} else {
format = PhysicalDiskFormat.valueOf(volume.getFormat().toString().toUpperCase());
}
vol = primaryPool.createPhysicalDisk(volume.getUuid(), format, volume.getProvisioningType(), disksize);
final VolumeObjectTO newVol = new VolumeObjectTO();
if (vol != null) {
newVol.setPath(vol.getName());
}
newVol.setSize(volume.getSize());
newVol.setFormat(ImageFormat.valueOf(format.toString().toUpperCase()));
return new CreateObjectAnswer(newVol);
} catch (final Exception e) {
s_logger.debug("Failed to create volume: ", e);
return new CreateObjectAnswer(e.toString());
}
}
use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.
the class VmwareStorageProcessor method createVolume.
@Override
public Answer createVolume(CreateObjectCommand cmd) {
VolumeObjectTO volume = (VolumeObjectTO) cmd.getData();
DataStoreTO primaryStore = volume.getDataStore();
try {
VmwareContext context = hostService.getServiceContext(null);
VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
if (morDatastore == null) {
throw new Exception("Unable to find datastore in vSphere");
}
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
// create data volume
VirtualMachineMO vmMo = null;
String volumeUuid = UUID.randomUUID().toString().replace("-", "");
String volumeDatastorePath = dsMo.getDatastorePath(volumeUuid + ".vmdk");
String dummyVmName = hostService.getWorkerName(context, cmd, 0);
try {
s_logger.info("Create worker VM " + dummyVmName);
vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
synchronized (this) {
try {
vmMo.createDisk(volumeDatastorePath, (int) (volume.getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey());
vmMo.detachDisk(volumeDatastorePath, false);
} catch (Exception e) {
s_logger.error("Deleting file " + volumeDatastorePath + " due to error: " + e.getMessage());
VmwareStorageLayoutHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo);
throw new CloudRuntimeException("Unable to create volume due to: " + e.getMessage());
}
}
VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setPath(volumeUuid);
newVol.setSize(volume.getSize());
return new CreateObjectAnswer(newVol);
} finally {
s_logger.info("Destroy dummy VM after volume creation");
if (vmMo != null) {
vmMo.detachAllDisks();
vmMo.destroy();
}
}
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
hostService.invalidateServiceContext(null);
}
String msg = "create volume failed due to " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new CreateObjectAnswer(e.toString());
}
}
use of org.apache.cloudstack.storage.command.CreateObjectAnswer 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.command.CreateObjectAnswer in project cloudstack by apache.
the class XenServerStorageProcessor method createVolume.
@Override
public Answer createVolume(final CreateObjectCommand cmd) {
final DataTO data = cmd.getData();
final VolumeObjectTO volume = (VolumeObjectTO) data;
try {
final Connection conn = hypervisorResource.getConnection();
final SR poolSr = hypervisorResource.getStorageRepository(conn, data.getDataStore().getUuid());
VDI.Record vdir = new VDI.Record();
vdir.nameLabel = volume.getName();
vdir.SR = poolSr;
vdir.type = Types.VdiType.USER;
vdir.virtualSize = volume.getSize();
VDI vdi;
vdi = VDI.create(conn, vdir);
vdir = vdi.getRecord(conn);
final VolumeObjectTO newVol = new VolumeObjectTO();
newVol.setName(vdir.nameLabel);
newVol.setSize(vdir.virtualSize);
newVol.setPath(vdir.uuid);
return new CreateObjectAnswer(newVol);
} catch (final Exception e) {
s_logger.debug("create volume failed: " + e.toString());
return new CreateObjectAnswer(e.toString());
}
}
use of org.apache.cloudstack.storage.command.CreateObjectAnswer in project cloudstack by apache.
the class Ovm3StorageProcessor method execute.
public Answer execute(CreateObjectCommand cmd) {
LOGGER.debug("execute: " + cmd.getClass());
DataTO data = cmd.getData();
if (data.getObjectType() == DataObjectType.VOLUME) {
return createVolume(cmd);
} else if (data.getObjectType() == DataObjectType.SNAPSHOT) {
return createSnapshot(cmd);
} else if (data.getObjectType() == DataObjectType.TEMPLATE) {
LOGGER.debug("Template object creation not supported.");
}
return new CreateObjectAnswer(data.getObjectType() + " object creation not supported");
}
Aggregations