Previous page

Next page

Locate page in Contents

Converting a Regular Virtual Machine to a Template

Any registered virtual machine can be converted to a template. This task is accomplished by modifying the virtual machine configuration. Only a single parameter must be modified: a flag indicating whether the machine is a regular virtual machine or a template, the rest will be handled automatically and transparently to you on the server side. The name of the function that allows to modify this parameter is PrlVm_SetTemplateSign.

The following code example illustrates how to convert a regular virtual machine to a template. Note that any of the virtual machine (or a template) configuration changes must begin with the PrlVm_BeginEdit and end with the PrlVm_BeginCommit function call. You should already know that these two functions are used to prevent collisions with other clients trying to modify the configuration of the same virtual machine or template at the same time.

PRL_RESULT ConvertVMtoTemplate(PRL_HANDLE hVm)

{

    PRL_HANDLE hJobBeginEdit = PRL_INVALID_HANDLE;

    PRL_HANDLE hJobCommit = PRL_INVALID_HANDLE;

    

    PRL_RESULT ret = PRL_ERR_UNINITIALIZED;

    PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED;

    

    // Begin of the VM configuration changes operation.

    hJobBeginEdit = PrlVm_BeginEdit(hVm);

    ret = PrlJob_Wait(hJobBeginEdit, 10000);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        PrlHandle_Free(hJobBeginEdit);

        return -1;

    }

    ret = PrlJob_GetRetCode(hJobBeginEdit, &nJobReturnCode);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        PrlHandle_Free(hJobBeginEdit);

        return -1;

    }

    // Check the job return code.

    if (PRL_FAILED(nJobReturnCode))

    {

        // Handle the error...

        PrlHandle_Free(hJobBeginEdit);

        return -1;

    }

    PrlHandle_Free(hJobBeginEdit);

    

    // Set a flag in the virtual machine configuration

    // indicating that we want it to become a template.

    PRL_BOOL isTemplate = PRL_TRUE;

    ret = PrlVmCfg_SetTemplateSign(hVm, isTemplate);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        return -1;

    }

    

    // Commit the changes.

    hJobCommit = PrlVm_Commit(hVm);

    // Check the results of the commit operation.

    ret = PrlJob_Wait(hJobCommit, 10000);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        PrlHandle_Free(hJobCommit);

        return -1;

    }

    ret = PrlJob_GetRetCode(hJobCommit, &nJobReturnCode);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        PrlHandle_Free(hJobCommit);

        return -1;

    }

    // Check the job return code.

    if (PRL_FAILED(nJobReturnCode))

    {

        // Handle the error...

        PrlHandle_Free(hJobCommit);

        return -1;

    }

    PrlHandle_Free(hJobCommit);

    

    return 0;

}