Pages

Wednesday, 26 April 2017

Create simple rootfs from scratch

Hey There!

In this post, we will learn to create a bare minimum rootfs from scratch to get our development board up and running.

rootfs is a type of file system which contains all the necessary files needed to run the development board. This link gives details of different directories in a root file system.

There are various tools available(for eg. Buildroot) which can create rootfs and system image very easily. But in this post we will create rootfs without using any tool. Following steps will create rootfs for BeagleBone Black but they are mostly applicable (with some minor changes) for other development boards as well.

The first step towards creating a rootfs is to cross compile a Linux Kernel. Please read this post to cross compile Linux kernel.

After cross compiling kernel, we will cross compile BusyBox. BusyBox is a complete environment for embeddded linux. It contains almost all the utilities (with limited capabilities) required for embedded linux. BusyBox can be downloaded from here.

$ tar -xvf busybox-1.26.2.tar.bz2
$ cd busybox-1.26.2/
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig //select busybox configurations
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- CONFIG_PREFIX=<path to rootfs dir> install

Now we create core device files in newly created rootfs
$ cd <path to rootfs dir>
$ mkdir dev
$ sudo mknod dev/console c 5 1
$ sudo mknod dev/null c 1 3
After creation of core device files, we create and populate some other important files
$ mkdir etc
$ cat >> etc/inittab
::sysinit:/bin/mount -t proc proc /proc
::sysinit:/bin/hostname -F /etc/hostname
::respawn:/sbin/getty -L ttyS0 115200 vt100
null::respawn:/sbin/syslogd -n -m 0
null::respawn:/sbin/klogd -n
null::shutdown:/usr/bin/killall klogd
null::shutdown:/usr/bin/killall syslogd
null::shutdown:/bin/umount -a -r
^D

The first process to be executed after kernel completes boot process is init. init process has responsibilities to setup system configurations and start applications, shutdown system applications and serve as parent process of all child processes whose parent has exited. Default init implementation reads its instructions from /etc/inittab. The format of instructions in /etc/inittab is id:runlevel:action:command
Default init resides in sbin/init, so we also need to check and fulfill dependencies of init.

#following command lists the dependencies of init
$ readelf -d sbin/init |grep NEEDED
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 0x00000001 (NEEDED)                     Shared library: [ld-linux.so.3]

We got 3 dependencies of init, i.e., libm.so.6, libc.so.6 and ld-linux.so.3. These dependencies are available in toolchain. I installed toolchain from apt-get, so these libraries are available at /usr/arm-linux-gnueabi/lib. Please copy these 3 libraries and their dependencies to lib/ directory of newly created rootfs.

#create mountpoint for proc
$ mkdir proc

#create a root user with an empty password
$ mkdir root
$ cat >> etc/passwd
root:x:0:0:root:/root:/bin/sh
^D
$ cat >> etc/shadow
root::10933:0:99999:7:::
^D
#designate a hostname for your system
$ cat >> etc/hostname
pcslinux
^D

#Install a basic udhcpc script
$ mkdir -p usr/share/udhcpc
$ cp ../busybox-1.26.2//examples/udhcp/simple.script usr/share/udhcpc/default.script

Our rootfs is ready for execution now. To use this rootfs, we need to put this rootfs along with kernel image, dtb file and other files on secondary memory. Secondary memory can be SD card, flash or NFS.
Steps to copy files and boot Linux from NFS are available here.

I hope this post was helpful. Please comment below with your queries, questions and feedback.

No comments:

Post a Comment

Booting Linux from NFS

Hi There! This post is about booting your linux from Network File System. Network File System means that the uImage, rootfs and ot...