Vijava 学习笔记之(Template 克隆虚拟机并修改Template 指定虚拟磁盘大小)分析

你猜 阅读:170 2021-03-31 22:30:12 评论:0

源代码:

package com.vmware.template; 
 
 
import com.vmware.util.Session; 
import com.vmware.vim25.*; 
import com.vmware.vim25.mo.*; 
import java.rmi.RemoteException; 
 
/** 
 * 指定模板创建虚拟机 
 * @auther 周志刚 
 * Created by vixuan-008 on 2015/5/14. 
 */ 
public class TemplateCreateVM { 
 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
 
 
        // vijava 
        com.vmware.vim25.mo.VirtualMachine templateVM = null; 
        com.vmware.vim25.mo.ResourcePool pool = null; 
        com.vmware.vim25.mo.HostSystem system=null; 
        com.vmware.vim25.mo.Datastore datastore = null; 
        ComputeResource computerResource = null; 
        InventoryNavigator inventoryNavigator = null; 
        Task task = null; 
        ServiceInstance instance = null; 
 
 
        String templatename="wintem3"; 
        String poolname="core"; 
        String hostname=""; 
        String virtualmachinename="xuniji4"; 
        String datastorename="local02-2"; 
 
        long disksizekb=10*1024*1024; 
        String diskmode="persistent"; 
 
        // 逻辑处理 
        try { 
           // Map map = util.getURL(); 
            String ip = "172.16.1.20"; 
            String username = "root"; 
            String password = "vmware"; 
 
            instance = Session.getInstance(ip, username, password); 
            if(instance!=null){ 
                System.out.println("vmware 连接成功"); 
            }else{ 
                System.out.println("vmware 连接失败,请检查vmware连接相关配置信息"); 
            } 
 
            Folder rootFolder = instance.getRootFolder(); 
 
            inventoryNavigator = new InventoryNavigator(rootFolder); 
            try { 
                templateVM = (com.vmware.vim25.mo.VirtualMachine) inventoryNavigator 
                        .searchManagedEntity("VirtualMachine", templatename); 
 
                if(templateVM!=null){ 
                    System.out.println("template 查询成功"); 
                }else{ 
                    System.out.println("template 查询失败,请仔细检查配置模板是否存在"); 
                } 
            } catch (RemoteException e) { 
                e.printStackTrace(); 
                System.out.println("虚拟机模板文件存在问题:" + e.getMessage()); 
            } 
            try{ 
                datastore=(com.vmware.vim25.mo.Datastore) inventoryNavigator 
                        .searchManagedEntity("Datastore", datastorename); 
            }catch (Exception e){ 
                e.printStackTrace(); 
                System.out.println("指定Datastore存在问题:" + e.getMessage()); 
            } 
 
            VirtualMachineRelocateSpec virtualMachineRelocateSpec = new VirtualMachineRelocateSpec(); 
            if (null != poolname && !"".equals(poolname)) { 
                try { 
                    pool = (com.vmware.vim25.mo.ResourcePool) inventoryNavigator 
                            .searchManagedEntity("ResourcePool", poolname); 
                    virtualMachineRelocateSpec.setPool(pool.getMOR()); 
 
                    virtualMachineRelocateSpec.setDatastore(datastore.getMOR()); 
                    if(pool!=null){ 
                        System.out.println("pool 查询成功"); 
                    }else{ 
                        System.out.println("pool 查询失败,请仔细检查配置资源池是否存在"); 
                    } 
                } catch (RemoteException e) { 
                    System.out.println("Vcenter资源池存在问题:" + e.getMessage()); 
 
 
                } 
 
            } else { 
 
                try { 
                    computerResource = (ComputeResource) inventoryNavigator.searchManagedEntity("ComputeResource", hostname); 
                    if(computerResource!=null){ 
                        if(computerResource.getResourcePool()!=null){ 
                            virtualMachineRelocateSpec.setPool(computerResource.getResourcePool().getMOR()); 
                        } 
                        virtualMachineRelocateSpec.setDatastore(datastore.getMOR()); 
                        virtualMachineRelocateSpec.setHost(computerResource.getHosts()[0].getMOR()); 
                    } 
 
 
                } catch (RemoteException e) { 
                    e.printStackTrace(); 
                    System.out.println("Vcenter下Esxi存在问题:" + e.getMessage()); 
                } 
 
            } 
            //虚拟机CPU和内存配置信息 
           VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec(); 
            // 设置CPU核数 
            configSpec.setNumCPUs(2); 
            // 设置内存大小 
            long memory=1024; 
            configSpec.setMemoryMB(memory); 
            // 设置虚拟机名称 
            configSpec.setName(virtualmachinename); 
            // 设置虚拟机描述 
            configSpec.setAnnotation("VirtualMachine Annotation"); 
            //更改磁盘大小 
            VirtualDeviceConfigSpec virtualDeviceConfigSpec = new VirtualDeviceConfigSpec(); 
            VirtualDeviceConfigSpec diskSpec = createDiskSpec( 
                    templateVM, "local02-2", 
                    disksizekb, diskmode); 
            if (diskSpec != null) { 
              System.out.println("创建disk不为空"); 
            } else { 
                System.out.println("创建disk为空"); 
            } 
 
 
            VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec(); 
            cloneSpec.setLocation(virtualMachineRelocateSpec); 
            cloneSpec.setPowerOn(true); 
            cloneSpec.setTemplate(false); 
            cloneSpec.setConfig(configSpec); 
 
 
            try { 
 
                task = templateVM.cloneVM_Task((Folder) templateVM.getParent(), 
                        virtualmachinename, cloneSpec); 
                String result = task.waitForTask(); 
                if (result.equals(Task.SUCCESS)) { 
                    System.out.println("模板生成虚拟机成功"); 
                } else { 
                    System.out.println("模板生成虚拟机失败,请查询Vcenter 上相关日志信息"); 
                } 
 
            } catch (RemoteException e) { 
                System.out.println("创建任务失败:" + e.getMessage()); 
                if (instance != null) { 
                    instance.getServerConnection().logout(); 
                    System.out.println("存在异常:" + e.getMessage()); 
                } 
 
            } 
 
        }catch(Exception e){ 
            e.printStackTrace(); 
        } 
 
    } 
 
    public static VirtualDeviceConfigSpec createDiskSpec(VirtualMachine vm,String dsName, 
                                                  long diskSizeKB, String diskMode) { 
        //虚拟磁盘配置信息 
        VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec(); 
 
        VirtualMachineConfigInfo vmConfig = (VirtualMachineConfigInfo)vm.getConfig(); 
        VirtualDevice[] vds = vmConfig.getHardware().getDevice(); 
        //虚拟磁盘相关操作 
        diskSpec.setOperation(VirtualDeviceConfigSpecOperation.add); 
        diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create); 
 
        VirtualDisk vd = new VirtualDisk(); 
        vd.setCapacityInKB(diskSizeKB); 
        diskSpec.setDevice(vd); 
        vd.setKey(0); 
        vd.setUnitNumber(0);
<span style="white-space:pre">	</span>//修改虚拟磁盘大小,主要就是修改已存在的SCSI 控制器,并重新设置VirtualDisk.setCapacityInKB(). 
        <span style="color:#ff0000;">int key = 0; 
        for(int k=0;k<vds.length;k++) 
        { 
            if(vds[k].getDeviceInfo().getLabel().equalsIgnoreCase("SCSI Controller 0")) 
            { 
                key = vds[k].getKey(); 
            } 
        } 
        vd.setControllerKey(key);</span> 
 
        VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo(); 
        String fileName = "[" + dsName + "]"; 
        diskfileBacking.setFileName(fileName); 
        diskfileBacking.setDiskMode(diskMode); 
        diskfileBacking.setThinProvisioned(true); 
        vd.setBacking(diskfileBacking); 
        return diskSpec; 
    } 
} 


声明

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

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号