1# Preparing Linux 2 3To boot Linux, a kernel image (`vmlinuz`) and a suitable initial RAM disk 4(`initrd.img`) need to be created. 5 6## Build the kernel 7 8The Linux kernel for the primary VM can be built using the following 9command-line: 10 11```shell 12git clone https://github.com/torvalds/linux.git 13cd linux 14ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make defconfig 15ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j24 16``` 17 18The compiled image is stored in `arch/arm64/boot/Image`. This will later be 19copied to the Hafnium RAM disk's root as `vmlinuz`. 20 21## Build the kernel Module 22 23From the Hafnium root directory, the following commands can be used to compile 24the kernel module, replacing `<kernel-path>` with the path to the kernel checked 25out in the previous section: 26 27```shell 28cd hafnium/driver/linux/ 29ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- KERNEL_PATH=<kernel-path> make 30``` 31 32The compiled module is called `hafnium.ko`, and will later be copied into the 33RAM disk for Linux. 34 35## Build Busybox 36 37To make Linux useful, it needs a shell. These following instructions will 38construct a file system for the Linux RAM disk with the BusyBox shell as the 39init process. 40 41```shell 42git clone git://busybox.net/busybox.git 43cd busybox 44ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make defconfig 45ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make menuconfig 46``` 47 48At this point you should ensure that the option `Settings > Build static binary 49(no shared libs)` is selected. Then you can proceed with the following commands: 50 51```shell 52ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j24 53ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make install 54cd _install 55mkdir proc 56mkdir sys 57mkdir -p etc/init.d 58cat <<EOF > etc/init.d/rcS 59#!bin/sh 60mount -t proc none /proc 61mount -t sysfs none /sys 62EOF 63chmod u+x etc/init.d/rcS 64grep -v tty ../examples/inittab > ./etc/inittab 65``` 66 67## Create a RAM disk for Linux 68 69At this point you can copy into the current directory additional files you may 70want in the RAM disk, for example, the kernel module built in the previous 71section. Assuming the BusyBox root directory is in the same parent directory as 72the Hafnium root directory: 73 74```shell 75cp ../../hafnium/driver/linux/hafnium.ko . 76``` 77 78Then run the following commands: 79 80```shell 81find . | cpio -o -H newc | gzip > ../initrd.img 82cd .. 83``` 84 85The resulting file is `initrd.img`. It should be copied to the Hafnium RAM 86disk's root. 87