Linux system boot

What happens when a linux system starts

view on github

Linux system startup

Table of contents

  1. System boot sequence
  2. System processes and daemons startup
  3. SysV and runlevels (obsolete)

System boot sequence

  • 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

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.

Kernel + initramfs : mini Debian system

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)
  • systemd has 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>.

Kernel + systemd : main Debian system

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.

Relevant packages and utilities:

  • agetty : opens a tty port, prompts for a login name and invokes /bin/login.
  • systemd-bootchart : performance analysis (forks systemd at startup, monitors performance, generates svg chart).

System processes and daemons startup

systemd : service manager

  • systemd spawns parallel processes according to the configuration units files :

    configuration scope directory where units reside
    Local configuration /etc/systemd/system
    Runtime configuration /run/systemd/system
    System-wide configuration /lib/systemd/system
    Default 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/system overrides a unit in /lib/systemd/system with the same name.

  • Overriding a system-wide unit with an empty file or a symlink to /dev/null disables said unit.

  • Custom units should be placed in /etc/systemd/system to survive package updates.

Units dependencies directives

  • systemd combines dependencies directives to create the global dependency tree :

    directive description
    Requires Units that will be started when the current unit is started
    If any of those units fail, the current unit will be stopped
    Wants Units that will be started when the current unit is started
    If any of those units fail, the current unit will continue running
    Conflicts Units that will be stopped when the current unit is started
    If any of those units is started, the current unit will be stopped
    Before Units to be started after the current unit is started
    After Units 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

Types of configuration units

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)

Bootup target unit selection

  • Default behavior is to symlink default.target to a target unit 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.

Units activation sequence during boot

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.special for a full list of available target units.

SysV and runlevels (obsolete)

  1. /sbin/init reads /etc/inittab configuration script

    • set path
    • starts memory swapping
    • checks filesystems
    • etc ...
  2. /sbin/init sets 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.
  3. /sbin/init runs scripts relevant to default run level

    • Scripts for run level n are located in /etc/rc(n)/..
    • /sbin/init first runs all the kill scripts (K...*) in the directory.
    • /sbin/init then 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).
  4. The system is now ready to accept logins

    • /sbin/init forks/exec to getty on tty1 and waits for some user to enter credentials.
    • Once credentials are entered, getty forks/exec to login, and then to default shell if credentials are correct.
    • Each time a login succeeds, another getty is forked on a new terminal to wait for the next login.