Use libguestfs to create VM templates on KVM easily

Virt-sysprep, the latest libguestfs tool for Linux, helps make deploying virtual machines uncomplicated by letting you create a template VM.

Libguestfs has been around for a long time in Linux KVM environments. What started as a tool to mount virtual machine image files is now a rich set of utilities providing several different functions -- including the option to create templates -- using the virt-sysprep utility.

If you want to deploy virtual machines (VMs) easily, templates are a useful tool. To set up a template, all system-specific properties need to be removed from the VM. On a Linux machine, specifics include SSH host keys, persistent network configuration (especially everything related to the MAC address) and user accounts. The virt-sysprep utility takes care of all this and can also be used to customize a VM by adding specific information.

In its current state, virt-sysprep works only on Linux. If you need to prepare a Windows system, you can use the Windows sysprep.exe utility. However, there are plans to include support for Windows in future virt-sysprep versions.

You can access virt-sysprep from the KVM host and use it to modify the disk image of the guest VM. It is best to set up a VM as a template machine rather than use an existing VM because the utility removes configuration settings and information specific to the VM. If you do use an existing VM, be aware that it will remove existing content that you may want to keep, so you should create a backup, snapshot or clone of the virtual disk file first.

Libguestfs' virt-sysprep utility does more than just remove files and configuration from the VM disk image file. On a KVM VM, specific information is stored in two different parts. The libvirt XML code contains specific information, like the name, UUID, path to the block device and network card MAC address. The disk image stores other specific information as well, including hostname, network configuration, UUID and SSH host keys.

Using virt-sysprep operations

By default, when using virt-sysprep, the entire system is cleaned to prepare a system image. When using virt-sysprep, you can also choose which specific operations you want to use. Use the command virt-sysprep --list-operations to get a list of all operations available with your version of the utility. Enabled operations are marked with an asterisk. If you want to change the default configuration, use --operations-, followed by all the specific operations you want to use.

For general purposes, you don't need to change many operations. The default configuration takes care of removing everything that is specific for a VM. It's important to think beyond the network configuration, including bash-history, cron-spool directories, log files and many of the other typical traces that are left on a machine that has been used.

After cleaning up a VM with libguestfs' virt-sysprep, you will need to add new configuration settings. A convenient way of doing this is by using the --firstboot option to run a script. This is basically the same procedure as the one executed after a fresh installation of Linux. By default, virt-sysprep removes all of the firstboot information. If you want firstboot to run on the VM that you've sysprepped, use the --firstboot option when issuing the virt-sysprep tool to run any specific shell script.

Dig Deeper on Virtual machine performance management