使用VSphere SDK 6.0 Java应用程序在创建(克隆)时自定义虚拟机

时间:2016-08-12 11:58:33

标签: java virtual-machine vsphere

我正在使用VSphere SDK 6.0进行虚拟机配置。我已成功使用以下方法从模板配置虚拟机

ManagedObjectReference cloneTask =
                vimPort.cloneVMTask(vmRef, vmFolderRef, cloneName, cloneSpec);

我还可以使用以下方法自定义内存,CPU

 VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
        configSpec.setMemoryMB(512l);
        configSpec.setNumCPUs(2);

我想知道使用SDK的更多自定义,例如 HarkDisk,N / W适配器,网络等详细信息等。帮助我通过虚拟机的自定义获取一些Java代码示例或者让我一些提及相同的链接。任何帮助都会很感激。

1 个答案:

答案 0 :(得分:0)

要添加磁盘和网络适配器,您需要创建VirtualDeviceConfigSpec。 创建完整的virtualdeviceconfigspec后,使用: VirtualMachineConfigSpec.DeviceChange = yourdeviceconfigspec;

这段代码是伪c#但与Java足够接近以获得想法。然后使用克隆/创建中的配置规范。

这是一个c#函数,可以添加3个半虚拟控制器和5个磁盘,所有这些都捆绑在一个列表中

        public List<VirtualDeviceConfigSpec> Add_Disks(int numberofDisks, ManagedObjectReference dsMoref, VimClient vsphere, string vmname, string dsname)
    {
        List<int> diskSize = new List<int> { 20000, 10000, 20000, 10000, 20000 };
        List<VirtualDeviceConfigSpec> specs = new List<VirtualDeviceConfigSpec>();

        //Add Three Paravirtual Controllers
        VirtualDeviceConfigSpec para1 = new VirtualDeviceConfigSpec();
        ParaVirtualSCSIController p1 = new ParaVirtualSCSIController();
        p1.Key = 1;
        p1.SharedBus = VirtualSCSISharing.noSharing;
        p1.BusNumber = 1;
        p1.HotAddRemove = true;
        para1.Device = p1;
        para1.Operation = VirtualDeviceConfigSpecOperation.add;
        specs.Add(para1);

        VirtualDeviceConfigSpec para2 = new VirtualDeviceConfigSpec();
        ParaVirtualSCSIController p2 = new ParaVirtualSCSIController();
        p2.Key = 2;
        p2.SharedBus = VirtualSCSISharing.noSharing;
        p2.BusNumber = 2;
        p2.HotAddRemove = true;
        para2.Device = p2;
        para2.Operation = VirtualDeviceConfigSpecOperation.add;
        specs.Add(para2);

        VirtualDeviceConfigSpec para3 = new VirtualDeviceConfigSpec();
        ParaVirtualSCSIController p3 = new ParaVirtualSCSIController();
        p3.Key = 3;
        p3.SharedBus = VirtualSCSISharing.noSharing;
        p3.BusNumber = 3;
        p3.HotAddRemove = true;
        para3.Device = p3;
        para3.Operation = VirtualDeviceConfigSpecOperation.add;
        specs.Add(para3);

        for (int i = 0; i < diskSize.Count; i++)
        {
            //set up standard disks specs
            bool thinProvisioned = true;
            int size = (diskSize[i]);
            int controllerKey = 0;
            int unitNumber = 0;

            switch (i)
            {
                case 0:
                    controllerKey = 1;
                    unitNumber = 0;
                    break;
                case 1:
                    controllerKey = 3;
                    unitNumber = 0;
                    break;
                case 2:
                    controllerKey = 2;
                    unitNumber = 0;
                    break;
                case 3:
                    controllerKey = 2;
                    unitNumber = 1;
                    break;
                case 4:
                    controllerKey = 3;
                    unitNumber = 1;
                    break;
            }

            VirtualDiskFlatVer2BackingInfo backInfo = new VirtualDiskFlatVer2BackingInfo();
            backInfo.ThinProvisioned = thinProvisioned;
            backInfo.DiskMode = "persistent";
            backInfo.Datastore = dsMoref;
            backInfo.FileName = "[" + dsname + "]" + vmname + "/" + vmname + controllerKey + "_" + unitNumber + ".vmdk";

            VirtualDisk tempDisk = new VirtualDisk();
            tempDisk.Backing = backInfo;
            tempDisk.CapacityInKB = (1024 * size);
            tempDisk.ControllerKey = controllerKey;
            tempDisk.Key = (1 + i);
            tempDisk.UnitNumber = unitNumber;

            VirtualDeviceConfigSpec tempConfigSpec = new VirtualDeviceConfigSpec();
            tempConfigSpec.Device = tempDisk;
            tempConfigSpec.FileOperation = VirtualDeviceConfigSpecFileOperation.create;
            tempConfigSpec.Operation = VirtualDeviceConfigSpecOperation.add;

            specs.Add(tempConfigSpec);
        }

        return specs;
    }