Create CentOS Fedora RHEL VM Template on KVM - How to do it








Are you trying to create KVM template? 

This guide is for you.


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.

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.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform KVM related queries.

In this context, we shall look into how to create a KVM template.


More information about KVM ?

Basically, KVM is an open source full virtualization solution for Linux. A VM template is a master copy image of a virtual machine that has VM disks, virtual devices, and settings that are mainly used to clone, convert, or deploy more virtual machines.

Moreover, they are highly useful when deploying high numbers of similar VMs that require consistency across deployments.


How to Create CentOS/Fedora/RHEL VM Templates on KVM ?

Follow the steps given below to create a KVM template.


1. How to install KVM ?

To begin, we verify the CPU support for Intel VT or AMD-V Virtualization extensions. In a few systems, this is disabled on BIOS. So you would need to enable it manually:

$ cat /proc/cpuinfo | egrep "vmx|svm"

You can do the same using the Iscpu command as well.

$ lscpu | grep Virtualization
Virtualization: VT-x

The KVM packages are distributed on RHEL 8 via the AppStream repository. So, install KVM on your RHEL 8 server by running the below commands:

$ sudo yum update
$ sudo yum install @virt

Now install the virtualization packages on the Fedora system by running:

$ sudo dnf -y install bridge-utils libvirt virt-install qemu-kvm

Once the installation completes, verify that Kernel modules are loaded:

$ lsmod | grep kvm
kvm_intel 233472 0
kvm 737280 1 kvm_intel

Also, make sure to install useful tools for virtual machine management:

$ sudo dnf -y install virt-top libguestfs-tools

Then ensure that the KVM service (libvirtd) is running and enabled to start at boot:

$ sudo systemctl start libvirtd
$ sudo systemctl enable libvirtd

Next, enable the vhost-net kernel module on Ubuntu/Debian:

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

 

2. Create CentOS/Fedora/RHEL VM

Before creating a baseline template, we need to install CentOS/Fedora or RHEL Linux servers. So it is better to keep the size of the disk small for Base OS installation.

Now let’s see the installation of CentOS 7 VM on KVM using the virt-install command-line tool.


i. First, 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

ii. Next, start to install 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
Below are the details that you need to 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.

After starting the installation, here is the output that you receive:

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.

Now open the 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’

Then check and set console installation parameters.


3. Preparing the CentOS/Fedora/RHEL VM template

i. After finishing the VM installation, login to the instance and update all system packages to the latest versions:

$ sudo yum -y update

ii. Install standard basic packages missing by running the below command:

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

iii. . Then install acpid and cloud-init packages:

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

iv. . Also, make sure to disable the zeroconf route

$ echo “NOZEROCONF=yes” | sudo tee -a /etc/sysconfig/network

v. Configure GRUB_CMDLINE_LINUX – For Openstack usage:

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

Then generate grub configuration:

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

vi. If you need other packages then you can install it on your baseline template.

vii. Once done, power off the virtual machine:

$ sudo poweroff

 

4. Cleaning VM template

For cleaning the instance, you need virt-sysprep tool:

$ sudo virt-sysprep -d centos7

After cleaning the VM, undefine the libvirt domain:

$ sudo virsh undefine centos7

Domain centos7 has been undefined

Now the KVM VM template is ready for use.


[Need urgent assistance with KVM queries? – We're available to help you. ]



Conclusion

This article covers how to create CentOS/Fedora/RHEL VM Templates on KVM. 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.


To install KVM in your Linux system:

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


How to 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.


How to Clean VM template ?

You need virt-sysprep tool for cleaning the instance.

$ sudo virt-sysprep -d centos7


For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on LinuxAPT.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com