简体   繁体   中英

Modify VM disk size using VI Java API

I've been tasked with developing a "common" web interface to various administrative tasks, and I'm currently working on virtual machine creation / cloning / configuration. Specifically, I'm trying to figure out how to modify an existing machine's hard disk size using the VI Java API ( http://vijava.sourceforge.net/ ).

I can see exmaples of how to remove disks ( http://sourceforge.net/p/vijava/code/HEAD/tree/trunk/src/com/vmware/vim25/mo/samples/vm/RemoveVmDisk.java ) and add disks ( http://sourceforge.net/p/vijava/code/HEAD/tree/trunk/src/com/vmware/vim25/mo/samples/vm/VmDiskOp.java ), but I can't quite see how to modify an existing disk.

I think I need to get a MOR to the existing disk and then somehow transform that into a VirtualDeviceConfigSpec which I then shove into a VirtualMachineConfigSpec... am I on the right track? Can someone give me a pointer on where to go with this?

The main change will be

diskSpec.setOperation(VirtualDeviceConfigSpecOperation.edit);

But I'm not sure this will be create or replace

diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.replace)

Have a look at the object VirtualDeviceConfigSpecFileOperation

With a starting helper from Reuben, here's what I cane up with:

VirtualMachine vm = (VirtualMachine) new \
      InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", guestName);
VirtualMachineConfigInfo vmci = vm.getConfig();
VirtualDevice [] devices = vmci.getHardware().getDevice();
VirtualDisk theDisk = null;
for ( int i=0; devices!=null && i<devices.length; i++) {
  if ( devices[i].getDeviceInfo().getLabel().equals("Hard disk 1")) {
    theDisk = (VirtualDisk)devices[i];
  }
}
if ( theDisk == null ) {
  return "False - disk not found";
}
theDisk.setCapacityInKB(Long.parseLong(diskSize)*1024*1024);
VirtualDeviceConfigSpec vdcs = new VirtualDeviceConfigSpec();
vdcs.setDevice(theDisk);
vdcs.setOperation(VirtualDeviceConfigSpecOperation.edit);
VirtualMachineConfigSpec vmcs = new VirtualMachineConfigSpec();
vmcs.setDeviceChange(new VirtualDeviceConfigSpec[]{vdcs});
Task task = vm.reconfigVM_Task(vmcs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM