码迷,mamicode.com
首页 > 编程语言 > 详细

vSphere SDK for Java - 为虚拟机添加磁盘

时间:2019-05-02 21:41:22      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:opera   trace   spec   virtual   hardware   roo   ber   代码   config   

示例代码:

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();
    }
}

 

vSphere SDK for Java - 为虚拟机添加磁盘

标签:opera   trace   spec   virtual   hardware   roo   ber   代码   config   

原文地址:https://www.cnblogs.com/vincenshen/p/10803254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!