Chapter 19. Sharing files between the host and its virtual machines
You might frequently require to share data between your host system and the virtual machines (VMs) it runs. To do so quickly and efficiently, you can use the virtio
file system (virtiofs
).
19.1. Sharing files between the host and its virtual machines by using virtiofs
By using the virtio
file system (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.
19.1.1. Sharing files between the host and Linux virtual machines by using the command line
When using RHEL 10 as your hypervisor, you can share files between your host system and its virtual machines (VM) by using the virtiofs
feature.
Prerequisites
- Virtualization is installed and enabled on your RHEL 10 host.
A directory is available 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
# mkdir /root/shared-files
Copy to Clipboard Copied! - The VM you want to share files with is using a Linux distribution as its guest operating system.
Procedure
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
# virsh edit vm-name
Copy to Clipboard Copied! 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>
<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>
Copy to Clipboard Copied! This example sets the
/root/shared-files
directory on the host to be visible ashost-file-share
to the VM.
Set up shared memory for the VM. To do so, add shared memory backing to the
<domain>
section of the XML configuration:<domain> [...] <memoryBacking> <access mode='shared'/> </memoryBacking> [...] </domain>
<domain> [...] <memoryBacking> <access mode='shared'/> </memoryBacking> [...] </domain>
Copy to Clipboard Copied! Boot up the VM.
virsh start vm-name
# virsh start vm-name
Copy to Clipboard Copied! 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
# mount -t virtiofs host-file-share /mnt
Copy to Clipboard Copied!
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
andstrictatime
, are not likely to work with virtiofs, and Red Hat discourages their use.
19.1.2. Sharing files between the host and Linux virtual machines by using the web console
You can use the RHEL web console to efficiently share files between your host system and its virtual machines (VM) by using the virtiofs
feature.
Prerequisites
- The web console VM plugin 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
# mkdir /home/centurion
Copy to Clipboard Copied! - The VM you want to share data with is using a Linux distribution as its guest operating system.
Procedure
In the
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
.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
.The Share a host directory with the guest dialog is displayed.
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
.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.