= 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. First we have do the network configuration. 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. Change the configuration as in the below, {{{ network: ethernets: eno1: dhcp4: no eno2: dhcp4: true eno3: dhcp4: true eno4: dhcp4: true version: 2 bridges: virbr0: interfaces: [eno1] addresses: - 192.168.0.10/24 gateway4: 192.168.0.254 nameservers: addresses: [192.248.1.161,1.1.1.1] }}} Once done execute below to test the connection and if OK Enter to accept the changes, {{{ sudo netplan try }}} and then check the connectivity. 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 – libvritd-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 }}}