# Setting up a KVM virtual machine on CentOS 7

I’ve been running virtual machines for many years. In the past, I mainly used VirtualBox as my hypervisor since it’s free and it can be installed on Linux/macOS/Windows. Recently, I was asked to install a VM on a remote server at work. This time, I decided to set up a KVM-based VM instead. This post describes the steps to set up KVM and create a VM.

## Terminologies

Before we dive into the steps, let me first introduce and explain a few terms.

• KVM. KVM stands for Kernel-based Virtual Machine. Essentially, this is a Linux kernel module that allows a guest VM to execute CPU instructions directly using the host’s hardware, achieving near-native performance. It requires that the host CPU has hardware virtualization support.
• QEMU. QEMU is by itself an emulator that can emulate CPU of different architectures (x86, MIPS, ARM, PowerPC, etc.) and other hardware devices including memory, disk, network cards, USB devices and more. When using together with KVM, QEMU can utilize it to run instructions from the guest directly on the host instead of emulating the CPU, while still emulating other necessary devices.
• libvirt. libvirt is a C library for managing various hypervisors including KVM.
• virsh. virsh provides a command line interface to the libvirt library. It’s part of the libvirt project. With virsh, you can easily manage your hypervisors on the command line.

## Setting up KVM

First of all, please make sure that your CPU supports hardware virtualization (Intel-VT or AMD-V) and you have it enabled in the BIOS. You could check the output of lscpu on your host.

$lscpu | grep -i virtualization Virtualization: VT-x  Next make sure KVM is enabled. You need both kvm and kvm_intel (or kvm_amd for AMD CPUs) to be loaded. $ lsmod | grep -i kvm
kvm_intel             229376 0
kvm                   724992 1 kvm_intel


Now let’s install QEMU, libvirt, and virt-install and enable libvirt. Virt-install is just a simple program that helps you create a VM with one command. It uses virsh underneath.

$sudo yum install qemu-kvm libvirt virt-install$ sudo systemctl start libvirtd
$sudo systemctl enable libvirtd  ## Creating a VM To create a VM, we first need an OS image. In my case I installed a CentOS 7 minimal. $ cd /var/lib/libvirt/boot
$wget http://mirror.nus.edu.sg/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1804.iso  Now let’s create the VM itself. $ virt-install --virt-type=kvm --name <name> \
--ram 32768 --vcpus=4 --os-variant=centos7.0 \
--cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1804.iso \
--network=network=default,model=virtio \
--disk path=/var/lib/libvirt/images/pbs.qcow2,size=20,bus=virtio,format=qcow2


In my example, I’m telling virt-install to create a VM with KVM as the hypervisor. The parameters should be self-explanatory. By default, libvirt sets up a NAT network (called the “default” network) which allows the guest to have Internet access. Therefore, I didn’t use any additional commands to setup the networking on the host.

When you run the command, the VM will first boot with the ISO you supplied and it will open a GUI for you to complete the installation of your OS just like how you install it on a bare-metal machine. Therefore, if you are installing on a remote server, make sure you SSH into the server with X11 forwarding enabled and that you have an X server running on your own computer. Alternatively, you could forward port 5901 on the remote server to your local machine with ssh -L and connect with a VNC client instead. Either way, you should be able to see the GUI and finish the installation successfully.

## Conclusion

In conclusion, I described a few important terminologies related to KVM and showed you how to actually run commands to install a VM with KVM on your Linux host. I hope now you have a better understanding of how KVM works and are able to create VMs on your machine with ease.