xref: /cloud-hypervisor/docs/virtiofs-root.md (revision 9acb69f3e349f6bdf96f259e83b0583c9a22af9d)
1# HOWTO VirtioFS rootfs
2
3A quick guide for using virtiofs as a cloud-hypervisor guest's rootfs (i.e.
4with no root block device). This document is a quick getting started guide.
5There are many more steps to take to make this a production ready, secure
6setup.
7
8## Prerequisites
9
101. virtiofsd from the qemu project
11   * We are using the Qemu version for now
12   * There is a Rust version being worked on that may be a better option in the future
13   * Part of the qemu-system-common package on Ubuntu
14   * Part of the qemu-common package on Fedora
152. cloud-hypervisor - the newer the better, but I tested with 0.12
163. a rootfs - This howto uses an alpine rootfs available here:
17   * https://dl-cdn.alpinelinux.org/alpine/v3.13/releases/x86_64/alpine-minirootfs-3.13.2-x86_64.tar.gz
18   * Others should work
19
20## To create the VM rootfs
21
22```bash
23mkdir rootfs/
24cd rootfs
25# this needs sudo to be able to set root permissions on fs components
26sudo tar -xf /path/to/alpine-minirootfs-3.13.1-x86_64.tar.gz
27# this will get created when the VM actually boots by the dhcp client
28# but we need it in the chroot to download packages
29sudo cp /etc/resolv.conf etc/
30# the alpine mini rootfs is meant for docker containers, we need a few extra
31# things for a working rootfs
32sudo chroot $PWD apk add openrc busybox-initscripts
33# we are using the paravirt console in cloud-hypervisor, so enable it in init
34# append it after the other console since it doesn't work just appending it
35sudo sed -i '/vt100/a \n# paravirt console\nhvc0::respawn:/sbin/getty -L hvc0 115200 vt100' etc/inittab
36# set no password for root user... you obviously don't want to do this for
37# any sort of production setup
38sudo sed -i 's/root:!::0:::::/root:::0:::::/' etc/shadow
39# set up init scripts
40for i in acpid crond
41    sudo ln -sf /etc/init.d/$i etc/runlevels/default/$i
42end
43for i in bootmisc hostname hwclock loadkmap modules networking swap sysctl syslog urandom
44    sudo ln -sf /etc/init.d/$i etc/runlevels/boot/$i
45end
46
47for i in killprocs mount-ro savecache
48    sudo ln -sf /etc/init.d/$i etc/runlevels/shutdown/$i
49end
50
51for i in devfs dmesg hwdrivers mdev
52    sudo ln -sf /etc/init.d/$i etc/runlevels/sysinit/$i
53end
54# setup network config
55echo 'auto lo
56iface lo inet loopback
57
58auto eth0
59iface eth0 inet dhcp
60' | sudo tee etc/network/interfaces
61
62```
63
64## To run the VM
65
66```bash
67# starting in the directory above rootfs
68sudo virtiofsd --socket-path=$PWD/virtiofs-rootfs.sock --shared-dir=$PWD/rootfs --cache=never &
69sudo cloud-hypervisor \
70    --cpus boot=1,max=1 \
71    --kernel vmlinux \
72    --fs tag=/dev/root,socket=$PWD/virtiofs-rootfs.sock \
73    --memory size=2G,shared=on \
74    --cmdline "console=hvc0 rootfstype=virtiofs root=/dev/root ro debug" \
75    --api-socket $PWD/ch.sock \
76    --rng \
77    --net ...
78```
79
80Note: an important part of the above is the `tag=/dev/root` and
81`root=/dev/root` parts. For whatever reason, it would only work with that as
82the tag.
83
84Note: another important bit is that the memory is shared. This is required for
85virtiofs
86
87## Message from the author
88
89If you find any issues or have suggestions, feel free to reach out to @iggy on
90the cloud-hypervisor slack. Also if this works for you, I'd like to know as
91well. It would also be nice to get steps for preparing other distribution root
92filesystems.