Vijava 学习笔记(指定虚拟机添加虚拟磁盘修订功能版本)分析

阿里 阅读:227 2021-03-31 22:30:55 评论:0

条件一:为指定的虚拟机添加新的磁盘,前提条件是虚拟机必须已经是按照了Vmware Tool 工具的虚拟机,否则以下的虚拟机添加虚拟磁盘功能代码可能会成功或不成功。

指定虚拟机设备截图:


源代码:

package com.vmware.client; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.*; 
import com.vmware.vim25.mo.*; 
 
/** 
 * Created by vixuan-008 on 2015/5/27. 
 */ 
public class AddVMDisk { 
    public static void main(String[] args) { 
        ServiceInstance serviceInstance = null; 
        try { 
            String vmname = "IAAS"; 
            //获取 HostSystem 
            serviceInstance = Session.getInstance("***", "root", "vmware"); 
 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname); 
            VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); 
            if (vm == null) { 
                System.out.println("No VM " + vmname + " found"); 
                serviceInstance.getServerConnection().logout(); 
                return; 
            } 
            int diskSize = 10*1024; 
            // mode: persistent|independent_persistent,independent_nonpersistent 
            String diskMode = "persistent"; 
            String diskName = "vijava_disk"; 
            VirtualDeviceConfigSpec vdiskSpec = createDiskSpec("local02-2",1000, diskSize, diskMode,vm); 
            VirtualDeviceConfigSpec[] vdiskSpecArray = {vdiskSpec}; 
            vmConfigSpec.setDeviceChange(vdiskSpecArray); 
 
            Task task = vm.reconfigVM_Task(vmConfigSpec); 
            String result = task.waitForMe(); 
            if (result.equals(Task.SUCCESS)) { 
                System.out.println("success"); 
            } else { 
                System.out.println("failed"); 
            } 
 
            serviceInstance.getServerConnection().logout(); 
 
 
        } catch (Exception e) { 
            e.printStackTrace(); 
            serviceInstance.getServerConnection().logout(); 
            return; 
        } 
    } 
 
    public static VirtualDeviceConfigSpec createDiskSpec(String dsName, 
                                                  int cKey, long diskSizeKB, String diskMode,VirtualMachine vm) { 
        VirtualDeviceConfigSpec diskSpec = 
                new VirtualDeviceConfigSpec(); 
        diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); 
        diskSpec.setFileOperation( 
                VirtualDeviceConfigSpecFileOperation.create); 
        VirtualDisk vd = new VirtualDisk(); 
        vd.setCapacityInKB(diskSizeKB); 
        diskSpec.setDevice(vd); 
        vd.setKey(getkey(vm)); 
        vd.setUnitNumber(getUnitNumber(vm)); 
        vd.setControllerKey(getcontrollerkey(vm)); 
        VirtualDiskFlatVer2BackingInfo diskfileBacking = 
                new VirtualDiskFlatVer2BackingInfo(); 
        String fileName = "[" + dsName + "]"+"vixan.cn127744"; 
        diskfileBacking.setFileName(fileName); 
        diskfileBacking.setDiskMode(diskMode); 
        diskfileBacking.setThinProvisioned(true); 
        vd.setBacking(diskfileBacking); 
        return diskSpec; 
    } 
 
    //获取虚拟机磁盘管理的controlerkey 
    public static int getcontrollerkey(VirtualMachine vm){ 
        int controllerkey=0; 
        if(vm!=null){ 
            VirtualDevice[] devices=vm.getConfig().getHardware().getDevice(); 
            if(devices !=null && devices.length>0){ 
                for(int i=0;i<devices.length;i++){ 
                    VirtualDevice device=devices[i]; 
                    if(device instanceof VirtualDisk){ 
                        controllerkey=device.getControllerKey(); 
                    } 
                } 
            } 
        } 
        return controllerkey; 
    } 
    //获取虚拟机已生成key 
    public static int getkey(VirtualMachine vm){ 
        int key=0; 
        if(vm!=null){ 
            VirtualDevice[] devices=vm.getConfig().getHardware().getDevice(); 
            if(devices !=null && devices.length>0){ 
                for(int i=0;i<devices.length;i++){ 
                        VirtualDevice device=devices[i]; 
                        if(device instanceof VirtualDisk){ 
                            key=device.getKey(); 
                        } 
                } 
            } 
        } 
        key=key+1; 
        return key; 
    } 
    //获取虚拟机已生成UnitNumber 
    public static int getUnitNumber(VirtualMachine vm){ 
        int UnitNumber=0; 
        if(vm!=null){ 
            VirtualDevice[] devices=vm.getConfig().getHardware().getDevice(); 
            if(devices !=null && devices.length>0){ 
                for(int i=0;i<devices.length;i++){ 
                    VirtualDevice device=devices[i]; 
                    if(device instanceof VirtualDisk){ 
                        UnitNumber=device.getUnitNumber(); 
                    } 
                } 
            } 
        } 
        UnitNumber=UnitNumber+1; 
        return UnitNumber; 
    } 
}


效果截图:



声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号