Installing KVM/QEMU/virt-manager on Manjaro Linux(Arch Linux)

Installing KVM/QEMU/virt-manager on Manjaro Linux(Arch Linux)

08 Sep 2020, 10:01am TZ +05:30
linux, manjaro, VM
tips, Software, tools

KVM is an inherently better way to work with Virtual Machines. It sands for Kernel Virtual Machine.

Books on libvirt QEMU and Virt Manager

After many tries, I finally managed to get it right.

All Thanks to this video : https://www.youtube.com/watch?v=itZf5FpDcV0

UPDATED: 12th Oct 2020 - Bridge Network fix and added more details on remote viewing.

Let’s Look at what steps I followed.

1. QEMU Installation #

sudo pacman -S qemu qemu-arch-extra ovmf bridge-utils dnsmasq vde2 \
 openbsd-netcat ebtables iptables

Package insights:

  • ovmf helps to do the UEFI Bios and Secure Boot setups.
  • bridge-utils for network bridge needed for VMs
  • vde2 for QEMU distributed ethernet emulation
  • dnsmasq the DNS forwarder and DHCP server
  • openbsd-netcat network testing tool (Optional)
  • ebtables and iptables to create packet routing and firewalls

2. Virt-Manager and libvirtd Service Install #

Virt-manager is a UI that helps to create and organize the VM’s. And virt-viewer is used to open remote window into the VM instance.

sudo pacman -S virt-manager virt-viewer

3. Start the Services #

To Autostart the services at boot:

# Enable Auto-Start of the Service
sudo systemctl enable libvirtd.service
# Start the Service Right now
sudo systemctl start libvirtd.service

In case you wish to see if the libvirtd.service has actually started or not:

sudo systemctl status libvirtd.service

Lean Approach

Note: In order to reduce the system Load you can skip the Autostart part. And do it every time you wish to use the service.

Just run, whenever you wish to work with KVM:

# Start the Service Right now
sudo systemctl start libvirtd.service

4. Configure the KVM #

Open the /etc/libvirt/libvirtd.conf for editing

sudo nano -cl /etc/libvirt/libvirtd.conf
The -cl in the nano would help you see line numbers and how many lines are actually remaining.

Here are the Lines to Edit:

  1. Uncomment the line 81 or so:
    unix_sock_group = "libvirt"
    
  2. Uncomment the line 98 or so:
    unix_sock_rw_perms = "0770"
    

Make sure to save the file before you exit.

5. Creating an New Network Bridge for VM. #

A new network bridge is needed allow a separate IP subnet for Guest OS’s in VM.

5.1 Create the Bridge description file #

Create a new file like br10.xml:

nano -cl /tmp/br10.xml

And type the content in br10.xml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<network>
  <name>br10</name>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='br10' stp='on' delay='0'/>
  <ip address='192.168.72.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.72.50' end='192.168.72.200'/>
    </dhcp>
  </ip>
</network>

This would help to register a new bridge network for the Guest OS’s. Here we are changing the default IP Address 192.168.72.X and also allowing the specific ports 1024-65535 to be forwarded over NAT. This helps to easily communicate with the Guest OS in the VM’s.

5.2. Register the Bridge Network: #

sudo virsh net-define /tmp/br10.xml

Now we have the network registered in the libvirtd.

5.3. Start the Bridge #

To Start this network bridge:

sudo virsh net-start br10

If you want to permanently enable the new network bridge, so that it Autostart at boot:

sudo virsh net-autostart br10

Lean Approach

Note: Permanently enabling this network would take resources. Hence the start command must be given every time before starting virt-manager.

sudo virsh net-start br10

6. Permissions for Current User #

In-order to be able to use the virt-manager as normal user, we need to add the user to the libvirt group.

sudo usermod -a -G libvirt $(whoami)
Reboot the PC or Computer after changing the options.

7. Reboot #

Yes, for all this to take effect we need to reboot the computer.


More Notes #

Here are few insights that I found while using the virt-manager.

Windows - Guest - Copy & Paste Clipboard #

In order to make sure that the copy & paste works between the host and guest, we need to install Windows guest tools = spice-guest-tools .

Note This only works if your Display is set to spice Server.

Remote Connection to Display of VM #

Typically the VM’s in KVM are destined to run like head-less unit. Hence the remote viewer = virt-viewer is needed.

In order to connect to VM use the address

spice://127.0.0.1:5900

Thats the default port on which the virt-manager hosts the VM display output.

Multiple Monitors for a VM #

This is possible by adding few instances of Video QXL.

This can be done in the configuration mode of the VM’s (a.k.a i or show hardware details after opening the VM).

Adding more USB devices to a VM #

Some times we need to mount more than 1 USB device to our Guest OS. This can be easily achieved by adding more USB Redirector to the VM.

This can be done in the configuration mode of the VM’s (a.k.a i or show hardware details after opening the VM).

CPU Pinning Video - Easy #

https://www.youtube.com/watch?v=Pb2upx53fUM

Use this command to check the Topology and CPU numbers.

lstopo

CPU Pinning Detail #

When thinking about CPUs, there are three concepts - socket, cores, and threads. 1

A socket (NUMA node) is a physical socket where the physical CPU capsules are placed. A normal PC only has one socket.

Cores are the number of CPU-cores per CPU. A modern standard CPU for a standard PC usually has two to four cores. Some CPUs can run more than one parallel thread per CPU-core. Intel has either one or two threads per core depending on the CPU model.

When lscpu says CPU(s), it means Socket(s) * Core(s) per socket * Thread(s) per core. virt-manager’s Logical host CPUs also means the same thing.

The default behavior of KVM guests is to run operations coming from the guest as a number of threads representing virtual processors. 2 Those threads are managed by the Linux scheduler.

CPU pinning limits which physical CPU cores the virtual CPUs are allowed to run on.

The ideal setup is one-to-one mapping such that virtual CPU cores match physical CPU cores while taking hyperthreading/SMT into account.

Hyperthreading/SMT is simply a very efficient way of running two threads on one CPU core at any given time.

The topology of the processor can be found out using lscpu -e. Column CORE shows the association of the physical/logical CPU cores.

In case of ThinkPad we have the following.

1
2
3
4
5
6
[demo@pc1:~]$ lscpu -e
CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE    MAXMHZ   MINMHZ
  0    0      0    0 0:0:0:0          yes 2800.0000 400.0000
  1    0      0    1 1:1:1:0          yes 2800.0000 400.0000
  2    0      0    0 0:0:0:0          yes 2800.0000 400.0000
  3    0      0    1 1:1:1:0          yes 2800.0000 400.0000

So CORE 0 has CPU 0, 2 and CORE 1 has CPU 1, 3.

So, if we want to allocate CORE 1 to the VM, we would have to do virsh edit vmname and do the following.

1
2
3
4
5
  <vcpu placement="static">2</vcpu>
  <cputune>
    <vcpupin vcpu='0' cpuset='1' />
    <vcpupin vcpu='1' cpuset='3' />
  </cputune>

Wish you All the Best #

Hope this would helpful to get you stared on KVM using QEMU and virt-manager as the front-end.

As always, I look forward to your suggestion and comments. DM me on Mastodon .

References #


  1. https://stackoverflow.com/a/40163228 ↩︎

  2. https://wiki.archlinux.org/index.php/PCI_passthrough_via_OVMF#CPU_pinning ↩︎