How to install KVM/QEMU in Ubuntu/Linux mint

Prerequisites

 
To run guests with more than 2 GB of RAM, you must have a 64-bit host system.
Before continuing with the installation, make sure your Ubuntu host machine supports KVM virtualization. The system should have either an Intel processor with the VT-x (vmx), or an AMD processor with the AMD-V (svm) technology support.

Run the following grep command to verify that your processor supports hardware virtualization:

    grep -Eoc '(vmx|svm)' /proc/cpuinfo

If the CPU supports hardware virtualization, the command will output a number greater than zero, which is the number of the CPU cores. Otherwise, if the output is 0 it means that the CPU doesn’t support hardware virtualization.
On some machines, the virtual technology extensions may be disabled in the BIOS by the manufacturers.
To check if VT is enabled in the BIOS, use the kvm-ok tool, which is included in the package. Enter the following commands as root or user with sudo privileges to install the cpu-checker package that includes the kvm-ok command:

    sudo apt update
    sudo apt install cpu-checker

Once installed, check if your system can run hardware-accelerated KVM virtual machines:

    kvm-ok

If the processor virtualization capability is not disabled in the BIOS the output will look something like this:

    INFO: /dev/kvm exists
    KVM acceleration cant be used

Otherwise, the command will print and a failure message and optionally a short message on how to enable the extension. The process of enabling the AMD-V or VT technology depends on your motherboard and processor type. Consult your motherboard documentation for the information about how to configure your system BIOS.

Installing KVM on Ubuntu 20.04
Run the following command to install KVM and additional virtualization management packages:

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


To be able to create and manage virtual machines, you’ll need to add your user to the “libvirt” and “kvm” groups. To do that, enter:
 

    sudo usermod -aG libvirt $USER
    sudo usermod -aG kvm $USER


    reboot
 

Create the Network bridge

    sudo apt install bridge-utils

Once the packages are installed, the libvirt daemon will start automatically. You can verify it by typing:

    sudo systemctl is-active libvirtd


Networking

    virsh list --all

For performance reasons, it is recommended to disable netfilter on bridges in the host. To do that, create a file called /etc/sysctl.d/bridge.conf and fill it in with this:

    net.bridge.bridge-nf-call-ip6tables=0
    net.bridge.bridge-nf-call-iptables=0
    net.bridge.bridge-nf-call-arptables=0

Then create a file called /etc/udev/rules.d/99-bridge.rules and add this line:

    ACTION=="add", SUBSYSTEM=="module", KERNEL=="br_netfilter",                RUN+="/sbin/sysctl -p /etc/sysctl.d/bridge.conf"

 Creating A bridge with netplan

# Let NetworkManager manage all devices on this system
network:
 version: 2
 renderer: NetworkManager

 ethernets:
   eth0:
     dhcp4: false
     dhcp6: false
     #addresses: [192.168.1.239/24]
     #gateway4: 192.168.1.1
     #mtu: 1500
     #nameservers:
     #  addresses: [8.8.8.8]

 ethernets:
   eth1:
     dhcp4: false
     dhcp6: false
     #addresses: [192.168.1.239/24]
     #gateway4: 192.168.1.1
     #mtu: 1500
     #nameservers:
     #  addresses: [8.8.8.8]

 bridges:
   br0:
     interfaces: [eth0, eth1]
     addresses: [192.168.151.2/24]
     gateway4: 192.168.151.1
     mtu: 1500
     nameservers:
       addresses: [192.168.151.1]
     parameters:
       stp: true
       forward-delay: 4
     dhcp4: no


That will set the flags to disable netfilter on bridges at the proper place in system start-up. Reboot to take effect.


Next, we need to disable the default networking that KVM installed for itself. You can use ip to see what the default network looks like

ip link

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether d4:be:d9:f3:1e:5f brd ff:ff:ff:ff:ff:ff
6: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:1d:5b:25 brd ff:ff:ff:ff:ff:ff
7: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:1d:5b:25 brd ff:ff:ff:ff:ff:ff

The entries virbr0 and virbr0-nic are what KVM installs by default.

Here’s how to remove the default KVM network:

    virsh net-destroy default
    virsh net-undefine default

Now you can run ip link again and the virbr0 and virbr0-nic should be gone


If they are not, you can remove them with

    ip link delete virbr0 type brigde

and

    ip link delete virbr0-nic


Declaring the KVM Bridged Network

Before the bridge can be used by a virtual machine it must be declared and added to the KVM network configuration

Begin by creating a definition file for the bridge network named host-bridge.xml that reads as follows:

<network>
  <name>host-bridge</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>

Next, use the file to define the new network:

     virsh net-define host-bridge.xml
     virsh net-start host-bridge
     virsh net-autostart host-bridge

And then list the networks to confirm it is set to autostart:

    virsh net-list --all

Once again list the networks to verify that the bridge network is now accessible within the KVM environment:


Fix "Cannot access storage file, Permission denied Error" in KVM Libvirt

Method 1:
Step 1: Edit /etc/libvirt/qemu.conf file:

    sudo nano /etc/libvirt/qemu.conf

Step 2: Find the user and group directives. By default, both are set to "root".

 [...]
 Some examples of valid values are:
 #
 user = "qemu"   # A user named "qemu"
 user = "+0"     # Super user (uid=0)
 user = "100"    # A user named "100" or a user with uid=100
 #
 #user = "root"
 The group for QEMU processes run by the system instance. It can be
 specified in a similar way to user.
 #group = "root"
 [...]

Uncomment both lines and replace root with your username and group with libvirt as shown below:

 [...]
 Some examples of valid values are:
 #
 user = "qemu"   # A user named "qemu"
 user = "+0"     # Super user (uid=0)
 user = "100"    # A user named "100" or a user with uid=100
 #
 user = "username"
 The group for QEMU processes run by the system instance. It can be
 specified in a similar way to user.
 group = "libvirt"
 [...]


Press CTRL+O and press ENTER to save the changes and press CTRL+X to exit the file.

Step 3: Restart libvirtd service:

    sudo systemctl restart libvirtd

 

Converting VDI images(VirtualBox images) to qcow2


sudo qemu-img convert -f vdi -O qcow2 /sorce.vdi /destination.qcow2

Convirting qcow2 to vdi

qemu-img convert -f qcow2 qcow2_file_name -O vdi vdi_file_name

Comments