Notes to self

How to set up shared folders in virt-manager

Do you need to set up a shared folder between your host OS and guests running in virt-manager? I will show you how to do it on your Fedora system which involves a SELinux policy change.

First of all, if you don’t have it already, install virt-manager and create a virtualized guest which should be pretty straightforward. Afterwards create a future share folder on your host and set up the permissions (for the purpose of this article I will grand all permissions):

root@host# mkdir /share
root@host# chmod 777 /share

Afterwards shut down the guest if it’s running and attach the new filesystem in virt-manager:

  1. Switch the view to detail hardware view: View > Details
  2. Go to Attach hardware > Filesystem
  3. Fill in the name of the source path (/share in our case) and virtual target path (anything you like, I will go with /sharepoint)
  4. Switch mode to Mapped if you need to have write access from the guest
  5. Confirm and start the VM again

virt_manager_add_shared_folder

Now you can mount your shared folder from the VM:

root@guest# mkdir /share
root@guest# mount -t 9p -o trans=virtio /sharepoint /share

Or permanently add it to /etc/fstab file:

root@guest# cat /etc/fstab
...
/sharepoint   /share    9p  trans=virtio,version=9p2000.L,rw    0   0

If you don’t have SELinux enabled everything should work now. If you do (which I recommend), you will need to add a policy for files under your /share folder on your host. SELinux won’t allow you to share this folder until it’s labeled svirt_image_t. Here is how to add this policy on your host using semanage:

root@host# semanage fcontext -a -t svirt_image_t "/share(/.*)?"
root@host# restorecon -vR /share

That should be it. If not, check the logs :).

Check out my book
Deployment from Scratch is unique Linux book about web application deployment. Learn how deployment works from the first principles rather than YAML files of a specific tool.
by Josef Strzibny
RSS