Boot - systemd

Boot

During the boot process, at one point the kernel is then initialized, and it searches for the partition containing the root filesystem and the init program. Frequently, this “root partition” and this init are located in a virtual filesystem that only exists in RAM (hence its name, “initramfs”). This filesystem is loaded in memory by the bootloader and it contains the bare minimum required by the kernel to load the “true” root filesystem. Once the root partition is mounted, the initramfs hands over control to the real init, and the machine goes back to the standard boot process.

This init program (which is the first process during boot), is actually a symbolic link to /lib/systemd/systemd by default. So the real initialization of the system (init) is provided by systemd. Systemd has the main task to execute several other processes, and to set up the system: keyboard, drivers, filesystems, network, services....


==========================


systemd "service" and "target" files


Systemd is reponsible for starting/stopping necessary processes, checking status, and restarting them if needed. All relevant informations related to these processes are stored in a so called "service file" of that process. For instance, the service file for SSH looks like this:

pi@raspberrypi:~ $ cat /lib/systemd/system/ssh.service
[Unit]
Description=OpenBSD Secure Shell server
After=network.target auditd.service
ConditionPathExists=!/etc/ssh/sshd_not_to_be_run

[Service]
EnvironmentFile=-/etc/default/ssh
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=sshd.service



Target files

A systemd “target file” describes a state of the system, where a set of services are known to be operational. It can be thought of as an equivalent of the old-style runlevel. One of the targets is local-fs.target; when it is reached, the rest of the system can assume that all local filesystems are mounted and accessible. Other targets include network online.target and sound.target.

The dependencies of a target can be listed either within the target file (in the Requires= line), or using a symbolic link to a service file in the /lib/systemd/system/targetname.target.wants/ directory. For instance, /etc/systemd/system/printer.target.wants/ contains a link to /lib/systemd/system/cups.service; systemd will therefore ensure CUPS is running in order to reach printer.target.

==========================


systemd --> systemctl

Several utilities allow the administrator to interact with systemd and control the state of the system and of each component. One of them is systemctl. When run without any arguments, it lists all the unit files known to systemd (except those that have been disabled), as well as their status.

systemctl status gives a better view of the services, as well as the related processes. If given the name of a service (as in systemctl status ntp.service), it returns even more details, as well as the last few log lines related to the service.

Starting/ stopping a service can be done by running systemctl start <servicename.service> (or stop, if we want to stop) (other subcommands include reload and restart.) To control whether it will get started automatically on boot, use systemctl enable servicename.service (or disable)

pi@raspberrypi:/lib/systemd/system $ systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
   Active: active (running) since Thu 2017-02-02 23:26:45 CET; 1 weeks 0 days ago
 Main PID: 539 (sshd)
   CGroup: /system.slice/ssh.service
           ├─539 /usr/sbin/sshd -D
           ├─567 sshd: pi [priv]
           ├─573 sshd: pi@pts/0
           ├─577 -bash
           ├─619 sshd: pi [priv]
           ├─629 sshd: pi [priv]
           ├─631 sshd: pi@pts/1
           ├─633 -bash
           ├─657 sshd: pi@notty
           ├─661 /usr/lib/openssh/sftp-server
           └─700 systemctl status ssh.service


==========================


journald --> journalctl (only in Debian, not in Raspbian)

An interesting feature of systemd is that it includes a logging component named journald.  The messages can be displayed with journalctl command. Without any arguments,
it shows all messages occurred since system boot. To narrow it, it is better to use with a service identifier:

# journalctl -u ssh.service
-- Logs begin at Tue 2015-03-31 10:08:49 CEST, end at Tue 2015-03-31 17:06:02 CEST.
Mar 31 10:08:55 mirtuel sshd[430]: Server listening on 0.0.0.0 port 22.
Mar 31 10:08:55 mirtuel sshd[430]: Server listening on :: port 22.
...

==========================

systemctl status                   overview of currently available services
systemctl status <service>         displays runtime status information (e.g. systemctl status ssh.service)
systemctl start <service>          activates a service (e.g. systemctl start ssh.service) (stop is possible as well) 
systemctl restart <service>        restarts a service
systemctl reload <service>         reloads configuration of a service
systemctl enable <service>         enables a service to be started automatically during boot (disable possible as well)

==========================

No comments:

Post a Comment