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?
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@example.com) (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: 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.
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).
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).