DiskProfile dskch = cmd.getDiskCharacteristics();
try {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid());
if (morDatastore == null)
throw new Exception("Unable to find datastore in vSphere");
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
if (dskch.getType() == Volume.Type.ROOT) {
// attach volume id to make the name unique
String vmdkName = dskch.getName() + "-" + dskch.getVolumeId();
if (cmd.getTemplateUrl() == null) {
// create a root volume for blank VM
String dummyVmName = getWorkerName(context, cmd, 0);
VirtualMachineMO vmMo = null;
try {
vmMo = prepareVolumeHostDummyVm(hyperHost, dsMo, dummyVmName);
if (vmMo == null) {
throw new Exception("Unable to create a dummy VM for volume creation");
}
String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), vmdkName);
synchronized (this) {
s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
VmwareHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo);
vmMo.createDisk(volumeDatastorePath, (int) (dskch.getSize() / (1024L * 1024L)), morDatastore, -1);
vmMo.detachDisk(volumeDatastorePath, false);
}
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), vmdkName, dskch.getSize(), null);
return new CreateAnswer(cmd, vol);
} finally {
vmMo.detachAllDisks();
s_logger.info("Destroy dummy VM after volume creation");
vmMo.destroy();
}
} else {
VirtualMachineMO vmTemplate = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(cmd.getTemplateUrl()), true);
if (vmTemplate == null) {
s_logger.warn("Template host in vSphere is not in connected state, request template reload");
return new CreateAnswer(cmd, "Template host in vSphere is not in connected state, request template reload", true);
}
ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
ManagedObjectReference morBaseSnapshot = vmTemplate.getSnapshotMor("cloud.template.base");
if (morBaseSnapshot == null) {
String msg = "Unable to find template base snapshot, invalid template";
s_logger.error(msg);
throw new Exception(msg);
}
if(dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmdkName))
dsMo.deleteFile(String.format("[%s] %s/", dsMo.getName(), vmdkName), dcMo.getMor(), false);
s_logger.info("create linked clone from template");
if (!vmTemplate.createLinkedClone(vmdkName, morBaseSnapshot, dcMo.getVmFolder(), morPool, morDatastore)) {
String msg = "Unable to clone from the template";
s_logger.error(msg);
throw new Exception(msg);
}
VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(vmdkName);
assert (vmMo != null);
// we can't rely on un-offical API (VirtualMachineMO.moveAllVmDiskFiles() any more, use hard-coded disk names that we know
// to move files
s_logger.info("Move volume out of volume-wrapper VM ");
dsMo.moveDatastoreFile(String.format("[%s] %s/%s.vmdk", dsMo.getName(), vmdkName, vmdkName),
dcMo.getMor(), dsMo.getMor(),
String.format("[%s] %s.vmdk", dsMo.getName(), vmdkName), dcMo.getMor(), true);
dsMo.moveDatastoreFile(String.format("[%s] %s/%s-delta.vmdk", dsMo.getName(), vmdkName, vmdkName),
dcMo.getMor(), dsMo.getMor(),
String.format("[%s] %s-delta.vmdk", dsMo.getName(), vmdkName), dcMo.getMor(), true);
s_logger.info("detach disks from volume-wrapper VM " + vmdkName);
vmMo.detachAllDisks();
s_logger.info("destroy volume-wrapper VM " + vmdkName);
vmMo.destroy();
String srcFile = String.format("[%s] %s/", dsMo.getName(), vmdkName);
dsMo.deleteFile(srcFile, dcMo.getMor(), true);
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), vmdkName, dskch.getSize(), null);
return new CreateAnswer(cmd, vol);
}
} else {