Menu Close

Chapter 19. Sharing files between the host and its virtual machines

You may frequently require to share data between your host system and the virtual machines (VMs) it runs. To do so quickly and efficiently, you can set up NFS or Samba file shares on your system. As a newly supported feature in RHEL 9, you can also use the virtiofs file system to share data with your Linux VMs.

19.1. Sharing files between the host and its virtual machines using virtiofs

When using RHEL 9 as your hypervisor, you can efficiently share files between your host system and its virtual machines (VM) using the virtiofs feature.

Prerequisites

  • Virtualization is installed and enabled on your RHEL 9 host.
  • A directory that you want to share with your VMs. If you do not want to share any of your existing directories, create a new one, for example named shared-files.

    # mkdir /root/shared-files
  • The VM you want to share data with is using a Linux distribution as its guest OS.

Procedure

  1. For each directory on the host that you want to share with your VM, set it as a virtiofs file system in the VM’s XML configuration.

    1. Open the XML configuration of the intended VM.

      # virsh edit vm-name
    2. Add an entry similar to the following to the <devices> section of the VM’s XML configuration.

      <filesystem type='mount' accessmode='passthrough'>
        <driver type='virtiofs'/>
        <binary path='/usr/libexec/virtiofsd' xattr='on'/>
        <source dir='/root/shared-files'/>
        <target dir='host-file-share'/>
      </filesystem>

      This example sets the /root/shared-files directory on the host to be visible as host-file-share to the VM.

  2. Add a NUMA topology for shared memory to the XML configuration. The following example adds a basic topology for all CPUs and all RAM.

    <cpu mode='host-passthrough' check='none'>
      <numa>
        <cell id='0' cpus='0-{number-vcpus - 1}' memory='{ram-amount-KiB}' unit='KiB' memAccess='shared'/>
      </numa>
    </cpu>
  3. Add shared memory backing to the <domain> section of the XML configuration:

    <domain>
     [...]
     <memoryBacking>
       <access mode='shared'/>
     </memoryBacking>
     [...]
    </domain>
  4. Boot up the VM.

    # virsh start vm-name
  5. Mount the file system in the guest operating system (OS). The following example mounts the previously configured host-file-share directory with a Linux guest OS.

    # mount -t virtiofs host-file-share /mnt

Verification

  • Ensure that the shared directory became accessible on the VM and that you can now open files stored in the directory.

Known issues and limitations

  • File-system mount options related to access time, such as noatime and strictatime, are not likely to work with virtiofs, and Red Hat discourages their use.

Troubleshooting

  • If virtiofs is not optimal for your usecase or supported for your system, you can use NFS or Samba instead.

19.2. Using the web console to share files between the host and its virtual machines using virtiofs

You can use the RHEL web console to efficiently share files between your host system and its virtual machines (VM) using the virtiofs feature.

Prerequisites

  • The web console VM plug-in is installed on your system.
  • A directory that you want to share with your VMs. If you do not want to share any of your existing directories, create a new one, for example named centurion.

    # mkdir /home/centurion
  • The VM you want to share data with is using a Linux distribution as its guest OS.

Procedure

  1. In the Virtual Machines interface, click the VM with which you want to share files.

    A new page opens with an Overview section with basic information about the selected VM and a Console section.

  2. Scroll to Shared directories.

    The Shared directories section displays information about the host files and directories shared with that VM and options to Add or Remove a shared directory.

    Image displaying the directories shared with the selected VM.
  3. Click Add shared directory.

    The Share a host directory with the guest dialog appears.

    Image displaying the Share a host directory with the guest dialog box.
  4. Enter the following information:

    • Source path - The path to the host directory that you want to share.
    • Mount tag - The tag that the VM uses to mount the directory.
  5. Set additional options:

    • Extended attributes - Set whether to enable extended attributes, xattr, on the shared files and directories.
  6. Click Share.

    The selected directory is shared with the VM.

Verification

  • Ensure that the shared directory is accessible on the VM and you can now open files stored in that directory.

19.3. Using the web console to remove shared files between the host and its virtual machines using virtiofs

You can use the RHEL web console to remove files shared between your host system and its virtual machines (VM) using the virtiofs feature.

Prerequisites

Procedure

  1. In the Virtual Machines interface, click on the VM from which you want to remove the shared files.

    A new page opens with an Overview section with basic information about the selected VM and a Console section.

  2. Scroll to Shared directories.

    The Shared directories section displays information about the host files and directories shared with that VM and options to Add or Remove a shared directory.

    Image displaying the directories shared with the selected VM.
  3. Click Remove next to the directory you wish to unshare with the VM.

    The Remove filesystem dialog appears.

    Image displaying the remove a filesystem dialog box.
  4. Click Remove.

    The selected directory is unshared with the VM.

Verification

  • The shared directory is no longer available and accessible on the VM.

19.4. Sharing files between the host and Linux virtual machines using NFS

For efficient file sharing between your RHEL 9 host system and the Linux VMs it is connected to, use the virtiofs feature. However, if virtiofs is not working for you or does not suit your use case, you can instead export an NFS share that your VMs can mount and access.

Prerequisites

  • The nfs-utils package is installed on the host.
  • A directory that you want to share with your VMs. If you do not want to share any of your existing directories, create a new one, for example named shared-files.

    # mkdir shared-files
  • The host is visible and reachable over a network for the VM. This is generally the case if the VM is connected using the NAT and bridge type of virtual networks. However, for the macvtap connection, you must first set up the macvlan feature on the host. To do so:

    1. Create a network device file in the host’s /etc/systemd/network/ directory, for example called vm-macvlan.netdev.

      # vim /etc/systemd/network/vm-macvlan.netdev
    2. Edit the network device file to have the following content. You can replace vm-macvlan with the name you chose for your network device.

      [NetDev]
      Name=vm-macvlan
      Kind=macvlan
      
      [MACVLAN]
      Mode=bridge
    3. Create a network configuration file for your macvlan network device, for example vm-macvlan.network.

      # vim /etc/systemd/network/vm-macvlan.network
    4. Edit the network configuration file to have the following content. You can replace vm-macvlan with the name you chose for your network device.

      [Match]
      Name=_vm-macvlan_
      
      [Network]
      IPForward=yes
      Address=192.168.250.33/24
      Gateway=192.168.250.1
      DNS=192.168.250.1
    5. Create a network configuration file for your physical network interface. For example, if your interface is enp4s0:

      # vim /etc/systemd/network/enp4s0.network

      If you are unsure what interface name to use, you can use the ifconfig command on your host to obtain the list of active network interfaces.

    6. Edit the physical network configuration file to make the physical network a part of the macvlan interface, in this case vm-macvlan:

      [Match]
      Name=enp4s0
      
      [Network]
      MACVLAN=vm-macvlan
    7. Reboot your host.
  • Optional: For improved security, ensure your VMs are compatible with NFS version 4 or later.

Procedure

  1. On the host, export a directory with the files you want to share as a network file system (NFS).

    1. Obtain the IP address of each virtual machine you want to share files with. The following example obtains the IPs of testguest1 and testguest2.

      # virsh domifaddr testguest1
      Name       MAC address          Protocol     Address
      ----------------------------------------------------------------
      vnet0      52:53:00:84:57:90    ipv4         192.168.124.220/24
      
      # virsh domifaddr testguest2
      Name       MAC address          Protocol     Address
      ----------------------------------------------------------------
      vnet1      52:53:00:65:29:21    ipv4         192.168.124.17/24
    2. Edit the /etc/exports file on the host and add a line that includes the directory you want to share, IPs of VMs you want to share with, and sharing options.

      Shared directory VM1-IP(options) VM2-IP(options) [...]

      For example, the following shares the /usr/local/shared-files directory on the host with testguest1 and testguest2, and enables the VMs to edit the content of the directory:

      /usr/local/shared-files/ 192.168.124.220(rw,sync) 192.168.124.17(rw,sync)
    3. Export the updated file system.

      # exportfs -a
    4. Ensure the NFS process is started:

      # systemctl start nfs-server
    5. Obtain the IP address of the host system. This will be used for mounting the shared directory on the VMs later.

      # ip addr
      [...]
      5: virbr0: [BROADCAST,MULTICAST,UP,LOWER_UP] mtu 1500 qdisc noqueue state UP group default qlen 1000
          link/ether 52:54:00:32:ff:a5 brd ff:ff:ff:ff:ff:ff
          inet 192.168.124.1/24 brd 192.168.124.255 scope global virbr0
             valid_lft forever preferred_lft forever
      [...]

      Note that the relevant network is the one being used use for connection to the host by the VMs you want to share files with. Usually, this is virbr0.

  2. On the guest OS of a VM specified in the /etc/exports file, mount the exported file system.

    1. Create a directory you want to use as a mount point for the shared file system, for example /mnt/host-share:

      # mkdir /mnt/host-share
    2. Mount the directory exported by the host on the mount point. This example mounts the /usr/local/shared-files directory exported by the 192.168.124.1 host on /mnt/host-share in the guest:

      # mount 192.168.124.1:/usr/local/shared-files /mnt/host-share

Verification

  • To verify the mount has succeeded, access and explore the shared directory on the mount point:

    # cd /mnt/host-share
    # ls
    shared-file1  shared-file2  shared-file3

19.5. Sharing files between the host and Windows virtual machines using Samba

For efficient file sharing between your RHEL 9 host system and the Windows VMs it is connected to, use the virtiofs feature. However, if virtiofs is not working for you or does not suit your use case, you can instead prepare a Samba server that your VMs can access.

Prerequisites

  • The samba packages are installed on your host. If they are not:

    # dnf install samba
  • The host is visible and reachable over a network for the VM. This is generally the case if the VM is connected using the NAT and bridge type of virtual networks. However, for the macvtap connection, you must first set up the macvlan feature on the host. To do so:

    1. Create a network device file, for example called vm-macvlan.netdev in the host’s /etc/systemd/network/ directory.

      # vim /etc/systemd/network/vm-macvlan.netdev
    2. Edit the network device file to have the following content. You can replace vm-macvlan with the name you chose for your network device.

      [NetDev]
      Name=vm-macvlan
      Kind=macvlan
      
      [MACVLAN]
      Mode=bridge
    3. Create a network configuration file for your macvlan network device, for example vm-macvlan.network.

      # vim /etc/systemd/network/vm-macvlan.network
    4. Edit the network configuration file to have the following content. You can replace vm-macvlan with the name you chose for your network device.

      [Match]
      Name=_vm-macvlan_
      
      [Network]
      IPForward=yes
      Address=192.168.250.33/24
      Gateway=192.168.250.1
      DNS=192.168.250.1
    5. Create a network configuration file for your physical network interface. For example, if your interface is enp4s0:

      # vim /etc/systemd/network/enp4s0.network

      If you are unsure what interface to use, you can use the ifconfig command on your host to obtain the list of active network interfaces.

    6. Edit the physical network configuration file to make the physical network a part of the macvlan interface, in this case vm-macvlan:

      [Match]
      Name=enp4s0
      
      [Network]
      MACVLAN=vm-macvlan
    7. Reboot your host.

Procedure

  1. On the host, create a Samba share and make it accessible for external systems.

    1. Add firewall permissions for Samba.

      # firewall-cmd --permanent --zone=public --add-service=samba
      success
      # firewall-cmd --reload
      success
    2. Edit the /etc/samba/smb.conf file:

      1. Add the following to the [global] section:

        map to guest = Bad User
      2. Add the following at the end of the file:

        #=== Share Definitions ===
        [VM-share]
        path = /samba/VM-share
        browsable = yes
        guest ok = yes
        read only = no
        hosts allow = 192.168.122.0/24

        Note that the hosts allow line restricts the accessibility of the share only to hosts on the VM network. If you want the share to be accessible by anyone, remove the line.

    3. Create the /samba/VM-share directory.

      # mkdir -p /samba/VM-share
    4. Enable the Samba service.

      # systemctl enable smb.service
      Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
    5. Restart the Samba service.

      # systemctl restart smb.service
    6. Allow the VM-share directory to be accessible and modifiable for the VMs.

      # chmod -R 0755 /samba/VM-share/
      # chown -R nobody:nobody /samba/VM-share/
    7. Add the SELinux Samba sharing label to /etc/samba/VM-share/

      # chcon -t samba_share_t /samba/VM-share/
  2. On the Windows guest operating system, attach the Samba share as a network location.

    1. Open the File Explorer and right-click "This PC".
    2. In the context menu, click Add a network location.

      virt Win10 network loc1
    3. In the Add Network Location wizard that opens, select "Choose a custom network location" and click Next.
    4. In the "Internet or network address" field, type host-IP/VM-share, where host-IP is the IP address of the host. Usually, the host IP is the default gateway of the VM. Afterwards, click Next.

      virt Win10 network loc2
    5. When the wizard asks if you want to rename the shared directory, keep the default name. This ensures the consistency of file sharing configuration across the VM and the guest. Click Next.
    6. If accessing the network location was successful, you can now click Finish and open the shared directory.