Compiling the Linux kernel and creating a bootable ISO
This revision is from 2024/01/21 14:48. You can Restore it.
Guides to compile the Linux kernel from source, create the file system from BusyBox and then run it using QEMU. But, Create a bootable ISO of it and boot it on a computer.
The Linux Kernel
At first, compile the Linux kernel, https://kernel.org/ and grab the latest Linux kernel.
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.7.1.tar.xz tar xf linux-6.7.1.tar.xz cd linux-6.7.1.tar.xz
General preparation environment to compile the kernel...
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
To configure the kernel:
make defconfig
This will create the default config file for compiling Linux. Then run
make menuconfig
to enter a TUI to edit the config file. One thing for older devices is unchecking 64 bit kernel. Use make -j $(nproc) to compile using all the CPU cores.
# Example: Using 4 parallel jobs make -j4
After compiling, you should get the arch/x86/boot/bzImage . If not, check the make file log.
BusyBox
Next use BusyBox to create a minimal file system. Get the source code and extract it.
wget https://busybox.net/downloads/busybox-1.36.1.tar.bz2 tar xf busybox-1.36.1.tar.bz2 cd busybox-1.36.1
Run...
make defconfig
to make a default configuration for the BusyBox. Then...
make menuconfig
to bring the TUI and edit the configs. The option which you must edit is located in Settings, and then Build static binary (no shared libs). The reason to enable this option is we do not want to compile Glibc into the Linux distro.
Use..
make -j $(nproc)
to compile BusyBox. To check if the compiled file is fine, use command...
file busybox
The file must be statically linked.
Creating The File System
Create the file system which contains BusyBox. Run make install . This will create a folder called _install and change directory to _install and you will see a hierarchy like Linux file system.
In this directory, run the following command to create the folders needed for kernel.
mkdir dev proc sys
Now create a file called init and open it with a text editor. Copy and paste the following data to it:
#!/bin/sh mount -t devtmpfs none /dev mount -t proc none /proc mount -t sysfs none /sys echo "Welcome to my Linux!" exec /bin/sh
Then make the script executable with...
chmod +x init
Done. BusyBox made one executable which is capable of providing us a lot of Linux utilities such as sh , echo , vi and so on. With make install we made a filesystem hierarchy which contains these programs as links to BusyBox executable. Next, we make a shell script called init. This script will be ran after kernel loads. At first, it mounts dev, proc and sys directories. It then prints a welcome message and runs sh to open a shell.
You can put ANY executable as init file as long as it is statically linked. You might also want to mount dev, sys and proc directories when the program starts using mount.h header. Last thing we need to do is to create the filesystem itself... To do so, run these commands inside _install directory.
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.cpio.gz
This will make the file initramfs.cpio.gz in upper directory.