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
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
compatiblestring 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'sbootargsvariable (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:
| Line | What it does |
|---|---|
[Unit] then Description=My sensor logger | A name for logs and systemctl status |
[Service] then ExecStart=/usr/local/bin/<program> | The program to run, by its full path |
Restart=on-failure | Starts it again if it crashes |
[Install] then WantedBy=multi-user.target | Starts it with the normal multi-user boot |
sudo systemctl enable --now <name>.service | Turns 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:
- Annotate the kernel and init lines of the log, using the timestamps to find where the time goes.
- Read
/proc/cmdline, change one boot argument, and see what changes. - Measure the boot with
systemd-analyzeand find the slowest services. - Write a systemd service that starts your own program at boot.
Resources
| Resource | Use in this module |
|---|---|
| Upstream Linux for Libre Computer boards | The kernel the kit boards run |
| Linux kernel documentation | Kernel parameters, the driver model and the boot process |
| libre.computer downloads | Images for the kit boards |
| systemd.service manual | Every option a service unit can carry |