Building a custom Linux OS for RaspberryPi using buildroot

Building a custom Linux OS for RaspberryPi using buildroot

I have been playing with my Raspberry pi for last couple of weeks. This weekend I thought I will build a custom Linux based OS and boot my Pi using it.

Lets look at the key parts of booting process:

  • A Bootloader
  • Device Tree Blob (dtb)
  • A Linux Kernel
  • A Root File system / Networking / Device Drivers
  • Init Process

Bootloader of raspberry pi is a completely proprietary software. It is available as a blob (binary package). I have explained the structure of /boot in my previous articles. But for quick reference please check this link:

https://www.raspberrypi.org/documentation/configuration/boot_folder.md

Boot process follows like this:

Bootloader runs and loads kernel to the RAM. Kernel then initialize the hardware devices and creates and mounts the root files system. Final step is to get the init program to run and starts the user level programs.

Okay! Does that sound so simple? Ohh noooo its a lot process. If you are thinking of downloading each individual software components, building and combining it manually to generate an image file? Its a lot of work! You will most probably end up with something which can't be even useful. So what are the alternatives?

There are softwares which takes the configuration files from the user and builds custom embedded OS.

What are they?

Yocto, Buildroot and FreeWRT are couple good examples.

I am using buildroot this time. But I promise I will build Linux from scratch in the near future. (https://www.linuxfromscratch.org)

Buildroot is a simple and easy to use tool for building you own custom Linux OS. It combines several makefiles to generate a single bootable image file.

Note: I am not covering the topic how buildroot works. I think that can be another article in the future.

Download buildroot from here:

https://buildroot.org/download.html

Buildroot uses configure files to get the information regarding the target hardware , required Linux kernel, software packages and host details.

Download and extract the buildroot source to a folder

wget https://buildroot.org/downloads/buildroot-2019.02.1.tar.gz

tar xvf buildroot-2019.02.1.tar.gz

cd buildroot-2019.02.1 

Before we proceed with building the system, Lets look at the CPU configuration of Raspberry Pi 2. For getting this information I used lscpu command on my currently running RPi. It will show up as follows.

No alt text provided for this image

This will be really useful for creating the configuration file.

buildroot supports following configuration options

  • make menuconfig - for the original curses-based configurator,
  • make nconfig - new curses-based configurator
  • make xconfig - for the Qt-based configurator
  • make gconfig - for the GTK-based configurator

When you run this command, It will show all the configurable part of buildroot system. You can manually configure all the possible options here.

To make the build processes simple and fast I have used the pre-existing config for Raspberry pi. This will be found in configs directory inside the buildroot directory. There are plenty of boards which has off the shelf buildroot support.

By make-ing this configuration file. buildroot will generate and basic Linux based OS image in /output/images.

Main components of this OS are busybox, networking layer and a root file system. One tty console is created on uart1 of raspberry pi. Using this we can login and use the system.

Okay start the build process.

make raspberrypi2_defconfig
No alt text provided for this image

This building process will take several minutes depending upon your host PC.

Once the build process is done, Check the output/images/ for an img.

Write the image into a MicroSD-card using dd command.

#unmount your sdcard first
sudo umount /dev/sdb1

#write img file to microsd card
sudo dd if=output/images/rpi3-sdcard.img of=/dev/sdb bs=1M

Plug-in the micro SD card to the RPi. Connect the USB to Serial adapter to the RPi as follows. Plug-in the USB to your host PC.

No alt text provided for this image

Power up the RPi. And type the following commands in your host PC. To check which tty port is used by USB, Use dmesg command. Now you can use any serial console monitor to login to your new Linux based OS. I am using screen for this purpose.

screen /dev/ttyUSB0 115200

Bingo!

No alt text provided for this image

Buildroot greets me with this screen message. Type root and press enter. Try some favorite commands. I tried ps -All

No alt text provided for this image

As you can see the first process displayed here is the init. This is a binary package provided by busybox.

Lets look at the root file system and the network interfaces..

No alt text provided for this image

The DHCP client is up and running ,I tried pinging it from my host PC and it worked!

No alt text provided for this image

Take a look the /bin and /user/bin directory.

No alt text provided for this image
No alt text provided for this image

All these bin files are provided by busyBox tool, If you try ls -ll command you will realize these bin files are symlinks (symbolic links) to busybox.

No alt text provided for this image

Now its the time to explore. :)

要查看或添加评论,请登录

Aswin Venu的更多文章

社区洞察

其他会员也浏览了