Building custom Linux for Raspberry Pi using Buildroot
Last Valentine’s Day you gifted your girlfriend a project you were working on , an internet connected speaker, which enabled you to send love songs to your sweetheart whenever you felt like dedicating a song . She loved it so much that she showed it to all her friends and they told their friends and somehow the word got around and an internet magazine picked up the story and you and your project was all over the internet ! Amidst all this popularity you saw that there were many people who were ready to spend their money to buy your speaker , a light bulb went on in your head and you decided to start a company and start making these speakers .
When you were prototyping you just used Raspbian/Raspberry Pi OS and used lots of “apt get install” commands to install the packages needed to make your project work . You realize now that Raspbian has lot of unnecessary packages which are not required for your project and also some packages which are missing which are required for your project , you now wish if only there was a way you could build a Linux firmware with only the necessary packages which you could then flash on an SD card to make it all work seamlessly . Hold onto your seat! for Buildroot helps you do just that .
Buildroot is an automated build system which takes a set of requirements — the list of packages to include, kernel configuration, etc. — and creates a self-contained root filesystem , together with a freshly built kernel , boot loader, firmware and config files ready to be placed onto the /boot partition of the SD card.
So let’s not waste time and let’s get started cooking our own flavor of Linux.
Before we start we need to satisfy some prerequisites, according to the Buildroot manual , Buildroot requires certain packages to be installed before starting the build. Lets install them using Ubuntu’s package manager
sudo apt install sed make binutils gcc g++ bash patch \gzip bzip2 perl tar cpio python unzip rsync wget libncurses-dev
Now that we have satisfied all the prerequisites lets download the latest stable release of Buildroot .
mkdir -p $HOME/buildroot
cd $HOME/buildroot
wget https://buildroot.org/downloads/buildroot-2020.02.2.tar.gz
tar xvzf buildroot-2020.02.2.tar.gz
cd buildroot-2020.02.2
Buildroot is a generic build system hence in order to build Linux for Raspberry Pi we need to configure appropriately . In order to configure buildroot for our board we start the configuration utility by entering make menuconfig
.
The configuration menu is nicely arranged in submenus which allow you to configure the buildroot for your target architecture . You can go through each submenu and its options to find out more about what each option does . Lucky for us Buildroot already includes config files for some popular boards , these default config files do all the basic configuration for us . To get a list of default config files available enter make list-defconfigs
:
You can see that Buildroot includes defconfig files for all the Raspberry pi boards released until now along with a bunch of other boards . To apply configuration settings for a board just type make <defconfig name>
, for example to apply default settings for Raspberry Pi 1 you can type,
make raspberrypi_defconfig
Now that you have applied the settings , you can actually start building but just for the sake of customization lets set a custom welcome message and also set a root password . Enter make menuconfig
and then choose System Configuration
submenu and then select System Banner
option and set it to whatever message you want , since we are building the firmware for our hypothetical IOT speaker let’s set the welcome message as “Welcome to My Awesome Speaker” . Now if you look at a few options below , you can see an option to set root password, set it to the password of your choice .
We are now done with our customization - exit the configuration and choose the “save” option as you leave . Initiate a full build of the system by executing :
make all
Buildroot now starts downloading the packages and starts compiling one by one , it will probably take around 2–3 hours depending on your host system’s processing power to complete the build.
Once the build is complete , your system image is generated under output/images
folder with the name “sdcard.img” , now choose your favorite image flashing software or the dd command to flash the sdcard image to sdcard . Once the firmware is flashed insert the SDcard into your Pi and connect the ground, Tx and Rx of USB to Serial cable to the ground (Pin6), Rx(Pin 10) and Tx(Pin 8) of Raspberry Pi , as shown below .
Connect the USB end of the USB to Serial cable to your computer and open the serial port using a serial terminal software such as Gtkterm with baud rate set to 115200 . Power up the system and you should see the custom welcome message you set in the previous step , enter the username as “root” and the root password you had set previously and Voilà! you just logged into your own custom built Linux.
Won’t it be cool if we can just log in using SSH instead of using those clunky serial cables? lets include a SSH software in our custom Linux. Buildroot includes more than 3000 packages which you can choose to include in your Linux image, the available packages are listed under “package/
” folder of buildroot . In order to have SSH access to our device , we shall enable Dropbear, a small SSH server which will let us log in remotely .
Go back to your host computer’s terminal window and enter make menuconfig
, choose Target packages
submenu and enable dropbear
under Networking applications
— exit and save your configuration on your way out— enter make
command to start compilation .
Once the compilation is complete , flash the newly compiled sdcard image — insert the sdcard to your Pi and connect the Ethernet cable . Open a terminal window on your host computer and enter :
ssh root@<your Raspberry_Pi's IP address>
You should now have access to your device’s terminal via SSH.
Summary:
In this article you learnt how to build a custom Linux for Raspberry Pi using Buildroot , in this brief introduction you changed the default System welcome message and also enabled a software package which enabled you to login remotely into your system using SSH . I hope this blog helped you to gain some insight into Buildroot below are some more resources for further reading:
Bootlin’s Buildroot practical lab manual: This training document provides an hands-on approach for learning Buildroot .
Buildroot User manual : Official user manual for Buildroot .
Happy Tinkering!