There are several ways to share files between your host and QEMU virtual machines. One of the most common and convenient methods is to use QEMU’s built-in virtio-9p file system sharing. This approach allows you to mount a host directory directly into the guest. Here’s how you can set it up:
1. Using the Virtio-9p Filesystem
On the Host: Specify the Shared Directory
When launching your QEMU virtual machine, add the -virtfs option to export a directory. For example, if you want to share the host directory /home/youruser/shared, you can start QEMU like this:
qemu-system-x86_64 \
-hda /path/to/your/disk_image.img \
-m 2048 \
-virtfs local,path=/home/youruser/shared,security_model=none,mount_tag=hostshare
Explanation of the options:
-virtfs local: Tells QEMU you’re sharing a local directory.
path=/home/youruser/shared: The host directory you want to share.
security_model=none: This option disables security checks. (Alternatively, you could use options like passthrough depending on your needs and guest configuration.)
mount_tag=hostshare: A tag used to identify this shared filesystem; you will reference this tag from the guest.
Note: The security_model option has important security implications. Choose the model that best fits your security requirements. For example, using passthrough will make the file ownership and permissions consistent with the host, which might be desirable in some cases.
Inside the Guest: Mount the Shared Directory
-
Ensure 9p Support:
Make sure your guest OS has support for the 9p filesystem. Most modern Linux distributions include this support in the kernel. If not, you might need to load the module:
sudo modprobe 9p
sudo modprobe 9pnet
sudo modprobe virtio_pci
sudo modprobe virtio_9p
-
Mount the Shared Directory:
Create a mount point (if it doesn’t already exist), for example /mnt/shared:
sudo mkdir -p /mnt/shared
Then, mount the shared folder using the mount tag you specified (hostshare):
sudo mount -t 9p -o trans=virtio,version=9p2000.L hostshare /mnt/shared
Here’s what the options mean:
-t 9p: Uses the 9p protocol.
-o trans=virtio: Specifies that the communication should use the virtio transport.
version=9p2000.L: Specifies the protocol version (this is a commonly used version).
-
Verify Access:
After mounting, you should see the contents of /home/youruser/shared in your guest’s /mnt/shared directory. You can read and write files there as permitted by your host’s file permissions.
2. Alternative Methods
If for any reason the 9p method doesn’t suit your needs, consider these alternatives:
-
NFS (Network File System):
Set up an NFS server on the host and mount it in the guest. This is especially useful if your guest and host are running different operating systems.
-
Samba/SMB:
Configure a Samba share on the host and access it from the guest (particularly if the guest is running Windows or if you need cross-platform sharing).
-
SSHFS:
If you have SSH access between the guest and host, you can mount a remote filesystem over SSH.
Each method has its own setup requirements and security considerations.
Summary
For many setups, the 9p filesystem is the easiest and most integrated way to share files between your host and QEMU virtual machines:
- Start QEMU with the
-virtfs option to share a specified directory.
- Mount the share inside the guest using the 9p protocol.
Choose the method that best fits your workflow, and make sure to tailor security settings to your environment. Enjoy your file sharing setup with QEMU!