vSphere SDK for Java - 为虚拟机添加磁盘
Posted vincenshen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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"), "[email protected]", "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(); } }
以上是关于vSphere SDK for Java - 为虚拟机添加磁盘的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 amazon sdk 为虚域生成预签名的 Amazon S3 url?
vsphere-automation-Python-sdk实践:过滤虚机
vSphere 6 ESXTOP Overview for Troubleshooting
使用群晖套件Active Backup for Business备份vSphere