3 Replies Latest reply on Sep 9, 2018 1:29 PM by doskiran

    Add an existing hard disk

    hilavergil Lurker

      I am trying to use vSphere Management SDK for Java to add an existing hard disk to a VM, which function can i use?

      QQ截图20180515141642.png

        • 1. Re: Add an existing hard disk
          hilavergil Lurker

            VirtualDeviceConfigSpec virtualdiskconfigspec = new VirtualDeviceConfigSpec();

            VirtualDisk tmpdisk = new VirtualDisk();

            VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo();

             int ckey = 0;

             int unitNumber = 0;

            List<Integer> getControllerKeyReturnArr = getControllerKey(testcentosMor);

             if (!getControllerKeyReturnArr.isEmpty()) {

            ckey = getControllerKeyReturnArr.get(0);

            unitNumber = getControllerKeyReturnArr.get(1);

            System.out.println("###WATCH ME###");

            System.out.println("controllerKey is "+ckey);

            System.out.println("unitNumber is "+unitNumber);

            System.out.println("##############");

            }

            String fileName = "[" + "datastore1" + "] " + "Redhat" + "/" + "Redhat"
             + ".vmdk";

            diskfileBacking.setFileName(fileName);

            diskfileBacking.setDiskMode("persistent");

           

            tmpdisk.setControllerKey(ckey);

            tmpdisk.setUnitNumber(unitNumber);

            tmpdisk.setBacking(diskfileBacking);

          // tmpdisk.setCapacityInKB(4194304);
          // tmpdisk.setKey(2001);

             virtualdiskconfigspec.setOperation(VirtualDeviceConfigSpecOperation.ADD);

          // virtualdiskconfigspec.setFileOperation(VirtualDeviceConfigSpecFileOperation.CREATE);
             virtualdiskconfigspec.setDevice(tmpdisk);

           

            List<VirtualDeviceConfigSpec> alvdcs =

             new ArrayList<VirtualDeviceConfigSpec>();

            alvdcs.add(virtualdiskconfigspec);

           

            VirtualMachineConfigSpec vmcsreconfig = new VirtualMachineConfigSpec();

            vmcsreconfig.getDeviceChange().addAll(alvdcs);

           

            System.out.printf(" Reconfiguring the Virtual Machine - [ %s ]", virtualMachineName);

            List<HostScsiDisk>tempdisks=new ArrayList<HostScsiDisk>();

            ManagedObjectReference task = vimPort.reconfigVMTask(testcentosMor, vmcsreconfig);

             if (getTaskResultAfterDone(task)) {

            System.out.printf("\n Reconfiguring the Virtual Machine "
             + " - [ %s ] Successful", virtualMachineName);

            } else {

            System.out.printf(" Reconfiguring the Virtual Machine "
             + " - [ %s ] Failed", virtualMachineName);

            }

          • 2. Re:Add an existing hard disk
            doskiran Hot Shot
            Sample java program to extend existing VM harddisk with specified capacity using vSphere API. https://code.vmware.com/samples/1821/extend-vm-hard-disk?h=Sample
             
            • 3. Re:Add an existing hard disk
              doskiran Hot Shot

              Java program to Add existing disk to Powered on VM sample -

              https://code.vmware.com/samples/2122/hot-add-existing-disk?h=Java