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