init System

making your services permanents

Process ID 1

  • Init system!
  • ancestor to all processes

Responsabilities

  • Setting up the name of the host
  • Setting up the time zone
  • Configuring peripherals
  • Starting deamons (services)
  • ... many others

Implementations

  • Sys V (traditional)
  • systemd

but why systemd?

still hungry

Advantages

  • starts process in parallel
  • handles hot plug deices (start services on demand)
  • manage resources (cgroups)
  • manage log entires (journald)
  • manage users (logind)
  • manage network (networkd)
  • commonly adopted

is it growing more?

  • VM
  • containers
  • booting

Systemd description

Much like a package management system, systemd defines a robust dependency model, not only among services but also among “targets,” systemd’s term for the operational modes that traditional init calls runlevels. systemd not only manages processes in parallel, but also manages network connections (networkd), kernel log entries (journald), and logins (logind).

UNIX and linux system administration handbook

Units and units files

An entity that is managed by systemd is know as a unit. The behaviour of a unit is specified in a unit file.

A service is a unit.

other types of units

  • Socket units
  • Target units
  • Mount units
  • Timer units
  • Device, Automount, Swap, units Slice, Scope

which encapsulate local IPC or network sockets in the system, useful for socket-based activation.

other types of units

  • Socket units
  • Target units
  • Mount units
  • Timer units
  • Device, Automount, Swap, units Slice, Scope

useful to group units, or provide well-known synchronization points during boot-up

other types of units

  • Socket units
  • Target units
  • Mount units
  • Timer units
  • Device, Automount, Swap, units Slice, Scope

mount points in thefile system

other types of units

  • Socket units
  • Target units
  • Mount units
  • Timer units
  • Device, Automount, Swap, units Slice, Scope

triggering activation of other units based on timers.

Unit locations

  • /usr/lib/systemd
  • /etc/systemd
  • $HOME/.config/systemd/user

Unit file example

[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf

[Service]
ExecStart=/usr/bin/rsync --daemon --no-detach

[Install]
WantedBy=multi-user.target

Parallelization startup

dependency mangaemnt in unit section

Wants, Requires, Requisite, Before, After

other types of units

  • Wants
  • Requires
  • Requisite
  • Before=, After=

weak requirement dependencies on other units. , if the listed units fail to start this unit will still be started.

other types of units

  • Wants
  • Requires
  • Requisite
  • Before=, After=

If one of the other units fails to activate, this unit will not be started. Besides, with or without

other types of units

  • Wants
  • Requires
  • Requisite
  • Before=, After=

if the units listed here are not started already, they will not be started and the starting of this unit will fail immediately.

other types of units

  • Wants
  • Requires
  • Requisite
  • Before=, After=

Those two settings configure ordering dependencies between units. When two units with an ordering dependency between them are shut down, the inverse of the start-up order is applied.

service section keyword

  • Type
  • ExecStart
  • ExecStartPre
  • ExecStartPost

This defines services by their process and daemonizing behavior.

  • Type
  • ExecStart
  • ExecStartPre
  • ExecStartPost

This directive set the path and arguments of the executable command for the unit.

  • Type
  • ExecStart
  • ExecStartPre
  • ExecStartPost

This specifies an additional command that executes before ExecStart.

  • Type
  • ExecStart
  • ExecStartPre
  • ExecStartPost

This configures the path to commands that will be executed after ExecStart.

service type

  • simple
  • exec
  • oneshot
  • notify

the service manager will consider the unit started immediately after the main service process has been forked off

  • simple
  • exec
  • oneshot
  • notify

the service manager will consider the unit started immediately after the main service binary has been executed.

  • simple
  • exec
  • oneshot
  • notify

the service manager will consider the unit up after the main process exits

  • simple
  • exec
  • oneshot
  • notify

the service manager will consider the unit when a callback is performed

wantedBy=target

Units that is used to define markers and operating modes

single-user, multi-user, graphical, network-online

target correspond to run-level

sshd unit

[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.target
Wants=sshd-keygen.target

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

cockpit service

[Unit]
Description=Cockpit Web Service
Documentation=man:cockpit-ws(8)
Requires=cockpit.socket
Requires=cockpit-wsinstance-http.socket cockpit-wsinstance-https-factory.socket
# ensure our DynamicUser exists
Requires=cockpit-ws-user.service
After=cockpit-ws-user.service
# we need to start after the sockets so that we can instantly forward incoming requests
After=cockpit-wsinstance-http.socket cockpit-wsinstance-https-factory.socket

[Service]
RuntimeDirectory=cockpit/tls
ExecStartPre=+/usr/libexec/cockpit-certificate-ensure --for-cockpit-tls
ExecStart=/usr/libexec/cockpit-tls
User=cockpit-ws
Group=cockpit-ws
...

[Install]
# Not present! Socket activated

System state

commandaction
systemctl statussystemctl show system status

working on a unit

commandaction
systemctl start unitStart a unit immediately
systemctl stop unitStop a unit immediately
systemctl enable unitenable/install a unit immediately
systemctl disable unitdisable/uninstall a unit immediately
systemctl status unitinspect a unit status
systemctl restart unitRestart a unit
systemctl daemon-reloadReload systemd manager configuration

status of a unit

➜  ~ sudo systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled)
    Drop-In: /usr/lib/systemd/system/service.d
             └─10-timeout-abort.conf
     Active: active (running) since Fri 2024-09-20 22:29:57 CEST; 1 week 4 days ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 1828 (sshd)
      Tasks: 1 (limit: 47576)
     Memory: 188.0K (peak: 3.9M swap: 1.2M swap peak: 1.2M)
        CPU: 105ms
     CGroup: /system.slice/sshd.service
             └─1828 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Sep 20 22:29:57 Derinoe systemd[1]: Starting sshd.service - OpenSSH server daemon...
Sep 20 22:29:57 Derinoe (sshd)[1828]: sshd.service: Referenced but unset environment variable evaluates to an empty string: OPTIONS
Sep 20 22:29:57 Derinoe sshd[1828]: Server listening on 0.0.0.0 port 22.
Sep 20 22:29:57 Derinoe sshd[1828]: Server listening on :: port 22.
Sep 20 22:29:57 Derinoe systemd[1]: Started sshd.service - OpenSSH server daemon.
Sep 26 17:05:30 Derinoe sshd[565393]: Connection closed by 192.168.112.177 port 57411

logging

➜  ~ journalctl -b -1
Sep 14 10:58:32 Derinoe kernel: Linux version 6.10.8-200.fc40.x86_64 (mockbuild@81267993c0ea4676bef76f8dd2b2ffea) (gcc (GCC) 14.2.1 202>
Sep 14 10:58:32 Derinoe kernel: Command line: BOOT_IMAGE=(hd0,gpt3)/vmlinuz-6.10.8-200.fc40.x86_64 root=UUID=fc98b53c-d69c-4ff7-9a46-e5>
Sep 14 10:58:32 Derinoe kernel: BIOS-provided physical RAM map:
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000000009f000-0x00000000000fffff] reserved
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x0000000000100000-0x000000004f23bfff] usable
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000004f23c000-0x000000004f23dfff] reserved
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000004f23e000-0x000000006b69efff] usable
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000006b69f000-0x000000006fa36fff] reserved
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000006fa37000-0x000000006fca9fff] ACPI NVS
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000006fcaa000-0x000000006fd0efff] ACPI data
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000006fd0f000-0x000000006fd0ffff] usable
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x000000006fd10000-0x0000000077ffffff] reserved
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x0000000078000000-0x00000000787fffff] usable
Sep 14 10:58:32 Derinoe kernel: BIOS-e820: [mem 0x0000000078800000-0x000000007f7fffff] reserved

Logging last boot, all services

logging a unit

➜  ~ journalctl -u greetd.service
-- Boot 9feeb89f09e240fe90a151e5b49021a1 --
Jul 29 14:03:03 Derinoe systemd[1]: Started greetd.service - Greeter daemon.
Jul 29 14:03:03 Derinoe greetd[2160]: pam_unix(greetd-greeter:session): session opened for user greetd(uid=980) by (uid=0)
Jul 29 14:03:21 Derinoe greetd[2252]: pam_unix(greetd:auth): check pass; user unknown
Jul 29 14:03:21 Derinoe greetd[2252]: pam_unix(greetd:auth): authentication failure; logname= uid=0 euid=0 tty= ruser= rhost=
Jul 29 14:03:21 Derinoe greetd[2252]: gkr-pam: error looking up user information

Exercise02

Make your jupyter-lab a service

Create the correct folders/files

$ mkdir -p ~/.config/systemd/user
$ touch ~/.config/systemd/user/jupyter.service

populate the service

[Unit]
Description=Jupyter

[Service]
Type=simple
ExecStart=/home/user00/jupyterlab/bin/jupyter-lab --port 8000
Restart=always

[Install]
WantedBy=default.target

enable and run

$ systemctl daemon-reload --user
$ systemctl enable jupyter --user
$ systemctl start jupyter --user
# inspect log
$ journalctl -u jupyter --user

Question:

What happened in the "~/.config/systemd/user" folder?