Creating a New Virtual Machine From a Template.The primary purpose of templates is to be used as patterns to create new virtual machines. New virtual machines are created from templates using the cloning functionality. We've already discussed how to clone a virtual machine in the Cloning a Virtual Machine section. The truth is, creating a virtual machine from a template is at all different than creating a clone of a virtual machine. The PRL_RESULT CreateVmFromTemplate(PRL_HANDLE hVm) { PRL_HANDLE hJob = PRL_INVALID_HANDLE; PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED; PRL_RESULT ret = PRL_ERR_UNINITIALIZED;
// Declare and populate variables that // will be used as input parameters // in the function that clones a VM.
// Virtual machine name. // Get the name of the original VM (template) and use // it in the new virtual machine name. You can // use any name that you like of course. char vm_name[1024]; PRL_UINT32 nBufSize = sizeof(vm_name); ret = PrlVmCfg_GetName(hVm, vm_name, &nBufSize); char new_vm_name[1024] = "Created from template "; strcat(new_vm_name, vm_name);
// Name of the target directory on the // host. // Empty string indicates that the default // directory should be used. PRL_CHAR_PTR new_vm_root_path = "";
// Virtual machine or template? // The cloning functionality allows to create // a new virtual machine or a new template. // True specifies to create a template. // False indicates to create a virtual machine. // We want to create a virtual machine here, so we // set it to PRL_FALSE. PRL_BOOL bCreateTemplate = PRL_FALSE;
// Begin the cloning operation. hJob = PrlVm_Clone(hVm, new_vm_name, new_vm_root_path, bCreateTemplate); // Wait for the job to complete. ret = PrlJob_Wait(hJob, 1000); if (PRL_FAILED(ret)) { // Handle the error... printf("Error: (%s)\n", prl_result_to_string(ret)); PrlHandle_Free(hJob); return -1; }
// Analyze the result of PrlVm_Clone. ret = PrlJob_GetRetCode(hJob, &nJobReturnCode); if (PRL_FAILED(ret)) { // Handle the error... PrlHandle_Free(hJob); return -1; } // Check the job return code. if (PRL_FAILED(nJobReturnCode)) { // Handle the error... printf("Error: (%s)\n", prl_result_to_string(nJobReturnCode)); PrlHandle_Free(hJob); return -1; } PrlHandle_Free(hJob); return 0; } |
||||
|