qemu-docker icon indicating copy to clipboard operation
qemu-docker copied to clipboard

[Question]: How can I share a folder from my host system with a guest virtual machine running in QEMU ?

Open DartSteven opened this issue 1 year ago • 6 comments

Is your question not already answered in the FAQ?

  • [X] I made sure the question is not listed in the FAQ.

Is this a general question and not a technical issue?

  • [X] I am sure my question is not about a technical issue.

Question

Hi,

I would like to share a folder from my host system with a guest virtual machine running in QEMU. I’ve been trying to find the right way to do this, but I haven’t been able to get it to work. Could you please provide a step-by-step guide on how to properly share a host folder with a guest VM?

Specifically, I tried using the following command:

volumes:

  • /media/lvm/backup/Roon-Backups:/data/storage/bck:rw

Could you please guide me on how to correctly set up folder sharing between the host and the guest? Any help would be greatly appreciated!

Thank you!

DartSteven avatar Sep 16 '24 14:09 DartSteven

could you tell how you are trying to achieve this? (Network sharing or just sharing offline?)

EagAnarchy avatar Sep 25 '24 21:09 EagAnarchy

could you tell how you are trying to achieve this? (Network sharing or just sharing offline?)

Sure! My idea is to use your fantastic project to run “Roon-Server.” You can check it out here: https://roon.app/. More specifically, the image to download is this one: https://download.roonlabs.net/builds/roonbox-linuxx64-nuc4-usb-factoryreset.img.gz. It is listed in this guide, section 3 “Download”: https://help.roonlabs.com/portal/en/kb/articles/rock-install-guide#3_Download.

In short, this is audio software for listening to music. My music is stored on a partition on my host, and the backup folder (which the application interacts with for regular backups) is also on a partition on my host. I would like to be able to pass these two directories (for example, /media/lvm/data/Music and /media/lvm/backup) to the virtual machine.

DartSteven avatar Sep 25 '24 21:09 DartSteven

Might there be some way to use QEMU's 9pfs to achieve this?

fizzyduck avatar Oct 02 '24 09:10 fizzyduck

Might there be some way to use QEMU's 9pfs to achieve this?

yes but how? i tried but always nothing comes from host to guest

DartSteven avatar Oct 02 '24 09:10 DartSteven

The way I am doing this is:

  1. Ensure that your guest has 9pfs support compiled in or available as a kernel module
  2. Add a volume in docker-compose.yml so that qemu will have access to your host's FS via a bind mount:
    volumes:
      - /mnt/shared:/mnt/shared
  1. Add an ARGUMENT parameter to the environment section of docker-compose.yml:
    environment:
       ARGUMENTS: "-fsdev local,id=fsdev0,path=/mnt/shared,security_model=none -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=host_share"
  1. Boot the guest and then attempt to mount the shared location:
mount -t 9p -o trans=virtio host_share /mnt/guest_mountpoint

I have just tested this and it appears to work as expected.

fizzyduck avatar Oct 02 '24 10:10 fizzyduck

What would be good is to put this in the main readme ;)

fizzyduck avatar Oct 02 '24 15:10 fizzyduck

Very useful information @fizzyduck

kroese avatar Mar 01 '25 14:03 kroese

@fizzyduck It's now added to the readme, thank you! Please verify if it works: https://github.com/qemus/qemu#how-do-i-share-files-with-the-host

kroese avatar Mar 14 '25 06:03 kroese

@kroese I just got round to testing this per your readme but I notice the readme misses an instruction to ensure that the ARGUMENTS compose parameter has the relevant additions:

-fsdev local,id=fsdev0,path=/mnt/shared,security_model=none -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=host_share

Noting that the fsdev portion related to the volume entry and the device portion sets up the tag (in this case host_share) which must be used in the mount command.

For your readme example, you would need:

       ARGUMENTS: "-fsdev local,id=fsdev0,path=/shared,security_model=none -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=shared"

Note the volume from the compose config (/shared) and the name to be used in the mount command (shared).

fizzyduck avatar Mar 16 '25 20:03 fizzyduck

@fizzyduck I added in the code that when it detects the /shared folder it automaticly adds those lines: https://github.com/qemus/qemu/blob/0944104d5589ba8906c17b21f56395d2eb548f36/src/config.sh#L26

So no ARGUMENTS should be needed at all.

kroese avatar Mar 16 '25 20:03 kroese

@kroese I just had a hunch you'd probably do that and went and looked at the PR to then notice you had already taken care of this - I've obviously not pulled the latest version! Apologies for that.

fizzyduck avatar Mar 16 '25 20:03 fizzyduck