Setting up KVM shared directory in ubuntu 14.04

In KVM virtualization you can share a filesystem directory from the host to the guest via 9p-virtio.

Since it’s not trivial to get working I’m going to share the few steps that you need to take.

  1. In virt-manager open the virtual machine that you want to access a directory.
    step1 VM
  2. Click “Add hardware”
    1. Choose Filesystem, mode=Squash
      Type the source path, the browse button will not get you anywhere you want to go.
      Put some name in the Target path – it’s used to identify the share on the guest machine.
      step2 add hardware
  3. If your guest machine is running while you are adding it you will have to shut down the guest before it works. (not restart! – the VM process has to be closed)
  4. You will likely hit the Read-only permissions problem (look at trouble shooting below)
  5. In your guest: test that it works (as root):
     mount -t 9p -o trans=virtio,version=9p2000.L /commonshare /mnt/test 
    1. In the unlikely event that you have character set problems, look in trouble shooting below.
    2. unmount it again after verifying:
      umount /mnt/test
  6. Making it permanent in /etc/fstab
     /commonshare /mnt/commonshare 9p trans=virtio,version=9p2000.L,access=any 0 0 

I have one problem (for my setup) that I’ve not solved yet: mounting the files with a given users file permissions UID/GID (hopefully to be solved soon, I’ve asked on the 9p-users list) such that my guest webserver can get write permissions without changing the permissions on the host. If you have a solution, please post it in the comments.

Trouble shooting:

  • Permisions: I had read-only access at first, make sure that the user running the qemu process (KVM, look in htop, “libvirt-qemu” i my case) has RW access to the files shared – e.g. by adding the user to the group who owns the files (/etc/group). Then shut down your VM and start it again (not restart – a restart will not give the qemu process the new group membership ).
  • Character set: I had a problem with non-ascii characters looked wrong, e.g. “bøger” looked like “b??ger”. That was because my Guest machine was not properly configured  with the locale (which I had noticed but ignored during dist-upgrade “Can’t set locale; make sure $LC_* and $LANG are correct!”). I solved it by running  “dpkg-reconfigure locales” and selecting the right locale – don’t remember exactly what I selected.
  • Ubuntu 13.10 or below: you will likely hit AppArmor problems: https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/943680
  • Which users should have access on guest VM: see slide 26 on http://www.slideshare.net/ericvh/9p-overview

Sources:

For references my setup consists of:

  • host: ubuntu 14.04 with KVM and libvirt
  • guest: debian 7.8
  • I control it with virt-manager which I run from my kubuntu desktop.