Creating virtual machine templates

The time spent creating a virtual machine is quite small, but the problem is that it doesn't have an operating system on it. Also, it is time consuming if you have to install and prepare the operating system on every new virtual machine.

If you consider the time taken to install the operating system, the updates configuration, and all the other components of a server, it can take hours. But using templates, you will be able to create new virtual machines with the operating system ready to be used, in less than five minutes.

Enterprises will most likely consider System Center for managing their environment, and Virtual Machine Manager (VMM) will handle this automatically. It will also combine several different profiles to give you a more flexible solution to mix and match templates, based on needs and specifications.

In case you don't have the System Center Virtual Machine Manager (SCVMM), you can use simpler templates on Hyper-V with a pre-configured operating system to save time during the virtual-machine creation.

Given that some system attributes are required to be unique, simply copying virtual hard drive files and building another virtual machine around them will not work. This recipe will show you how to create and prepare a virtual machine that can be used as a template in an environment without System Center.

Getting ready...

To save time and steps in this recipe, make sure that you already have a virtual machine created with the operating system that you want to use as a template, and that all the other software applications and components you need are available as well. The process shown in this recipe applies to Windows Server 2008, Windows Vista, and newer OS versions. For other versions, make sure that you run Sysprep to reset the computer name, SID, and license details.

Some roles, such as Active Directory Domain Services, don't support imaging using Sysprep.

Make sure that your application can be used in conjunction with Sysprep.

For example, SQL Server SysPrep-related setup actions can be accessed through the Installation Center. The Advanced page of the Installation Center has two options: Image preparation of a standalone instance of SQL Server and Image completion of a prepared standalone instance of SQL Server. The Prepare and Complete sections describe the installation process in detail. For more information, see https://msdn.microsoft.com/en-us/library/ee210754.aspx.

How to do it...

The following steps demonstrate how to prepare an existing operating system to be used as an image and how to create a base VHD for a VM template:

  1. After creating a virtual machine with the operating system and other software applications, clients, automatic IP configuration, and so on, open Windows Explorer and navigate to the following path:

    C:WindowsSystem32Sysprep

  2. Right-click on sysprep.exe and select Run as administrator.
  3. Click on the drop-down list under System Cleanup Action and then select Enter System Out-of-Box Experience (OOBE).
  4. Check the Generalize checkbox and, in Shutdown Options, select Shutdown, as shown in the following screenshot:
    How to do it...
  5. Click on OK and wait for the system to be turned off.
  6. After the shutdown process, open the virtual machine VHD(X) path and rename it to identify your template.
  7. In the example shown in the following screenshot, you can see different virtual hard disks being used as templates:
    How to do it...
  8. To create a virtual machine from the template, copy one of the VHD(X) used as a template to another location.
  9. Create a new virtual machine using the template virtual hard disk. The system will reboot and the sysprep process will continue, prompting the product key and the administrator's password.
  10. Specify the installation information and, after the first login, you may change the computer name and any other necessary configuration, such as network settings and client options.
  11. After these changes, your new virtual machine created from a template will be ready to be used.

How it works...

The process to create a virtual machine from a template consists of two steps.

The first step is to install a virtual machine with the operating system (OS), software, clients, updates, and all the configuration settings needed for the template. Then, to reset the computer name and the computer Security IDentifier (SID) to be used on any new VM, you should run the System Preparation (Sysprep) tool. This will mean that you can use the same operating system with existing settings, but with a new name and SID. It is important not to turn on the template VM again to prevent Sysprep from finishing the process to reset the OS settings.

After that, you can rename the VHD used to create the template VM so that it can be identified.

The second step, which is used for every VM created from the template, consists of the VHD being copied to the location where the new VM will sit and the new VM creation using the existing VHD(X).

By using VHD(X) as templates, you can save hours of installing, updating, and configuring the OS and all its software.

There's more...

If you deploy virtual machines on a regular basis you will understand this problem. It is far more efficient to install and update Windows once, and then copy the virtual hard disk each time you want to create a virtual machine, rather than installing Windows each time. The problem is that if you do this, you find that your original template soon gets out of date, and you can either choose to slow down deployment times by applying updates, or to be insecure.

The good news is, Mr. Hyper-V, Ben Armstrong (Principal Program Manager lead on the Hyper-V team) published a neat PowerShell script that deals with this problem, called Image Factory for Hyper-V, to keep your virtual machine templates updated.

What this PowerShell script does is create and maintain a set of Windows virtual hard disks for you that are always up to date.

The great thing about this approach is that it simultaneously ensures that all your images are always up to date without wasting time reinstalling Windows unnecessarily, and without downloading duplicated updates.

For more information, please refer to the following link:

https://blogs.msdn.microsoft.com/virtual_pc_guy/2015/06/16/script-image-factory-for-hyper-v/

See also

The Creating and adding virtual hard disks to virtual machines recipe from Chapter 3, Managing Disk and Network Settings

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.4