Willing to create KVM template? We can help you with it.
Here at Bobcares, we have seen several such KVM related queries as part of our Server Management Services for web hosts and online service providers.
Today we’ll take a look at how to create a KVM template.
Know more about KVM
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
Now let’s take a look at how our Support Engineers create a KVM template.
1. Installing KVM
Firstly, 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.
1. 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
2. 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
1. After finishing the 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 by running the below command.
$ sudo yum install -y epel-release vim bash-completion wget curl telnet net-tools unzip lvm2
3. Then install acpid and cloud-init packages.
$ sudo yum -y install acpid cloud-init cloud-utils-growpart
$ sudo systemctl enable –now acpid
4. Also, make sure to disable the zeroconf route
$ echo “NOZEROCONF=yes” | sudo tee -a /etc/sysconfig/network
5. 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
6. If you need other packages then you can install it on your baseline template.
7. 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 any further assistance with KVM queries? – We’re available to help you]
Conclusion
Today, we saw how to create CentOS/Fedora/RHEL VM Templates on KVM.
0 Comments