Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Create a KVM Virtual Machine Template - DesignLinux

Jan 13 2021
designlinux 0 Comments

A virtual machine template is essentially a copy of the installed virtual machine that comes in handy when you want to deploy multiple instances of virtual machines. Creating a template is a 3 step process that involves creating a virtual machine, installing all the requisites packages that you want to be installed, and finally cleaning up the template.

Let’s go ahead and see how you can accomplish this.

Step 1: Installing KVM in Linux

The first step is to install KVM on your system. We have comprehensive tutorials on:

  • How to Install KVM on Ubuntu 20.04
  • How to Install KVM on CentOS 8

Additionally, ensure that the libvirtd daemon is running and is enable to automatically kick on bootup.

$ sudo systemctl enable libvirtd
$ sudo systemctl start libvirtd

Verify if the libvirtd daemon is running.

$ sudo systemctl status libvirtd
Verify libvirtd Status
Verify libvirtd Status

If you are running a Ubuntu / Debian system, ensure that the vhost-net image is loaded.

$ sudo modprobe vhost_net

Step 2: Create a KVM Virtual Image

Before we get to creating a template, we need to, first of all, have an installation instance. On the command-line, we are going to create a 20G CentOS 8 KVM image using the qemu-img command as shown.

$ sudo qemu-img create -o preallocation=metadata -f qcow2 /var/lib/libvirt/images/centos8.qcow2 20G
Create CentOS KVM Image
Create CentOS KVM Image

Next, use the virt-install command to create a CentOS 8 virtual machine as shown.

$ sudo virt-install --virt-type kvm --name centos8 --ram 2096 \
--disk /var/lib/libvirt/images/centos8.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--os-type=linux --os-variant=rhel7.0 \
--location=/home/tecmint/Downloads/CentOS-8-x86_64-1905-dvd1.iso
Create CentOS 8 Virtual Machine
Create CentOS 8 Virtual Machine

This launches the virtual machine instance. You can confirm this by heading over to the virt-manager and opening the console window as shown. What you can see is the default welcome page for the installer. Be sure to complete the installation to the very end.

CentOS 8 Virtual Machine Installation
CentOS 8 Virtual Machine Installation

Step 3: Creating KVM Virtual Machine Template Image

Once the installation is complete, log into the VM and update all the system packages.

$ sudo dnf update

Install the prerequisite packages that you feel are essential to get started with. In this case, I will install EPEL release, wget, curl, net-tools, and vim. This may be different for your case.

$ sudo dnf install epel-release wget curl net-tools vim

If you intend to deploy your template on a cloud platform, install the cloud-init packages as shown.

$ sudo dnf install cloud-init cloud-utils-growpart acpid

Next, disable the zeroconf route.

$ echo "NOZEROCONF=yes" >> /etc/sysconfig/network

Once you are done, be sure to power off your virtual machine and clean up the VM template image as shown.

$ sudo virt-sysprep -d centos8

The virt-sysprep is a command-line utility that resets a virtual machine in order for clones to be made from it. It removes entries such as SSH host keys, log files, user accounts, and some persistent network configurations. To use the command, first, you must always make sure that the VM is powered off.

$ sudo virt-sysprep -d centos8
Clone CentOS 8 Virtual Machine
Clone CentOS 8 Virtual Machine

Lastly, invoke the command shown to undefine the VM domain.

$ sudo virsh undefine centos8
Undefine VM Domain
Undefine VM Domain

The template image is now ready for cloning and deployment.

Related

Tags: CentOS Tips, KVM Tips, Ubuntu Tips

How to Install Different PHP (5.6, 7.x and 8.0) Versions in Ubuntu

Prev Post

How to Setup CentOS Stream from AWS Marketplace

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 259
  • 614,631

DesignLinux.com © All rights reserved

Go to mobile version