How to Create a Virtual Machine in Linux with KVM

When it comes to creating virtual machines (VMs) there are few utilities as useful as kernel-based virtual machine or KVM. KVM is a type 2 hypervisor that was developed by Qumranet to help create and maintain virtual machines. Today it is one of the most popular ways to create a virtual machine in Linux.

If you’re familiar with other tools like VirtualBox don’t be surprised if you find KVM a little off-putting at first. KVM isn’t particularly user-friendly but it does allow you lots of control over the virtual machine parameters. Once you’ve taken the time to get used to KVM then you’ll find you have a ton of configuration options available to you. In this article, we’re going to look at how to create a virtual machine in Linux.

Requirements:

  • Root access

Packages:

  • qemu-kvm – The main package
  • libvirt -The libvirtd server
  • libvirt-client – Contains virsh and other utilities
  • virt-install – Install virtual machines
  • virt-viewer – Display console for virtual machines

Setup

The very first thing you need to do is check that the CPU has support for virtualization. Nine times out of ten your CPU will have this, but you still need to check to make sure. To do this enter the following command:

$ cat /proc/cpuinfo 

Once you’ve entered this command you want to scroll through the list of results until you see CPU ‘flags’. The flags you’re looking for are named svm and vmx. svm is if you are running an AMD processor and the latter is if Intel is the manufacturer.

Next you need to make sure that all the necessary kernel modules have been loaded. To check this, run the following command:

# ls mod | grep kvm
kvm_intel               200704     0
kvm                       598016      1 kvm_intel
irqbypass             16384        1 kvm

In the example above the kvm and kvm_intel modules have been loaded. It is important to note that if you’re using an AMD processor then you will see the kvm_Amd module instead. On some occasions, modules won’t load automatically. If this happens you can load them up manually by using the following command:

# modprobe kvm_intel

To finish the setup you need to load the libvirtd daemon. To launch the libvirtd daemon you need to enter the following command:

# systemctl enable --now libvirtd

Creating a Virtual Machine

Now that we’ve taken care of the initial setup it is time to create and configure our virtual machine. To do this we need to enter the virt-install command:

# virt-install --name=linuxconfig-vm \
--vcpus=1 \
--memory=1024 \
--cdrom=/tmp/debian-9.0.0-amd64-netinst.iso \
--disk size=5 \
--os-variant=debian8

There’s a lot going on in the command above, and the various options can be identified as follows:

  • –name option – Assigns a name to the virtual machine
  • –vcpus – Specifies the number of CPUs to be configured
  • –memory – option used to determine how much memory is available for the guest machine in MiB and —cdrom
  • –disk – Used to determine media storage for the guest. You can use the size option to determine the size of the virtual disk and path to specify the path to be used for the disk. If no path is selected the disk can found at $HOME/ .local/share/libvirt/images
  • –os-variant – Used to configure the guest towards one OS version. If you’d like to view all supported systems you can also enter this command: $ osinfo-query os

Once this is done the virt-viewer package will be installed and the guest OS installer will be launched:

Debian Install Screenshot

Related post: Docker vs Virtual Machines

Interacting with Virtual Machines with the Virsh Utility

The virsh utility is one of the best ways to interact with your virtual machines. To start using the virsh utility you need to check how many configured guests there are available. You can do this by running the following command:

# virsh list --all

Entering this command will show you the id, name, and state of configured guests (including whether they are active or not.) The virsh command can be used a number of ways, for example:

  • virsh shutdown – shuts down the guest
  • virsh destroy – Shutdown the guest via brute force
  • virsh undefine – Delete a guest machine
  • virsh edit – Edit machine parameters

Here we’re going to look at how you can change machine parameters:

# virsh edit linuxconfig-vm

If you’d like to increase the number of VCPUs you need to use the following command:

<vcpu placement=‘static’>1</vcpu>

To increase the number of machines you would enter:

<vcpu placement=‘static’>2</vcpu>

To make the changes, you need to reboot the virtual machine. To do this enter the following:

# virsh reboot linuxconfig-vm

Starting Virtual Machines on Boot

There are many guests that you will want to have available upon startup. By using the virsh command you can make sure that you have your virtual machines available on boot. To do this you need to enter the following command:

# virsh autostart linuxconfig-vm

If you want to deactivate this command at anytime you can enter this:

#virsh autostart --disable linuxconfig-vm

Cloning Virtual Machines

Rather than creating new virtual machines all the time, you can also clone existing machines. To do this you need to enter the following command:

virt-clond \ 
--original=linuxconfig-vm \
--name=linuxconfig-vm-clone \
--file=/var/lib/libvirt/images/linuxconfig/vm.qcow2

The options of this command are:

  • –original – The name of the guest that you want to clone
  • –name – The name of the new guest
  • –file – Used to refer to the virtual hard disks that you want to clone. Once the command is successfully completed a new domain will be created called linuxconfig-vm-clone

To verify the domain you would enter the following command:

# virsh list --all

Final Words

Setting up a virtual machine with KVM doesn’t need to be complicated if you familiarize yourself with the fundamentals. In this article, we’ve just configured the basics but as you become more experienced you can add more parameters to configure your virtual machines more effectively. For now, these settings will get you off the ground and ready to enter the world of KVM virtual machines.

See also: