LFCS - KVM Installation

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
344
Reaction score
387
Credits
11,920
Kernel-based Virtual Machine (KVM) allows for Virtual Machines (VM) to be created and run on a machine with an existing Operating System (OS). The overall KVM core is a loadable module. Along with the core module is a processor model specific module. These work together to create a VM that allows you to run other Operating Systems on your host system.

All of this is possible by a Central Processing Unit (CPU) that provides the ability of Virtualization.

Keep in mind that these will not completely work on a system that is a virtual machine. Use physical hardware to install KVM. Do not install in VirtualBox. You can run the 'virsh' command from within VirtualBox to connect to remote systems, but you cannot create virtual machines locally.

Virtualization Abilities

If you are by chance running an older system, it may not have the ability for CPU Virtualization. We can test this to verify if your CPU has the ability.

Run the following command:

Code:
cat /proc/cpuinfo | grep flags | grep vmx
cat /proc/cpuinfo | grep flags | grep svm

You should see that ‘vmx’ is highlighted in Figure 1. If your system is an AMD, then it should work with the second command and highlight ‘svm’. Intel processors support 'vmx' and AMD processors support 'svm'.

Figure 1.jpg

FIGURE 1

The 'vmx' or 'svm' shows that the processor has the ability for Virtualization. If you do not see the 'vmx' or 'svm' output, then you cannot perform steps in this article on your system.

KVM Installation

Some tests we run can show if the system loaded the KVM module, so let's do that now.

For CentOS 7, perform:

Code:
sudo yum install qemu-kvm virt-install virt-manager libvirt openssh-askpass -y

The command will install the packages. Hopefully, you already have 'libvirt' installed, but I added it in the list.

For Ubuntu systems, use:

Code:
sudo apt install libvirt-daemon-system virtinst qemu-kvm virt-manager ksshaskpass -y

This should now have the KVM installed, but we'll need a little configuration.

KVM Configuration

The configuration is basic. Any users that could connect to the Host system remotely, or locally, needs to be added to the 'libvirt' group. It may be best to add 'root' to the group as well, just in case you should connect as root through an SSH connection. Perform the commands:

Code:
sudo usermod -a -G libvirt root
sudo usermod -a -G libvirt USER

Just switch 'USER' for the user that you might use for connecting. If it is the current user, then just replace it with '$USER'.

There is a file that needs to be edited at '/etc/ssh/sshd_config'. A Line needs to be uncommented or added:

Code:
PermitRootLogin yes

Save the file and restart the SSH service with:

Code:
sudo systemctl restart sshd

NOTE: Be sure to edit the 'sshd_config' and NOT 'ssh_config'.

Sometimes, you may need to set a ROOT password that is not always set at the installation of the Operating System (OS). Use the command:

Code:
sudo passwd root

After you make the changes, you should logout and sign back into the OS.

Module Virtualization

There are two basic manufacturers of x86_64 CPUs for the PC: Intel and AMD.

The modules are 'kvm-intel.ko' for the Intel processors and 'kvm-amd.ko for the AMD processors. For the core KVM, there is the 'kvm.ko' module.

You can tell which one your system is using with the command:

Code:
lsmod | grep kvm

The results should show the modules installed for your system. The listing should show the 'kvm.ko' loadable module. Also, the listing should show the processor file as discussed at the beginning of the article ('kvm-intel.ko' or 'kvm-amd.ko'). See Figure 2 for an example output.

Figure 2.jpg

FIGURE 2

Local or Remote Connection


We can perform a local or remote connection with either a Command-Line Interface (CLI) program call 'virsh', or we can use a Graphical User Interface (GUI) call 'virt-manager'.

To start 'virsh', we only need to enter 'virsh' at a command-line. The default command that is interpreted without parameters is 'virsh -c qemu:///system'. The default connection is to the local system. If you want to connect to another system, you need the IP Address or Hostname, and username. The command is 'virsh -c qemu+ssh://user@IP-Address/system'. As you can see, the SSH protocol is being used. The remote system to which you are connecting needs to have SSH installed, enabled and started. To test things beforehand, try to SSH into the system as ROOT or whichever user you may be using. As long as you can make an SSH connection and you have KVM installed and running, the connection should work.

If you have any connection issues, check and make sure you performed every step. If the CLI connection will not work, the GUI connection will not work either.

To open the GUI, it should be called 'Virtual Machine Manager'. Once started, there should be a line for 'QEMU/KVM' for the local machine, if you have installed and enabled KVM. Otherwise, go to the menu and select file, then 'Add Connection' from the drop-down menu. You can see the resulting window that appears in Figure 3.

Figure 3.jpg

FIGURE 3

Check the box for 'Connect to remote host'. Set the Username if it is not 'root' and type in the 'Hostname' for the remote system. You should see the generated Uniform Resource Identifier (URI) as we discussed previously for the command-line. When done entering information and everything is correct, click on 'Connect'. You can then make the connection.

Conclusion

This is the initial setup of KVM, but we will get to adding Virtual Machines to the system.

Be very sure to follow the steps carefully. It doesn't take much, so the connection won't work. Any minor mistake and the connections will fail every time.
 
Last edited:


For RPM based systems, you can install this like ...

dnf install -y qemu ipxe-roms-qemu libvirt-client-qemu libvirt-daemon-driver-qemu libvirt-daemon-qemu qemu-audio-alsa qemu-audio-pipewire qemu-audio-spice qemu-block-ssh qemu-device-display-qxl qemu-device-display-virtio-gpu qemu-device-usb-host qemu-device-usb-redirect qemu-img qemu-kvm qemu-sanity-check qemu-system-x86 qemu-tools qemu-ui-curses qemu-ui-gtk qemu-user qemu-user-binfmt qemu-user-static-x86 virt-manager

You may not need all of those packages, but I use the audio, usb, video, etc... the virt-manager is handy if you like a GUI.
 
For RPM based systems, you can install this like ...
Or just use "groupinstall"
Code:
dnf group install "Virtualization Host"
 

Staff online

Members online


Latest posts

Top