Sharing files between your host system and a QEMU virtual machine (VM) can be done in several ways, depending on your needs. Here are the most common methods:
1. Using Virtio-FS (Recommended for Linux Guests)
Virtio-FS is a high-performance shared filesystem for QEMU/KVM, ideal for Linux guests.
If your guest supports networking, you can set up an SMB or NFS share.
Steps (SMB Example):
Set Up an SMB Share on the Host:
Install Samba (sudo apt install samba on Debian/Ubuntu).
Configure /etc/samba/smb.conf:
ini
[shared]
path = /path/to/host/directory
browsable = yes
read only = no
guest ok = yes
Restart Samba:
sh
sudo systemctl restart smbd
Access the Share from the Guest:
On Linux guests:
sh
sudo mount -t cifs //host-ip/shared /mnt/shared -o guest
On Windows guests, use \\host-ip\shared in File Explorer.
4. Using SSH/SCP (For Occasional File Transfers)
If you just need to transfer files occasionally, use scp or rsync:
sh
# From host to guest (if guest has SSH server)
scp file.txt user@guest-ip:/path/in/guest
# From guest to host
scp user@host-ip:/path/in/host/file.txt .
5. Using a Shared Disk Image (For Persistent Storage)
You can attach a virtual disk (e.g., QCOW2) to both host and guest.
Steps:
Create a Shared Disk:
sh
qemu-img create -f qcow2 shared_disk.qcow2 1G
Attach it to QEMU:
sh
-drive file=shared_disk.qcow2,format=qcow2
Format and Mount in Guest:
Use fdisk/mkfs to format, then mount it.
Which Method Should You Use?
For Linux guests: Virtio-FS (best performance) or 9p.
For Windows guests: SMB or Virtio-FS (with drivers).