knaxuslearn · build · ship

Projects / Embedded Linux image / Phase 3

Phase 3 of 4 · about 2.5 hours

What is inside and how it starts

You end with a clear picture of the boot, a start-up script of your own, and a kernel you changed.

Booting Linux is a chain. Each link hands over to the next. When one link breaks, the symptoms depend on which one.

Each link hands over to the next. Know which link failed and you know where to look.

Step 3.1: Read the boot log (about 30 minutes)

Boot the image and log in. The kernel keeps its messages in a buffer:

dmesg | head -40
dmesg | grep -i -E "command line|mounted root|Run /sbin/init"
cat /proc/cmdline

You should find three messages: the command line the kernel was given, a line saying the root filesystem was mounted, and a line saying it runs /sbin/init as the first program. The command line comes from QEMU's -append option and tells the kernel where its root filesystem is, something like root=/dev/vda. /dev/vda is the virtual disk that holds rootfs.ext*.

Look at what is mounted:

mount
cat /proc/mounts | head

The root is the disk image. proc, sysfs and a few others are virtual filesystems that the kernel makes up. /proc and /sys are not files on a disk. They are windows into the running kernel.

Step 3.2: Init and the start scripts (about 25 minutes)

The first program the kernel starts is called init. It has process number 1. In this image init is part of BusyBox, a single program that contains small versions of hundreds of Linux commands. Check:

ls -l /bin/ls /sbin/init
busybox --list | wc -l
cat /etc/inittab
ls /etc/init.d

ls and init are both links to one file, busybox. That is why the image is small. /etc/inittab tells init what to do: mount the basic filesystems, run rcS, start a login prompt on the console, and what to do on shutdown. Read the lines and guess what each does. Your file may differ in small ways.

rcS runs every script in /etc/init.d whose name starts with S, in name order, each with the argument start. At shutdown it runs them with stop in reverse. A number in the name sets the order. S01 runs before S50.

Step 3.3: A start script of your own (about 40 minutes)

We add a service to the overlay. It does little, but it follows the rules every real one follows. Create /work/overlay/etc/init.d/S50knaxus:

#!/bin/sh
case "$1" in
  start)
    logger -t knaxus "service started"
    ;;
  stop)
    logger -t knaxus "service stopped"
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

Make it executable with chmod +x. Run make, boot, log in, and look for its message:

grep knaxus /var/log/messages
/etc/init.d/S50knaxus stop
grep knaxus /var/log/messages

You should see one line from the boot and, after the manual stop, a second. logger hands a message to the system log, and BusyBox's log daemon writes it to /var/log/messages. If the first grep finds nothing, check that the script is executable. A start script that is not executable is skipped, and nothing says so.

Think it through: why does the script live at S50 and not S00? What would break if it ran before the log daemon started? Look at the names in /etc/init.d to see where the log daemon sits.

Step 3.4: BusyBox and the kernel (about 35 minutes)

BusyBox and the kernel have their own settings, with their own menus:

make busybox-menuconfig
make linux-menuconfig

Open the BusyBox menu and browse. Each applet, such as ls, ping or wget, is a switch. A smaller system turns off what it does not need. Leave without saving.

Now a real kernel change. In make linux-menuconfig, go to General setup and open Local version - append to kernel release. Set it to -knaxus. Save and exit. A change in the kernel menu does not rebuild by itself. Tell Buildroot:

make linux-rebuild
make

The kernel rebuild takes a few minutes. Boot, log in and run uname -r. The release now ends in -knaxus.

Pitfall: linux-menuconfig edits a file inside output/build. It is gone after make clean. Anything you want to keep must be saved into a file that you control. In Phase 4 we replace this edit with a small file that survives.

Step 3.5: Measure what you built (about 20 minutes)

Embedded work is about budgets. On your build machine, list what is big:

ls -lh output/images
du -sh output/target/usr output/target/lib output/target/bin

Then, inside the guest:

df -h /
free -m
ps

Write down four numbers: the size of the root filesystem image, the used space on /, the free memory, and the number of processes. A real product has a budget for each: flash size, memory, how fast it must boot. Without the numbers you cannot tell whether a change made things better.

Real Buildroot: a product with a few megabytes of flash often turns off most BusyBox applets, drops the C library features it does not use, and picks a smaller library such as musl. Buildroot can do all of that from the same menus.

Phase 3 done

You can read a boot log and name each link in the chain. You wrote a start script, changed a BusyBox and kernel setting, and measured the result. One problem remains: some of our work lives in places that disappear when the build is cleaned. Phase 4 puts everything in one project folder.