What systemd does at boot
Video 067 of 140 in Virtualization Foundations, in the unit “The boot sequence, end to end”. 3:52, with English and Hindi audio.
Near the end of the boot, the kernel hands control to init, the first program. On Ubuntu, that program is systemd. It runs as process ID one, and every other process on the server descends from it. systemd mounts the disks, starts the services and opens the logins. This article explains how it does this, why it starts services in parallel, and which commands show what it is doing.
What you will learn
- systemd is process ID one, the first program the kernel starts.
- It mounts the filesystems listed in the fstab file, then aims for the default target.
- It starts services in parallel, as soon as the things they need are ready.
- Units are the things systemd manages, such as services and disk mounts.
- systemctl and systemd-analyze show which units failed and which were slowest at boot.
Why systemd starts services in parallel
Older init systems ran start-up scripts one after another, in a fixed order. Each script had to finish before the next one began. That made boot slow. One service that waited on the network held up every service after it.
systemd starts services in parallel instead. Each one starts as soon as the things it needs are ready, so services that do not depend on each other start together.
What systemd does at boot, in order
- Runs as process ID one, the first process the kernel starts.
- Reads the fstab file, a list of disks and folders to attach at boot, and attaches each one to the file tree.
- Aims for the default target, a named group of services.
- Starts each service once the things it depends on are ready.
- Opens the logins: the console waits at a login prompt, and SSH listens for remote logins.
Units and targets
Inside systemd, the things it manages are called units. A service, a disk mount and a network socket are all units.
A target is a unit too. It is a group of other units that a boot should reach, such as the group a server boots to.
Asking systemd what it is doing
You can ask systemd about its own state with a few read-only commands. The exercise below shows the name of process ID one, the target the boot aims for, the units that failed, and the slowest units at boot.
Note: An empty list from systemctl --failed means that no unit failed during this boot.
Try it yourself
You need a shell on an Ubuntu server. Each command asks systemd about itself or about the last boot, and none of them change the server.
Print the name of process ID one, the first process the kernel starts.
ps -p 1 -o comm=What you should see: The name of process ID one, which should be systemd.
Show the target that the boot is aimed at.
systemctl get-defaultWhat you should see: The target the boot is aimed at.
List the units that failed.
systemctl --failedWhat you should see: The units that failed. An empty list means none failed.
Show the slowest units at boot, with the time each one took.
systemd-analyze blame | head -5What you should see: The slowest units at boot, each with the time it took.
Terms
- systemd
- The service manager that runs as process ID one and starts the services a server needs.
- Unit
- A named thing that systemd manages, such as a service, a disk mount or a network socket.
- Target
- A unit that groups other units, which a boot should reach.
- fstab
- The file that lists the disks and folders to attach at boot, and where each one is attached.
Check yourself
Why does systemd start services in parallel?
So one slow service does not hold up every service behind it. Each service starts when the things it needs are ready.
What is a target?
A unit that groups other units, such as the group a server boots to.
What does systemctl --failed show?
The units that did not start. An empty list means no failures were recorded during this boot.
Previous: The kernel and the initramfs. Next: Where a boot stops when it fails.