use of com.vmware.vim25.VirtualDevice 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.VirtualDevice 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.VirtualDevice in project cloudstack by apache.
the class VirtualMachineMO method attachDisk.
public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController) throws Exception {
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain) + ", datastore: " + morDs.getValue());
int controllerKey = 0;
int unitNumber = 0;
if (DiskControllerType.getType(diskController) == DiskControllerType.ide) {
// IDE virtual disk cannot be added if VM is running
if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
throw new Exception("Adding a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. Please re-try when VM is not running.");
}
// Get next available unit number and controller key
int ideDeviceCount = getNumberOfIDEDevices();
if (ideDeviceCount >= VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER) {
throw new Exception("Maximum limit of devices supported on IDE controllers [" + VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER + "] is reached.");
}
controllerKey = getIDEControllerKey(ideDeviceCount);
unitNumber = getFreeUnitNumberOnIDEController(controllerKey);
} else {
controllerKey = getScsiDiskControllerKey(diskController);
unitNumber = -1;
}
synchronized (_mor.getValue().intern()) {
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, controllerKey, vmdkDatastorePathChain, morDs, unitNumber, 1);
controllerKey = newDisk.getControllerKey();
unitNumber = newDisk.getUnitNumber();
VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) newDisk.getBacking();
String vmdkFileName = backingInfo.getFileName();
DiskControllerType diskControllerType = DiskControllerType.getType(diskController);
VmdkAdapterType vmdkAdapterType = VmdkAdapterType.getAdapterType(diskControllerType);
if (vmdkAdapterType == VmdkAdapterType.none) {
String message = "Failed to attach disk due to invalid vmdk adapter type for vmdk file [" + vmdkFileName + "] with controller : " + diskControllerType;
s_logger.debug(message);
throw new Exception(message);
}
updateVmdkAdapter(vmdkFileName, vmdkAdapterType.toString());
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(newDisk);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
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 - attachDisk() done(failed)");
throw new Exception("Failed to attach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
}
_context.waitForTaskProgressDone(morTask);
}
if (s_logger.isTraceEnabled())
s_logger.trace("vCenter API trace - attachDisk() done(successfully)");
}
use of com.vmware.vim25.VirtualDevice 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.VirtualDevice in project cloudstack by apache.
the class VirtualMachineMO method getVmdkFileBaseNames.
public List<String> getVmdkFileBaseNames() throws Exception {
List<String> vmdkFileBaseNames = new ArrayList<String>();
VirtualDevice[] devices = getAllDiskDevice();
for (VirtualDevice device : devices) {
if (device instanceof VirtualDisk) {
vmdkFileBaseNames.add(getVmdkFileBaseName((VirtualDisk) device));
}
}
return vmdkFileBaseNames;
}
Aggregations