This guide explains what is Virt-builder, how to install Virt-builder in Linux and how to build virtual Machine images with Virt-builder in Linux.
Table of Contents
What is Virt-builder?
Virt-builder is a command line tool for building variety of virtual machine images for local or cloud use, easily as well as quickly. It also has many options to customize the images. You can install new application(s) on the VM image, set host name, set root password, run a command or script when the guest VM boots for the first time, add or edit files in the disk image and more. All of these tasks can be done from command line and doesn't require root permissions.
Virt-builder downloads the cleanly prepared, digitally signed OS templates, so you don't have to manually install the OS. All you have to do is just use the Virt-manager GUI or Virt-install command line tool to instantly fire up the VMs with the predefined templates. Virt-builder provides minimal OS templates for popular Linux and Unix variants. You can of course create your own template as well.
Install Virt-builder on Linux
Virt-builder is part of the Libguestfs library, so make sure you have installed it as described in the following guide.
Build Virtual Machine Images With Virt-builder
Building virtual machines' images with Virt-builder is quite easy and straight forward.
List available virtual machine templates
First, list the available OS templates. To do so, run:
$ virt-builder --list
As of writing this guide, the following templates were available:
centos-6 x86_64 CentOS 6.6 centos-7.0 x86_64 CentOS 7.0 centos-7.1 x86_64 CentOS 7.1 centos-7.2 aarch64 CentOS 7.2 (aarch64) centos-7.2 x86_64 CentOS 7.2 centos-7.3 x86_64 CentOS 7.3 centos-7.4 x86_64 CentOS 7.4 centos-7.5 x86_64 CentOS 7.5 centos-7.6 x86_64 CentOS 7.6 centos-7.7 x86_64 CentOS 7.7 centos-7.8 x86_64 CentOS 7.8 centos-8.0 x86_64 CentOS 8.0 centos-8.2 x86_64 CentOS 8.2 cirros-0.3.1 x86_64 CirrOS 0.3.1 cirros-0.3.5 x86_64 CirrOS 0.3.5 debian-10 x86_64 Debian 10 (buster) debian-6 x86_64 Debian 6 (Squeeze) debian-7 sparc64 Debian 7 (Wheezy) (sparc64) debian-7 x86_64 Debian 7 (wheezy) debian-8 x86_64 Debian 8 (jessie) debian-9 x86_64 Debian 9 (stretch) fedora-26 aarch64 Fedora® 26 Server (aarch64) fedora-26 armv7l Fedora® 26 Server (armv7l) fedora-26 i686 Fedora® 26 Server (i686) fedora-26 ppc64 Fedora® 26 Server (ppc64) fedora-26 ppc64le Fedora® 26 Server (ppc64le) fedora-26 x86_64 Fedora® 26 Server fedora-27 aarch64 Fedora® 27 Server (aarch64) fedora-27 armv7l Fedora® 27 Server (armv7l) fedora-27 i686 Fedora® 27 Server (i686) fedora-27 ppc64 Fedora® 27 Server (ppc64) fedora-27 ppc64le Fedora® 27 Server (ppc64le) fedora-27 x86_64 Fedora® 27 Server fedora-28 i686 Fedora® 28 Server (i686) fedora-28 x86_64 Fedora® 28 Server fedora-29 aarch64 Fedora® 29 Server (aarch64) fedora-29 i686 Fedora® 29 Server (i686) fedora-29 ppc64le Fedora® 29 Server (ppc64le) fedora-29 x86_64 Fedora® 29 Server fedora-30 aarch64 Fedora® 30 Server (aarch64) fedora-30 i686 Fedora® 30 Server (i686) fedora-30 x86_64 Fedora® 30 Server fedora-31 x86_64 Fedora® 31 Server fedora-32 x86_64 Fedora® 32 Server freebsd-11.1 x86_64 FreeBSD 11.1 scientificlinux-6 x86_64 Scientific Linux 6.5 ubuntu-10.04 x86_64 Ubuntu 10.04 (Lucid) ubuntu-12.04 x86_64 Ubuntu 12.04 (Precise) ubuntu-14.04 x86_64 Ubuntu 14.04 (Trusty) ubuntu-16.04 x86_64 Ubuntu 16.04 (Xenial) ubuntu-18.04 x86_64 Ubuntu 18.04 (bionic) fedora-18 x86_64 Fedora® 18 fedora-19 x86_64 Fedora® 19 fedora-20 x86_64 Fedora® 20 fedora-21 aarch64 Fedora® 21 Server (aarch64) fedora-21 armv7l Fedora® 21 Server (armv7l) fedora-21 ppc64 Fedora® 21 Server (ppc64) fedora-21 ppc64le Fedora® 21 Server (ppc64le) fedora-21 x86_64 Fedora® 21 Server fedora-22 aarch64 Fedora® 22 Server (aarch64) fedora-22 armv7l Fedora® 22 Server (armv7l) fedora-22 i686 Fedora® 22 Server (i686) fedora-22 x86_64 Fedora® 22 Server fedora-23 aarch64 Fedora® 23 Server (aarch64) fedora-23 armv7l Fedora® 23 Server (armv7l) fedora-23 i686 Fedora® 23 Server (i686) fedora-23 ppc64 Fedora® 23 Server (ppc64) fedora-23 ppc64le Fedora® 23 Server (ppc64le) fedora-23 x86_64 Fedora® 23 Server fedora-24 aarch64 Fedora® 24 Server (aarch64) fedora-24 armv7l Fedora® 24 Server (armv7l) fedora-24 i686 Fedora® 24 Server (i686) fedora-24 x86_64 Fedora® 24 Server fedora-25 aarch64 Fedora® 25 Server (aarch64) fedora-25 armv7l Fedora® 25 Server (armv7l) fedora-25 i686 Fedora® 25 Server (i686) fedora-25 ppc64 Fedora® 25 Server (ppc64) fedora-25 ppc64le Fedora® 25 Server (ppc64le) fedora-25 x86_64 Fedora® 25 Server opensuse-13.1 x86_64 openSUSE 13.1 opensuse-13.2 x86_64 openSUSE 13.2 opensuse-42.1 x86_64 openSUSE Leap 42.1 opensuse-tumbleweed x86_64 openSUSE Tumbleweed
As you can see, there are multiple OS templates available.
Before building a virtual machine image, you might want to look into the installation notes of the guest OS to know what is in there.
For example, to view the install notes of Debian 10, run:
$ virt-builder --notes debian-10
Sample output:
Debian 10 (buster) This is a minimal Debian install. This image is so very minimal that it only includes an ssh server This image does not contain SSH host keys. To regenerate them use: --firstboot-command "dpkg-reconfigure openssh-server" This template was generated by a script in the libguestfs source tree: builder/templates/make-template.ml Associated files used to prepare this template can be found in the same directory.
Build a virtual machine image
I wanted to download the OS templates in a specific directory, so I created this directory:
$ mkdir virtbuilder $ cd virtbuilder/
Let us build the Debian 10 virtual machine using command:
$ virt-builder debian-10
Sample output:
[ 4.8] Downloading: http://builder.libguestfs.org/debian-10.xz ##################################################################################### 100.0% [ 83.2] Planning how to build this image [ 83.2] Uncompressing [ 101.2] Opening the new disk [ 119.8] Setting a random seed virt-builder: warning: random seed could not be set for this type of guest [ 119.9] Setting passwords virt-builder: Setting random password of root to 66xW1CaIqfM8km2v [ 121.5] Finishing off Output file: debian-10.img Output size: 6.0G Output format: raw Total usable space: 5.8G Free space: 4.9G (84%)
As you can see, this command has built the minimal Debian 10 image. It will not have any user accounts. It will only have random root password and the bare minimum installed software.
The output name of the image should be same as the template name. You can change it as per your liking using the -o
option:
$ virt-builder debian-10 -o ostechnix.img
By default, the resulting image format is .img
. You can convert it to different format, for example Qcow2, like below:
$ virt-builder debian-10 --format qcow2
By default, Virt-builder will build the image same as the host OS architecture. For example, if your host OS is 64-bit, it will then build 64-bit VM. You can change this to 32-bit (if available) using --arch
option.
$ virt-builder debian-10 --arch i686
Want to build a custom-size image? It is also possible. The following command will build a VM with size 50 GB:
$ virt-builder debian-10 --size 50G
The guest OS is resized automatically using virt-resize command as it is copied to the output.
Set root password
Like I already mentioned, a random password will be assigned to the root user account while building the image. If you want to set a specific password for the root user, use --root-password
option like below:
$ virt-builder centos-8.2 --format qcow2 --root-password password:centos
Sample output:
[ 5.1] Downloading: http://builder.libguestfs.org/centos-8.2.xz ##################################################################################### 100.0% [ 249.2] Planning how to build this image [ 249.2] Uncompressing [ 271.3] Converting raw to qcow2 [ 281.1] Opening the new disk [ 319.9] Setting a random seed [ 320.4] Setting passwords [ 323.0] Finishing off Output file: centos-8.2.qcow2 Output size: 6.0G Output format: qcow2 Total usable space: 5.3G Free space: 4.0G (74%)
The above command will build CentOS 8.2 image and assign password for the root user as "centos".
You can also set password from a text file:
$ virt-builder centos-8.2 --root-password file:~/ostechnix.txt
To disable the root password, run:
$ virt-builder centos-8.2 --root-password disabled
Lock root account:
$ virt-builder centos-8.2 --root-password locked
Lock root account and disable root password:
$ virt-builder centos-8.2 --root-password locked:disabled
To assign root password, but lock the root account, use the following options:
--root-password locked:file:FILENAME --root-password locked:password:PASSWORD
We can use the root password after unlocking the root user with "passwd -u
" command.
Create users
To create user accounts while building a virtual machine image, run:
$ virt-builder centos-8.2 --firstboot-command 'useradd -m -p "" sk ; chage -d 0 sk'
The above command will create a user called "sk
" with no password and force him to set password at his first login.
Set hostname
To set the hostname to the VM:
$ virt-builder centos-8.2 --hostname virt.ostechnix.local
Sample output:
[ 4.7] Downloading: http://builder.libguestfs.org/centos-8.2.xz [ 7.2] Planning how to build this image [ 7.2] Uncompressing [ 31.0] Opening the new disk [ 41.9] Setting a random seed [ 42.0] Setting the hostname: virt.ostechnix.local [ 42.1] Setting passwords virt-builder: Setting random password of root to MRn7fj1GSaeCAHQx [ 44.4] Finishing off Output file: centos-8.2.img Output size: 6.0G Output format: raw Total usable space: 5.3G Free space: 4.0G (74%)
Install software on VM image
To install packages on a VM, run:
$ virt-builder debian-10 --install vim
Sample output:
[ 5.8] Downloading: http://builder.libguestfs.org/debian-10.xz [ 7.4] Planning how to build this image [ 7.4] Uncompressing [ 25.3] Opening the new disk [ 29.7] Setting a random seed virt-builder: warning: random seed could not be set for this type of guest [ 29.8] Installing packages: vim [ 93.2] Setting passwords virt-builder: Setting random password of root to 45Hj5yxh8vRqLDcu [ 94.9] Finishing off Output file: debian-10.img Output size: 6.0G Output format: raw Total usable space: 5.8G Free space: 4.8G (82%)
To install multiple packages, mention them within quotes, with comma-separated like below:
$ virt-builder debian-10 --install "apache2,htop"
Update all packages in the VM:
$ virt-builder centos-8.2 --update
If your VM uses SELinux, you need to do SELinux relabeling after installing or updating packages:
$ virt-builder centos-8.2 --update --selinux-relabel
Customize VM images
Virt-builder has many options to customize an image. For instance, you can run a specific command/script when the first time VM boots using command:
$ virt-builder debian-10 --firstboot-command 'apt -y update'
To add a line in the VM, run:
$ virt-builder centos-8.2 --append-line '/etc/hosts:192.168.225.1 server.ostechnix.local'
Caching templates
By default, all templates will be downloaded from the network for the first time. Since the size of the templates is large, the downloaded templates will be cached in user's home directory.
You can print the details of the cache directory and which templates are currently cached using the following command:
$ virt-builder --print-cache
Sample output:
cache directory: /home/sk/.cache/virt-builder [...] centos-7.8 x86_64 no centos-8.0 x86_64 no centos-8.2 x86_64 cached cirros-0.3.1 x86_64 no cirros-0.3.5 x86_64 no debian-10 x86_64 cached debian-6 x86_64 no debian-7 sparc64 no [...]
You can also verify it by manually looking in the cache folder:
$ ls $HOME/.cache/virt-builder centos-8.2.x86_64.1 debian-10.x86_64.1
To download all available templates to your local cache folder, run:
$ virt-builder --cache-all-templates
If you don't want to cache the template while building the image, use --no-cache
option.
To delete all cached templates, run:
$ virt-builder --delete-cache [ 0.0] Deleting: /home/sk/.cache/virt-builder
Importing disk images into hypervisor
Well, you have downloaded your desired OS and customized it as per your liking. Now what? Just import the image and run a VM using the newly created disk image with a hypervisor. We already have written a step by step guide to create a KVM virtual machine using Qcow2 image. This guide is written specifically for Qcow2, however it is same for importing .img
format disk images too.
Virt-builder has hundreds of commands and options. I covered only the basic commands here. For more details, refer the manual age.
$ man virt-builder
Featured Photo by Igor Starkov from Pexels.