Richard Bruno
navigation
Home
admin









SystemD
October 18th, 2016

Table des matières



- units vs unit-files
- Target
- Journaux
- [Service] Section
- [Unit] Section
- [Install] Section
- Scripts
- De la doc ?
- Divers
- Et le fichier inittab ?
- Démarrage interactif


units vs unit-files



list-units command only displays units that systemd has attempted to parse and load into memory. Since systemd will only read units that it thinks it needs, this will not necessarily include all of the available units on the system.

list-units : unités chargées et actives
list-units -a : unités chargées mais inactives

To see every available unit file within the systemd paths, including those that systemd has not attempted to load, you can use the list-unit-files command instead.

Exemple d'un système pour lequel le firewall n'est pas activé.

L'unité n'est pas chargée et active

# systemctl list-units |grep firew
#


L'unité n'est pas chargée et inactive

# systemctl list-units --all |grep firew
#



L'unité existe cependant sur le système :

# systemctl list-unit-files |grep firew
firewalld.service
#



On peut la charger :

# systemctl enable firewalld.service
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/basic.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
#



Source : https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units

Target



Lists currently loaded target units.

#systemctl list-units --type target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
[...]



Changes the current target

systemctl isolate name.target



Default runlevel

Le runlevel par défaut est défini ainsi :

[root@centos7 system]# ls -al /etc/systemd/system
total 16
drwxr-xr-x. 10 root root 4096 10 sept. 22:00 .
drwxr-xr-x. 4 root root 4096 9 sept. 22:20 ..
drwxr-xr-x. 2 root root 54 9 sept. 22:22 basic.target.wants
lrwxrwxrwx. 1 root root 41 9 sept. 22:22 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root 44 9 sept. 22:20 dbus-org.freedesktop.Avahi.service -> /usr/lib/systemd/system/avahi-daemon.service
lrwxrwxrwx. 1 root root 46 9 sept. 22:21 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root 57 9 sept. 22:21 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root 37 9 sept. 22:30 default.target -> /lib/systemd/system/multi-user.target
[...]




[root@centos7 system]# ls -al /lib/systemd/system/*.target
[...]
lrwxrwxrwx. 1 root root 15 9 sept. 22:20 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 9 sept. 22:20 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 9 sept. 22:20 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 9 sept. 22:20 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 9 sept. 22:20 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 9 sept. 22:20 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 9 sept. 22:20 /lib/systemd/system/runlevel6.target -> reboot.target
[...]



ce qui peut se vérifier :

[root@centos7 system]# systemctl get-default
multi-user.target



Note :
to set the default target, use the command systemctl set-default targetname.target. targetname can be, among others:

rescue.target
multi-user.target
graphical.target
reboot.target



Journaux



root@filer-profilesb:/lib/systemd/system# journalctl -xn
-- Logs begin at lun. 2014-12-15 16:53:27 CET, end at mar. 2014-12-16 13:21:15 CET. --
déc. 16 13:21:15 filer-profilesb systemd[1]: iptables.service: main process exited, code=exited, status=203/EXEC
déc. 16 13:21:15 filer-profilesb systemd[1]: Unit iptables.service entered failed state.
déc. 16 13:21:15 filer-profilesb systemd[3535]: Failed at step EXEC spawning /root/ipt: Exec format error
-- Subject: Le processus /root/ipt n'a pas pu être exécuté
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Le processus /root/ipt n'a pas pu être exécuté, et a donc echoué.
--
-- Le code d'erreur renvoyé est 8.
[...]



[Service] Section



Type=
[...]
Behavior of oneshot is similar to simple; however, it is expected that the process has to exit before systemd starts follow-up units. RemainAfterExit= is particularly useful for this type of service. This is the implied default if neither Type= or ExecStart= are specified.
[...]

RemainAfterExit=

Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no.

Source : http://www.freedesktop.org/software/systemd/man/systemd.service.html

[Unit] Section



Requires=¶

Configures requirement dependencies on other units. If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or its activation fails, this unit will be deactivated. This option may be specified more than once or multiple space-separated units may be specified in one option in which case requirement dependencies for all listed names will be created. Note that requirement dependencies do not influence the order in which services are started or stopped. This has to be configured independently with the After= or Before= options. If a unit foo.service requires a unit bar.service as configured with Requires= and no ordering is configured with After= or Before=, then both units will be started simultaneously and without any delay between them if foo.service is activated. Often it is a better choice to use Wants= instead of Requires= in order to achieve a system that is more robust when dealing with failing services.

Note that dependencies of this type may also be configured outside of the unit configuration file by adding a symlink to a .requires/ directory accompanying the unit file. For details see above.

Wants=

A weaker version of Requires=. Units listed in this option will be started if the configuring unit is. However, if the listed units fail to start or cannot be added to the transaction, this has no impact on the validity of the transaction as a whole. This is the recommended way to hook start-up of one unit to the start-up of another unit.

Note that dependencies of this type may also be configured outside of the unit configuration file by adding symlinks to a .wants/ directory accompanying the unit file. For details, see above.

Source : http://www.freedesktop.org/software/systemd/man/systemd.unit.html

[Install] Section



"This section is not interpreted by systemd(1) during runtime. It is used exclusively by the enable and disable commands of the systemctl(1) tool during installation of a unit"

Source : http://www.freedesktop.org/software/systemd/man/systemd.unit.html

WantedBy=, RequiredBy=

This option may be used more than once, or a space-separated list of unit names may be given.
This has the effect that a dependency of type Wants= or Requires= is added from the listed unit to the current unit. The primary result is that the current unit will be started when the listed unit is started.

Scripts



Où sont les scripts
Dans /lib/systemd/system (<=> /usr/lib/systemd/system)

Si vous voulez en ajouter, il faut le faire dans /etc/systemd/system

[root@centos7 systemd]# ls -al /lib/systemd/system
total 780
drwxr-xr-x. 25 root root 12288 10 sept. 21:59 .
drwxr-xr-x. 12 root root 4096 9 sept. 22:20 ..
-rw-r--r--. 1 root root 275 10 juin 06:15 arp-ethers.service
-rw-r--r--. 1 root root 205 10 juin 03:48 atd.service
-rw-r-----. 1 root root 602 10 juin 05:31 auditd.service
lrwxrwxrwx. 1 root root 14 9 sept. 22:20 autovt@.service -> getty@.service
-rw-r--r--. 1 root root 1044 10 juin 07:26 avahi-daemon.service
-rw-r--r--. 1 root root 874 10 juin 07:26 avahi-daemon.socket
-rw-r--r--. 1 root root 546 10 juin 07:35 basic.target
drwxr-xr-x. 2 root root 4096 9 sept. 22:20 basic.target.wants
-r--r--r--. 1 root root 341 9 juin 21:38 blk-availability.service
-rw-r--r--. 1 root root 379 10 juin 07:35 bluetooth.target
-rw-r--r--. 1 root root 160 2 avril 17:30 brandbot.path
-rw-r--r--. 1 root root 101 2 avril 17:30 brandbot.service
-rw-r--r--. 1 root root 722 10 juin 07:35 console-getty.service
-rw-r--r--. 1 root root 745 10 juin 07:35 console-shell.service
[...]



Ils ressemblent à quoi les scripts ?

[root@centos7 system]# more /lib/systemd/system/ntpd.service
[Unit]
Description=Network Time Service
After=syslog.target ntpdate.service sntp.service

[Service]
Type=forking
EnvironmentFile=-/etc/sysconfig/ntpd
ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@centos7 system]#



Notes :
Unit files are built from several configurable sections, including unit descriptions and dependencies. Systemd also allows administrators to explicitly define a service's dependencies and load them before the given service starts by editing the unit files. Each unit file has a line that starts After= that can be used to define what service is required before the current service can start. WantedBy=lines specify that a target requires a given unit.


Exec format error

déc. 16 13:58:05 zzz systemd[3757]: Failed at step EXEC spawning /bin/test.sh: Exec format error
-- Subject: Le processus /bin/test.sh n'a pas pu être exécuté
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Le processus /bin/test.sh n'a pas pu être exécuté, et a donc echoué.



Solution : ne pas oublier le shebang en début de script...

De la doc ?




https://www.alionet.org/content.php?194-Systemd-comment-ca-marche
https://www.alionet.org/content.php?198-Systemd-comment-ca-marche-Partie-2
https://www.alionet.org/content.php?228-Systemd-comment-%E7a-marche-3%E9me-et-derni%E8re-partie
http://www.openlogic.com/wazi/bid/351296/An-introduction-to-systemd-for-CentOS-7

Divers



Activation du service pour un lancement au boot

[root@centos7 ]# yum install ntp



[root@centos7 ]# systemctl enable ntp



Vérification :

[root@centos7 ]# systemctl list-unit-files |grep ntp
ntpd.service enabled
ntpdate.service disabled



Lister les services

Liste les fichiers définissant des services :

systemctl -a [list-unit-files]



Liste des services activés (enable) :

systemctl -a [list-units]



The state will usually be "enabled", "disabled", "static", or "masked". In this context, static means that the unit file does not contain an "install" section, which is used to enable a unit. As such, these units cannot be enabled. Usually, this means that the unit performs a one-off action or is used only as a dependency of another unit and should not be run by itself.
Source : https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units


Obtenir des infos sur les services

root@filer-profilesb:/lib/systemd/system# systemctl status iptables
● iptables.service - lancement iptables
Loaded: loaded (/lib/systemd/system/iptables.service; enabled)
Active: failed (Result: start-limit) since lun. 2014-12-15 17:35:54 CET; 6min ago
Process: 1285 ExecStop=/bin/freeiptables (code=exited, status=203/EXEC)
Process: 1284 ExecStart=/bin/iptables (code=exited, status=203/EXEC)
Main PID: 1284 (code=exited, status=203/EXEC)

déc. 15 17:35:54 filer-profilesb systemd[1]: iptables.service start request repeated too quickly, refusing to start.
déc. 15 17:35:54 filer-profilesb systemd[1]: Failed to start lancement iptables.
déc. 15 17:35:54 filer-profilesb systemd[1]: Unit iptables.service entered failed state.
root@filer-profilesb:/lib/systemd/system#




Lister les services en échec

systemctl --failed



Relance de systemd après modification des services

systemctl daemon-reload



Et le fichier inittab ?



Il n'est plus utilisé : cf ici

Démarrage interactif



"Il existe un mode de démarrage interactif qui permet de confirmer chaque lancement de service en passant au noyau le paramètre « systemd.confirm_spawn=1 »"

Source : http://linuxfr.org/news/%C3%A9volutions-techniques-de-systemd




Dernières modifs
CentOS (November 1st, 2020)
SystemD (October 18th, 2016)
RHEL 6.4 (October 18th, 2016)
RHEL 6.3 bugs et cie (October 18th, 2016)
Fedora (October 18th, 2016)
Firewalld (October 18th, 2016)
Gestion des paquets (October 18th, 2016)

Contact
Pour m'envoyer un mail,
Pour me laisser un commentaire :
richard.brunooo
chez
gmail.com


powered by kure, modified by Bruno