Table des matières

Systemd admin howto

https://wiki.debian.org/systemd

basics commands

''systemctl'' : List all running services
''systemctl start example1'' : Activates the service "example1" immediately
''systemctl stop example1'' : Deactivates the service "example1" immediately
''systemctl restart example1'' : Restarts the service "example1" immediately: 
''systemctl status example1'' : Shows status of the service "example1"
''systemctl enable example1'' : Enables "example1" to be started on bootup
''systemctl disable example1'' : Disables "example1" to neot start during bootup
''systemctl list-units --type=service''  : list all service units

add a service

vi /etc/systemd/system/<service_name>.service
chmod 644 /etc/systemd/system/<service_name>.service
systemctl --system daemon-reload
systemctl start mon-service.service
systemctl enable mon-service.service

debugging

See all systemctl since last boot : journalctl -xa
Remove “quiet” from Kernel command line (so called “cmdline” or “grub line”) : GRUB_CMDLINE_LINUX=“systemd.log_target=kmsg systemd.log_level=debug” ⇒ update-grub ⇒ check : cat /proc/cmdline after reboot
Increase verbosity via /etc/systemd/system.conf : uncomment : “LogLevel=debug” and “LogTarget=syslog-or-kmsg”.
Boot an emergency shell : Add systemd.unit=rescue.target to Kernel command line.
Enable the debug shell : systemctl enable debug-shell.service (You can do this in a chroot environment after booting a rescue system.) This start a root shell on TTY 9.
See : http://freedesktop.org/wiki/Software/systemd/Debugging

Warning

bind mount : 'umount $CHROOT/dev' will unmount /dev !

mount --bind --make-rslave / $CHROOT
mount --bind --make-rslave /dev/ $CHROOT/dev

Optimize

systemd-analyze
systemd-analyze blame
systemd-analyze critical-chain [UNIT...]

Graphs, dump, etc : http://www.freedesktop.org/software/systemd/man/systemd-analyze.html

Understand

Systemd-bootchart : http://www.freedesktop.org/software/systemd/man/systemd-bootchart.html

apt-get install graphviz
systemd-analyze plot > plot.svg
systemd-analyze dot --order | dot -Tsvg > systemd.svg

See bootchart2 ?

Unit files definition precedence

  1. 1st, running : /run/systemd/system
  2. 2nd, customs : /etc/systemd/system
  3. 3rd, originals : /lib/systemd/system

Systemd delta

systemd-delta : To see all local configuration:
systemd-delta /run : To see all runtime configuration:
systemd-delta systemd/system : To see all system unit configuration changes:
systemd-delta --type=extended /run/systemd/system : To see all runtime "drop-in" changes for system units:

Systemd service files

from : https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files

Some features

Unit files definition precedence

types of units : <servicename>.<typeofunit>, ex : sshd.service

units files : case sensitive sections

[Unit] section directives (mandatory, describes the unit, the dependancies, the conditions)

Unit-Specific Section Directives ( directives that only apply to a specific type)

not available for device, target, snapshot, and scope.

[Service] Section

[Socket] Section (socket-based activation, Each socket unit must have a matching service unit that will be activated when the socket receives activity)

[Mount] Section (leading slash is removed, all other slashes are translated into dashes "-", and all dashes and unprintable characters are replaced with C-style escape codes ; implicit dependency on other mounts above it in the hierarchy)

[Automount] Section (allows an associated .mount unit to be automatically mounted at boot, Mount notation)

[Swap] Section (swap file or device, Mount notation)

[Path] Section (defines a filesystem path that systmed can monitor for changes)

Another unit must exist that will be be activated when certain activity is detected at the path location. Path activity is determined thorugh inotify events.

[Timer] Section (used to schedule tasks to operate at a specific time or after a certain delay.)

[Slice] Section : man systemd.resource-control

[Install] Section Directives (optional)

Creating Instance Units from Template Unit Files

Provide flexibility in configuring units by allowing certain parts of the file to utilize dynamic information that will be available at runtime. identification :

Templating (within the usual unit definition)