Chapitre 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 file shares on your system. Alternatively, you can also use the virtiofs to share data with your Linux and Windows VMs.

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

For efficient file sharing between your RHEL 9 host system and the virtual machines (VMs) it is connected to, you can export an NFS share that your VMs can mount and access.

+ However with Linux VMs, it is usually more convenient to use the virtiofs feature.

Conditions préalables

  • The nfs-utils package is installed on the host.

    # dnf install nfs-utils -y
  • 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
  • When connected to a VM, the host is visible and reachable over a network. This is generally the case if the VM uses the NAT and bridge type of virtual networks.
  • Optional: For improved security, ensure your VMs are compatible with NFS version 4 or later.

Procédure

  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 VM with which you want to share files. 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 it 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)
      Note

      If you want to share a directory with a Windows VM, you must ensure the Windows NFS client has write permissions in the shared directory. A simple way to do so, is to use the all_squash, anonuid, and anongid options in the /etc/exports file.

      Par exemple :

      /usr/local/shared-files/ 192.168.124.220(rw,sync,all_squash,anonuid=<directory-owner-UID>,anongid=<directory-owner-GID>)

      The <directory-owner-UID> and <directory-owner-GID> are the UID and GID of the local user that owns the shared directory on the host.

      To explore other options for managing NFS client permissions, follow the Securing NFS guide.

    3. Export the updated file system.

      # exportfs -a
    4. Ensure the nfs-server service is running.

      # 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 that is used for connecting to the host by the VMs you want to share files with. Usually, this is virbr0.

  2. Mount the shared directory on a Linux VM that is specified in the /etc/exports file.

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

    Dans cet exemple :

    • 192.168.124.1 is the IP address of the host.
    • /usr/local/shared-files is a file-system path to the exported directory on the host.
    • /mnt/host-share is a mount point on the VM. The mount point must be an empty directory.
  3. To mount the shared directory on a Windows VM that is specified in the /etc/exports file:

    1. Open a PowerShell shell prompt as an administrator.
    2. Install the NFS-Client package. The installation command is different for the server and desktop versions of Windows.

      On a server version of Windows:

      # Install-WindowsFeature NFS-Client

      On a desktop version of Windows:

      # Enable-WindowsOptionalFeature -FeatureName ServicesForNFS-ClientOnly, ClientForNFS-Infrastructure -Online -NoRestart
    3. Mount the directory exported by the host on a Windows VM.

      # C:\Windows\system32\mount.exe -o anon \\192.168.124.1\usr\local\shared-files Z:

      Dans cet exemple :

      • 192.168.124.1 is the IP address of the host.
      • /usr/local/shared-files is a file system path to the exported directory on the host.
      • Z: is the drive letter that will be used as a mount point. You must choose a drive letter that is not in use on the system.

Vérification

  • To verify you can share files between the host and the VM, list the content of the shared directory on the VM. In the following example, replace <mount_point> with a file system path to the mounted shared directory.

    $ ls <mount_point>
    shared-file1  shared-file2  shared-file3