Vijava学习笔记之(为指定的SCSI控制器,添加关联磁盘。主要针对虚拟机)分析

无情 阅读:245 2021-03-31 22:31:05 评论:0

源代码:

package com.vmware.virtualmachine; 
 
import com.vmware.util.Session; 
import com.vmware.vim25.*; 
import com.vmware.vim25.mo.*; 
 
 
 
 
/** 
 * Created by vixuan-008 on 2015/5/27. 
 */ 
public class VmCreate { 
    public static void main(String[] args) { 
        ServiceInstance serviceInstance=null; 
        try { 
 
            String vmname = "vm";//虚拟机名称 
            long diskSizeKB = 10*1024; //虚拟磁盘大小 
            // mode: persistent|independent_persistent, 
            // independent_nonpersistent 
            String diskMode = "persistent"; //虚拟磁盘类型 
            String datastoreName = "local05"; //存储器名称 
             serviceInstance = Session.getInstance("172.16.1.20", "root", "vmware"); 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            VirtualMachine vm = (VirtualMachine) new InventoryNavigator( 
                    rootFolder).searchManagedEntity("VirtualMachine", vmname); 
 
            if (vm == null) { 
                System.out.println("No VM " + vmname + " found"); 
                serviceInstance.getServerConnection().logout(); 
                return; 
            } 
            // create vm config spec 
            VirtualMachineConfigSpec vmSpec = 
                    new VirtualMachineConfigSpec(); 
            // create virtual devices 
            int cKey = 1000; 
            VirtualDeviceConfigSpec diskSpec = createDiskSpec( 
                    datastoreName, cKey, diskSizeKB, diskMode); 
            vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] 
                    {diskSpec}); 
            // call the createVM_Task method on the vm folder 
           getVirtualMachieDevice(vm); 
            Task task = vm.reconfigVM_Task(vmSpec); 
            String result = task.waitForMe(); 
            if (result == Task.SUCCESS) { 
                System.out.println("VM Created Sucessfully"); 
            } else { 
                System.out.println("VM could not be created. "); 
            } 
        } catch (Exception e) { 
            e.printStackTrace(); 
            System.out.println(e.getMessage()); 
            serviceInstance.getServerConnection().logout(); 
            return; 
        } 
    } 
 
   
 
 
 
 
 
    static VirtualDeviceConfigSpec createDiskSpec(String dsName, 
                                                  int cKey, long diskSizeKB, String diskMode) 
    { 
        VirtualDeviceConfigSpec diskSpec = 
                new VirtualDeviceConfigSpec(); 
        diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); 
        diskSpec.setFileOperation( 
                VirtualDeviceConfigSpecFileOperation.create); 
 
        VirtualDisk vd = new VirtualDisk(); 
        vd.setCapacityInKB(diskSizeKB); 
        diskSpec.setDevice(vd); 
        vd.setKey(-1); 
        vd.setUnitNumber(-1); 
        vd.setControllerKey(cKey); 
 
        VirtualDiskFlatVer2BackingInfo diskfileBacking = 
                new VirtualDiskFlatVer2BackingInfo(); 
        String fileName = "["+ dsName +"]"+"111333333111222.vmk"; 
        diskfileBacking.setFileName(fileName); 
        diskfileBacking.setDiskMode(diskMode); 
        diskfileBacking.setThinProvisioned(true); 
        vd.setBacking(diskfileBacking); 
        return diskSpec; 
    } 
 
    public static void getVirtualMachieDevice(VirtualMachine vm){ 
        VirtualMachineConfigInfo vmConfig = (VirtualMachineConfigInfo)vm.getConfig(); 
        VirtualDevice[] vds = vmConfig.getHardware().getDevice(); 
        for(int k=0;k<vds.length;k++) 
        { 
         //  if(vds[k] instanceof VirtualSCSIController ){ 
               System.out.println("----------start------------"); 
                    System.out.println("name:"+vds[k].getDeviceInfo().getLabel()); 
                     System.out.println("key:"+vds[k].getKey()); 
               System.out.println("----------end------------"); 
       //    } 
        } 
    } 
 
 
 
} 


声明

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

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号