Mount share folder in QEMU with same permission as host



This content originally appeared on DEV Community and was authored by Franz Wong

Background

I would like to share a folder between guest and host in QEMU. File permission should be the same as the user in host.

Solution

1. Create a folder called shared in host

mkdir -p shared

2. Add a parameter virtfs when starting VM with QEMU

-virtfs local,path=shared,mount_tag=shared,security_model=mapped-xattr

3. After the VM is started, login to the VM and create mount point

mkdir -p /mnt/shared

4. Mount the folder

sudo mount -t 9p -o trans=virtio,version=9p2000.L shared /mnt/shared

5. Check the permission of mount point

ls -l -d /mnt/shared

It should be something like the following. On my host machine, my user ID is 501 and primary group ID is 20. (It’s common setting in MacOS). In the guest VM, the group with ID 20 is called dialout. (You can check in /etc/group).

drwxr-xr-x 3 501 dialout 96 Jul 14 03:44 /mnt/shared

However, my user in guest VM has uid 1000 and the primary group id is 1000 too. We need to make a mapping.

id
uid=1000(franz) gid=1000(dev_users) groups=1000(dev_users),27(sudo)

6. Install bindfs in guest VM

sudo apt install -y bindfs

7. Create the mapping

sudo bindfs --map=501/1000:@dialout/@1000 /mnt/shared /mnt/shared

8. Check the permission of mount point again

ls -l -d /mnt/shared

This time the permission is correct.

drwxr-xr-x 3 franz dev_users 96 Jul 14 03:44 /mnt/shared

Reference: 9p/virtfs share not writable


This content originally appeared on DEV Community and was authored by Franz Wong