Linux system boot
What happens when a linux system starts
- Different programs run in a sequence to bring the system from powered off to online and functional.
UEFI : boot manager
| operation | device / partition / file |
|---|---|
| Reads GPT partition table | /dev/sda |
| Loads EFI system partition |
/dev/sda1 mounted on /boot/efi
|
| Autodetects path to boot loader and starts it | /boot/efi/EFI/debian/grubx64.efi |
- UEFI then hands control to the boot loader.
GRUB : boot loader
| operation | file / module / command |
|---|---|
| Reads selected boot menu option : | /boot/grub/grub.cfg |
| -> Loads GRUB modules |
gzio, part_gpt, ext2 (image decompression and file systems support) |
| -> Specifies root filesystem partition | search --no-floppy --fs-uuid --set=root <uuid> |
| -> Specifies kernel image | linux /boot/vmlinuz-<version>-<arch> root=UUID=<uuid> ro quiet |
| -> Specifies initramfs image | initrd /boot/initrd.img-<version>-<arch> |
| Loads kernel image | /boot/vmlinuz-<version>-<arch> |
- GRUB then hands control to the kernel.
Note : <uuid> is the uuid of the partition to be mounted at / as described in /etc/fstab.
| operation | file / command |
|---|---|
| Kernel | |
| -> Mounts initramfs in memory | /boot/initrd.img-<version>-<arch> |
| -> Starts the scheduler | |
-> Starts systemd (PID 1) |
/sbin/init -> /lib/systemd/systemd |
| -> Starts idle/swapper process (PID 0) | |
systemd |
|
| -> Sets up user space | |
| -> Starts system daemons and services (see below) |
-
systemdhas been started by the kernel and will operate the system until shutdown.
Note : contents of the initramfs image can be inspected using lsinitramfs -l /boot/initrd.img-<version>-<arch>.
| operation | file / command |
|---|---|
| Kernel | |
| -> Mounts root filesystem | /etc/fstab |
| -> Switches from initramfs to root filesystem |
/dev/sda2 is mounted on /
|
systemd |
|
| -> Activates default.target | /lib/systemd/system/default.target |
| -> Removes temporary files | /usr/lib/systemd/system/systemd-tmpfiles-clean.service |
- The system has now reached its desired state and is up and functional.
Note : /tmp, /var/lock and /var/run are wiped out on reboot.
-
agetty: opens a tty port, prompts for a login name and invokes/bin/login. -
systemd-bootchart: performance analysis (forkssystemdat startup, monitors performance, generates svg chart).
-
systemdspawns parallel processes according to the configuration units files :configuration scope directory where units reside Local configuration /etc/systemd/systemRuntime configuration /run/systemd/systemSystem-wide configuration /lib/systemd/systemDefault configuration at package install /usr/lib/systemd/system -
Configuration units are searched in those directories, in that order (search stops on first matching unit).
-
For instance, a unit in
/etc/systemd/systemoverrides a unit in/lib/systemd/systemwith the same name. -
Overriding a system-wide unit with an empty file or a symlink to
/dev/nulldisables said unit. -
Custom units should be placed in
/etc/systemd/systemto survive package updates.
-
systemdcombines dependencies directives to create the global dependency tree :directive description RequiresUnits that will be started when the current unit is started If any of those units fail, the current unit will be stopped WantsUnits that will be started when the current unit is started If any of those units fail, the current unit will continue running ConflictsUnits that will be stopped when the current unit is started If any of those units is started, the current unit will be stopped BeforeUnits to be started after the current unit is started AfterUnits to be started before the current unit is started -
All the necessary steps to boot the system into the desired state reside in the configuration units :
- File systems initialization
- Device drivers initialization
- Services initialization
| file extension | role |
|---|---|
*.service |
Configured service/daemon |
*.device |
Hardware device |
*.mount |
File system mount point |
*.automount |
File system automount point |
*.swap |
Swap device or file |
*.path |
Path monitoring |
*.socket |
IPC/network/FIFO sockets |
*.timer |
Timer based activation (logs rotation, man db update, etc...) |
*.slice |
Slices (cgroup resources management) |
*.scope |
Groups of externally created system processes |
*.target |
Roughly equivalent to runlevels (target configuration is "reached", like runlevel) |
- Default behavior is to symlink
default.targetto atargetunit file :
/lib/systemd/system/default.target -> /lib/systemd/system/graphical.target- It's also possible to pass an argument to the kernel command line :
/boot/vmlinuz-<version>-<arch> systemd.unit="$target_unit_path"- Activating the default target creates a dependencies tree that bring the system into a working state.
| action | configuration unit |
|---|---|
| Swap memory | /lib/systemd/system/swap.target |
| Local file systems | /lib/systemd/system/local-fs.target |
| Encrypted volumes | /lib/systemd/system/cryptsetup.target |
| -> System initialized | /lib/systemd/system/sysinit.target |
| Timers-based service activation | /lib/systemd/system/timers.target |
| Rescue shell (eq runlevel 1) | /lib/systemd/system/rescue.target |
| Paths-based service activation | /lib/systemd/system/paths.target |
| Sockets-based service activation | /lib/systemd/system/sockets.target |
| -> System started (daemons can start) | /lib/systemd/system/basic.target |
| Ready to accept logins (eq runlevel 3) | /lib/systemd/system/multi-user.target |
| Start x server session | /lib/systemd/system/display-manager.service |
| Login in graphical mode (eq runlevel 5) | /lib/systemd/system/graphical.target |
- See
man systemd.specialfor a full list of available target units.
-
/sbin/initreads/etc/inittabconfiguration script- set path
- starts memory swapping
- checks filesystems
- etc ...
-
/sbin/initsets default run level (a run level is a configuration of processes)runlevel # description Halt 0 Shuts down system Single-User Mode 1 Does not configure network interfaces, start daemons, or allow non-root logins Multi-User Mode 2 Does not configure network interfaces or start daemons. Multi-User Mode with Networking 3 Starts the system normally. Undefined 4 Not used/User-definable X11 5 Same as runlevel 3 + display manager (X) Reboot 6 Reboots the system - A run level is the state the system must reach in order for a given service to start and work properly.
- Thus, when all services belonging to run level n-1 have been started, services belonging to run level n can be started.
-
/sbin/initruns scripts relevant to default run level- Scripts for run level n are located in
/etc/rc(n)/.. -
/sbin/initfirst runs all the kill scripts (K...*) in the directory. -
/sbin/initthen runs all the start scripts (S...*) in the directory. - All scripts in
/etc/rc(n)/.are symlinks to scripts located in/etc/init.d/.. - Starting sequence of the services depends on the symlinks order (numeric/alphabetical).
- Scripts for run level n are located in
-
The system is now ready to accept logins
-
/sbin/initforks/exec togettyontty1and waits for some user to enter credentials. - Once credentials are entered,
gettyforks/exec tologin, and then to default shell if credentials are correct. - Each time a login succeeds, another
gettyis forked on a new terminal to wait for the next login.
-