Building custom filesystem for your embedded board (PART 2)

Rhythm Chopra
5 min readOct 19, 2018

--

This is the second part of series, Libretech tritium Board Bringup.
Part 1 available here.

In PART 1 , we build bootloader and kernel from source and after booting, ended up with a kernel panic.
Now let’s continue and resolve that kernel panic by creating the filesystem.

I have planned to build Kali Filesystem, but I think, other linux distros like ubuntu etc. can also be build the same way, with some tweaks.

So, if your host system is already Kali, then you might not need these steps, but for other system like ubuntu to build Kali Filesystem, just follow following dependencies.

Add kali repository to apt sources, requires sudo privilege

sudo su
echo “deb http://http.kali.org/kali kali-rolling main non-free contrib” >> /etc/apt/sources.list
echo “deb-src http://http.kali.org/kali kali-rolling main non-free contrib” >> /etc/apt/sources.list

Install kali gpg-signatures for sources added, requires sudo privilege

sudo su
wget -q -O — https://archive.kali.org/archive-key.asc | sudo apt-key add

Install following packages required to build filesystem

sudo apt install kali-archive-keyring debootstrap qemu-user-static 

Add kali-rolling to debootstrap scripts, requires sudo privilege

cd ‘/usr/share/debootstrap/scripts/’
sudo su
(echo “default_mirror http://http.kali.org/kali"; sed -e “s/debian-archive-keyring.gpg/kali-archive-keyring.gpg/g” sid) > kali
ln -sf kali kali-rolling
cd -

Now, let’s get started with building filesystem

Run debootstrap for initial setup

sudo debootstrap — foreign — keyring=/usr/share/keyrings/kali-archive-keyring.gpg — include=kali-archive-keyring — arch armhf kali-rolling kali-rolling http://http.kali.org/kali

If http://http.kali.org/kali, fails to respond you can try changing the mirror.
Now sit back and wait till it finish…

After it finishes, copy qemu-arm-static to new filesystem directory and run second stage of debootstrap

cp /usr/bin/qemu-arm-static kali-rolling/usr/bin/
sudo LANG=C systemd-nspawn -M kali_machine -D kali-rolling/ /debootstrap/debootstrap — second-stage

Again sit back and wait, after this you will have the complete filesystem build.

Now that you have the new filesystem ready on your host computer, you still haven’t installed any core packages you might require on boot, ssh and network interfaces being the most common. So lets just complete this.
To install default packages, we will use systemd-nspawn to access the newly created filesystem on host machine only.
Binding host machine resolv.conf will prevent any DNS resolution issues.

sudo LANG=C systemd-nspawn -M kali_machine — bind /etc/resolv.conf:/etc/resolv.conf -D kali-rolling/ /bin/bash

First thing first, change root password to whatever you want

echo “root:toor” | chpasswd

Update sources.list and do apt upgrade

echo “deb http://http.kali.org/kali kali-rolling main non-free contrib” >> /etc/apt/sources.list
echo “deb-src http://http.kali.org/kali kali-rolling main non-free contrib” >> /etc/apt/sources.list
apt-get update
apt-get upgrade -y

Install basic linux packages

apt-get install -y ssh console-common binutils locales git man-db lshw wpasupplicant
apt-get install -y kali-defaults e2fsprogs usbutils firmware-linux-free

Some kali-specific packages, that might be of interest

apt install -y aircrack-ng ethtool hydra libnfc-bin mfoc nmap passing-the-hash sqlmap winexe wireshark

Enable ssh start on boot and root login over ssh

systemctl enable ssh
sed -i -e ‘s/^#PermitRootLogin.*/PermitRootLogin yes/’ /etc/ssh/sshd_config

Now finally, just change the `hostname` for the system and update `network interfaces` and the file system would be ready

If files doesn’t exist, create them.

 echo “kali-rolling” > /etc/hostname cat << EOF > /etc/network/interfaces
# Local loopback
auto lo
iface lo inet loopback
# Wired adapter #1
allow-hotplug eth0
no-auto-down eth0
iface eth0 inet dhcp
EOF
echo “nameserver 8.8.8.8” >/etc/resolv.conf
exit

Now, we need to visit kernel again, to push kernel image and corresponding kernel modules into the filesystem.

Go to linux kernel directory and run following commands
If you don’t have kernel image already build, follow these steps otherwise you can skip these.

make ARCH=arm sunxi_defconfig
make -j 8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs

Now apart from kernel image and device tree, we have to build kernel modules also. Update INSTALL_MOD_PATH accordingly

make -j 8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules
make -j 8 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=/path/to/kali-rolling/ modules_install

Now simply copy kernel image and device tree, update paths accordingly

cp arch/arm/boot/zImage /path/to/kali-rolling/boot/
cp arch/arm/boot/dts/sun8i-h2-plus-libretech-all-h3-cc.dtb /path/to/kali-rolling/boot/device_tree.dtb

Now we will create a system image of created file-system and flash bootloader in it

Create a file of enough size

dd if=/dev/zero of=kali-rolling.img bs=1M count=2000 status=progress

Create partition table

fdisk ${hostname}.img << EOF
n
p
1
2048
a
p
w
EOF

Attach a loop-device to image so that it appears as a block device to the host filesystem

loop_device=`losetup -f — show ${hostname}.img`
partprobe ${loop_device}

Create `ext4` filesystem

mkfs.ext4 ${loop_device}p1

Flash bootloader to image

If you don’t have it already build, follow these steps, otherwise you can skip these.
Go to bootloader directory and run following commands, with your board configuration

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- libretech_all_h3_cc_h2_plus_defconfig
#this was only required for my board.
sed -i ‘s/CONFIG_ENV_FAT_DEVICE_AND_PART=.*/CONFIG_ENV_FAT_DEVICE_AND_PART=”0:auto”/g’ .config
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

Flash bootloader into image file

sudo dd if=u-boot-sunxi-with-spl.bin of=${loop_device} bs=1024 seek=8 status=progress
cd -

For sure you don’t want to enter a set of command into u-boot terminal for booting up the system. So in directory with image file, just create the following boot.cmd file into the target filesystem, and it’ll be executed as soon as you power it on, sudo permissions required

sudo sucat << ‘EOF’ > kali-rolling/boot/boot.cmd
setenv bootargs console=ttyS0,115200 root=/dev/mmcblk0p1 rootwait panic=10 rw rootfstype=ext4 net.ifnames=0
load mmc 0:1 $kernel_addr_r /boot/zImage
load mmc 0:1 $fdt_addr_r /boot/device_tree.dtb
bootz $kernel_addr_r — $fdt_addr_r
EOF
mkimage -A arm -T script -C none -d kali-rolling/boot/boot.cmd kali-rolling/boot/boot.scr

Now, mount the image file and follow the steps.

sudo mount -o loop ${loop_device}p1 /mnt
sudo rsync -HPavz -q kali-rolling/ /mnt
sudo umount /mnt

Dissociate the loop device

losetup -d ${loop_device}

Flash image file to sdcard

Now your image file is ready to be pushed onto sdcard and boot the board.
Flash it using following command, Change device name accordingly

sudo dd if=kali-rolling.img of=/dev/sdX status=progress

Now insert sdcard into the board and power it on.
If everything in above steps went well, it should be booting. Use minicom to see logs and login to system with credentials you added above

Now in third one, we’ll be adding new modules to kernel like usb-storage, wifi, soundcard because the kernel we build now is bare minimum and doesn’t have these enabled by default.

Part 3 is available here

Cheers.

--

--

Rhythm Chopra

Tech Enthusiast | Software Engineer | Trying out new stuff