QEMU/KVM on Ubuntu and sharing files between host and guests

How to install QEMU and KVM on latest Ubuntu systems and how to share files between host and guests under QEMU. QEMU is a processor emulator using dynamic translation to achieve fast emulation speed and it can optionally use an in-kernel accelerator, like KVM.

VirtualBox and VMWare. No way, don’t use anything other than QEMU

QEMU creates a virtual machine, similar to VMWare, Virtualbox, KVM, and Xen. It allows you to run one operating system from within another operating system and the machine memory resources will be divided between the native OS (the HOST) and the virtual machine (the GUEST) OS.  You can also use QEMU by command-line interface or you can create, delete, run, stop, and manage your virtual machines graphically. The tool virt-manager (or Boxes) allows you to use a graphical interface to interact with QEMU and KVM.

The virt-manager application is a desktop user interface for managing virtual machines through libvirt. It primarily targets KVM VMs but also manages Xen and LXC (Linux containers). It presents a summary view of running domains, their live performance, and resource utilization statistics. Wizards enable the creation of new domains, configuration, and adjustment of a domain’s resource allocation and virtual hardware.

First, to install QEMU and KVM (and virt-manager too) on the latest Ubuntu versions (both LTS or not) you need to perform this command on a terminal emulator:

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

Remember, QEMU emulates everything, including the CPU, so it is slower than the host system. Hardware acceleration will not be as effective for a software OS emulator. Nevertheless, it is a pretty good way of testing a distribution, without having to repartition your drive or set up new hardware. Anyway, with the latest Intel Core CPUs and AMD Ryzen CPUs, the performance is really incredible and you will not find issues using several different virtual machines, but don’t forget to have enough RAM onboard.

Virt-Manager. Settings for the shared folder
Virt-Manager. Settings for the shared folder

After you have a virtual machine up and running, maybe you want to share some files between the HOST and the GUEST operating system. The clipboard works out of the box, but file-sharing not and I have found a lot of trouble getting file sharing between HOST and GUEST to work properly. So, let’s see what to do to make it working (great) again.

Create a folder in your HOST machine that you would like to share between HOST and GUEST. So in a terminal:
$ mkdir /home/your-username/vmshare

To make the file sharing from the GUEST working, we need to change permission on this folder:
$ sudo chown libvirt-qemu /home/your-username/vmshare

In virt-manager, click on the ‘i’ icon to show virtual machine details and click ‘Add Hardware’, then on the newly opened window click to ‘Filesystem’. Fill the information like these:
Type: mount
Driver: default
Mode: Squash
Source path: /home/your-username/vmshare
Target path: /vmshare

Now, you can start your Virtual Machine. Open a terminal window in the VM (GUEST) and create a folder like the one in the HOST machine.
$ mkdir /home/guest-username/vmshare

Then mount the filesystem onto that folder:
$ sudo mount -t 9p -o trans=virtio,version=9p2000.L /vmshare /home/guest-username/vmshare

If you want to automount the folder at every login, you can use the .rc-local.
$ sudo vi /etc/rc.local

Put this in the editor:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

mount -t 9p -o trans=virtio,version=9p2000.L /vmshare /home/guest-username/vmshare

exit 0

Finally, you can move and copy files through the 2 machines, the real one, and the virtual one with your simple /vmshare folder.

Note: if you have a Windows guest, you can share a folder in the local network with your Linux virt system via SAMBA and access it over the Windows filesystem directly. More info at What is Samba?.

Useful links

QEMU User Documentation – https://qemu.weilnetz.de/doc/qemu-doc.html

KVM (for Kernel-based Virtual Machine) – https://www.linux-kvm.org/page/Main_Page

QEMU Mounting the shared path – https://wiki.qemu.org/Documentation/9psetup#Mounting_the_shared_path

Getting started with QEMU – https://drewdevault.com/2018/09/10/Getting-started-with-qemu.html

Manage virtual machines with virt-manager – https://virt-manager.org/

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *