Virtualization Virt-builder Lead image: Lead Image © Petris, fotolia.com
Lead Image © Petris, fotolia.com
 

Virt-builder generates VM images in a flash

Greased Lightning

The virt-builder tool lets you create new virtual machines in just a few seconds. We take a first look at the brand new tool. By Oliver Frommel

The Linux universe is taking giant steps in the field of virtualization. At first, there was only VMware, and then Xen was very popular for a time. Meanwhile, KVM (Kernel Virtual Machine) has become the dominant hypervisor system; for example, Red Hat's enterprise virtualization solution is based on it. Red Hat seeks to grab some of VMware's market share with its RHEV (Red Hat Enterprise Virtualization) advanced development, but given that Microsoft's Hyper-V has the same ambitions, this could prove to be a lofty goal.

Red Hat puts much energy into developing the Linux kernel, the hypervisor, and the whole infrastructure. An example is libguestfs, which supports access to and working with virtual disk images. It was presented in Linux Magazine by Red Hat developer Richard W.M. Jones [1], who has now added a new tool to the collection. For some time, Jones has been experimenting with Supermin appliances: extremely small images (around 100KB) that boot in seconds. Based on this, he has developed the virt-builder [2] tool, which greatly accelerates the installation of new VM images. Otherwise, this can be a rather lengthy process that includes performing a normal installation of a Linux distribution.

Lightning VMs

With virt-builder, the whole process happens a bit faster; a new VM image is created on the basis of stripped-down templates that include only the most essential components – a Linux kernel, the basic daemons, and systemd. Drawing on more tools from the libguestfs package, virt-builder modifies the template to suit the administrator's needs – for example, setting the root password, configuring the network, and so on.

Virt-builder is provided in the libguestfs packages as of version 1.24, although no current Linux distribution has these packages on board. Thus, if you want to try out virt-builder, you'll need to install from the source code, which can be downloaded as a TAR package or directly from the GitHub repository. For Fedora 20, Jones already offers prebuilt libguestfs RPMs of version 1.24, but the distribution is currently beta. Conveniently, libguestfs does not need to be installed throughout the system just to try out the new tools. With special run scripts, you can run the programs directly in the source tree.

The dependencies for libguestfs can be conveniently and easily resolved on Debian/Ubuntu by issuing the

apt-get builddep libguestfs

command. Additionally, the flex, bison, curl, and gnupg packages are required. Because the tools also use files from the host filesystem, it must be up to date

apt-get update
apt-get dist-upgrade

(for Debian/Ubuntu). After completing all this work, you can ./configure the source and source code and then compile on a multicore system with make -j<number of cores>. To compile libguestfs from the GitHub repository, the developers recommend the script in Listing 1, which draws on Listing  2.

Listing 1: localconfigure

. localenv
./autogen.sh \
  --prefix /usr \
  --libdir /usr/lib \
  --disable-static \
  --enable-gtk-doc \
  -C \
  "$@"

Listing 2: localenv

export SKIP_TEST_PARALLEL_MOUNT_LOCAL=1
export SKIP_TEST_FILE_ARCHITECTURE_11=1

Normally, virt-builder does not need root privileges to generate an image. However, it does need to access the kernel image and initial ramdisk of the hosts, which are unfortunately not accessible to typical users on Ubuntu. So, you either have to run virt-builder with sudo or adjust the access permissions for the /boot directory to match. For a text of the compiled tool, do a make quickcheck. If everything works, you can start using virt-builder.

The virt-builder --list command outputs an overview of the available systems. As mentioned, the tool must be run with a run script in the source code tree:

./run builder/virt-builder --list

The output is shown in Figure 1.

Virt-builder displays a list of the Linux systems it can install.
Figure 1: Virt-builder displays a list of the Linux systems it can install.

Unfortunately, however, you can't actually build an image at this point, because you still need the appropriate templates for these systems; they normally reside in the builder/website/ directory [3]. To download, for example, an image for installing Fedora 19, change to the builder/website directory and issue the following command when you get there:

wget http://libguestfs.org/download/builder/fedora-19.xz

The image is only 164MB in size, so the download is done quickly. Back in the main directory of the libguestfs distribution, you can now create a new Fedora image with the call:

./run builder/virt-builder fedora-19 --output fedora19.img --hostname fedora19

This command tells the tool to generate a random root password for the new system. If you do not want this to happen, you can specify a new password at the command line. For security reasons – so that it does not appear in the process list – the password is not passed in directly as a parameter but via a file. The password is kept in cleartext in the file and passed to the tool using the --root-password file:Rootpass.txt option.

You can modify the Linux installation that this creates to suit your own needs in a variety of ways. For example, virt-builder -notes template lists the kickstart and installation scripts used so that you can customize the installation (Figure 2).

The image can be customized via the kickstart file and installation script.
Figure 2: The image can be customized via the kickstart file and installation script.

In the case of Fedora 19, everything is over after 45 seconds, and a new disk image exists on the hard disk. If desired, you can immediately boot it with Qemu or use virt-install to import it into a Linux and Libvirt-based virtualization infrastructure:

sudo virt-install --name fedora19 --import --ram 2048 --disk /var/lib/libvirt/images/fedora19.img

The new system needs a bit longer to complete the first boot because it has to run the first boot script. After this, it then takes about 10 seconds for the login prompt to appear. If you take a look at the virtual filesystem after logging in, you will get an impression of the resource consumption: The newly installed Linux system only uses about 700MB.

Beyond the installation, virt-builder can also set the hostname of the new VM, create users, change passwords, install packages, and edit arbitrary files. Because the virtual machines can be created so quickly, they are also useful for quickly completing some task with a different Linux distribution. Jones demonstrates in his blog how he used virt-builder to build packages for other distributions [4]. The construction of a complete libvirt package took about six minutes, compared with about two minutes on a non-virtualized host system.

When this issue went to press, another interesting discussion was underway on virt-builder [5]: Jones had to justify the fact that he wrote virt-builder in the relatively exotic OCaml programming language, which, in the words of his colleague, "only he understands."