19.2. Sharing files between the host and its virtual machines using virtiofs
Using virtiofs, you can share files between your host and your virtual machines (VM) as a directory tree that works the same as the local file system structure. You can use virtiofs to perform the following tasks:
19.2.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.
Conditions préalables
- 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 operating system.
Procédure
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.
Open the XML configuration of the intended VM.
# virsh edit vm-name
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 ashost-file-share
to the VM.
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>
Add shared memory backing to the
<domain>
section of the XML configuration:<domain> [...] <memoryBacking> <access mode='shared'/> </memoryBacking> [...] </domain>
Boot up the VM.
# virsh start vm-name
Mount the file system in the guest operating system. The following example mounts the previously configured
host-file-share
directory with a Linux guest operating system.# mount -t virtiofs host-file-share /mnt
Vérification
- 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
andstrictatime
, are not likely to work with virtiofs, and Red Hat discourages their use.
Résolution de problèmes
-
If
virtiofs
is not optimal for your usecase or supported for your system, you can use NFS instead.
19.2.2. Sharing files between the host and Windows virtual machines using virtiofs
When using RHEL 9 as your hypervisor, you can efficiently share files between your host system and Windows virtual machines (VM) using the virtiofs
feature along with the virtio-win
package.
You can run the virtiofs
service in case insensitive mode on a Windows VM using the virtiofs.exe
command and the -i
parameter.
Conditions préalables
- You have configured your VM’s XML configuration file to use virtiofs. For detailed information, see Section 19.2.1, « Sharing files between the host and its virtual machines using virtiofs ».
- You have the latest version of the Windows File System Proxy installer. For more information, see WinFsp installer.
-
You have installed the
virtio-win
package on your Windows VM. For more information, see Installing virtio drivers on a Windows guest.
Procédure
On your Windows VM, install WinFsp. To do so, use the installation program and follow the prompts.
In the Custom Setup window of the installation wizard, select the features you want to install on the VM.
Start the virtiofs service:
# sc start VirtioFsSvc
Navigate to This PC:
File Explorer → This PC
Virtiofs should be available on the Windows VM as the first available drive letter starting with
z:
and going backwards. For example,my_viofs (Z:)
.ImportantYou must restart the virtiofs service after each VM reboot to access the shared directory.
Optional: To set up additional virtiofs instances:
Stop the virtiofs service:
# sc stop VirtioFsSvc # sc config VirtioFsSvc start=demand
Configure the WinFSP.Launcher service to set up multiple virtiofs instances:
# "C:\Program Files (x86)\WinFsp\bin\fsreg.bat" virtiofs "<path to the binary>\virtiofs.exe" "-t %1 -m %2"
Mount virtiofs instances to drives.
For example, to mount virtiofs with the tag
mount_tag0
to theY:
drive:"C:\Program Files (x86)\WinFsp\bin\launchctl-x64.exe" start virtiofs viofsY mount_tag0 Y:
- Repeat the previous step to mount all of your virtiofs instances.
To unmount the virtiofs instance:
"C:\Program Files (x86)\WinFsp\bin\launchctl-x64.exe" stop virtiofs viofsY
Vérification
On your Windows VM, navigate to This PC:
File Explorer → This PC
-
If you did not specify a mount point when setting up the virtiofs service, it will use the first available drive letter starting with
z:
and going backwards. - If you have multiple virtiofs instances set up, they will appear as drives with the letters you had assigned to the instances.
-
If you did not specify a mount point when setting up the virtiofs service, it will use the first available drive letter starting with
19.2.3. Sharing files between the host and its virtual machines using virtiofs in the web console
You can use the RHEL web console to efficiently share files between your host system and its virtual machines (VM) using the virtiofs
feature.
Conditions préalables
- 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 operating system.
Procédure
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.
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.
Click Add shared directory.
The Share a host directory with the guest dialog appears.
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.
Set additional options:
-
Extended attributes - Set whether to enable extended attributes,
xattr
, on the shared files and directories.
-
Extended attributes - Set whether to enable extended attributes,
Click Share.
The selected directory is shared with the VM.
Vérification
- Ensure that the shared directory is accessible on the VM and you can now open files stored in that directory.
19.2.4. 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.
Conditions préalables
- The web console VM plug-in is installed on your system.
- The directory is no longer being used by the VM.
Procédure
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.
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.
Click Remove next to the directory that you want to unshare with the VM.
The Remove filesystem dialog appears.
Click Remove.
The selected directory is unshared with the VM.
Vérification
- The shared directory is no longer available and accessible on the VM.