Creating a New Virtual MachineThe first step in creating a new virtual machine is to create a blank virtual machine and register it with the Parallels Service. A blank virtual machine is the equivalent of a hardware box with no operating system installed on the hard drive. Once a blank virtual machine is created and registered, it can be powered on and an operating system can be installed on it. In this section, we will discuss how to create a typical virtual machine for a particular OS type using a sample configuration. By using this approach, you can easily create a virtual machine without knowing all of the little details about configuring a virtual machine for a particular operating system type. The steps involved in creating a typical virtual machine are:
The following sample demonstrates how to create a new virtual machine. The sample assumes that the client program has already obtained a server object handle ( PRL_HANDLE hVm = PRL_INVALID_HANDLE; PRL_HANDLE hVmCfg = PRL_INVALID_HANDLE; PRL_HANDLE hResult = PRL_INVALID_HANDLE; PRL_RESULT nJobRetCode; PRL_RESULT ret;
// Obtain a new virtual machine handle. ret = PrlSrv_CreateVm(hServer, &hVm); if (PRL_FAILED(ret)) { // Error handling goes here... return ret; }
// Get the host config info. hJob = PrlSrv_GetSrvConfig(hServer); ret = PrlJob_Wait(hJob, 10000);
// Check the return code of PrlSrv_GetSrvConfig. PrlJob_GetRetCode(hJob, &nJobRetCode); if (PRL_FAILED(nJobRetCode)) { fprintf(stderr, "Error: %s\n", prl_result_to_string(nJobRetCode)); PrlHandle_Free(hJob); PrlHandle_Free(hVm); return nJobRetCode; }
// Get a handle to the object containing the result of PrlSrv_GetSrvConfig, // and then get a hosts configuration handle from it. ret = PrlJob_GetResult(hJob, &hResult); PRL_HANDLE hSrvCfg = PRL_INVALID_HANDLE; PrlResult_GetParam(hResult, &hSrvCfg);
// Free job and result handles. PrlHandle_Free(hJob); PrlHandle_Free(hResult);
// Now that we have the host configuration data, // we can set the default configuration for the new virtual machine. ret = PrlVm_GetConfig(hVm, &hVmCfg); ret = hVmCfg, // VM config handle. hSrvCfg, // Host config data. PVS_GUEST_VER_WIN_2003, // Target OS version. PRL_TRUE); // Create and connect devices.
if (PRL_FAILED(ret)) { fprintf(stderr, "Error: %s\n", prl_result_to_string(ret)); PrlHandle_Free(hSrvCfg); PrlHandle_Free(hVmCfg); PrlHandle_Free(hVm); return ret; }
PrlHandle_Free(hSrvCfg);
// Set the virtual machine name. ret =
// The following two calls demonstrate how to modify // some of the default values of the virtual machine configuration. // These calls are optional. You may remove them to use the default values. //
// Set RAM size for the machine to 256 MB. ret =
// Set virtual hard disk size to 20 GB. // First, get the handle to the hard disk object using the // PrlVmCfg_GetHardDisk function. The index of 0 is used // because the default configuration has just one virtual hard disk. // After that, use the handle to set the disk size. PRL_HANDLE hHDD = PRL_INVALID_HANDLE; ret = PrlVmCfg_GetHardDisk(hVmCfg, 0, &hHDD); ret = PrlVmDevHd_SetDiskSize(hHDD, 20000);
// Create and register the machine with the Parallels Service. // This is an asynchronous call. Returns a job handle. hJob = PrlVm_Reg(hVm, // VM handle. "", // VM root directory (using default). PRL_TRUE); // Using non-interactive mode.
// Wait for the operation to complete. ret = PrlJob_Wait(hJob, 10000);
// Check the return code of PrlVm_Reg. PrlJob_GetRetCode(hJob, &nJobRetCode); if (PRL_FAILED(nJobRetCode)) { fprintf(stderr, "Error: %s\n", prl_result_to_string(nJobRetCode)); PrlHandle_Free(hJob); PrlHandle_Free(hVmCfg); PrlHandle_Free(hVm); return nJobRetCode; }
// Delete handles. PrlHandle_Free(hJob); PrlHandle_Free(hVmCfg); PrlHandle_Free(hVm); |
||||
|