(Last Updated On: May 5, 2019)

How Can I create a VM template for CentOS / Fedora / RHEL Linux distributions on KVM?. I love KVM and I use it for all my home Virtualization labs. KVM is an open source full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). VM Template is a master copy of a VM instance from which you can clone, convert or deploy more virtual machines.

VM Templates are more useful when deploying high numbers of similar VMs that require consistency across deployments. If something goes wrong in an instance created from the Template, you can clone a fresh VM from the template with minimal effort.

Step 1: Install KVM

Install KVM in your Linux system by referring to a relevant article from the list below.

How to install KVM on RHEL / CentOS 8

How to install KVM on Fedora

Install KVM on CentOS 7 / Ubuntu / Debian / SLES

Install KVM on Arch Linux / Manjaro

The KVM service (libvirtd) should be running and enabled to start at boot.

sudo systemctl start libvirtd
sudo systemctl enable libvirtd

Enable vhost-net kernel module on Ubuntu/Debian.

sudo modprobe vhost_net
echo vhost_net | sudo tee -a /etc/modules

Step 2: Create CentOS / Fedora / RHEL VM

Before we can create a baseline template, we need installation of CentOS / Fedora or RHEL Linux server. I recommend you keep the disk size small for Base OS installation. This guide will demonstrate installation of CentOS 7 VM on KVM using the virt-install command line tool.

1. Create a 10 GB VM image using qemu-img.

$ sudo qemu-img create -o preallocation=metadata -f qcow2 /var/lib/libvirt/images/centos.qcow2 10G
Formatting '/var/lib/libvirt/images/centos.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 preallocation=metadata lazy_refcounts=off refcount_bits=16

2. Start Installation of your baseline operating system.

sudo virt-install --virt-type kvm --name centos7 --ram 1024 \
--disk /var/lib/libvirt/images/centos.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--os-type=linux --os-variant=rhel7.0 \
--location=/home/jmutai/iso/CentOS-7-x86_64-Minimal-1810.iso

Replace:

  • centos7 with the name of Base instance.
  • /var/lib/libvirt/images/centos.qcow2 with the path to disk image
  • /home/jmutai/iso/CentOS-7-x86_64-Minimal-1810.iso with path to your ISO file
  • default with a name of KVM network to use.

On starting the installation, you should get the output:

Starting install...
Setting input-charset to 'UTF-8' from locale.
Retrieving file vmlinuz...                                                                                                      | 6.3 MB  00:00:00     
Setting input-charset to 'UTF-8' from locale.
Retrieving file initrd.img...                                                                                                   |  50 MB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

Open your Virt Manager to finish the installation.

For console installation, use the following lines instead.

sudo virt-install --virt-type kvm --name centos7 --ram 1024 \
   --disk /var/lib/libvirt/images/centos.qcow2,format=qcow2 \
   --network network=default \
   --os-type=linux --os-variant=rhel7.0 \
   --location=/home/jmutai/iso/CentOS-7-x86_64-Minimal-1810.iso \
   --graphics none \
   --console pty,target_type=serial \
   --extra-args 'console=ttyS0,115200n8 serial'

Check and set console installation parameters.

Step 3: Prepare CentOS / Fedora / RHEL VM template

1. Update system

After you finish VM installation, login to the instance and update all system packages to the latest versions.

sudo yum -y update

2. Install standard basic packages missing:

sudo yum install -y epel-release vim bash-completion wget curl telnet net-tools unzip lvm2 

3. Install acpid and cloud-init packages.

sudo yum -y install acpid cloud-init cloud-utils-growpart
sudo sudo systemctl enable --now acpid

4. Disable the zeroconf route

echo "NOZEROCONF=yes" | sudo tee -a /etc/sysconfig/network

5. Configure GRUB_CMDLINE_LINUX – For Openstack usage.

If you plan on exporting template to Openstack Glance image service, edit the /etc/default/grub file and configure the GRUB_CMDLINE_LINUX option. Your line should look like below – remove rhgb quiet and add console=tty0 console=ttyS0,115200n8.

GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap console=tty0 console=ttyS0,115200n8"

Generate grub configuration.

sudo grub2-mkconfig -o /boot/grub2/grub.cfg

6. Install other packages you need on your baseline template.

7. When done, power off the virtual machine.

sudo poweroff

Step 4: Clean VM template

You need virt-sysprep tool for cleaning the instance.

sudo virt-sysprep -d centos7

Output:

Undefine the libvirt domain after cleaning up the VM.

$ sudo virsh undefine centos7
Domain centos7 has been undefined

Your KVM VM template is ready for use.

Step 5: Using KVM templates

You can manually Provision VMs from the template or automate Provisioning of VMs on KVM with Terraform.