Building the Minimal Rootfs Using Busybox

Rootfs (Root File System) is very important for starting the linux kernel image. The kernel image acts according to the roots during boot . Normally Rootfs is created and placed in the Flash memory of the device, which could be either your Android phone or your Personal computer or Root Partition in your SD card Partition. During Booting Process the kernel executes the /sbin/init which in turn looks for the inittab or init.d/rcS file in /etc directory. All the applications will reside inside this root file system.

We normally use busybox to create minimal rootfs.

How To Create a Rootfs:

As now we know that the Rootfs is very important or mandatory for starting the kernel. Now we need to create the Rootfs. The rootfs which we will create here contains only the minimum stuff’s required to boot the kernel, so we can’t expect it to behave like our distribution’s rootfs. That’s why its a minimal Rootfs. There are some mandatory directories which should be present in rootfs.

/bin , /sbin , /etc, /dev, /lib, /usr

Cross Compiling Busybox:

BusyBox is a collection of cut down versions of common UNIX utilities compiled into a single small executable. This makes BusyBox an ideal foundation for resource constrained systems.

Prerequisites:

Assuming an Ubuntu 14.04 build machine.
$ sudo apt-get install gcc-arm-linux-gnueabi
$ sudo apt-get install libncurses5-dev
$ sudo apt-get install gawk

BusyBox can be built either as a static binary with no external libraries required, or built shared libraries such as GLIBC (default). This setting can be found under BusyBox Settings -> Build Options -> Build BusyBox as a static binary (no shared libs). Here we generally choose to build BusyBox to require GLIBC as it is highly likely we will want to run additional applications that will require GLIBC sometime in the future.

Download the Busybox source from busybox.net .

wget http://busybox.net/downloads/busybox-1.24.1.tar.bz2

Extract the tar with

$ tar -xvf busybox-1.24.1.tar.bz2. 

Then, cross compile the source for ARM platform using the following commands.

$ cd busybox-1.24.1
$ sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- defconfig
$ sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig

At the menu, you can configure BusyBox options. As static or shared. After configuration just build.

$ sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
$ sudo make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- install CONFIG_PREFIX=/media/Root(path to ROOT)

After the successful completion of the above step, you can see 3 directories (bin, sbin, usr) and one file (linuxrc) created in your ROOT directory. remove linuxrc using rm. Apart from these, we need few more directories mentioned above to boot the kernel. So, move to the RFS location and create the following files and directories.

$ mkdir etc proc sys dev etc/init.d usr/lib
$ mkdir home home/root

Cross Compiling GLIBC:

Download, build and install GLIBC:

$ wget http://ftp.gnu.org/gnu/libc/glibc-2.22.tar.xz
$ tar -xJf glibc-2.22.tar.xz
$ mkdir glibc-build cd glibc-build/
$ ../glibc-2.22/configure arm-linux-gnueabi –target=arm-linux-gnueabi –build=i686-pc-linux-gnu –prefix= –enable-add-ons
$ make
$ make install install_root=/home/export/rootfs

Some programs may require libgcc_s.so, otherwise you will receive an error:

cp /usr/lib/gcc-cross/arm-linux-gnueabi/4.7.3/libgcc_s.so.1  /media/export/rootfs/lib 

Now we must mount the /proc & /sys filesystem and populate the /dev nodes. This can be done at runtime by creating a file called etc/init.d/rcS and adding:

$ vi etc/init.d/rcS

#!bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
mount -t tmpfs none /var
mount -t tmpfs none /dev
echo /sbin/mdev > /proc/sys/kernel/hotplug
/sbin/mdev -s
mkdir /var/log

$ chmod +x etc/init.d/rcS  

That’s it…

You have successfully created one Custom RFS using Busybox for ARM. Just insert the SD card into Board’s slot and hold the Boot switch while powering up. This will boot your linux kernel using Custom built RFS.

Please Leave a Detailed Comment if any issue is there.

Leave a comment