vSphere SDK for Java - 為虛擬機添加磁盤
阿新 • • 發佈:2019-05-02
com tro onf pro exception 獲取 mini () url
示例代碼:
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()); } publicServiceInstance vcInit(){ try { return new ServiceInstance(new URL("https://10.110.164.133/sdk"), "[email protected]", "VMware1!", true); } catch (MalformedURLException | RemoteException e) { e.printStackTrace(); } return null; }privateVirtualMachine 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(); } }
vSphere SDK for Java - 為虛擬機添加磁盤