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.
  1. Configure Host Shared Memory:
    • Open Virt-Manager.
    • Go to Memory settings and check Enable shared memory.
  2. 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).
  3. Mount Inside the Guest VM:
    • Create a mount point: sudo mkdir /mnt/shared
    • Mount the filesystem: sudo mount -t virtiofs myshare /mnt/shared.
  4. Persistent Mount (Optional):
    • Add this line to /etc/fstab in the guest:
      myshare /mnt/shared virtiofs rw,noatime,_netdev 0 2.
 
 
Method 2: Using 9p (VirtFS)
If your system doesn't support virtio-fs, use the 9p protocol.
  1. Add Filesystem Hardware:
    • In Virt-Manager, add a Filesystem.
    • Driver: Select default or path.
    • Mode: Use passthrough (requires root) or mapped (safer for permissions).
    • Source path: The host directory.
    • Target path: A tag name (e.g., host_share).
  2. Mount Inside the Guest VM:
    • sudo mount -t 9p -o trans=virtio,version=9p2000.L host_share /mnt/

Comments

Popular posts from this blog

RHEL - How to back out a failed patch

Vathsa's- Linux - SysOps and DevOps

Local Yum Repository for Oracle Linux 8