Tuesday 19 December 2017

Create a rootfs with Debian debootstrap

I wanted to create a custom rootfs instead of using the RCN debian rootfs image. Following are the steps:

We create the first stage of rootfs as follows:


sudo debootstrap --arch=armhf --foreign stretch <target_dir>


This would start downloading the initial bootstrap packages for a successful chroot.

Next I do the following to start off the second stage


1
2
3
sudo chroot <target_dir>
export LANG=C
/debootstrap/debootstrap --second-stage

Next I modify the sources.list file as per my Debian distro from https://debgen.simplylinux.ch/

I do the following to update and configure packages:

1
2
3
4
apt update
apt-get install locales dialog
dpkg-reconfigure locales
apt install openssh-server ntpdate

Next I run passwd to change my root password.

I then do the following to move the files to the target partition from which Linux uses as its root file system

1
2
cd target_dir
sudo cp -rpv . /mnt/devmnt

Please note that I use the "p" flag to preserve the permissions.

This concludes the basic rootfs. There are still things missing like networking which I will experiment and further add to this post.

For network connection setup please edit the interfaces file as provided in the Debian network configuration wiki.

Sample of my network configuration file:


1
2
3
4
5
6
7
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto eth0
allow-hotplug eth0
iface eth0 inet dhcp

I had to install ifupdown2 packages so that there are no errors in the systemd scripts. It uses a ifquery which was not present in the rootfs as the package it was present i.e. ifupdown2 was missing.

Also I installed the udhcpd package for DHCP. I am not sure how much it helps but if there is a problem with the interface getting an IP via DHCP this might help.

Apart from that I created a normal user with sudo access. Creation is as simple as "adduser <username>" and adding the user to sudo group as "adduser <username> sudo"

When chrooting to the directory it might start complaining about pts. We can mount the pts psuedo filesystem as follows:


1
mount --bind /dev/pts <rootfs directory>/dev/pts

For further references we can see the excellent debian wiki at https://wiki.debian.org/chroot

We will have to install the modules, firmware and headers installation to the rootfs. We can create a temporary directory to verify if all is ok and then copy it to our root filesystem.

We do the modules,firmware and headers installation as follows:

1
2
3
LOADADDR=0x80008000 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make -j10 firmware_install INSTALL_FW_PATH=../firmware_path/ 
LOADADDR=0x80008000 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make -j10 modules_install INSTALL_MOD_PATH=../modules_path/
LOADADDR=0x80008000 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make -j10 headers_install INSTALL_HDR_PATH=../headers_path/

Next we copy the contents of firmware_path to <mountdir>/lib/firmware, modules_path to <mountdir>/lib/modules and headers_path to <mountdir>/usr/

No comments:

Post a Comment