Linux system boot sequence

What happens when a linux system starts

view on github

System boot sequence

  1. UEFI (boot manager)

    operation 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
    Hands control to boot loader ...
  2. GRUB (boot loader)

    operation file / command
    Reads selected boot menu option : /boot/grub/grub.cfg
    --> load GRUB modules gzio, part_gpt, ext2 (image decompression and file systems support)
    --> root filesystem partition search --no-floppy --fs-uuid --set=root <uuid>
    --> specify kernel image linux /boot/vmlinuz-<version>-<arch> root=UUID=<uuid> ro quiet
    --> specify initrd/initramfs image initrd /boot/initrd.img-<version>-<arch>
    Loads kernel image /boot/vmlinuz-<version>-<arch>
    Hands control to the kernel ...
  3. Mini Debian system

    operation file / command
    Kernel
    --> mounts initramfs in memory
    --> starts scheduler
    --> starts init/systemd (PID 1) /sbin/init -> /lib/systemd/systemd*
    --> starts idle/swapper process (PID 0)
    init/systemd
    --> sets up user space
    --> starts system daemons and services
  4. Main Debian system

    operation file / command
    Kernel
    --> Mounts root filesystem /etc/fstab
    --> Switches from initramfs to root filesystem /dev/sda2 mounted on /
    init/systemd
    --> Activates default.target /lib/systemd/system/default.target -> /lib/systemd/system/multi-user.target
    --> Removes temporary files /usr/lib/systemd/system/systemd-tmpfiles-clean.service

    Note : /tmp, /var/lock and /var/run will be wiped out at reboot

  5. 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)

Systemd configuration units

  1. /lib/systemd/systemd spawns parallel processes according to the units configuration 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 files are searched in the above directories, in that order (search stops on first matching unit)
    • for instance, a unit in /etc/systemd/system will override the /lib/systemd/system unit of the same name
    • Overriding a system-wide unit with an empty file or a symlink to /dev/null will disable said unit
    • All custom units should be placed in /etc/systemd/system to survive package updates
  2. Units dependencies directives details :

    • Requires
      • list of units that will be started when the current unit is started
      • if any of those units fail, the current unit will be stopped
    • Wants
      • list of units that will be started when the current unit is started
      • if any of those units fail, the current unit will continue running
    • Conflicts
      • list of 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
      • list of units to be started after the current unit is started
    • After
      • list of units to be started before the current unit is started
  3. 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
  4. Configuration units types :

    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)
  5. Bootup target unit selection

    • pass argument to kernel command line
    /boot/vmlinuz-<version>-<arch> systemd.unit=<path>
    • symlink default.target to unit file
    /lib/systemd/system/default.target -> /lib/systemd/system/graphical.target
    • Activating the default target will create a dependencies tree that will bring the system into a working state
  6. Main configuration units activation sequence during boot (cf man systemd.special(7))

    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 initialization /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
    --> basic bootup (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

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) 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 (from where ?) on a new terminal to wait for the next login