One of the most essential tools in the
libguestfs package is
virt-builder. Let’s say that you really don’t want to build a VM from scratch, either because you don’t have the time or you just cannot be bothered. We will use CentOS 8 for this example, although the list of supported distributions is now roughly 50 (distributions and their sub-versions), as you can see in the following screenshot:
In our test scenario, we need to create a CentOS 8 image as soon as possible, and create a VM out of that image. All of the ways of deploying VMs so far have been based on the idea of installing them from scratch, or cloning, or templating. These are either start-from-zero or deploy-first-template-or-template-second-provision-later types of mechanisms. What if there’s another way?
virt-builder provides us with a way of doing just that. By issuing a couple of simple commands, we can import a CentOS 8 image, import it to KVM, and start it. Let’s proceed, as follows:
- First, let’s use
virt-builderto download a CentOS 8 image with specified parameters, as follows:
- A logical next step is to do
virt-install—so, here we go:
- If this seems cool to you, let’s expand on that. Let’s say that we want to take a
virt-builderimage, add a
yumpackage group called
Virtualization Hostto that image, and, while we’re at it, add the root’s SSH key. This is what we’d do:
In all reality, this is really, really cool—it makes our life much easier, does quite a bit of work for us, and does it in a pretty simple way, and it works with Microsoft Windows operating systems as well. Also, we can use custom
virt-builder repositories to download specific VMs that are tailored to our own needs, as we’re going to learn next.
Obviously, there are some pre-defined
virt-builder repositories (http://libguestfs.org/ is one of them), but we can also create our own. If we go to the
/etc/virt-builder/repos.d directory, we’ll see a couple of files there (
libguestfs.conf and its key, and so on). We can easily create our own additional configuration file that will reflect our local or remote
virt-builder repository. Let’s say that we want to create a local
virt-builder repository. Let’s create a config file called
local.conf in the
/etc/virt-builder/repos.d directory, with the following content:
Then, copy or move an image to the
/root/virt-builder directory (we will use our
centos-8.0.img file created in the previous step, which we will convert to
xz format by using the
xz command), and create a file called
index in that directory, with the following content:
[Packt01] name=PacktCentOS8 osinfo=centos8.0 arch=x86_64 file=centos-8.0.img.xz checksum=ccb4d840f5eb77d7d0ffbc4241fbf4d21fcc1acdd3679 c13174194810b17dc472566f6a29dba3a8992c1958b4698b6197e6a1689882 b67c1bc4d7de6738e947f format=raw size=8589934592 compressed_size=1220175252 notes=CentOS8 with KVM and SSH
A couple of explanations.
checksum was calculated by using the
sha512sum command on the
compressed_size are real sizes of the original and XZd file. After this, if we issue the
virt-builder --list |more command, we should get something like this:
You can clearly see that our
Packt01 image is at the top of our list, and we can easily use it to deploy new VMs. By using additional repositories, we can greatly enhance our workflow and reuse our existing VMs and templates to deploy as many VMs as we want to. Imagine what this, combined with
virt-builder‘s customization options, does for cloud services on OpenStack, Amazon Web Services (AWS), and so on.
The next topic on our list is related to snapshots, a hugely valuable and misused VM concept. Sometimes, you have concepts in IT that can be equally good and bad, and snapshots are the usual suspect in that regard. Let’s explain what snapshots are all about.