virt-builder and virt-builder repos – KVM

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:

Figure 8.24 – virt-builder supported OSes, and CentOS distributions

Figure 8.24 – virt-builder supported OSes, and CentOS distributions

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:

  1. First, let’s use virt-builder to download a CentOS 8 image with specified parameters, as follows:
    Figure 8.25 – Using virt-builder to grab a CentOS 8.0 image and check its size

    Figure 8.25 – Using virt-builder to grab a CentOS 8.0 image and check its size

  2. A logical next step is to do virt-install—so, here we go:

    Figure 8.26 – New VM configured, deployed, and added to our local KVM hypervisor

    Figure 8.26 – New VM configured, deployed, and added to our local KVM hypervisor

  3. If this seems cool to you, let’s expand on that. Let’s say that we want to take a virt-builder image, add a yum package group called Virtualization Host to that image, and, while we’re at it, add the root’s SSH key. This is what we’d do:
Figure 8.27 – Adding Virtualization Host

Figure 8.27 – Adding Virtualization Host

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.

virt-builder repositories

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:

[local]
uri=file:///root/virt-builder/index

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 centos-8.0.img.xz. size and 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:

Figure 8.28 – We successfully added an image to our local virt-builder repository

Figure 8.28 – We successfully added an image to our local virt-builder repository

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.

Related Articles

How to add swap space on Ubuntu 21.04 Operating System

How to add swap space on Ubuntu 21.04 Operating System

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM it use swap space to move inactive pages from RAM. Swap space can be created into Linux system in two ways, one we can create a...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

14 − 6 =