Mount share folder in QEMU with same permission as host

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 virtf…


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


Print Share Comment Cite Upload Translate Updates
APA

Franz Wong | Sciencx (2024-07-14T04:15:32+00:00) Mount share folder in QEMU with same permission as host. Retrieved from https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/

MLA
" » Mount share folder in QEMU with same permission as host." Franz Wong | Sciencx - Sunday July 14, 2024, https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/
HARVARD
Franz Wong | Sciencx Sunday July 14, 2024 » Mount share folder in QEMU with same permission as host., viewed ,<https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/>
VANCOUVER
Franz Wong | Sciencx - » Mount share folder in QEMU with same permission as host. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/
CHICAGO
" » Mount share folder in QEMU with same permission as host." Franz Wong | Sciencx - Accessed . https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/
IEEE
" » Mount share folder in QEMU with same permission as host." Franz Wong | Sciencx [Online]. Available: https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/. [Accessed: ]
rf:citation
» Mount share folder in QEMU with same permission as host | Franz Wong | Sciencx | https://www.scien.cx/2024/07/14/mount-share-folder-in-qemu-with-same-permission-as-host/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.