Home KVM Setup A Shared Folder Between KVM Host And Guest

Setup A Shared Folder Between KVM Host And Guest

By sk
Published: Last Updated on 118.9K views

After creating a new Virtual machine, you may want to share files and folders between the KVM host and the KVM virtual machine. In this brief guide, we will see how to setup a shared folder between KVM host and guest virtual machine using Virt-manager in Linux.

Before configuring a shared folder, make sure you have installed Virt-manager on your KVM host.

If you already have installed complete KVM Virtualization Package group, you don't need to install Virt-manager separately. Virt-manager is part of the KVM virtualization package group. Otherwise, you may need to install it as shown in the above link.

Create a new Virtual machine from Virt-manager interface. It is very straight-forward and easy! Don't start the VM yet. We need to create shared folder in our KVM host.

Create Shared Folders In KVM Host

Create a new shared folder in your KVM host system that you want to share to the guest machines.

For the purpose of this guide, I created a shared folder named "KVM_Share" in my $HOME directory.

[ostechnix@kvmhost ~]$ mkdir ~/KVM_Share

Next, set up required permissions for the shared folder. I wanted to edit and modify the contents of this shared folder from my guest virtual machines, so I gave full permissions to the shared folder.

[ostechnix@kvmhost ~]$ chmod 777 ~/KVM_Share

Next set the correct SELinux type for the KVM shared folder:

$ sudo semanage fcontext -a -t svirt_image_t "/home/sk/KVM_Share(/.*)?"

This command sets the default file context for anything under and including the shared folder i.e. /home/sk/KVM_Share in our case.

After you running the above command, it appends a line to the /etc/selinux/targeted/contexts/files/file_contexts.local file which makes the change persistent.

The appended line may look like this:

$ cat /etc/selinux/targeted/contexts/files/file_contexts.local
 This file is auto-generated by libsemanage
 Do not edit directly.
 /usr/lib/chromium-browser    system_u:object_r:bin_t:s0
 /usr/lib/chromium-browser/chromium-browser.sh    system_u:object_r:bin_t:s0
 /usr/lib/chrome-sandbox    system_u:object_r:chrome_sandbox_exec_t:s0
 /home/sk/KVM_Share(/.*)?    system_u:object_r:svirt_image_t:s0

Next, run the following command to tell SELinux to restore file contexts on everything underneath the shared directory to what we have as being the default (i.e what semanage changes).

$ sudo restorecon -vR /home/sk/KVM_Share

If you don't set the correct SELinux type for the KVM shared folder, you will encounter with the following error when starting the VM.

Error starting domain: internal error: qemu unexpectedly closed the monitor: 2021-05-06T09:30:21.239353Z qemu-system-x86_64: -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=/hostshare,bus=pci.5,addr=0x0: cannot initialize fsdev 'fsdev-fs0': failed to open '/home/sk/KVM_Share': Permission denied
 Traceback (most recent call last):
   File "/usr/share/virt-manager/virtManager/asyncjob.py", line 65, in cb_wrapper
     callback(asyncjob, *args, *kwargs)   File "/usr/share/virt-manager/virtManager/asyncjob.py", line 101, in tmpcb     callback(args, **kwargs)
   File "/usr/share/virt-manager/virtManager/object/libvirtobject.py", line 57, in newfn
     ret = fn(self, *args, **kwargs)
   File "/usr/share/virt-manager/virtManager/object/domain.py", line 1329, in startup
     self._backend.create()
   File "/usr/lib64/python3.9/site-packages/libvirt.py", line 1353, in create
     raise libvirtError('virDomainCreate() failed')
 libvirt.libvirtError: internal error: qemu unexpectedly closed the monitor: 2021-05-06T09:30:21.239353Z qemu-system-x86_64: -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=/hostshare,bus=pci.5,addr=0x0: cannot initialize fsdev 'fsdev-fs0': failed to open '/home/sk/KVM_Share': Permission denied

Now it is time to configure shared folder in Virt-manager.

Setup A Shared Folder Between KVM Host And Guest

Open Virt-manager and make sure the guest system is turned off. Select the guest machine and click Open button on the menu bar to show the virtual machine console and details.

Show the virtual machine console and details in Virt-manager
Show the virtual machine console and details in Virt-manager

In the next window, click "Show virtual hardware details" button on the menu bar and click "Add Hardware" button on the left pane.

Click "Add Hardware" button in Virt-manager
Click "Add Hardware" button in Virt-manager

Click "Filesystem" tab on the left pane and the enter the shared folder location in Source path and enter mount tag under the Target path. Here, I am sharing "~/KVM_Share" directory and my mount tag is /hostshare. You can specify the name of mount_tag as you wish. Please note that mount_tag is not a path. The mount_tag is just a name used to identify the host's share. Click Finish and Apply to update the changes.

Setup a shared folder between KVM host and guest
Setup a shared folder between KVM host and guest

One last thing you should do is select the proper access mode for accessing the shared folder.

Select the security mode for accessing the shared folder in Virt-manager
Select the security mode for accessing the shared folder in Virt-manager

There are three possible values. They are passthrough, mapped and squash.

  • passthrough - Specifies that the source (i.e. shared folder) is accessed with the User's permission settings that are set from inside the guest virtual machine. i.e. Files are stored using the same credentials as they are created on the guest. This is the default access mode if one is not specified.
  • mapped - Specifies that the source is accessed with the permission settings of the hypervisor. If you want to have read and write access from the guest machine, you must choose this mode.
  • squash - Similar to 'passthrough', the exception is that failure of privileged operations like chown are ignored. This makes a passthrough-like mode usable for people who run the hypervisor as non-root.

After selecting your preferred access mode, click Apply button. We have now configured the shared folder for KVM host and guest system from Virt-manager.

Now, power on the guest virtual machine and create a mount point in your guest machine to mount the shared folder of KVM host system:

[sk@kvmguest ~]$ mkdir ~/hostfiles

Finally, mount the shared folder in the guest machine using command:

[sk@kvmguest ~]$ sudo mount -t 9p -o trans=virtio /hostshare hostfiles/

The shared folder ~/KVM_Share is mounted on the hostfiles directory in your virtual machine. From now on, you can access the contents of the shared folder from your guest virtual machine. You can also edit and delete the contents of the shared folder on both KVM host and guest systems.

To automatically mount the shared folder every time at boot, add the following line to /etc/fstab file in your guest system:

/hostshare /hostfiles 9p trans=virtio,version=9p2000.L,rw 0 0

Hope this helps.

Resource:

You May Also Like

19 comments

João Costa August 12, 2021 - 3:20 pm

Thank you for the detailed write up. Note that if the guest is using a plain CentOS 8 install, the 9p kernel module is not available by default and it needs to be installed separately. You will see an error such as:

mount: /hostfiles: unknown filesystem type ‘9p’.

Alternatively, one can use something like sshfs which I personally found simpler to setup.

Reply
sk August 12, 2021 - 6:53 pm

Thanks for the heads up. I will test this guide with CentOS 8 guest soon and update it accordingly.

Reply
jelabarre October 15, 2021 - 10:47 pm

I presume this ONLY works if you’re running virt-manager as root. I just tried your settings and it gave me a “permission denied” on the ~/KVM_Share directory in my own home directory.

Reply
sk October 18, 2021 - 8:30 pm

Did you add your user to libvirt group?

$ sudo usermod -a -G libvirt $(whoami)

Reply
Andy December 19, 2021 - 5:44 am

The command semanage doesn’t seem to exist on Ubuntu. I tried sudo apt-get install -y semanage-utils but this didn’t fix it.

Reply
sk December 19, 2021 - 1:31 pm

Semanage command is available on RPM-based systems only. If you use Ubuntu, you can skip this step.

Reply
Carsten February 16, 2022 - 3:54 pm

Thanks for the helpful article, Senthil!

Two suggestions:

1. The SElinux section is irrelevant for non-RPM distros, like you said in the comments. It would be helpful to point it out directly in the article.

2. The auto-mount via /etc/fstab does not work, because kernel modules 9p, 9p_virtio are not yet loaded when trying to mount. An easy fix is to add _netdev to the line in /etc/fstab so it looks like this:
/hostshare /hostfiles 9p trans=virtio,version=9p2000.L,rw,_netdev 0 0
(https://superuser.com/a/1565275/1643686, credits to Mikael Gueck)

Best, Carsten

Reply
sk February 16, 2022 - 5:30 pm

Thank you for your inputs, Carsten. I will check and update the guide accordingly.

Reply
Hartmut February 21, 2022 - 4:34 pm

Thanks for this detailed and helpful guide, Senthil!
Does this also work if the guest machine is windows 10, and how is /hostshare mounted in the windows filesystem?
Cheers, Hartmut

Reply
sk February 21, 2022 - 9:24 pm

Hi, I don’t have a Windows guest now. I did a google search and found this link. Hope this helps. https://unix.stackexchange.com/questions/86071/use-virt-manager-to-share-files-between-linux-host-and-windows-guest

Reply
Hartmut February 23, 2022 - 3:27 pm

Thanks, your link gave the solution. I was successfull using virtiofs (https://libvirt.org/kbase/virtiofs.html) on the host side and winfsp (http://www.secfs.net/winfsp/) on the windows10 guest side. Followed this guide (https://virtio-fs.gitlab.io/howto-windows.html) and it worked without further hassle. Thanks again, I appreciate your help!

Reply
sk February 23, 2022 - 4:12 pm

You’re welcome. Happy to help.

Reply
jelabarre March 16, 2022 - 3:50 am

But “virtiofs” isn’t available in “user” mode. I’d prefer to run my VMs as non-root.

It seems libvirt is far less functional when you try to run it as a regular user. And I have my account as a member of libvirt, kvm AND qemu.

Reply
Thomas July 18, 2022 - 1:59 am

With a shared folder in my home directory, I did not have to change the labels. I run KVM as a normal user (added to the libvirt group). So guessing virt processes can access home folders? I looked at a red hat guide that also skipped the labelling step do don’t think my set up is odd

Reply
Thomas July 18, 2022 - 1:03 pm

This may be due to a change in driver for sharing the filesystem. I don’t have a choice but to use virtiofs (instead of mount in the above screenshot). When I boot up a VM I now see a “virtiofs” process which my shared folder as argument and the context “virtd_t” (the VM process has type svirt_t). Looking up the selinux manage for virtd_t it says it has access to all files on the system. So I suppose that using virtiofs sets up a server sort of process that has access to all files and so the VM can access whatever is set-up with virtiofs even though the context may be different.

Thanks for the great write up!

Reply
Peter Brandon September 15, 2022 - 12:44 am

Thanks so much for this–there is little out there that explains clearly how to set up shared folders in KVM, and most of it seems specific to Redhat. I’ve got a Debian guest and host and the instructions above, ignoring the SELinux commands, opened up a shared folder for me.

One issue, though: Files I share from my guest appear on my host as from owner libvirt-qemu (same for group). Is there some way to make that come out with ownership by my host OS persona? Is this expected? If not, a possible issue is that I’m using virt-manager to manage and start my VM, and so am not starting with a command line in which I can readily specify /hostshare .

Reply
sk September 15, 2022 - 11:09 am

Hi Peter,

I deleted the setup, so I couldn’t comment about this issue at the moment.

Reply
Peter Brandon September 16, 2022 - 8:45 pm

Still, thanks for the very helpful instructions! I think for now, I’ll create a script I can run on the shared folder that converts ownership and permissions so the outcome is more like a typical file in my home directory. As far as I can tell, I’ll have to type my sudo password every time I use this–not ideal, but workable.

Reply
Jim April 20, 2023 - 9:26 pm

Thank you. So helpful. Debian 11 host running Fedora 38 in KVM with shared folders. Yay. My Gnomeboxes failed with a known bug so virt-manager runs the vm just fine.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More