Introduction

This first article I will show you how to install KVM in Debian 9 and how to create a CentOS 7 Virtual Guest.

IMPORTANT NOTICE!

Now, an important thing to keep in mind is that depending on the Linux distribution you are using, the names of the packages to install could be different. This is really important, as you could find yourself in strange situations trying to install packages with the name from another distribution, wondering why that particular package is not in the repositories.


With this exercises, we will cover the Virtualization competences, which is a great thing. I will show you everything using only the command line because while you are in the exam, the only available environment for you is the CLI. As I know that all those tasks are easier to accomplish using the GUI, you will need to know how to do them using only the command line! Below are the Virtualization competences listed:


  • Virtualization - 5%

    • Configure a hypervisor to host virtual guests
    • Access a VM console
    • Configure systems to launch virtual machines at boot
    • Evaluate memory usage of virtual machines
    • Resize RAM or storage of VMs


Preliminary requirements

Before stepping into installing KVM, you should be certain that your system has virtualization available and enabled. For this, you should look for vmx (for Intel procs) or svm (for AMD procs) inside the cpuinfo file:

cat /proc/cpuinfo | grep vmx

Now, check if the proper kernel modules for virtualization have been loaded.

lsmod | grep kvm


Installing KVM

To install the Virtualization stack you will need to install the following packages from the repositories, at first:

  • qemu-kvm
  • libvirt-clients
  • libvirt-daemon-system
  • virt-manager

Thus, in Debian 9, you will use the command:

sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system virt-manager


Download the CentOS 7 iso file

Before creating a virtual machine, you will need to download the iso file of CentOS 7. I will show you how to download the iso file using the command line, but in order to do this, you will need to know the address/location of the file. I will use a mirror that is available in my country (Romania), but you should use one that is available to yours. You will find a list of available mirrors for your country at the following address: http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso

From all the mirrors listed, I will choose this one: http://ftp.ines.lug.ro/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso. Now, in order to download the file, I will use the wget command, as follows:

wget -c 'http://ftp.ines.lug.ro/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso'

Now, to check that everything is in order, I will use the command:

cd ~/Downloads
ls -la
total 4418804
drwxr-xr-x.  4 alexandru alexandru       4096 Mar 17 15:33 .
drwx--x---+ 23 alexandru alexandru       4096 Mar 17 11:23 ..
drwxr-xr-x.  2 alexandru alexandru       4096 Mar 17 11:24 centos
-rw-rw-r--.  1 alexandru alexandru  851459712 Sep  6  2017 CentOS-7-x86_64-Minimal-1708.iso
drwxr-xr-x.  2 alexandru alexandru       4096 Mar 17 11:25 debian
-rw-rw-r--.  1 alexandru alexandru     100023 Aug 24  2017 dropbox.rpm

And you will see that the CentOS iso file is there.

Now, in order to avoid any problems, you will have to copy your iso file from the Downloads directory to the /var/lib/libvirt/images/ directory. For this, you should run the command:

sudo cp -p /home/alexandru/Downloads/CentOS-7-x86_64-Minimal-1708.iso /var/lib/libvirt/images/


Prepare to create a new Virtual Guest

Configure the NAT and default Network

NAT is the most common method to share network connections. In order to do this, you must configure the host with the command:

sudo virsh net-list --all

This command will show you a default network connection that is inactive and has no autostart. To change this, issue the commands:

sudo virsh net-autostart default
sudo virsh net-start default

and then check to see if everything is according to the plan:

sudo virsh net-list --all

The output should be as follows:

 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

Now, if you run the command

sudo brctl show

you will see that there is an isolated bridge device available, but it uses NAT and has no physical interface added.

bridge name	bridge id		STP enabled	interfaces
virbr0		8000.52540049910c	yes		virbr0-nic

To be sure that everything will work as planning, you should uncomment the line net.ipv4.ip_forward = 1 in the /etc/sysctl.conf file.

Now, you should continue to create a Virtual Guest.


Create a new CentOS 7 Virtual Guest

To create a new virtual machine with KVM, you will need to use the virt-install utility. This utility has several parameters that you will need to fill in order for the VM to be created correctly. Let’s see them. You can also read about them using the man virt-install command.

NOTE: the – sign is actually two hyphens like - - but without the space between them!

  • –name = the name of the virtual machine you are about to create
  • –os-type = the type of the os, in our case Linux
  • –os-variant = what variant of the os type is it, in our case rhel7
  • –disk path = location of the disk image
  • –disk size = the size of the disk image to be created - in GiB
  • –disk bus = the
  • –graphics = is the way to connect to the virtual machine - usually it is spice
  • –vcpu = the number of virtual cpu’s
  • –ram = the amount of RAM in megabytes
  • –cdrom = the source path of the installation iso file
  • –network = the bridge adapter to be used by the virtual machine

Before setting up a virtual guest, I will strongly advice to restart your Debian system, otherwise you will run into some very specific errors. Thus, run the command:

sudo shutdown -r now

The command to setup a CentOS Virtual Guest is as follows:

sudo virt-install --name=centos7 --os-type=Linux --os-variant=rhel7 --memory=1024 --vcpus 1 --disk path=/var/lib/libvirt/images/centos.qcow2,bus=virtio,size=9 --graphics=spice --cdrom /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1708.iso --network=default

This command will automatically switch to the gui and virt-manager interface, showing you the installation options.

Now, I will not talk about how to use the virsh command line, as I did this already in the former CentOS 7 KVM post. All the commands are the same, as they are virsh specific, and are independent on the underlying distribution that you use.

Enjoy!