Skip to content
Libre Computer
Platform

Module 8 ยท Lab 5

Linux boots

The kernel is the last thing the firmware loads. From its first line on the console to the login prompt, it takes over the hardware the earlier stages set up, then hands the system to the first user process.

Where this stage sits

The boot chain: each stage loads the nextBoot ROMfixed in the chipmodule 3First stageclocks, consolemodule 4Memory bring-uptimings, trainingmodule 5Secure monitorTF-A, stays residentmodule 6U-Bootstorage, USB, networkmodule 6UEFIstandard interfacemodule 7Linux kerneldrivers, rootmodule 8initsystemd, servicesmodule 8runs from On-chip ROMruns from On-chip SRAMruns from DRAMPower and reset come first (module 2); I/O, drivers and your project run on the finished system (modules 9 to 12).
Where this module sits in the boot chain.

Key ideas

  • The kernel starts with an early console, so it can report problems before its real serial driver is loaded.
  • It reads the device tree to learn the memory, CPUs and peripherals, then starts the other CPU cores through the secure monitor.
  • Each driver is matched to a device-tree node by its compatible string and probed. A driver whose clock, regulator or GPIO is not ready yet is deferred and tried again later.
  • An initramfs can hold what is needed to find the real root filesystem. The kernel mounts root and starts init, process 1, which on most distributions is systemd.
  • systemd starts every service, including the login prompt on the serial console. Arguments on the kernel command line (read back in /proc/cmdline) change the kernel's behaviour without rebuilding it. In a hand boot from the U-Boot prompt they come from U-Boot's bootargs variable (lab 4); in a UEFI boot the distribution's boot-loader entry sets them (lab 5).

A service of your own

systemd starts programs from unit files. A minimal service that starts your program at boot is a file /etc/systemd/system/<name>.service with three sections:

LineWhat it does
[Unit] then Description=My sensor loggerA name for logs and systemctl status
[Service] then ExecStart=/usr/local/bin/<program>The program to run, by its full path
Restart=on-failureStarts it again if it crashes
[Install] then WantedBy=multi-user.targetStarts it with the normal multi-user boot
sudo systemctl enable --now <name>.serviceTurns it on for every boot, and starts it now
systemctl status <name> and journalctl -u <name>Whether it is running, and its log

On the bench

In short, what you do in lab 5, where the steps are given in full:

  1. Annotate the kernel and init lines of the log, using the timestamps to find where the time goes.
  2. Read /proc/cmdline, change one boot argument, and see what changes.
  3. Measure the boot with systemd-analyze and find the slowest services.
  4. Write a systemd service that starts your own program at boot.

Resources

ResourceUse in this module
Upstream Linux for Libre Computer boardsThe kernel the kit boards run
Linux kernel documentationKernel parameters, the driver model and the boot process
libre.computer downloadsImages for the kit boards
systemd.service manualEvery option a service unit can carry