Tuesday, May 8, 2018

Building a Minimal Busybox based Linux distro

Introduction :- This post shall help us build a minimal linux distro from kernel upwards alongwith a root file system which can be mounted as a Ramdisk and also a startup process /init which will take us from the kernel space to user space after booting is complete. I have used a debian based virtualbox VM for the build process.

1.     Download and extract the latest linux kernel.
       
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.16.7.tar.xz
tar xvf linux-4.16.7.tar.xz
cd linux-4.16.7/
(one can download and extract kernel without using wget and tar commands using gui tools )
2.    Create a config file to be used for the kernel compilation

make allnoconfig

3.    Tweak the config file created above to add or remove functionalities into the linux kernel.

make menuconfig
Enable following options :-
General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support
Executable file formats / Emulations ---> Kernel support for ELF binaries 

Exit and make sure to save the configuration 

4.    Compile the kernel

make

5.    Get Busybox : Download and statically link Busybox. 

wget https://busybox.net/downloads/busybox-1.28.3.tar.bz2

6.    What is busybox ?
BusyBox combines tiny versions of many common UNIX utilities into a single small executable. It provides replacements for most of the utilities you usually find in GNU fileutils, shellutils, etc.

7.    Why we need it ?
It will provide us with the shell / command line utils like ls , cp , ifconfig, ifup, route, etc

8.   Configure and build the busybox 
(a)  Extract the busybox download
tar xjvf busybox-1.28.3.tar.bz2

(b) Now generate a minimal .config file
cd busybox-1.28.3/ make allnoconfig

(c)   Configure busybox build options 
make menuconfig

We need to enable:
Settings ---> Support files > 2 GB
Settings ---> Support Unicode
Settings ---> Build Options ---> Build static binary (no shared libs)

.....and add  support for some executables to it. You will get most of the options to enable utilities below the Settings menu option.

Exit by saving changes to the .config file in the busybox-1.28.3 directory

(d)  finally build and install the static linked busybox binary
make
make install

9.   Builiding a root filesystem.  After compiling busybox, in the same directory we have a directory called _install, which will be used for building a root filesystem by us.

(a)     Copy the _install folder as rootfs folder 
mv _install ../rootfs

(b)  Remove the linuxrc
cd ../rootfs
rm linuxrc

(c)  We create some dirs - dev, proc, sys, and tmp. 
mkdir dev proc sys tmp

(d)  We need at least one device, which is the console 
sudo mknod dev/console c 5 1

(e)       We need to create an init script , next which will provide us a shell after the booting is complete. We will use gedit to create a file init inside the directory rootfs :-
#!/bin/ash
mount -t proc none /proc
mount -t sysfs none /sys
/bin/ash

(f)        Change the file permission of /rootfs/init as under :-
chmod +x init

(g)  Finally, we archive the initramfs with cpio and compress it with gzip:

find . | cpio -H newc -o | gzip > ../rootfs.cpio.gz 

10.  Lastly, we will put our kernel image and the root filesystem alongwith busybox into a usb thumb drive, so that we can boot a bare metal machine with it.

(a)  Make a single partition on the usb and mount it on your debian system :-

mkfs.ext3 /dev/sdb1
mount /dev/sdb1 /mnt

(b)  Install grub on the usb 

grub-install --root-directory=/mnt /dev/sdb

(c)      Copy the kernel image bzImage file and the rootfilesystem (incl busybox static binary) rootfs.cpio.gz file to the root partition of the usb i.e /mnt 

(d)     Unmount your usb and plug it into another system, which has been configured to be boot up from a usb disk.

(e)     When  you get the grub prompt enter the following :-

set root=(hd0,msdos1)
linux  /bzImage
initrd /rootfs.cpio.gz
boot

Watch your minimal busybox based linux distro come to live.....! It will give a command prompt. Enter some commands and navigate through the directory to explore your minimal linux system.

Enjoy!