¿Quién soy?

Hablemos de systemd

¿De dónde sale systemd?

¿Quién usa systemd?

¿Qué es systemd?

¿Qué es systemd? (II)

¿Qué es systemd? (III)

¿Qué es systemd? (IV)

¿Qué es systemd? (V)

Unidad básica: Servicios

[root@localhost ~]# ls -l /etc/systemd/system/multi-user.target.wants/sshd.service
lrwxrwxrwx. 1 root root 36 uzt 14 20:26 /etc/systemd/system/multi-user.target.wants/sshd.service -> /usr/lib/systemd/system/sshd.service

[root@localhost ~]# cat /etc/systemd/system/multi-user.target.wants/sshd.service

[Unit]
Description=OpenSSH server daemon

After=syslog.target network.target auditd.service

[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

Características - Ficheros de servicio

Características - Ficheros de servicio - ejemplos

Características - Seguimiento de todo lo que se lanza

Características - autorespawn

Características - Gestión de recursos

.include /usr/lib/systemd/system/httpd.service

[Service]
CPUShares=1500
MemoryLimit=1G
BlockIOWeight=500
BlockIOWeight=/dev/disk/by-id/ata-SAMSUNG_MMCRE28G8MXP-0VBL1_DC06K01009SE009B5252 750
BlockIOWeight=/home/lennart 750
BlockIOReadBandwith=/var/log 5M

Características - Slices

Características - Journal

Características - Journal

Características - Journal (II)

Características - Journal - ejemplos

Características - Journal - más ejemplos

Características - Journal - Sellado

Características - Journal centralizado

Características - Watchdog

Características - Activación de servicios - Socket

Características - Activación de servicios - "inetd"

$ cat sshd.socket
[Unit]
Description=SSH Socket for Per-Connection Servers

[Socket]
ListenStream=22
Accept=yes

[Install]
WantedBy=sockets.target
cat sshd@.service
[Unit]
Description=SSH Per-Connection Server

[Service]
ExecStart=-/usr/sbin/sshd -i
StandardInput=socket

Características - Activación de servicios - Hardware

SUBSYSTEM=="printer", TAG+="systemd", ENV{SYSTEMD_WANTS}="printer.target"
SUBSYSTEM=="usb", KERNEL=="lp*", TAG+="systemd", ENV{SYSTEMD_WANTS}="printer.target"

Características - Activación de servicios - Path

[Unit]
Description=CUPS Printer Service Spool

[Path]
DirectoryNotEmpty=/var/spool/cups/d*

[Install]
WantedBy=multi-user.target

Características - Activación de servicios - Timer

$ cat /etc/systemd/system/timer-daily.timer
[Unit]
Description=Hourly Daily

[Timer]
OnBootSec=10min
OnUnitActiveSec=1d
Unit=timer-daily.target

[Install]
WantedBy=basic.target
$ cat /etc/systemd/system/timer-daily.target
[Unit]
Description=Daily Timer Target
StopWhenUnneeded=yes
$ cat /etc/systemd/system/timer-daily.target.wants/logrotate.service
[Unit]
Description=Update man-db

[Service]
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/bin/logrotate /etc/logrotate.conf
[Unit]
Description=Proceso intermitente

[Timer]
OnCalendar=Mon-Thu,Sat,Sun *-*-* 00:00:00
Unit=keinukari.target

[Install]
WantedBy=basic.target

Características - Debug

Características - Profiling

         10.257s plymouth-quit-wait.service
          9.480s firewalld.service
          5.598s accounts-daemon.service
          3.544s systemd-udev-settle.service
          3.465s systemd-logind.service
          3.465s rtkit-daemon.service

Características - Compatibilidad SysV

$ repoquery -q --qf="%{name}" --whatprovides '*/init.d/*' | grep -v sysv | grep -v initscript | grep -v systemv | sort -u | wc -l
156
$ repoquery -q --qf="%{name}" --whatprovides '*/lib/systemd/system/*' | sort -u | wc -l
626

Características - Seguridad

Características - Parámetros de ejecución

API journal

API journal

#include <stdio.h>

#define PREFIX_NOTICE "<5>"

int main(int argc, char *argv[]) {
        printf(PREFIX_NOTICE "Hello World\n");
        return 0;
}

API journal

#include <systemd/sd-journal.h>

int main(int argc, char *argv[]) {
        sd_journal_print(LOG_NOTICE, "Hello World");
        return 0;
}
$ gcc `pkg-config --cflags --libs libsystemd-journal` test.c -o test
$ ./test
$ journalctl -o verbose -o json-pretty /tmp/test
{
        "__CURSOR" : "s=2081d5de28a04e728de5058c502f7332;i=217d;b=7a39a4a3858f49149ed0f36f813d5f12;m=585ecd78;t=4e20a1ef5ebf5;x=73da254fe12d8871",
        "__REALTIME_TIMESTAMP" : "1374433003826165",
        "__MONOTONIC_TIMESTAMP" : "1482607992",
        "_BOOT_ID" : "7a39a4a3858f49149ed0f36f813d5f12",
        "_UID" : "0",
        "_GID" : "0",
        "_MACHINE_ID" : "7725dfc225d14958a625ddaaaea5962b",
        "PRIORITY" : "5",
        "_TRANSPORT" : "journal",
        "_HOSTNAME" : "localhost.localdomain",
        "_SELINUX_CONTEXT" : "unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023",
        "_AUDIT_SESSION" : "2",
        "_AUDIT_LOGINUID" : "0",
        "_SYSTEMD_CGROUP" : "/user/0.user/2.session",
        "_SYSTEMD_SESSION" : "2",
        "_SYSTEMD_OWNER_UID" : "0",
        "MESSAGE" : "Hello World",
        "CODE_FILE" : "test.c",
        "CODE_LINE" : "4",
        "CODE_FUNC" : "main",
        "SYSLOG_IDENTIFIER" : "test",
        "_PID" : "1886",
        "_COMM" : "test",
        "_EXE" : "/tmp/test",
        "_CMDLINE" : "./test",
        "_SOURCE_REALTIME_TIMESTAMP" : "1374433003822603"
}

API journal

#include <systemd/sd-journal.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
        sd_journal_send("MESSAGE=Hello World!",
                        "MESSAGE_ID=52fb62f99e2c49d89cfbf9d6de5e3555",
                        "PRIORITY=5",
                        "HOME=%s", getenv("HOME"),
                        "TERM=%s", getenv("TERM"),
                        "PAGE_SIZE=%li", sysconf(_SC_PAGESIZE),
                        "N_CPUS=%li", sysconf(_SC_NPROCESSORS_ONLN),
                        NULL);
        return 0;
}
$ gcc `pkg-config --cflags --libs libsystemd-journal` test.c -o proba
$ ./proba
$ journalctl -o verbose -o json-pretty journalctl MESSAGE_ID=52fb62f99e2c49d89cfbf9d6de5e3555
{
        "__CURSOR" : "s=2081d5de28a04e728de5058c502f7332;i=2189;b=7a39a4a3858f49149ed0f36f813d5f12;m=70c24a2d;t=4e20a375968aa;x=47dc6e8366092932",
        "__REALTIME_TIMESTAMP" : "1374433412999338",
        "__MONOTONIC_TIMESTAMP" : "1891781165",
        "_BOOT_ID" : "7a39a4a3858f49149ed0f36f813d5f12",
        "_UID" : "0",
        "_GID" : "0",
        "_MACHINE_ID" : "7725dfc225d14958a625ddaaaea5962b",
        "PRIORITY" : "5",
        "_TRANSPORT" : "journal",
        "_HOSTNAME" : "localhost.localdomain",
        "_SELINUX_CONTEXT" : "unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023",
        "CODE_FUNC" : "main",
        "CODE_FILE" : "test3.c",
        "CODE_LINE" : "13",
        "MESSAGE" : "Hello World!",
        "MESSAGE_ID" : "52fb62f99e2c49d89cfbf9d6de5e3555",
        "HOME" : "/root",
        "TERM" : "xterm",
        "PAGE_SIZE" : "4096",
        "N_CPUS" : "2",
        "SYSLOG_IDENTIFIER" : "proba",
        "_PID" : "2079",
        "_SOURCE_REALTIME_TIMESTAMP" : "1374433412998249"
}

API journal

from systemd import journal
journal.send('Hello world')
journal.send('Hello, again, world', FIELD2='Greetings!', FIELD3='Guten tag')

Otras funcionalidades - localectl

# localectl
   System Locale: LANG=eu_ES.UTF-8
       VC Keymap: es
      X11 Layout: es,us
     X11 Variant: ,
     X11 Options: grp:alt_shift_toggle
# localectl set-keymap es, us
# localectl
   System Locale: LANG=eu_ES.UTF-8
       VC Keymap: es,
VC Toggle Keymap: us
      X11 Layout: es,us
     X11 Variant: ,
     X11 Options: grp:alt_shift_toggle

Otras funcionalidades - hostnamectl

# hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 7725dfc225d14958a625ddaaaea5962b
           Boot ID: 2ab16e9c39da4387b62819cd24af08a3
    Virtualization: vmware
  Operating System: Fedora 19 (Schrödingerâs Cat)
       CPE OS Name: cpe:/o:fedoraproject:fedora:19
            Kernel: Linux 3.9.9-302.fc19.x86_64
      Architecture: x86_64

Otras funcionalidades - loginctl

# loginctl list-sessions
   SESSION        UID USER             SEAT
         1       1000 kaxero           seat0
        11          0 root
        17          0 root
        18          0 root

4 sessions listed.
# loginctl session-status 11
11 - root (0)
           Since: ar. 2013-07-16 20:35:28 CEST; 6 days ago
          Leader: 13217 (sshd)
          Remote: 192.168.2.65
         Service: sshd; type tty; class user
           State: active
          CGroup: systemd:/user/0.user/11.session
                  ââ13217 sshd: root@pts/1
                  ââ13223 -bash
# loginctl terminate-session 11
# echo $?
0
# loginctl terminate-session 11
Failed to issue method call: No such file or directory
# echo $?
1

Otras funcionalidades - timedatectl

# timedatectl
      Local time: ar. 2013-07-23 18:26:13 CEST
  Universal time: ar. 2013-07-23 16:26:13 UTC
        RTC time: ar. 2013-07-23 16:26:49
        Timezone: Europe/Madrid (CEST, +0200)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  ig. 2013-03-31 01:59:59 CET
                  ig. 2013-03-31 03:00:00 CEST
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  ig. 2013-10-27 02:59:59 CEST
                  ig. 2013-10-27 02:00:00 CET

Otras funcionalidades - Gestión de volúmenes cifrados

Comparación

Table 1. Comparación sistemas de arranque
SysV systemd Upstart OpenRC

Paralelismo

No

Si

Si

No

Respawn

No

Si

Si

No

Servicios no declarativos

No

Si

Si

No

Runlevels extra

No

Si

Si

No

Log early-boot

No

Si

No

No

Servicios levantados por socket

No

Si

No

No

Servicios levantados por path

No

Si

No

No

Dependencias resueltas en código

No

Si

No

Si

Enlaces de interes

EOF