Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mounting a shared folder between the host and guest via virtiofs #82

Closed
wants to merge 6 commits into from

Conversation

nathanchance
Copy link
Member

virtiofs, available in QEMU 5.2 or newer and Linux guests 5.4 or newer,
is a more modern way to pass local folders along to QEMU, as it takes
advantage of the fact that the folders are on the same machine as the
hypervisor.

To use virtiofs, we first need to run virtiofsd, which is included with
most base QEMU packages. Once we find it, we run it in the background
and connect to it using some QEMU parameters, which were shamelessly
taken from the official virtiofs website:

https://virtio-fs.gitlab.io/howto-qemu.html

To use it within the guest (you can use a different path than
/mnt/shared but mount -t virtio shared must be used):

  / # mkdir /mnt/shared
  / # mount -t virtiofs shared /mnt/shared
  / # echo "$(uname -a)" >/mnt/shared/foo

On the host:

  $ cat shared/foo
  Linux (none) 6.1.0-rc8-next-20221207 #2 SMP PREEMPT Wed Dec  7 14:56:03 MST 2022 aarch64 GNU/Linux

This does require guest kernel support (CONFIG_VIRTIO_FS=y), otherwise
it will not work inside the guest:

  / # mount -t virtiofs shared /mnt/shared
  mount: mounting shared on /mnt/shared failed: No such device

Closes: #81

utils.py Show resolved Hide resolved
boot-qemu.py Show resolved Hide resolved
boot-qemu.py Outdated Show resolved Hide resolved
boot-qemu.py Outdated Show resolved Hide resolved
boot-qemu.py Outdated Show resolved Hide resolved
@nathanchance
Copy link
Member Author

I just pushed a change to warn when CONFIG_VIRTIO_FS might not be set and print some information about using the folder within the guest. PTAL.

This will be useful for a follow up commit, which will attempt to look
for a binary relative to QEMU's prefix. It happens to simplify the code
as well.

Signed-off-by: Nathan Chancellor <[email protected]>
virtiofs, available in QEMU 5.2 or newer and Linux guests 5.4 or newer,
is a more modern way to pass local folders along to QEMU, as it takes
advantage of the fact that the folders are on the same machine as the
hypervisor.

To use virtiofs, we first need to run virtiofsd, which is included with
most base QEMU packages. Once we find it, we run it in the background
and connect to it using some QEMU parameters, which were shamelessly
taken from the official virtiofs website:

https://virtio-fs.gitlab.io/howto-qemu.html

To use it within the guest (you can use a different path than
/mnt/shared but 'mount -t virtio shared' must be used):

  # mkdir /mnt/shared
  # mount -t virtiofs shared /mnt/shared
  # echo "$(uname -a)" >/mnt/shared/foo

On the host:

  $ cat shared/foo
  Linux (none) 6.1.0-rc8-next-20221207 ClangBuiltLinux#2 SMP PREEMPT Wed Dec  7 14:56:03 MST 2022 aarch64 GNU/Linux

This does require guest kernel support (CONFIG_VIRTIO_FS=y), otherwise
it will not work inside the guest:

  / # mount -t virtiofs shared /mnt/shared
  mount: mounting shared on /mnt/shared failed: No such device

Link: ClangBuiltLinux#81
Signed-off-by: Nathan Chancellor <[email protected]>
Fix the following backtrace:

  Traceback (most recent call last):
    File ".../boot-qemu.py", line 870, in <module>
      launch_qemu(config)
    File ".../boot-qemu.py", line 770, in launch_qemu
      virtiofsd = utils.find_first_file(qemu_prefix, virtiofsd_locations)
    File ".../utils.py", line 52, in find_first_file
      f"No files from list ('{', '.join(possible_files)}') could be found within '{relative_root}'!"
  TypeError: sequence item 0: expected str instance, PosixPath found

Signed-off-by: Nathan Chancellor <[email protected]>
launch_qemu() has gotten quite unweildy in terms of indentation level
and readability. Fix it by breaking it a part into several different
functions, each of which has full documentation behind it to understand
what they are doing and why they exist.

Take the opportunity to optimize launch_qemu_gdb() a bit by pulling as
many unnecessary changes out of the while loop as possible.

There are a couple of minor changes around spacing in this, which came
about due to changes to the order in which strings are printed.

Signed-off-by: Nathan Chancellor <[email protected]>
This will come in handy in trying to warn people when they are missing
configurations needed for certain features.

While we are at it, make getting the configuration value a little more
robust using re.search() + match groups.

Signed-off-by: Nathan Chancellor <[email protected]>
1. Warn the user if CONFIG_VIRTIO_FS=y cannot be found in the
   configuration (or if the configuration cannot be found).

2. Print information about using the shared folder within the guest.

Signed-off-by: Nathan Chancellor <[email protected]>
@nathanchance
Copy link
Member Author

I've rebased on latest main, as there have been quite a few changes to the gdb section, which was refactored in 6c41476.

@nathanchance nathanchance deleted the virtiofs branch March 13, 2023 19:07
@nathanchance
Copy link
Member Author

v2: #93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

automate fs share between host+guest
2 participants