Embedded Systems

Build Own OS for Raspberry or Custom OS According To Requirement

Hello all of you. I think you all must be doing fine and waiting for our new post. So here I am with something new for you people.

Today we’ll learn how simply we can get a mini Linux OS for any platform required.So before proceeding further, you must have some knowledge about what are the basic requirements for an OS to boot.

The very first thing which gets loaded into memory (RAM) after we power on the system, is our boot loader.Boot loader is a small program that is loaded in the RAM and then initiates the call to the KERNEL which is the base of any operating system.Here is the booting sequence of Raspberry-pi:-After the call to the kernel is made, all the necessary modules are loaded into the memory (i.e. loading of kernel we can say). All other modules that are not really necessary, called as loadable modules are situated in the ” /lib/modules “ directory. Loadable modules are loaded only whenever they are required(Eg. If you plugged in a serial device via USB, the “usbserial.ko” module will be loaded).

And after the kernel is fully loaded,it calls “/sbin/init “or “/bin/init” program to load a shell, which further calls ” /etc/init.d/rcS ” file and all the script written in the file is executed on the shell.

So here is your freshly booted Linux system. But you may be wondering from where all this “/sbin/init” , ” /etc/init.d/rcS” files came ?

Let me clear you this, that all these files will be residing in the second or any other partition of your disk which kernel will call to mount on the / or root of the Linux file system.

So its all about the prerequisites,that you must know about. Proceeding further, we will now do whatever I told you. So get ready.

First thing is how you’ll boot. Either with an SD card or via flash memory mounted on your PI.

Mount that flash/ SD card on another working Linux system.

Then type in command: sudo fdisk -l”

Type in your password if prompted, and you’ll see a list of disks and their partitions attached to your system (also your internal hard drive).

Find out your disk whether it is sdb(most probably) or sdc or something else(you may take help with the total size of the disk displayed, to identify your disk into which you are going to install the custom OS).

Now you have to format the disk and make at least two partitions as shown in above image [can also work with just one but not recommended].

Size of first partition must be greater than 30 MB at least which will be your BOOT partition and must be formatted with a FAT file system so as first stage boot loader can recognize the files that are into it and can read all of the files residing into that partition.

Rest of the size you can put into the second partition one or make two of it and format it using ext4 file system(Linux recommended).

Now how to format?

This you can do easily with either command line or using Gparted disk utility tool.

For beginners, I will recommend to use Gparted to create partitions of the disk.

Run Gparted using command: “sudo gparted”

Create the partitions of the drive (as discussed above) where you want to install your custom OS.

(*Do not forget to select the disk on which your are going to perform partitioning. Otherwise you may damage your running PC.)

After you’ve done partitioning, copy all the boot files from the given raspberry pi git repository.(Only the boot directory Contents.)

You may clone this git repository or directly download from browser.

After the above steps, you would be able to boot up your raspberry pi using that SD card. It will boot up, will load the kernel in ram , but will not show up anything else or may show ‘no init found. Try giving option init=path/to/init’ at the end.

Now all you need to do is to give some init program to your kernel so that it can invoke a shell to you where you can perform all of your tasks.

So just again mount your SD card to your working Linux PC and open up the second partition that is formatted with ext4 file system and create the file hierarchy as you see in almost all Linux /bin, /boot, /dev, /etc, /mnt, /proc, etc..

But what about the contents that resides inside those directories?

Some of them you have to create by yourself, like the basic utility commands eg, ls, grep, vi, etc… and some of the content, kernel will provide you, like everything inside /proc directory.

So create these directories first in your second partition.

You may use command: mkdir bin boot dev proc etc mnt var lib

Now you must have heard about busy-box. It is a software that provides us Unix-commands in just a single executable file.

So this means that we can use busy-box as our Linux utility commands like ls, ln, mkdir, mknod, init, etc… and its very simple. Just by making links by the name you want to use it ,(shortcut files in windows are referred to as links in Linux), so compile the busy-box for your required architecture and install it to your custom path to get the executable ‘busy-box‘.

Simple steps to compile* busy-box are: make && make install

If you cross compiling busy-box for arm , then use required prefix such as : “ARCH=arm CROS_COMPILE=arm-linux-gnueabi-”

For installation to custom path, you’ll have to use: “make CONFIG_PREFIX=/path/to/dir/ install” instead of “make install”

*Tip: If you are unable to compile latest versions of busy-box, try compiling the older versions say 1.20. Also all the compiled executables are placed in some directory where you have compiled. So you can simply use ‘find‘ command to locate the folder in which all the executables are placed and simply copy it to bin directory.

After the executable is created, you have to create link using command: ln -s ./busybox ls for ls command.

Before that, just copy the busy-box executable to another empty directory so that u may not get confused.

And similarly for each command that is supported by busy-box.

You can get a list of all supported commands using ./busybox –help

Now all the basic commands have been created.

You have to copy all the links and the executable ‘busy-box‘ to the “/bin” directory of the second partition.

So now your dummy system is almost ready now.

Just keep in mind which partition you are mounting as root, which is given in cmdline.txt file located at the boot partition, given as: root=/dev/mmcblk0p2

Now its all set. Try to boot your raspberry pi from the same SD card in which you performed all above operations.

If you get the line saying : “Press enter to activate this console…”

you did it correctly.

Otherwise you must be stuck somewhere in between. Let me know where ever problem arises.

Now how to add devices to your OS and a few more things about startup apps will be discussed in next Post..!!

So stay right here for the next post.