Shared folders in virt manager with Linux guest
I was running some Linux VMs on Fedora with virt manager recently, but ran into problems trying to mount a shared folder.
The issue came about because I did not want to use a simple network share to share data between the host and guest. Neither did I want to use spice webdav to create a share (this requires virt viewer on top of virt manager). Instead, my goal was to automatically mount my $HOME/Public
directory in the host as a regular directory, with the guest having direct access to its contents.
As it turned out, this was not an easy feat. Only after hours of trying did I manage to get it working. Part of it was due to libvirt itself, but part of it was due to Fedora and SELinux. Anyway, here’s a quick explanation of how to get a shared folder working on Fedora:
Procedure
I will be using /home/user/Public
as the target shared folder on the host. Change it to the directory you want to share.
Creating a shared folder in virt manager
- Click on the device information button in the toolbar
- At the bottom left, click
Add Device
- Select the
Filesystem
device type - Enter the following options:
- Type:
mount
- Mode:
mapped
- Source:
/home/user/Public
- Target:
hostshare
- Note: This is NOT the path on the guest where the folder will be mounted. It is just a name given to the share.
- Type:
Setting SELinux permissions for the shared folder
This is only necessary if your host system has SELinux enabled (e.g. Fedora).
sudo semanage fcontext -a -t svirt_image_t /home/user/Public
sudo restorecon -vR /home/user/Public
Note: You MUST specify the full path to the folder when executing the semanage
command. Do not use a relative path.
Mounting the shared folder in the guest
Now boot into your guest system and execute the following command to mount the shared folder at a target location:
mount -t 9p -o trans=virtio hostshare /path/to/target/on/guest
Automatic mount
To make the share mount automatically, add the following line to /etc/fstab
:
# /etc/fstab
host /path/to/target/on/guest 9p trans=virtio,rw,_netdev 0 0
The _netdev
option tells the system to treat it as a network device so it doesn’t mount too early in the boot sequence. Mounting too early would result in an error and your system not booting. If that happens you should revert your /etc/fstab
file by booting from a live ISO.