use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.
the class VirtualMachineMO method getAllDiskDevice.
public VirtualDisk[] getAllDiskDevice() throws Exception {
List<VirtualDisk> deviceList = new ArrayList<VirtualDisk>();
List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
if (devices != null && devices.size() > 0) {
for (VirtualDevice device : devices) {
if (device instanceof VirtualDisk) {
deviceList.add((VirtualDisk) device);
}
}
}
return deviceList.toArray(new VirtualDisk[0]);
}
use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.
the class VirtualMachineMO method getDiskDatastorePathChain.
@Deprecated
public List<Pair<String, ManagedObjectReference>> getDiskDatastorePathChain(VirtualDisk disk, boolean followChain) throws Exception {
VirtualDeviceBackingInfo backingInfo = disk.getBacking();
if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
throw new Exception("Unsupported VirtualDeviceBackingInfo");
}
List<Pair<String, ManagedObjectReference>> pathList = new ArrayList<Pair<String, ManagedObjectReference>>();
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
if (!followChain) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
return pathList;
}
Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
VirtualMachineFileInfo vmFilesInfo = getFileInfo();
DatastoreFile snapshotDirFile = new DatastoreFile(vmFilesInfo.getSnapshotDirectory());
DatastoreFile vmxDirFile = new DatastoreFile(vmFilesInfo.getVmPathName());
do {
if (diskBackingInfo.getParent() != null) {
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
diskBackingInfo = diskBackingInfo.getParent();
} else {
// try getting parent info from VMDK file itself
byte[] content = null;
try {
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), diskBackingInfo.getFileName());
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
} catch (Exception e) {
// if snapshot directory has been changed to place other than default. VMware has a bug
// that its corresponding disk backing info is not updated correctly. therefore, we will try search
// in snapshot directory one more time
DatastoreFile currentFile = new DatastoreFile(diskBackingInfo.getFileName());
String vmdkFullDsPath = snapshotDirFile.getCompanionPath(currentFile.getFileName());
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), vmdkFullDsPath);
content = getContext().getResourceContent(url);
if (content == null || content.length == 0) {
break;
}
pathList.add(new Pair<String, ManagedObjectReference>(vmdkFullDsPath, diskBackingInfo.getDatastore()));
}
VmdkFileDescriptor descriptor = new VmdkFileDescriptor();
descriptor.parse(content);
if (descriptor.getParentFileName() != null && !descriptor.getParentFileName().isEmpty()) {
// create a fake one
VirtualDiskFlatVer2BackingInfo parentDiskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
parentDiskBackingInfo.setDatastore(diskBackingInfo.getDatastore());
String parentFileName = descriptor.getParentFileName();
if (parentFileName.startsWith("/")) {
int fileNameStartPos = parentFileName.lastIndexOf("/");
parentFileName = parentFileName.substring(fileNameStartPos + 1);
parentDiskBackingInfo.setFileName(vmxDirFile.getCompanionPath(parentFileName));
} else {
parentDiskBackingInfo.setFileName(snapshotDirFile.getCompanionPath(parentFileName));
}
diskBackingInfo = parentDiskBackingInfo;
} else {
break;
}
}
} while (diskBackingInfo != null);
return pathList;
}
use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.
the class VirtualMachineMO method getFreeUnitNumberOnIDEController.
public int getFreeUnitNumberOnIDEController(int controllerKey) throws Exception {
int freeUnitNumber = 0;
List<VirtualDevice> devices = (List<VirtualDevice>) _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
int deviceCount = 0;
int ideDeviceUnitNumber = -1;
if (devices != null && devices.size() > 0) {
for (VirtualDevice device : devices) {
if (device instanceof VirtualDisk && (controllerKey == device.getControllerKey())) {
deviceCount++;
ideDeviceUnitNumber = device.getUnitNumber();
}
}
}
if (deviceCount == 1) {
if (ideDeviceUnitNumber == 0) {
freeUnitNumber = 1;
}
// else freeUnitNumber is already initialized to 0
} else if (deviceCount == 2) {
throw new Exception("IDE controller with key [" + controllerKey + "] already has 2 device attached. Cannot attach more than the limit of 2.");
}
return freeUnitNumber;
}
use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.
the class VirtualMachineMO method getDiskCurrentTopBackingFileInChain.
public String getDiskCurrentTopBackingFileInChain(String deviceBusName) throws Exception {
List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
if (devices != null && devices.size() > 0) {
for (VirtualDevice device : devices) {
if (device instanceof VirtualDisk) {
s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber());
VirtualDeviceBackingInfo backingInfo = ((VirtualDisk) device).getBacking();
if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
String deviceNumbering = getDeviceBusName(devices, device);
if (deviceNumbering.equals(deviceBusName))
return diskBackingInfo.getFileName();
}
}
}
}
return null;
}
use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.
the class VirtualMachineMO method detachDisk.
// vmdkDatastorePath: [datastore name] vmdkFilePath
public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastorePath, boolean deleteBackingFile) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + vmdkDatastorePath + ", deleteBacking: " + deleteBackingFile);
// Note: if VM has been taken snapshot, original backing file will be renamed, therefore, when we try to find the matching
// VirtualDisk, we only perform prefix matching
Pair<VirtualDisk, String> deviceInfo = getDiskDevice(vmdkDatastorePath);
if (deviceInfo == null) {
s_logger.warn("vCenter API trace - detachDisk() done (failed)");
throw new Exception("No such disk device: " + vmdkDatastorePath);
}
// IDE virtual disk cannot be detached if VM is running
if (deviceInfo.second() != null && deviceInfo.second().contains("ide")) {
if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
throw new Exception("Removing a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. " + "Please re-try when VM is not running.");
}
}
List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(deviceInfo.first());
if (deleteBackingFile) {
deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.DESTROY);
}
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
reConfigSpec.getDeviceChange().add(deviceConfigSpec);
ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
boolean result = _context.getVimClient().waitForTask(morTask);
if (!result) {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachDisk() done (failed)");
throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
// VMware does not update snapshot references to the detached disk, we have to work around it
SnapshotDescriptor snapshotDescriptor = null;
try {
snapshotDescriptor = getSnapshotDescriptor();
} catch (Exception e) {
s_logger.info("Unable to retrieve snapshot descriptor, will skip updating snapshot reference");
}
if (snapshotDescriptor != null) {
for (Pair<String, ManagedObjectReference> pair : chain) {
DatastoreFile dsFile = new DatastoreFile(pair.first());
snapshotDescriptor.removeDiskReferenceFromSnapshot(dsFile.getFileName());
}
Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
String dsPath = getSnapshotDescriptorDatastorePath();
assert (dsPath != null);
String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), dsPath);
getContext().uploadResourceContent(url, snapshotDescriptor.getVmsdContent());
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - detachDisk() done (successfully)");
return chain;
}
Aggregations