Folder sharing between host and KVM guest systems
By Fenguoerbian in KVM
September 25, 2020
Introduction
This note is about how to set up file share between host and guest virtual machines. There are various methods
- fileshare through network protocol, e.g. smb.
- fileshare through SPICE-webdav.
- files system passthrough.
- use a usb drive and USB redirection between host and guest systems.
File share via network protocol
SAMBA host on the host Linux machine
I will use my Linux Ubuntu 20.04 host machine as an example. There are many guides online such as this one and this one.
-
Install
samba
sudo apt install samba
You can use
systemctl status smbd.service
to check the samba status. -
Backup the default sambda configuration file and parse a clean one from it. This script comes from this post
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_backup sudo bash -c 'grep -v -E "^#|^;" /etc/samba/smb.conf_backup | grep . > /etc/samba/smb.conf'
-
Setup the folder on the host machine for sharing. In my machine, I’m using the
/home/lovby/smb_share/
folder. Therefore I need to add these settings in thesmb.conf
file[Public] comment = Public shared folder path = /home/lovby/smb_share/ read only = no # allow guest user guest ok = yes directory mask = 0777 creat mask = 0777 writable = yes acl allow execute always = yes
This folder will have name
Public
on the samba sharing. And the setting will allow any guest user to have full access in the folder. This might be DANDEROUS especially when there are other machines which can reach out to your host machine. Hence you should also consider set up account restriction for samba, or just stop the sharing service if you are not using it. You can check more details about these settings byman smb.conf
-
When the configuration is done, run the following commands
# testparm will check your smb.conf file for possible mistakes testparm # if there are no mistakes, then restart the smb service in order to reload the conf file. sudo systemctl restart smbd.service
-
(Optional) If you are not comforable with setting up shared folders with command line tools used in previous 2 steps, you can use
nautilus
’s GUI to set up sharing folder. Just right click on the folder you want to share, and chooselocal network share -> share this folder
, then set theshare name
and the corresponding rights for read, write, guest access, etc. Also grant requirements for modification your folder if being asked bynautilus
. -
Connect from your Windows VM. This might be slightly different for various Windows versions.
-
Go to
Windows file explorer -> (Network) -> Map network drive
-
Select driver letter and type the samba share location. In this case, it’s
\\host-ip-address\Public
where
host-ip-address
is your host machine’s IP address if you’re using bridged network for this VM (which means host and guest systems are in the same subnet hence their IPs should look likexxx.yyy.zzz.aaa
andxxx.yyy.zzz.bbb
). But if you’re using NAT network configuration, then fill in thenetwork gateway
address obtained from you guest VM. AndPublic
is the sharing’s name I set up before in the.conf
file. -
You can check the
Connect using different credentials
if you want to manually provide user information.
-
SAMBA host on the guest Windows machine
Windows 10 comes with a built-in sambda service which can be used for folder sharing. Additional settings can be found at this post.
-
Navigate to the folder you want to share, right click on it and select
propoerties
. -
Select
sharing -> advanced sharing
-
Check
share this folder
and adjust userpremissions
such as read, write, user accessiblity, etc. -
Click
apply
andOK
. -
At your host machine, you can open
nautilus -> other locations -> connect to server
-
Inputh the smb server location
smb://guest-ip-address/share-folder-name
where
guest-ip-address
is the ip address of your guest system no matter you’re using bridged or NAT network configuration. When prompt, use the user information saved on the windows guest system to log into the shared folder.
SPICE folder sharing
This is not recommend for transferring large files. Information about this setting could be find on the internet from like
https://www.spice-space.org/spice-user-manual.html#_folder_sharing
https://github.com/lofyer/spice-webdav
https://www.guyrutenberg.com/2018/10/25/sharing-a-folder-a-windows-guest-under-virt-manager/
-
On the host machine, set up Spice webdav port using either one of these methods:
-
Using virt-manager
In the hardware details, click on “Add Hardware”, then select “Channel”. Add aSpice port
device type with theorg.spice-space.webdav.0
name. -
Using libvirt
In order to set up folder sharing, qemu needs to expose a org.spice-space.webdav.0 virtio port, associated with a corresponding Spice port:<devices> <channel type='spiceport'> <source channel='org.spice-space.webdav.0'/> <target type='virtio' name='org.spice-space.webdav.0'/> </channel> </devices>
-
Using QEMU
In order to set up folder sharing, qemu needs to expose a org.spice-space.webdav.0 virtio port, associated with a corresponding Spice port:-device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel1,id=channel1,name=org.spice-space.webdav.0 -chardev spiceport,name=org.spice-space.webdav.0,id=charchannel1
-
-
Windows guest configuration:
- Disable windows firewall.
- Downlaod the
spice-webdav-windows-service
and install it as
Administrator
. - Make sure the
spice-webdav
service is running fromtask manager
’sservice
tab. - Make sure the
spice-webdav
service is running fromservices.msc
.
-
When the guest system is running, connect to it using
virt-viewer
(NOTvirt-manager
) asAdmnistrator
( you may need tosu
orsudo
). Then at the openedremote-viewer
chooseFile -> Preferences
menu to enable spice folder share. The default shared directory is the XDG Public Share directory (ie ~/Public if you use a regular system). -
If the shared folder is not showed up in windows guest system’s
This PC
, you can manually runC:\Program File\SPICE webdavd\map-drive.bat
as
Admnistrator
.
Filesystem passthrough
You can find instructions at
http://www.linux-kvm.org/page/9p_virtio
It relys on the 9p-virtio
driver and it’s not friendly with Windows.
USB redirection
this can be found in [another guide about device passthroug]( https://fenguoerbian.github.io/blog/device-passthrough-in-kvm/).
- Posted on:
- September 25, 2020
- Length:
- 5 minute read, 915 words
- Categories:
- KVM
- Tags:
- KVM file share samba NAT bridge spice passthrough