What Is KVM?
To understanding Linux KVM, it is important to an understanding of KVMs basic features. There are many useful advantages to using Linux KVM virtualization to deploy instances for internal growth or end user utilization such as:
- Allocate More Memory Than Available
Virtual machines can be allocated more memory than physically available on the hardware. This allows for the creation of more VMs.
- Testing And Live Replication
Test your live environment side by side
- Private Networking
Databses can be set up with only private access, allowing your publicaly accessable applications to make private calls to your databases.
- Automatic NUMA Balancing
This helps to improve performance.
- Optimize Efficiency
Creating virtual machines optimizes workflows and creates isolated environments for applications.
Install KVM On CentOS
Kernel-based Virtual Machine (KVM) is virtualization software for Linux. KVM will turn your server into a hypervisor.
Here, I will show you how to setup a virtualized environment with KVM in CentOS.
STEP 1 – Install KVM
We need to install KVM tools to our system with the following command.
# sudo dnf -y update
# sudo dnf -y install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install virt-top
Start and enable the libvirtd service.
# systemctl start libvirtd
# systemctl enable libvirtd
STEP 2 – Verify Installation
Now to verify the modules have been loaded, the following command will need to be run.
# lsmod | grep -i kvm
STEP 3 – Configure Network Bridge
By default dhcpd based network is configured automatically by libvirtd. You can verify this by issueing the following command:
# nmcli connection show
# virsh net-list
# virsh net-dumpxml default
Initially, virtual machines will only have access to other virtual machines on the same local network.
A private network will have been created by libvirt. The last command above can be used to verify it.
A bridge is necessary in order to access other machines outside of the local network. To create a bridge, we need to know the NICs that are available. We can view this with the following command:
# sudo nmcli connection show
Take note of the DEVICE name and open the network file to edit it with your favourite text editor.
# vi /etc/sysconfig/network-scripts/ifcfg-[DEVICE-NAME]
Add the following line to the network file:
Now, we need to create the bridge.
DEVICE="br0" BOOTPROTO="dhcp" IPV6INIT="yes" IPV6_AUTOCONF="yes" ONBOOT="yes" TYPE="Bridge" DELAY="0"
Add a bridge slave.
# sudo nmcli connection add type bridge-slave autoconnect yes con-name br1 ifname br1 master br0
Activate the bridge
sudo nmcli connection up br0
STEP 4 – Create Virtual Machine
To create virtual machines, we will need an ISO image or 2.
# cd /var/lib/libvirt/iso/
Now we have an ISO, we can go ahead and create our virtual machine with virt-install.
# sudo virt-install --virt-type=kvm --name=centos8 --ram=2048 --vcpus=1 --os-variant=centos8.3 --file-size=30 --nonsparse --cdrom=/var/lib/libvirt/iso/CentOS-8.3.2011-x86_64-boot.iso --network bridge=br0,model=virtio --graphics vnc
The following arguements are necessary for creating virtual machines.
|Custom name of the VM|
|Disk file location of the VM|
|Allocated file size of the VM|
|The command which allocates the entire disk|
|Specifies which tool would be used for GUI installation (e.g. vnc, spice)|
|Number of virtual CPUs that will be used|
|Amount of RAM allocated|
|Network used for the VM|
|Type of the operating system|
|If you are not sure about the OS variant, type “generic”|