Folder sharing between host and KVM guest systems

Introduction

This note is about how to set up file share between host and guest virtual machines. There are various methods

  1. fileshare through network protocol, e.g. smb.
  2. fileshare through SPICE-webdav.
  3. files system passthrough.
  4. 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.

  1. Install samba

    sudo apt install samba       

    You can use systemctl status smbd.service to check the samba status.

  2. 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'
  3. 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 the smb.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 by

      man smb.conf
  4. 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
  5. (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 choose local network share -> share this folder, then set the share name and the corresponding rights for read, write, guest access, etc. Also grant requirements for modification your folder if being asked by nautilus.

  6. Connect from your Windows VM. This might be slightly different for various Windows versions.

    1. Go to Windows file explorer -> (Network) -> Map network drive

    2. 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 like xxx.yyy.zzz.aaa and xxx.yyy.zzz.bbb). But if you’re using NAT network configuration, then fill in the network gateway address obtained from you guest VM. And Public is the sharing’s name I set up before in the .conf file.

    3. 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.

  1. Navigate to the folder you want to share, right click on it and select propoerties.

  2. Select sharing -> advanced sharing

  3. Check share this folder and adjust user premissions such as read, write, user accessiblity, etc.

  4. Click apply and OK.

  5. At your host machine, you can open nautilus -> other locations -> connect to server

  6. 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/
  1. 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 a Spice port device type with the org.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
  2. Windows guest configuration:

    1. Disable windows firewall.
    2. Downlaod the spice-webdav-windows-service and install it as Administrator.
    3. Make sure the spice-webdav service is running from task manager’s service tab.
    4. Make sure the spice-webdav service is running from services.msc.
  3. When the guest system is running, connect to it using virt-viewer(NOT virt-manager) as Admnistrator( you may need to su or sudo). Then at the opened remote-viewer choose File -> 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).

  4. If the shared folder is not showed up in windows guest system’s This PC, you can manually run

     C:\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](/post/device-passthrough-in-kvm/).

Chao Cheng
Chao Cheng
Statistician

My research interests include applied statistics and machine learning.

Related