Skip to content

Instantly share code, notes, and snippets.

@kylewlacy
Last active May 6, 2021 17:44
Show Gist options
  • Save kylewlacy/db82aeb1c7142cedf26a4a10fe580d1d to your computer and use it in GitHub Desktop.
Save kylewlacy/db82aeb1c7142cedf26a4a10fe580d1d to your computer and use it in GitHub Desktop.
Attaching a directory to a libvirtd VM

Attaching a directory from a host to a libvirtd guest VM

NOTE: These instructions aren't comprehensive! This just covers the main roadblocks every time I need to re-do this process...

(For context, I'm using a Debian 10 host and a Debian 10 guest)

1. Create the directory and update the permissions so libvirtd-qemu has access:

host $ mkdir vmshared
host $ chown libvirt-qemu:libvirt-qemu vmshared
host $ chmod 0750 vmshared

2. Edit the domain's config to add a filesystem device:

host $ sudo virsh edit my-vm
<domain type='kvm'>
  <!-- ... -->
  <devices>
    <!-- ... -->
    <filesystem type='mount' accessmode='mapped'>
      <source dir='/path/to/vmshared'/>
      <target dir='vmshared'/>
      <!-- NOTE: To add multiple devices, use different bus values -->
      <address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
    </filesystem>
  </devices>
</domain>

3. Add an fstab entry in the guest for the new directory

my-vm $ sudo vim /etc/fstab
# <file system>              <mount point>  <type>  <options>
# ^^^^^^^^^^^^^
# this is from `virsh edit`
vmshared                     /mnt/vmshared  9p      trans=virtio,version=9p2000.L,rw,noauto,x-systemd.automount,x-systemd.mount-timeout=30s,x-systemd.device-timeout=30s,_netdev 0 0

4. Shutdown the VM

host $ sudo virsh shutdown my-vm

3. Restart AppArmor

host $ sudo systemctl restart apparmor

NOTE: this is needed because editing the VM's config auto-generates AppArmor config files for the directory (somewhere under /etc/apparmor.d/libvirt/libvirt-*.files)

5. Start the VM

host $ sudo virsh start my-vm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment