= KVM Installation = KVM, (Kernel-based Virtual Machine) is a free and opensource virtualization platform for the Linux kernel. When installed on a Linux system, it becomes a Type-2 hypervisor. Here we look at how to install KVM on Ubuntu 20.04 LTS. == Preparing Host Machine for KVM == For this lab we will be using a Ubuntu 20.04 Desktop VM running on VirtualBox. To make lab easy we have uploaded a pre-installed disk file and you can download it here. Please download it and import to your VirtualBox. Once imported run it you may need to change VM hardware configurations like CPU and Memory to suit your computer. Select the VM and go to settings window. Change CPU and Memory to suit your availble resources. Go to Network tab make the Attached to: value to 'Bridged Adapter' and select the connection you are connect from Name: drop down list. To enable KVM virtualization you need to enable virtualization for your processor. Since we are using either Intel or AMD based processor we need to enable VT-x/AMD-V on the host machine. But our Ubuntu Desktop is not on a bare-metal and it is also a VM. Then we need to enable Nested virtualization for the Ubuntu Desktop VM. To enable this go to System => Processor and select the check box 'Enable Nested VT-x/AMD-v'. If it is greyed out then open the terminal on your computer and enter below. {{{ VBoxManage modifyvm --nested-hw-virt on }}} Now view the Processor tab and you should see that the above option is enabled. Now we are good to go ahead and run the VM. Click the start button. You can login to the system using below passwords. user:password Once login open the terminal application to do the rest of the installations. == Network Configuration == First we have do the network configuration. Right now in your network settings DHCP is automatically enabled and you should have a IP address received. To check the IP address enter below in the terminal. {{{ ip addr show }}} Now we are going to disable DHCP in our network configurations and enable static IP configuration. We will also put same IP received from DHCP to the static IP configuration to avoid any IP conflict among us. Also notice the interface name. For the VirtualBox VM it will be mostly enp0s3. If we need the VMs to receive the same IP range as in the Host server, then we need to create a network bridge interface. Network bridge 'virbr0' is already created with NAT enabled. Now need to configure it in bridge mode. Please open the terminal application Change the configuration as in the below, {{{ sudo nano /etc/netplan/01-network-manager-all.yaml }}} {{{ network: ethernets: enp0s3: dhcp4: no eno2: dhcp4: true eno3: dhcp4: true eno4: dhcp4: true version: 2 bridges: virbr0: interfaces: [enp0s3] addresses: - /23 gateway4: 10.1.1.254 nameservers: addresses: [192.248.1.161,1.1.1.1] }}} Once done execute below to test the connection. Before the timeout you can check the connectivity by pinging to the IP from the host computer. if ping is working then press Enter to accept the changes, {{{ sudo netplan try }}} Once we are done with the network configuration we will go to installation of the KVM. == Installation of KVM packages == Before installing KVM on Ubuntu, we are first going to verify if the hardware supports KVM. A minimum requirement for installing KVM is the availability of CPU virtualization extensions such as AMD-V and Intel-VT. To check whether the Ubuntu system supports virtualization, run the following command. {{{ egrep -c '(vmx|svm)' /proc/cpuinfo }}} An outcome greater than 0 implies that virtualization is supported To check if your system supports KVM virtualization execute the command {{{ sudo apt install cpu-checker sudo kvm-ok }}} With the confirmation that our system can support KVM virtualization, we are going to install KVM, To install KVM, virt-manager, bridge-utils and other dependencies, run the command {{{ apt install -y qemu qemu-kvm libvirt-daemon libvirt-clients virt-manager bridge-utils }}} Before proceeding further, we need to confirm that the virtualization daemon – libvirtd-daemon – is running. To do so, execute the command {{{ sudo systemctl status libvirtd systemctl enable --now libvirtd }}} Check if KVM modules are loaded, {{{ lsmod | grep -i kvm }}} To view available Guest os variants, {{{ apt install libosinfo-bin osinfo-query os }}} == Creating a Virtual Machine == The virt-install command-line tool is used for creating virtual machines on the terminal. A number of parameters are required when creating a virtual machine. {{{ sudo virt-install --name perfsonar --os-variant centos7.0 --vcpus 4 --ram 16384 --cdrom /home/lg/downloads/pS-Toolkit-4.4.4-CentOS7-FullInstall-x86_64-2022Apr04.iso --network bridge=virbr0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole --disk size=100 }}} To list the created VM, {{{ virsh list --all }}} [[Image(https://ws.learn.ac.lk/raw-attachment/wiki/Csle2022/Agenda/virtualization/kvm1.png)]] Now the VM is created with a installation image mounted and ready for the installation. To continue the installation we will use a VNC client to connect to the graphical console of the VM. To connect with the VM we need to get the TCP port number the VM is listening on. To get the port number we will execute this first. {{{ virsh vncdisplay perfsonar }}} [[Image(https://ws.learn.ac.lk/raw-attachment/wiki/Csle2022/Agenda/virtualization/kvm2.png)]] The given is the VNC port number which is not exactly same as TCP port number. But we can derive the TCP port number using VNC port number because they have correlation. If the VNC port is :0 then TCP port will be 5900 and if the VNC port is :1 TCP port will be 5901 and so on. Using this relationship we can get the TCP port number and connect the graphical console of the VM. Then we can continue the installation as we normally do in a computer with monitor. == Virtual Machine management == To manage the Virtual machines we can use below commands. Shutdown/start VM, {{{ virsh shutdown/start vmname }}} Ungraceful Shutdown VM, {{{ virsh destroy vmname }}} Delete VM, {{{ virsh undefine vmname }}} Delete Disk, {{{ virsh vol-delete --vol /var/lib/libvirt/images/sp-vm.qcow2 }}}