vSphere SDK for Java - 为虚拟机添加磁盘
示例代码:
import com.vmware.vim25.*; import com.vmware.vim25.mo.*; import org.springframework.stereotype.Service; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import java.util.concurrent.atomic.AtomicInteger; @Service public class VirtualMachineService { private ServiceInstance vCenterServiceInstance; private InventoryNavigator inventoryNavigator; public VirtualMachineService() { vCenterServiceInstance = this.vcInit(); inventoryNavigator = new InventoryNavigator(vCenterServiceInstance.getRootFolder()); } public ServiceInstance vcInit(){ try { return new ServiceInstance(new URL("https://10.110.164.133/sdk"), "administrator@vsphere.local", "VMware1!", true); } catch (MalformedURLException | RemoteException e) { e.printStackTrace(); } return null; }private VirtualMachine getVirtualMachineByName(String virtualMachineName) throws RemoteException { return (VirtualMachine) inventoryNavigator.searchManagedEntity("VirtualMachine", virtualMachineName); }public static VirtualDeviceConfigSpec createVirtualDiskSpec(int diskSizeGb, Integer unitNumber, Integer controllerKey, String fileName ) { // assemble diskBackingInfo VirtualDiskFlatVer2BackingInfo diskBackingInfo = new VirtualDiskFlatVer2BackingInfo(); diskBackingInfo.setThinProvisioned(true); diskBackingInfo.setDiskMode("persistent"); diskBackingInfo.setFileName(fileName); // assemble virtualDisk VirtualDisk virtualDisk = new VirtualDisk(); virtualDisk.setBacking(diskBackingInfo); virtualDisk.setUnitNumber(unitNumber); virtualDisk.setCapacityInKB(diskSizeGb * 1024 * 1024); virtualDisk.setControllerKey(controllerKey); virtualDisk.setKey(-1); // assemble virtualDeviceConfigSpec VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create); diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); diskSpec.setDevice(virtualDisk); return diskSpec; } // 获取虚拟机磁盘管理的ControllerKey private int getControllerKeyByVirtualMachine(VirtualMachine vm) { AtomicInteger controllerKey = new AtomicInteger(); if (vm != null) { VirtualDevice[] devices = vm.getConfig().getHardware().getDevice(); if (devices != null && devices.length > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { controllerKey.set(device.getControllerKey()); } } } } return controllerKey.get(); } // 获取虚拟机已生成UnitNumber private int getUnitNumberByVirtualMachine(VirtualMachine vm) { AtomicInteger unitNumber = new AtomicInteger(); if (vm != null) { VirtualDevice[] devices = vm.getConfig().getHardware().getDevice(); if (devices != null && devices.length > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { unitNumber.set(device.getUnitNumber()); } } } } unitNumber.set(unitNumber.get() + 1); return unitNumber.get(); } private String getFreeDataStoreName(VirtualMachine vm, int size) throws Exception { Datastore[] dataStores = vm.getDatastores(); for(Datastore datastore: dataStores) { DatastoreSummary ds = datastore.getSummary(); if(ds.getFreeSpace() > size) { return ds.getName(); } } return null; } public static void main(String[] args) throws Exception { VirtualMachineService virtualMachineService = new VirtualMachineService(); VirtualMachine virtualMachine = virtualMachineService.getVirtualMachineByName("T-001"); Integer controllerKey = virtualMachineService.getControllerKeyByVirtualMachine(virtualMachine); Integer unitNumber = virtualMachineService.getUnitNumberByVirtualMachine(virtualMachine); int diskSize = 2; String diskName = "test_disk"; String dsName = virtualMachineService.getFreeDataStoreName(virtualMachine, diskSize); String fileName = "[" + dsName + "] " + virtualMachine.getName() + "/" + diskName + ".vmdk"; VirtualDeviceConfigSpec virtualDeviceConfigSpec = createVirtualDiskSpec(diskSize, unitNumber, controllerKey, fileName); VirtualMachineConfigSpec virtualMachineConfigSpec = new VirtualMachineConfigSpec(); VirtualDeviceConfigSpec[] diskSpecArray = {virtualDeviceConfigSpec}; virtualMachineConfigSpec.setDeviceChange(diskSpecArray); Task task = virtualMachine.reconfigVM_Task(virtualMachineConfigSpec); task.waitForTask(); } }