Menu

How to create a dynamic/fixed/differencing vhd via j-Interop

Eric Song
2016-06-30
2016-07-01
  • Eric Song

    Eric Song - 2016-06-30

    Hi, all
    I am now trying to create virtual hard disks to our hyper-v vms via j-Interop. I wrote the following codes to create corresponding vhds but failed.

    public static void createFixedVHD(String path, long size) throws JIException
    {
    try {
    IJIDispatch imageManagementService = getImageManagementService();
    JIVariant[] tmp = imageManagementService.callMethodA("CreateFixedVirtualHardDisk",
    new Object[] { new JIString(path), new Long(size), JIVariant.EMPTY_BYREF() });

            int res = tmp[0].getObjectAsInt();
            if (res == 0) {
                System.out.println("Successfully created a fixed disk.");
            } else {
                // 4096 means that the method is executing asynchronously (in
                // the background) and you need to check
                // its Job object to see when it’s finished and if it was successful
                if (res == 4096) {
                    System.out.println("Creating fixed disk ...");
                    String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
                    monitorJobState(jobPath, imageManagementService);
                } else {
                    System.out.println("Error occurred while creating fixed disk.");
                    System.exit(1);
                }
            }
        } catch (JIException e) {
            System.out.println(e.getErrorCode());
            System.out.println(e.getMessage());
        }   
     }
    
       To create dynamic vhds I replace "CreateFixedVirtualHardDisk" with "CreateDynamicVirtualHardDisk", the definition of CreateFixedVirtualHardDisk is below:
    
       uint32 CreateFixedVirtualHardDisk(
          [in]  string              Path,
          [in]  uint64             MaxInternalSize,
          [out] CIM_ConcreteJob REF Job
       );
    
     When I called this function using 
     createFixedVHD("D:\\VirtualHardDisks\\New Virtual Hard Disk_2.vhdx", 100*1024*1024*1024), 
     an exception is captured like:
    
      -2147352570
      Unknown name. [0x80020006]
    

    Can anybody help me? Thanks.

     

    Last edit: Eric Song 2016-06-30
    • Vikram Roopchand

      Hi,

      This exception means that the COM server does not recognize the function
      name you sent as a a valid function within its implementation. Does this
      function actually exist ?

      thanks,
      best regards,
      Vikram

      On Thu, Jun 30, 2016 at 12:59 PM, Eric Song songyangeric@users.sf.net
      wrote:

      Hi, all
      I am now trying to create virtual hard disks to our hyper-v vms via
      j-Interop. I wrote the following codes to create corresponding vhds but
      failed.

      public static void createFixedVHD(String path, long size) throws
      JIException
      {
      try {
      IJIDispatch imageManagementService = getImageManagementService();
      JIVariant[] tmp =
      imageManagementService.callMethodA("CreateFixedVirtualHardDisk",
      new Object[] { new JIString(path), new Long(size), JIVariant.EMPTY_BYREF()
      });

          int res = tmp[0].getObjectAsInt();
          if (res == 0) {
              System.out.println("Successfully created a fixed disk.");
          } else {
              // 4096 means that the method is executing asynchronously (in
              // the background) and you need to check
              // its Job object to see when it’s finished and if it was
              // successful
              if (res == 4096) {
                  System.out.println("Creating fixed disk ...");
                  String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
                  monitorJobState(jobPath, imageManagementService);
              } else {
                  System.out.println("Error occurred while creating fixed disk.");
                  System.exit(1);
              }
          }
      } catch (JIException e) {
          System.out.println(e.getErrorCode());
          System.out.println(e.getMessage());
      }
      

      }

      To create dynamic vhds I replace "CreateFixedVirtualHardDisk" with "CreateDynamicVirtualHardDisk", the definition of CreateFixedVirtualHardDisk is below:

      uint32 CreateFixedVirtualHardDisk(
      [in] string Path,
      [in] uint64 MaxInternalSize,
      [out] CIM_ConcreteJob REF Job
      );

      When I called this function using
      createFixedVHD("D:\VirtualHardDisks\New Virtual Hard Disk_2.vhdx", 10010241024*1024),
      an exception is captured like:

      -2147352570
      Unknown name. [0x80020006]

      Can anybody help me? Thanks.

      How to create a dynamic/fixed/differencing vhd via j-Interop
      https://sourceforge.net/p/j-interop/discussion/840678/thread/42b55a64/?limit=25#c985


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/840678/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
      • Eric Song

        Eric Song - 2016-07-01

        Thanks for your reply, Vikram.

        I reread the Microsoft (link is listed below) about the defination of the Msvm_ImageManagementService class. This function CreateFixedVirtualHardDisk really exists. Do you mean that j-Interop does not support such call?

        https://msdn.microsoft.com/en-us/library/cc136845(v=vs.85).aspx

        The Msvm_ImageManagementService class has these methods.
        Method Description

        CompactVirtualHardDisk
        Compacts a dynamic virtual hard disk file. Compacting is the process of unallocating unused portions of the virtual hard disk.

        ConvertVirtualHardDisk
        Converts the type of an existing virtual hard disk.

        CreateDifferencingVirtualHardDisk
        Creates a differencing .vhd file.

        CreateDynamicVirtualHardDisk
        Creates a dynamically expanding virtual hard disk (.vhd) file.

        CreateFixedVirtualHardDisk
        Creates a fixed-sized .vhd file.

        GetVirtualHardDiskInfo
        Retrieves information about the virtual hard disk files.

        .
        .
        .

        I can successfully call the function GetVirtualHardDiskInfo via the codes though. Any possibility that I pass wrong parameters when calling this function? I am wondering what type to use as this function needs a uint64 as input para for disk size.

        Hope for your reply. Thanks.

         
        • Vikram Roopchand

          Hi,

          Can you show me the method signature and definition of

          createFixedVHD("D:\VirtualHardDisks\New Virtual Hard Disk_2.vhdx",
          10010241024*1024),

          Thanks,
          Best regards,
          Vikram

          On Jul 1, 2016 7:57 AM, "Eric Song" songyangeric@users.sf.net wrote:

          Thanks for your reply, Vikram.

          I reread the Microsoft (link is listed below) about the defination of the
          Msvm_ImageManagementService class. This function CreateFixedVirtualHardDisk
          really exists. Do you mean that j-Interop does not support such call?

          https://msdn.microsoft.com/en-us/library/cc136845(v=vs.85).aspx

          The Msvm_ImageManagementService class has these methods.
          Method Description

          CompactVirtualHardDisk
          Compacts a dynamic virtual hard disk file. Compacting is the process of
          unallocating unused portions of the virtual hard disk.

          ConvertVirtualHardDisk
          Converts the type of an existing virtual hard disk.

          CreateDifferencingVirtualHardDisk
          Creates a differencing .vhd file.

          CreateDynamicVirtualHardDisk
          Creates a dynamically expanding virtual hard disk (.vhd) file.

          CreateFixedVirtualHardDisk
          Creates a fixed-sized .vhd file.

          GetVirtualHardDiskInfo
          Retrieves information about the virtual hard disk files.

          .
          .
          .

          I can successfully call the function GetVirtualHardDiskInfo via the codes
          though. Any possibility that I pass wrong parameters when calling this
          function? I am wondering what type to use as this function needs a uint64
          as input para for disk size.

          Hope for your reply. Thanks.

          How to create a dynamic/fixed/differencing vhd via j-Interop
          https://sourceforge.net/p/j-interop/discussion/840678/thread/42b55a64/?limit=25#c985/27fa/1224


          Sent from sourceforge.net because you indicated interest in
          https://sourceforge.net/p/j-interop/discussion/840678/

          To unsubscribe from further messages, please visit
          https://sourceforge.net/auth/subscriptions/

           
          • Eric Song

            Eric Song - 2016-07-01
            public static void createFixedVHD(String path, long size) throws JIException
                {
                    try {
                        IJIDispatch imageManagementService = getImageManagementService();
                        JIVariant[] tmp = imageManagementService.callMethodA("CreateFixedVirtualHardDisk",
                                new Object[] { new JIString(path), new Long(size), JIVariant.EMPTY_BYREF() });
                        int res = tmp[0].getObjectAsInt();
                        if (res == 0) {
                            System.out.println("Successfully created a fixed disk.");
                        } else {
                            // 4096 means that the method is executing asynchronously (in
                            // the background) and you need to check
                            // it’s Job object to see when it’s finished and if it was
                            // successful
                            if (res == 4096) {
                                System.out.println("Creating fixed disk ...");
                                String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
                                monitorJobState(jobPath, imageManagementService);
                            } else {
                                System.out.println("Error occurred while creating fixed disk.");
                                System.exit(1);
                            }
                        }
                    } catch (JIException e) {
                        System.out.println(e.getErrorCode());
                        System.out.println(e.getMessage());
                    }   
                }    
            

            The WMI defination of the CreateFixedVirtualHardDisk function is :

            uint32 CreateFixedVirtualHardDisk(
            [in] string Path,
            [in] uint64 MaxInternalSize,
            [out] CIM_ConcreteJob REF Job
            );
            

            I call my function via :

            String filePath = "C:\\Hyper-V\\VirtualDisks\\songe1_test\\Virtual Hard Disks\\songe1_test_vdisk_test.vhdx";
            long size = 100 * 1024 * 1024 * 1024L;
            createFixedVHD(filePath, size);
            

            Thanks for your help.

             
            • Vikram Roopchand

              Hi,

              This seems okay. Please use the IJIUnsigned interface instead of "new
              Long(size)" as parameter.

              thanks,
              best regards,
              Vikram

              On Fri, Jul 1, 2016 at 11:43 AM, Eric Song songyangeric@users.sf.net
              wrote:

              public static void createFixedVHD(String path, long size) throws JIException
              {
              try {
              IJIDispatch imageManagementService = getImageManagementService();
              JIVariant[] tmp = imageManagementService.callMethodA("CreateFixedVirtualHardDisk",
              new Object[] { new JIString(path), new Long(size), JIVariant.EMPTY_BYREF() });
              int res = tmp[0].getObjectAsInt();
              if (res == 0) {
              System.out.println("Successfully created a fixed disk.");
              } else {
              // 4096 means that the method is executing asynchronously (in
              // the background) and you need to check
              // it’s Job object to see when it’s finished and if it was
              // successful
              if (res == 4096) {
              System.out.println("Creating fixed disk ...");
              String jobPath = tmp[1].getObjectAsVariant().getObjectAsString2();
              monitorJobState(jobPath, imageManagementService);
              } else {
              System.out.println("Error occurred while creating fixed disk.");
              System.exit(1);
              }
              }
              } catch (JIException e) {
              System.out.println(e.getErrorCode());
              System.out.println(e.getMessage());
              }
              }

              The WMI defination of the CreateFixedVirtualHardDisk function is :

              uint32 CreateFixedVirtualHardDisk(
              [in] string Path,
              [in] uint64 MaxInternalSize,
              [out] CIM_ConcreteJob REF Job
              );

              I call my function via :

              String filePath = "C:\Hyper-V\VirtualDisks\songe1_test\Virtual Hard Disks\songe1_test_vdisk_test.vhdx";long size = 100 * 1024 * 1024 * 1024L;createFixedVHD(filePath, size);

              Thanks for your help.

              How to create a dynamic/fixed/differencing vhd via j-Interop
              https://sourceforge.net/p/j-interop/discussion/840678/thread/42b55a64/?limit=25#c985/27fa/1224/206b/776a


              Sent from sourceforge.net because you indicated interest in
              https://sourceforge.net/p/j-interop/discussion/840678/

              To unsubscribe from further messages, please visit
              https://sourceforge.net/auth/subscriptions/

              --
              The Mind is a place of its own. It can make a heaven out of hell or a hell
              out of heaven. Attitude is everything. No matter how adverse conditions
              maybe, one has the capacity to turn things around by one's Determination,
              Perseverance and Hardwork.

              John Milton
              (Paradise Lost)

               

Log in to post a comment.