xref: /aosp_15_r20/external/crosvm/docs/book/src/running_crosvm/custom_kernel_rootfs.md (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1# Custom Kernel / Rootfs
2
3This document explains how to build a custom kernel and use debootstrap to build a rootfs for
4running crosvm.
5
6For an easier way to get started with prebuilt images, see [Example Usage](./example_usage.md)
7
8### Build a kernel
9
10The linux kernel in chromiumos comes preconfigured for running in a crosvm guest and is the easiest
11to build. You can use any mainline kernel though as long as it's configured for para-virtualized
12(virtio) devices
13
14If you are using the chroot for ChromiumOS development, you already have the kernel source.
15Otherwise, you can clone it:
16
17```bash
18git clone --depth 1 -b chromeos-6.6 https://chromium.googlesource.com/chromiumos/third_party/kernel
19```
20
21Either way that you get the kernel, the next steps are to configure and build the bzImage:
22
23```bash
24cd kernel
25CHROMEOS_KERNEL_FAMILY=termina ./chromeos/scripts/prepareconfig container-vm-x86_64
26make olddefconfig
27make -j$(nproc) bzImage
28```
29
30This kernel does not build any modules, nor does it support loading them, so there is no need to
31worry about an initramfs, although they are supported in crosvm.
32
33### Build a rootfs disk
34
35This stage enjoys the most flexibility. There aren't any special requirements for a rootfs in
36crosvm, but you will at a minimum need an init binary. This could even be `/bin/bash` if that is
37enough for your purposes. To get you started, a Debian rootfs can be created with [debootstrap].
38Make sure to define `$CHROOT_PATH`.
39
40```bash
41truncate -s 20G debian.ext4
42mkfs.ext4 debian.ext4
43mkdir -p "${CHROOT_PATH}"
44sudo mount debian.ext4 "${CHROOT_PATH}"
45sudo debootstrap stable "${CHROOT_PATH}" http://deb.debian.org/debian/
46sudo chroot "${CHROOT_PATH}"
47passwd
48echo "tmpfs /tmp tmpfs defaults 0 0" >> /etc/fstab
49echo "tmpfs /var/log tmpfs defaults 0 0" >> /etc/fstab
50echo "tmpfs /root tmpfs defaults 0 0" >> /etc/fstab
51echo "sysfs /sys sysfs defaults 0 0" >> /etc/fstab
52echo "proc /proc proc defaults 0 0" >> /etc/fstab
53exit
54sudo umount "${CHROOT_PATH}"
55```
56
57> Note: If you run crosvm on a testing device (e.g. Chromebook in Developer mode), another option is
58> to share the host's rootfs with the guest via virtiofs. See the
59> [virtiofs usage](./advanced_usage.md#virtiofs-as-rootfs).
60
61You can simply create a disk image as follows:
62
63```bash
64fallocate --length 4G disk.img
65mkfs.ext4 ./disk.img
66```
67
68[debootstrap]: https://wiki.debian.org/Debootstrap
69