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?

  • I never tried to 9p as root FS, but I think that with proper initrd there is nothing that can block you from doing that. – Alexander Kudrevatykh Oct 6 '13 at 17:19
  • 1
    jor1k does this – Janus Troelsen Feb 26 '15 at 14:13
  • What would be beautiful is if we could use overlayfs with / and the 9p mount as lowers, then chroot into it. But it fails to be very useful because writes to the lowers are undefined... There are however other FUSER filesystems that allow that I think, but I haven't reached it yet. – Ciro Santilli 新疆改造中心 六四事件 法轮功 Oct 17 at 16:01

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 (debian-kernel@lists.debian.org) (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.
  • 1
    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 – Rutger Nijlunsing Jan 4 '14 at 20:03
  • 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.. Mar 14 at 19:17

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:

  • open()...unlink()...fstat() on a file.

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).

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).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.