| 1 |
<?xml version='1.0'?> <!--*-nxml-*--> |
| 2 |
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
| 3 |
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
| 4 |
<!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
| 5 |
|
| 6 |
<refentry id="bootup"> |
| 7 |
|
| 8 |
<refentryinfo> |
| 9 |
<title>bootup</title> |
| 10 |
<productname>systemd</productname> |
| 11 |
</refentryinfo> |
| 12 |
|
| 13 |
<refmeta> |
| 14 |
<refentrytitle>bootup</refentrytitle> |
| 15 |
<manvolnum>7</manvolnum> |
| 16 |
</refmeta> |
| 17 |
|
| 18 |
<refnamediv> |
| 19 |
<refname>bootup</refname> |
| 20 |
<refpurpose>System bootup process</refpurpose> |
| 21 |
</refnamediv> |
| 22 |
|
| 23 |
<refsect1> |
| 24 |
<title>Description</title> |
| 25 |
|
| 26 |
<para>A number of different components are involved in the boot of a Linux system. Immediately after |
| 27 |
power-up, the system firmware will do minimal hardware initialization, and hand control over to a boot |
| 28 |
loader (e.g. |
| 29 |
<citerefentry><refentrytitle>systemd-boot</refentrytitle><manvolnum>7</manvolnum></citerefentry> or |
| 30 |
<ulink url="https://www.gnu.org/software/grub/">GRUB</ulink>) stored on a persistent storage device. This |
| 31 |
boot loader will then invoke an OS kernel from disk (or the network). On systems using EFI or other types |
| 32 |
of firmware, this firmware may also load the kernel directly.</para> |
| 33 |
|
| 34 |
<para>The kernel (optionally) mounts an in-memory file system, which looks for the root file system. |
| 35 |
Nowadays this is implemented as an "initramfs" — a compressed CPIO archive that the kernel extracts into |
| 36 |
a tmpfs. In the past normal file systems using an in-memory block device (ramdisk) were used, and the |
| 37 |
name "initrd" is still used to describe both concepts. It's the boot loader or the firmware that loads |
| 38 |
both the kernel and initrd/initramfs images into memory, but the kernel which interprets it as a file |
| 39 |
system. |
| 40 |
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> may be used |
| 41 |
to manage services in the initrd, similarly to the real system.</para> |
| 42 |
|
| 43 |
<para>After the root file system is found and mounted, the initrd hands over control to the host's system |
| 44 |
manager (such as |
| 45 |
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>) stored in |
| 46 |
the root file system, which is then responsible for probing all remaining hardware, mounting all |
| 47 |
necessary file systems and spawning all configured services.</para> |
| 48 |
|
| 49 |
<para>On shutdown, the system manager stops all services, unmounts all non-busy file systems (detaching |
| 50 |
the storage technologies backing them), and then (optionally) jumps into the exitrd. The exitrd is backed |
| 51 |
by tmpfs and unmounts/detaches the remaining file systems, including the real root. As a last step, the |
| 52 |
system is powered down.</para> |
| 53 |
|
| 54 |
<para>Additional information about the system boot process may be |
| 55 |
found in |
| 56 |
<citerefentry project='man-pages'><refentrytitle>boot</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para> |
| 57 |
</refsect1> |
| 58 |
|
| 59 |
<refsect1> |
| 60 |
<title>System Manager Bootup</title> |
| 61 |
|
| 62 |
<para>At boot, the system manager on the OS image is responsible |
| 63 |
for initializing the required file systems, services and drivers |
| 64 |
that are necessary for operation of the system. On |
| 65 |
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry> |
| 66 |
systems, this process is split up in various discrete steps which |
| 67 |
are exposed as target units. (See |
| 68 |
<citerefentry><refentrytitle>systemd.target</refentrytitle><manvolnum>5</manvolnum></citerefentry> |
| 69 |
for detailed information about target units.) The boot-up process |
| 70 |
is highly parallelized so that the order in which specific target |
| 71 |
units are reached is not deterministic, but still adheres to a |
| 72 |
limited amount of ordering structure.</para> |
| 73 |
|
| 74 |
<para>When systemd starts up the system, it will activate all |
| 75 |
units that are dependencies of <filename>default.target</filename> |
| 76 |
(as well as recursively all dependencies of these dependencies). |
| 77 |
Usually, <filename>default.target</filename> is simply an alias of |
| 78 |
<filename>graphical.target</filename> or |
| 79 |
<filename>multi-user.target</filename>, depending on whether the |
| 80 |
system is configured for a graphical UI or only for a text |
| 81 |
console. To enforce minimal ordering between the units pulled in, |
| 82 |
a number of well-known target units are available, as listed on |
| 83 |
<citerefentry><refentrytitle>systemd.special</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para> |
| 84 |
|
| 85 |
<para>The following chart is a structural overview of these |
| 86 |
well-known units and their position in the boot-up logic. The |
| 87 |
arrows describe which units are pulled in and ordered before which |
| 88 |
other units. Units near the top are started before units nearer to |
| 89 |
the bottom of the chart.</para> |
| 90 |
|
| 91 |
<!-- note: do not use unicode ellipsis here, because docbook will replace that |
| 92 |
with three dots anyway, messing up alignment --> |
| 93 |
<programlisting> cryptsetup-pre.target veritysetup-pre.target |
| 94 |
| |
| 95 |
(various low-level v |
| 96 |
API VFS mounts: (various cryptsetup/veritysetup devices...) |
| 97 |
mqueue, configfs, | | |
| 98 |
debugfs, ...) v | |
| 99 |
| cryptsetup.target | |
| 100 |
| (various swap | | remote-fs-pre.target |
| 101 |
| devices...) | | | | |
| 102 |
| | | | | v |
| 103 |
| v local-fs-pre.target | | | (network file systems) |
| 104 |
| swap.target | | v v | |
| 105 |
| | v | remote-cryptsetup.target | |
| 106 |
| | (various low-level (various mounts and | remote-veritysetup.target | |
| 107 |
| | services: udevd, fsck services...) | | | |
| 108 |
| | tmpfiles, random | | | remote-fs.target |
| 109 |
| | seed, sysctl, ...) v | | | |
| 110 |
| | | local-fs.target | | _____________/ |
| 111 |
| | | | | |/ |
| 112 |
\____|______|_______________ ______|___________/ | |
| 113 |
\ / | |
| 114 |
v | |
| 115 |
sysinit.target | |
| 116 |
| | |
| 117 |
______________________/|\_____________________ | |
| 118 |
/ | | | \ | |
| 119 |
| | | | | | |
| 120 |
v v | v | | |
| 121 |
(various (various | (various | | |
| 122 |
timers...) paths...) | sockets...) | | |
| 123 |
| | | | | | |
| 124 |
v v | v | | |
| 125 |
timers.target paths.target | sockets.target | | |
| 126 |
| | | | v | |
| 127 |
v \_______ | _____/ rescue.service | |
| 128 |
\|/ | | |
| 129 |
v v | |
| 130 |
basic.target <emphasis>rescue.target</emphasis> | |
| 131 |
| | |
| 132 |
________v____________________ | |
| 133 |
/ | \ | |
| 134 |
| | | | |
| 135 |
v v v | |
| 136 |
display- (various system (various system | |
| 137 |
manager.service services services) | |
| 138 |
| required for | | |
| 139 |
| graphical UIs) v v |
| 140 |
| | <emphasis>multi-user.target</emphasis> |
| 141 |
emergency.service | | | |
| 142 |
| \_____________ | _____________/ |
| 143 |
v \|/ |
| 144 |
<emphasis>emergency.target</emphasis> v |
| 145 |
<emphasis>graphical.target</emphasis></programlisting> |
| 146 |
|
| 147 |
<para>Target units that are commonly used as boot targets are |
| 148 |
<emphasis>emphasized</emphasis>. These units are good choices as |
| 149 |
goal targets, for example by passing them to the |
| 150 |
<varname>systemd.unit=</varname> kernel command line option (see |
| 151 |
<citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>) |
| 152 |
or by symlinking <filename>default.target</filename> to them. |
| 153 |
</para> |
| 154 |
|
| 155 |
<para><filename>timers.target</filename> is pulled-in by |
| 156 |
<filename>basic.target</filename> asynchronously. This allows |
| 157 |
timers units to depend on services which become only available |
| 158 |
later in boot.</para> |
| 159 |
</refsect1> |
| 160 |
|
| 161 |
<refsect1> |
| 162 |
<title>User manager startup</title> |
| 163 |
|
| 164 |
<para>The system manager starts the <filename>user@<replaceable>uid</replaceable>.service</filename> unit |
| 165 |
for each user, which launches a separate unprivileged instance of <command>systemd</command> for each |
| 166 |
user — the user manager. Similarly to the system manager, the user manager starts units which are pulled |
| 167 |
in by <filename>default.target</filename>. The following chart is a structural overview of the well-known |
| 168 |
user units. For non-graphical sessions, <filename>default.target</filename> is used. Whenever the user |
| 169 |
logs into a graphical session, the login manager will start the |
| 170 |
<filename>graphical-session.target</filename> target that is used to pull in units required for the |
| 171 |
graphical session. A number of targets (shown on the right side) are started when specific hardware is |
| 172 |
available to the user.</para> |
| 173 |
|
| 174 |
<programlisting> |
| 175 |
(various (various (various |
| 176 |
timers...) paths...) sockets...) (sound devices) |
| 177 |
| | | | |
| 178 |
v v v v |
| 179 |
timers.target paths.target sockets.target sound.target |
| 180 |
| | | |
| 181 |
\______________ _|_________________/ (bluetooth devices) |
| 182 |
\ / | |
| 183 |
V v |
| 184 |
basic.target bluetooth.target |
| 185 |
| |
| 186 |
__________/ \_______ (smartcard devices) |
| 187 |
/ \ | |
| 188 |
| | v |
| 189 |
| v smartcard.target |
| 190 |
v graphical-session-pre.target |
| 191 |
(various user services) | (printers) |
| 192 |
| v | |
| 193 |
| (services for the graphical session) v |
| 194 |
| | printer.target |
| 195 |
v v |
| 196 |
<emphasis>default.target</emphasis> graphical-session.target</programlisting> |
| 197 |
|
| 198 |
</refsect1> |
| 199 |
|
| 200 |
<refsect1> |
| 201 |
<title>Bootup in the initrd</title> |
| 202 |
|
| 203 |
<para>Systemd can be used in the initrd as well. It detects the initrd environment by checking for the |
| 204 |
<filename>/etc/initrd-release</filename> file. The default target in the initrd is |
| 205 |
<filename>initrd.target</filename>. The bootup process is identical to the system manager bootup until |
| 206 |
the target <filename>basic.target</filename>. After that, systemd executes the special target |
| 207 |
<filename>initrd.target</filename>. |
| 208 |
|
| 209 |
Before any file systems are mounted, the manager will determine whether the system shall resume from |
| 210 |
hibernation or proceed with normal boot. This is accomplished by |
| 211 |
<filename>systemd-hibernate-resume.service</filename> which must be finished before |
| 212 |
<filename>local-fs-pre.target</filename>, so no filesystems can be mounted before the check is complete. |
| 213 |
|
| 214 |
When the root device becomes available, |
| 215 |
<filename>initrd-root-device.target</filename> is reached. |
| 216 |
If the root device can be mounted at |
| 217 |
<filename>/sysroot</filename>, the |
| 218 |
<filename>sysroot.mount</filename> unit becomes active and |
| 219 |
<filename>initrd-root-fs.target</filename> is reached. The service |
| 220 |
<filename>initrd-parse-etc.service</filename> scans |
| 221 |
<filename>/sysroot/etc/fstab</filename> for a possible |
| 222 |
<filename>/usr/</filename> mount point and additional entries |
| 223 |
marked with the <emphasis>x-initrd.mount</emphasis> option. All |
| 224 |
entries found are mounted below <filename>/sysroot</filename>, and |
| 225 |
<filename>initrd-fs.target</filename> is reached. The service |
| 226 |
<filename>initrd-cleanup.service</filename> isolates to the |
| 227 |
<filename>initrd-switch-root.target</filename>, where cleanup |
| 228 |
services can run. As the very last step, the |
| 229 |
<filename>initrd-switch-root.service</filename> is activated, |
| 230 |
which will cause the system to switch its root to |
| 231 |
<filename>/sysroot</filename>. |
| 232 |
</para> |
| 233 |
|
| 234 |
<programlisting> : (beginning identical to above) |
| 235 |
: |
| 236 |
v |
| 237 |
basic.target |
| 238 |
| emergency.service |
| 239 |
______________________/| | |
| 240 |
/ | v |
| 241 |
| initrd-root-device.target <emphasis>emergency.target</emphasis> |
| 242 |
| | |
| 243 |
| v |
| 244 |
| sysroot.mount |
| 245 |
| | |
| 246 |
| v |
| 247 |
| initrd-root-fs.target |
| 248 |
| | |
| 249 |
| v |
| 250 |
v initrd-parse-etc.service |
| 251 |
(custom initrd | |
| 252 |
services...) v |
| 253 |
| (sysroot-usr.mount and |
| 254 |
| various mounts marked |
| 255 |
| with fstab option |
| 256 |
| x-initrd.mount...) |
| 257 |
| | |
| 258 |
| v |
| 259 |
| initrd-fs.target |
| 260 |
\______________________ | |
| 261 |
\| |
| 262 |
v |
| 263 |
initrd.target |
| 264 |
| |
| 265 |
v |
| 266 |
initrd-cleanup.service |
| 267 |
isolates to |
| 268 |
initrd-switch-root.target |
| 269 |
| |
| 270 |
v |
| 271 |
______________________/| |
| 272 |
/ v |
| 273 |
| initrd-udevadm-cleanup-db.service |
| 274 |
v | |
| 275 |
(custom initrd | |
| 276 |
services...) | |
| 277 |
\______________________ | |
| 278 |
\| |
| 279 |
v |
| 280 |
initrd-switch-root.target |
| 281 |
| |
| 282 |
v |
| 283 |
initrd-switch-root.service |
| 284 |
| |
| 285 |
v |
| 286 |
Transition to Host OS</programlisting> |
| 287 |
</refsect1> |
| 288 |
|
| 289 |
<refsect1> |
| 290 |
<title>System Manager Shutdown</title> |
| 291 |
|
| 292 |
<para>System shutdown with systemd also consists of various target |
| 293 |
units with some minimal ordering structure applied:</para> |
| 294 |
|
| 295 |
<programlisting> (conflicts with (conflicts with |
| 296 |
all system all file system |
| 297 |
services) mounts, swaps, |
| 298 |
| cryptsetup/ |
| 299 |
| veritysetup |
| 300 |
| devices, ...) |
| 301 |
| | |
| 302 |
v v |
| 303 |
shutdown.target umount.target |
| 304 |
| | |
| 305 |
\_______ ______/ |
| 306 |
\ / |
| 307 |
v |
| 308 |
(various low-level |
| 309 |
services) |
| 310 |
| |
| 311 |
v |
| 312 |
final.target |
| 313 |
| |
| 314 |
___________________________/ \_________________________________ |
| 315 |
/ | | | \ |
| 316 |
| | | | | |
| 317 |
v | | | | |
| 318 |
systemd-reboot.service | | | | |
| 319 |
| v | | | |
| 320 |
| systemd-poweroff.service | | | |
| 321 |
v | v | | |
| 322 |
<emphasis>reboot.target</emphasis> | systemd-halt.service | | |
| 323 |
v | v | |
| 324 |
<emphasis>poweroff.target</emphasis> | systemd-kexec.service | |
| 325 |
v | | |
| 326 |
<emphasis>halt.target</emphasis> | systemd-soft-reboot.service |
| 327 |
v | |
| 328 |
<emphasis>kexec.target</emphasis> | |
| 329 |
v |
| 330 |
<emphasis>soft-reboot.target</emphasis></programlisting> |
| 331 |
|
| 332 |
<para>Commonly used system shutdown targets are <emphasis>emphasized</emphasis>.</para> |
| 333 |
|
| 334 |
<para>Note that |
| 335 |
<citerefentry><refentrytitle>systemd-halt.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>, |
| 336 |
<filename>systemd-reboot.service</filename>, <filename>systemd-poweroff.service</filename> and |
| 337 |
<filename>systemd-kexec.service</filename> will transition the system and server manager (PID 1) into the second |
| 338 |
phase of system shutdown (implemented in the <filename>systemd-shutdown</filename> binary), which will unmount any |
| 339 |
remaining file systems, kill any remaining processes and release any other remaining resources, in a simple and |
| 340 |
robust fashion, without taking any service or unit concept into account anymore. At that point, regular |
| 341 |
applications and resources are generally terminated and released already, the second phase hence operates only as |
| 342 |
safety net for everything that could not be stopped or released for some reason during the primary, unit-based |
| 343 |
shutdown phase described above.</para> |
| 344 |
</refsect1> |
| 345 |
|
| 346 |
<refsect1> |
| 347 |
<title>The exitrd</title> |
| 348 |
|
| 349 |
<para>The "exitrd" is a concept symmetrical to the initrd. When the system manager is shutting down and |
| 350 |
<filename>/run/initramfs/shutdown</filename> exists, it will switch root to |
| 351 |
<filename>/run/initramfs/</filename> and execute <filename>/shutdown</filename>. This program runs from |
| 352 |
the tmpfs mounted on <filename>/run/</filename>, so it can unmount the old root file system and perform |
| 353 |
additional steps, for example dismantle complex storage or perform additional logging about the shutdown. |
| 354 |
</para> |
| 355 |
</refsect1> |
| 356 |
|
| 357 |
<refsect1> |
| 358 |
<title>See Also</title> |
| 359 |
<para><simplelist type="inline"> |
| 360 |
<member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> |
| 361 |
<member><citerefentry project='man-pages'><refentrytitle>boot</refentrytitle><manvolnum>7</manvolnum></citerefentry></member> |
| 362 |
<member><citerefentry><refentrytitle>systemd.special</refentrytitle><manvolnum>7</manvolnum></citerefentry></member> |
| 363 |
<member><citerefentry><refentrytitle>systemd.target</refentrytitle><manvolnum>5</manvolnum></citerefentry></member> |
| 364 |
<member><citerefentry><refentrytitle>systemd-halt.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member> |
| 365 |
<member><citerefentry><refentrytitle>systemd-soft-reboot.service</refentrytitle><manvolnum>8</manvolnum></citerefentry></member> |
| 366 |
</simplelist></para> |
| 367 |
</refsect1> |
| 368 |
|
| 369 |
</refentry> |