Previous page

Next page

Locate page in Contents

Hard Disks

Modifying the size of the existing hard disk image

A virtual machine may have more than one virtual hard disk. To select a disk that you would like to modify, first retrieve the list of the available disks, as shown in the following example:

PRL_HANDLE hHDD = PRL_INVALID_HANDLE;

PRL_UINT32 nCount;

  

// Get the number of disks available.

PrlVmCfg_GetHardDisksCount(hVmCfg, &nCount);

  

// Iterate through the list.

for (PRL_UINT32 i = 0; i < nCount; ++i)

{

    // Obtain a handle to the hard disk object.

    ret = PrlVmCfg_GetHardDisk(hVmCfg, i, &hHDD);

    

    // The code selecting the desired HDD goes here...

    // {

         // Modify the disk size.

         // The hard disk size is specified in megabytes.

         ret = PrlVmDevHd_SetDiskSize(hHDD, 20000);

    // }

}

Adding a new hard disk

In this example, we will add a hard disk to a virtual machine. The following options are available:

  • You may create new or use an existing image file for your new disk.
  • Creating a dynamically expanding or a fixed-size disk. The expanding drive image will be initially created with a size of zero. The space for it will be allocated dynamically on as-needed basis. The space for the fixed-size disk will be allocated fully at the time of creation.
  • Choosing the maximum disk size.

Creating a new image file

In the first example, we will create a new disk image and will add it to a virtual machine.

PRL_HANDLE hJobBeginEdit = PRL_INVALID_HANDLE;

PRL_HANDLE hJobCommit = PRL_INVALID_HANDLE;

PRL_RESULT nJobRetCode = PRL_INVALID_HANDLE;

  

// Timestamp the beginning of the configuration changes operation.

// The hVm specifies the virtual machine that we'll be editing.

//

hJobBeginEdit = PrlVm_BeginEdit(hVm);

ret = PrlJob_Wait(hJobBeginEdit, 10000);

PrlJob_GetRetCode(hJobBeginEdit, &nJobRetCode);

if (PRL_FAILED(nJobRetCode))

{

    fprintf(stderr, "Error: %s\n", prl_result_to_string(nJobRetCode));

    PrlHandle_Free(hJobBeginEdit);

    return nJobRetCode;

}

    

// Create a new device handle.

// This will be our new virtual hard disk.

PRL_HANDLE hHDD = PRL_INVALID_HANDLE;

ret = PrlVmCfg_CreateVmDev(

        hVmCfg, // The target virtual machine.

        PHT_VIRTUAL_DEV_HARD_DISK, // Device type.

        &hHDD); // Device handle.

  

// Set disk type to "expanding".

ret = PrlVmDevHd_SetDiskType(hHDD, PHD_EXPANDING_HARD_DISK);

    

// Set max disk size, in megabytes.

ret = PrlVmDevHd_SetDiskSize(hHDD, 32000);

    

// This option determines whether the image file will be splitted

// into chunks or created as a single file.

ret = PrlVmDevHd_SetSplitted(hHDD, PRL_FALSE);

    

// Choose and set the name for the new image file.

// We must set both the "friendly" name and the "system" name.

// For a virtual device, use the name of the new image file in both

// functions. By default, the file will be

// created in the virtual machine directory. You may specify a

// full path if you want to place the file in a different

// directory.

//

ret = PrlVmDev_SetFriendlyName(hHDD, "harddisk4.hdd");

ret = PrlVmDev_SetSysName(hHDD, "harddisk4.hdd");

    

// Set the emulation type.

ret = PrlVmDev_SetEmulatedType(hHDD, PDT_USE_IMAGE_FILE);

    

// Enable the new disk on successful creation.

ret = PrlVmDev_SetEnabled(hHDD, PRL_TRUE);

    

// Create the new image file.

hJob = PrlVmDev_CreateImage(hHDD,

            PRL_TRUE, // Do not overwrite if the file exists.

            PRL_TRUE); // Use non-interactive mode.

    

// Commit the changes.

hJobCommit = PrlVm_Commit(hVm);

  

// Check the results of the commit operation.

ret = PrlJob_Wait(hJobCommit, 10000);

PrlJob_GetRetCode(hJobCommit, &nJobRetCode);

if (PRL_FAILED(nJobRetCode))

{

    fprintf(stderr, "Commit error: %s\n", prl_result_to_string(nJobRetCode));

    PrlHandle_Free(hJobCommit);

    return nJobRetCode;

}

Using an existing image file

In the next example, we will use an existing image file to add a virtual hard disk to a virtual machine. The procedure is similar to the one described above, except that you don't have to specify the disk parameters and you don't have to create an image file.

// Timestamp the beginning of the configuration changes operation.

// The hVm specifies the virtual machine that we'll be editing.

//

hJobBeginEdit = PrlVm_BeginEdit(hVm);

ret = PrlJob_Wait(hJobBeginEdit, 10000);

PrlJob_GetRetCode(hJobBeginEdit, &nJobRetCode);

if (PRL_FAILED(nJobRetCode))

{

    fprintf(stderr, "Error: %s\n", prl_result_to_string(nJobRetCode));

    PrlHandle_Free(hJobBeginEdit);

    return nJobRetCode;

}

    

// Create a device handle.

PRL_HANDLE hHDD = PRL_INVALID_HANDLE;

ret = PrlVmCfg_CreateVmDev(

               hVmCfg, // Target virtual machine.

               PHT_VIRTUAL_DEV_HARD_DISK, // Device type.

               &hHDD); // Device handle.

    

// In this example, these two functions are used

// to specify the name of the existing image file.

// By default, it will look for the file in the

// virtual machine directory. If the file is located

// anywhere else, you must specify the full path here.

//

ret = PrlVmDev_SetFriendlyName(hHDD, "harddisk4.hdd");

ret = PrlVmDev_SetSysName(hHDD, "harddisk4.hdd");

    

// Set the emulation type.

ret = PrlVmDev_SetEmulatedType(hHDD, PDT_USE_IMAGE_FILE);

    

// Enable the drive on completion.

ret = PrlVmDev_SetEnabled(hHDD, PRL_TRUE);

    

// Commit the changes.

hJobCommit = PrlVm_Commit(hVm);

If the commit operation is successful, a hard disk will be added to the virtual machine and will appear in the list of the available devices.