Building Busybox to get root file system to Linux kernel

Previously, we have built linux kernel, Qemu and booted them using qemu.

But, in previous attempts, we loaded a simple hello world program in the kernel.

Now, we can try to create a further more improved linux system.

For that, we need to get a Root FileSystem.

Here is a explanation on what is a Root File System.

So, we need a set of utilities according to our need to run using the linux kernel.

To create all these utilities, it will take certain effort. To make it simple, we can use a project called,

BusyBox – The Swiss Army Knife of Embedded Linux

Busybox, is a set of utilities, compiled into a single executable and shown as multiple executable’s are available, by creating soft links with different names.

TODO : Here is a example of how it can be achieved.

Creating Busybox based Root FS.

Here is an overview of what we are going to do.

  1. Download the busybox source.
  2. Configure
  3. Build
  4. Create Root File System image
  5. Load using Qemu.

Here, its detailed.

1) Download the busybox source.

Busybox source can be downloaded from busybox FTP site, here.

mkdir src;
cd src;
wget -c http://www.busybox.net/downloads/busybox-1.21.0.tar.bz2
tar -xf busybox-1.21.0.tar.bz2
cd busybox-1.21.0;

2) Configure busybox

BusyBox has menu based configuration. Any ways, to make it simpler to configure, busybox provides a set of configurations, as below.

make target Description
help Show the complete list of make options
defconfig Enable a default (generic) configuration
allnoconfig Disable all applications (empty configuration)
allyesconfig Enable all applications (complete configuration)
allbareconfig Enable all applications, but no subfeatures
config Text-based configurator
menuconfig N-curses (menu-based) configurator
all Build the BusyBox binary and documentation (./docs)
install Build and make it ready to install at INSTALL PREFIX directory (./_install)
busybox Build the BusyBox binary
clean Clean the source tree
distclean Completely clean the source tree
sizes Emit the text/data sizes of the enabled applications

Out of these, we are going to use, defconfig.

export ARCH=arm;
export CROSS_COMPILE=arm-none-linux-gnueabi-;
make defconfig;

Now, we need to customize this busybox, so that we can run it along with linux kernel as individual.

Build busybox as a static library, as we wont have libc in the linux kernel we are booting.

This can be done by following.

make menuconfig
# Enable   Busybox Settings--->Build Options---> [*] Build BusyBox as a static binary (no shared libs)

3) Build busybox

Now, we can build the busybox, using make install command

make -j4 install

Now,after build, we would have got the busybox install directory at busyboz-1.21.0/_install/

[Update] Note : If the build fails with error as below, Source

networking/lib.a(inetd.o): In function `register_rpc':
inetd.c:(.text.register_rpc+0x2c): undefined reference to `pmap_unset'
inetd.c:(.text.register_rpc+0x42): undefined reference to `pmap_set'
networking/lib.a(inetd.o): In function `prepare_socket_fd':
inetd.c:(.text.prepare_socket_fd+0x52): undefined reference to
`bindresvport'
collect2: error: ld returned 1 exit status
make: *** [busybox_unstripped] Error 1

Disable RPC support as below

Via menuconfig:
 Networking Utilities  --->
   []   Support RPC services

4) Create Root Filesystem image.

[TODO] As we have seen, initramfs supports newc file format. We are going to create a cpio archive to hold the root filesystem.

$ mkdir ../init/
$ cd _install;
$ find . | cpio -o --format=newc > ../../init/rootfs.img
$ cd ../../init;
$ file rootfs.img
   ASCII cpio archive (SVR4 with no CRC)
$ cd ../

5) Booting using initramfs we have built.

Now, we have the kernel built previously, rootfs that we have built now.

Lets boot it using qemu

$ qemu-system-arm -M versatilepb -kernel zImage -initrd init/rootfs.img -append "root=/dev/ram rdinit=/bin/sh"

The system would have booted and the shell prompt would be shown.
You can try ls on this to find something like this..

/ # ls
bin    dev    linuxrc    root    sbin    usr
/ #

Voila!! Very basic utility is working!!

Advertisements

Author: Shingu

Search in pursuit. Help me if you can.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s