Mount a host file system inside a KVM virtual machine
Mount a host file system inside a KVM virtual machine
To mount a host file system inside a KVM virtual machine, the modern and most efficient method is using virtio-fs. Alternatively, for older systems, the 9p (VirtFS) protocol can be used.
Method 1: Using virtio-fs (Recommended)
Virtio-fs provides near-native performance by leveraging shared memory between the host and the guest.
- Configure Host Shared Memory:
- Open Virt-Manager.
- Go to Memory settings and check Enable shared memory.
- Add Filesystem Hardware:
- Click Add Hardware > Filesystem.
- Driver: Select
virtiofs. - Source path: Enter the directory on your host (e.g.,
/home/user/shared). - Target path: Enter a "mount tag" (a label like
myshare).
- Mount Inside the Guest VM:
- Create a mount point:
sudo mkdir /mnt/shared - Mount the filesystem:
sudo mount -t virtiofs myshare /mnt/shared.
- Create a mount point:
- Persistent Mount (Optional):
- Add this line to
/etc/fstabin the guest:myshare /mnt/shared virtiofs rw,noatime,_netdev 0 2.
- Add this line to
Method 2: Using 9p (VirtFS)
If your system doesn't support virtio-fs, use the 9p protocol.
- Add Filesystem Hardware:
- In Virt-Manager, add a Filesystem.
- Driver: Select
defaultorpath. - Mode: Use
passthrough(requires root) ormapped(safer for permissions). - Source path: The host directory.
- Target path: A tag name (e.g.,
host_share).
- Mount Inside the Guest VM:
sudo mount -t 9p -o trans=virtio,version=9p2000.L host_share /mnt/
Comments
Post a Comment