How To Build Custom Lightweight Linux Distribution Based on Buildroot, For Embedded Application

    This this article i will be showing you how create a lightweight linux distribution, From Uboot to linux kernel till root file system . in short a complete Distribution which you can use in you application

     We will be working on Ubuntu x86_64 you will need minimum 20GB of free space for source and other packages. As beaglebone is ARM system and we are on a intel x86_64 machine we need to use cross compiler. which will on ubuntu machine and produce result for ARM machine.


you may need to install git (if not already installed)

Getting Cross compiler.

There is a linaro arm cross compiler present in Ubuntu application repository.

you can install it with

sudo apt-get install gcc-arm-linux-gnueabihf

or 

you can just download cross compiler linaro website

wget  https://releases.linaro.org/components/toolchain/binaries/5.3-2016.05/arm-linux-gnueabihf/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabihf.tar.xz

extract with
tar xvf gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabihf.tar.xz

export CC=`pwd`/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-


Cross Compile Uboot 

download uboot source

git clone https://github.com/u-boot/u-boot
cd u-boot/
git checkout v2016.09-rc2 -b tmp

wget -c https://rcn-ee.com/repos/git/u-boot-patches/v2016.09-rc2/0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch

patch -p1 < 0001-am335x_evm-uEnv.txt-bootz-n-fixes.patch



you may need to install device tree compiler
before compiler 
apt-get install device-tree-compiler


make ARCH=arm CROSS_COMPILE=${CC} distclean
make ARCH=arm CROSS_COMPILE=${CC} am335x_evm_defconfig
make ARCH=arm CROSS_COMPILE=${CC}

after build finish there will be file named "u-boot" will be present in directory. this is the file of our interest. we will copy this into correct location into our sd card later on.


Cross Compiler Kernel 

you may need to install few package before you can build
sudo apt-get install lzma lzop
git clone https://github.com/RobertCNelson/bb-kernel
cd bb-kernel/

checkout currently stable latest branch

git checkout origin/am33x-v4.6 -b tmp

start a script which will automatically download required cross compiler, linux source other required stuff.
set git email id and name before executing this script,

git config --global user.email "you@yourdomain.com"
git config --global user.name "your name"

./build_kernel.sh


it may take a while for downloading and building,after a little while it will show you kernel configuration screen ,to chose what you will like to include into kernel build. just exit if you are not sure about it , default configuration is already set.

If build successful then <kernel version>.zImage , kernel modules as <kernel version>.tar.gz  , firmware and device tree tar file will be present in deploy directory.

we need all of this later on.

Making File system with Buildroot

Firt of download buildroot. Latest available https://buildroot.org/download.html  

wget https://buildroot.org/downloads/buildroot-2016.08.tar.bz2
tar xvjf buildroot-2016.08.tar.bz2

apply beaglebone default configuration

make beaglebone_defconfig

you have to run menuconfig for selecting specific options.

set
Toolchain---> Custom Kernel Headers Series (4.6.x) (same as linux version)
Toolchain---> C library ( glibc )
Toolchain---> glibc version (2.23)
Toolchain---> GCC compiler version (gcc 5.x)
Toolchain---> Enable C++ support


you can set system name , root password under
System Configuration ---> Root Password

as we have build kernel , uncheck Linux Kernel
Kernel---> Linux Kernel
Bootloaders---> uncheck U-Boot

Filesystem images---> tar the root file system


Run Make

make


after build finish , output root file system will be as tar file in output/images directory.

Preparing SD Card
you will not need very large SD card as this is embedded system and we can simply chose which package we need which we don't .
SD card even as small as 128MB will do in certain situations. i am Using 1GB card

First you need to format the sdcard, we will be creating two partition in ,
1. 64 MB , FAT32 partition , for kernel image , Device Tree and uboot. (if you want you can have it 32 MB).
2. Rest of SD card as Ext4 paration, for Rootfile system, you will need space to store you programs so size of this partition all depends on how many packages you will be having. Few hundred MB is more than enough, you can have descent working system in as low as few MBs,


we will be using fdisk command to create partition
start fdisk with apropriate disk from /dev/<yourdisk> , SD card  may come up as /dev/mmcblk or /dev/sda or /dev/sdb so on depending on howmany disk you have . remember to select only the disk not its partition like /dev/mmcblk0p1 ,  or /dev/sdb1 , p1 and number 1 indicates first partition in the drive.
( you may need to sudo with these command depending on you permissions)
you can use blkid command to get list of all the available drives.

fdisk /dev/mmcblk0          (you may be denied permission without sudo. )


after launching fdisk, you will have a new terminal , hit p to print all the partition. as shown in the image above, my disk has one Empty partition . we will delete all the partition present will d for each partition.

now wen you hit p to print all the partition list you will get nothing.
now hit n to create new partition. then p for primary , hit Enter for default partition number, hit Enter one more time for default 2048 First sector, now Last sector need to be calculated according to how big you boot partition to be , we need 64MB so  , ((64*1024*1024(byte))/512 (sector size))+2048 (first sector) == 133120

it will create a simple 64 MB linux partition , we need to change it's type to FAT32, for that hit t , for FAT32 partition type hit c .(you can view whole list of supported partition by L)

now if you hit p you will our partition which we just created.

now we to create a new partition for root file system this partition will be simple linux partition.

hit n  , p for primary , 2 for second partition , hit Enter for default fist sector , hit Enter for default End partition it will take up whole SD card .
you can always See Partition list with p. 
set boot flag to fist partition with a , and then 1 for partition number.

in then end hit w to write the partition info to SD card and exit.


now you have to partition , but they are not formatted.
you need to run two more commands to format.

mkfs.vfat /dev/mmcblk0p1 -n BOOT   ( you may need to change "mmcblk0p1" to your sd cards  fat32 partition  , BOOT is label )

mkfs.ext4 /dev/mmcblk0p2 -L root ( you may need to change "mmcblk0p2" to your sd cards linux partition)



now you have nicely formatted sd card ready to for copying , uboot , kernel and file system.


Comments