12

Is it possible to use a folder shared from the host to the guest via virtfs/9p as the root file system inside the guest?

Loosely related to my previous question: Can virtfs/9p be used to share the same host folder with multiple guests?

0xC0000022L
  • 16,189
  • 24
  • 102
  • 168

3 Answers3

10

Yes, see for instance how to boot a VM with the FS of the host:

Add the 9p modules to the host initramfs (that's the easiest way albeit not the cleanest, to have an initrd with the needed modules):

printf '%s\n' 9p 9pnet 9pnet_virtio | sudo tee -a /etc/initramfs-tools/modules
sudo update-initramfs -u

qemu -kernel "/boot/vmlinuz-$(uname -r)" \
  -initrd "/boot/initrd.img-$(uname -r)" \
  -fsdev local,id=r,path=/,security_model=none \
  -device virtio-9p-pci,fsdev=r,mount_tag=r \
  -nographic \
  -append 'root=r ro rootfstype=9p rootflags=trans=virtio console=ttyS0 init=/bin/sh'

If you run it as a normal user, there are files it won't be able to access, but you should be able to get the the shell prompt and it won't do any damage:

[    0.000000] Linux version 3.10-3-amd64 ([email protected]) (gcc version 4.7.3 (Debian 4.7.3-7) ) #1 SMP Debian 3.10.11-1 (2013-09-10)
[    0.000000] Command line: root=r rootfstype=9p rootflags=trans=virtio console=ttyS0 init=/bin/sh
[...]
Loading, please wait...
[    0.564122] systemd-udevd[52]: starting version 204
[...]
Begin: Loading essential drivers ... [    1.007951] FS-Cache: Loaded
[    1.009958] 9p: Installing v9fs 9p2000 file system support
[    1.012880] FS-Cache: Netfs '9p' registered for caching
done.
Begin: Running /scripts/init-premount ... done.
[...]
sh-4.2# ls /
bin   home            lib32       media    opt   safe  tmp      vmlinuz.old
boot  initrd.img      lib64       mnt      proc  sbin  usr
dev   initrd.img.old  libx32      old      root  srv   var
etc   lib             lost+found  old-tmp  run   sys   vmlinuz
sh-4.2# poweroff -f
[   56.958724] ACPI: Preparing to enter system sleep state S5
[   56.960332] Power down.
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • 2
    Additionally, you could use 'security_model=mapped' to be able to fully access the underlying file-system since it stores ownership and other privileged file information in extended attributes of the file. This also allows you to mount the fs read-write instead of readonly. See also [link](https://lists.gnu.org/archive/html/qemu-devel/2010-05/msg02673.html) – Rutger Nijlunsing Jan 04 '14 at 20:03
  • 1
    Modern Linux seems to ignore the "root=r" command line option and unconditionally looks for the mount_tag "/dev/root". Any objection to updating the answer to reflect that? – R.. GitHub STOP HELPING ICE Mar 14 '18 at 19:17
  • I second that `/dev/root` comment. Here it always looks for `/dev/root` too, no matter what mount_tag I use. – user1593842 Oct 19 '21 at 21:43
4

As sensible an idea as this seems at first, do not do this. 9P in its current state cannot handle some fairly basic operations, including:

9P at present is not in a fit state to be used in production.

While you can make a bootable system with 9P as the root file system, operating that VM will cause significant grief - if using Debian, the defect listed above will prevent apt-get upgrade from working. Patches to fix this problem have gone nowhere for years.

If you insist on doing this, the rootflags should be "rootflags=trans=virtio,cache=mmap", otherwise read/write memory mapping will not work (used by, for example, MariaDB).

L29Ah
  • 793
  • 5
  • 19
TMR
  • 43
  • 3
  • Even though I don't like it, thanks for the input. I am currently pondering placing boot and root in a proper file container and _some_ of the data that is supposed to be shared into 9p ... – 0xC0000022L Apr 11 '20 at 23:40
1

Yes, sure. Add to kernel command line:

root=host rootfstype=9p rootflags=trans=virtio

And you may boot without initrd (if 9P is compiled into the kernel, rather than as modules).

TMR
  • 43
  • 3
socketpair
  • 131
  • 2