systemd vs sysvinit part 2

Tags: systemctl, linux, systemd
Publish Date: 2016-05-15

For a quick and dirty cheatsheet, see my other post

Differences between systemd and sysvinit (a.k.a init) - Part 2

In part 1 of this article, I have provided an overview of how sysvinit boots up and manages services. Here in part 2, I will show how this has changed with systemd.

 

Systemd - Boot Process

While in sysvinit there is a concept of runlevels, systemd is based on targets.

The idea behind a target is that a collection of units have to be activated when the system reaches a certain target. You can see the activation of units like dependencies for targets.

A target could be something like the old runlevels (e.g. multi-user.target), but it could also be something like hardware or network detection (e.g. network-online.target). A unit could be something like a service, mount, socket etc., but a target itself is also a unit.

So you can interpret this as: "Make sure that certain services, mounts, sockets etc are activated when my system reaches a certain state."

For a list of units in systemd see systemctl -t help

[root@10 system]# systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
snapshot
timer
swap
path
slice
scope

During the boot process, the first process (pid 1) is (/lib/systemd/)systemd. In fact, you will see that the old /sbin/init is a symlink to /lib/systemd/systemd

[root@10 system]# ls -l /sbin/ | grep systemd
lrwxrwxrwx. 1 root root       22 Jul 14  2015 init -> ../lib/systemd/systemd

Since systemd is no longer based on runlevels, it has gotten rid of the /etc/inittab file, where the default runlevel used to be defined. Instead, systemd finds its default target through /etc/systemd/system/default.target.

[root@10 system]# ls -l /etc/systemd/system
total 4
drwxr-xr-x. 2 root root   54 Jul 14  2015 basic.target.wants
lrwxrwxrwx. 1 root root   41 Jul 14  2015 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root   46 Jul 14  2015 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root   57 Jul 14  2015 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx  1 root root   41 May 13 11:21 default.target -> /usr/lib/systemd/system/multi-user.target
drwxr-xr-x. 2 root root   85 Jul 14  2015 default.target.wants
drwxr-xr-x. 2 root root   31 Jul 14  2015 getty.target.wants
drwxr-xr-x. 2 root root 4096 Apr  6 09:45 multi-user.target.wants
drwxr-xr-x. 2 root root   49 Jul 14  2015 sockets.target.wants
drwxr-xr-x. 2 root root   59 Jul 14  2015 sysinit.target.wants
drwxr-xr-x. 2 root root   43 Jul 14  2015 system-update.target.wants

As you can see, my default.target is symlinked to multi-user.target. Which is a bit comparable to the old runlevel 3.

We can get and set the default runlevel with systemctl set-default and systemctl get-default

[root@10 system]# systemctl get-default 
multi-user.target
[root@10 system]# systemctl set-default graphical.target
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'

For simplicity's sake, let's assume that I have set my default target back to multi-user.target. If we take a look at the multi-user.target file, we will see the following:

[root@10 system]# cat /usr/lib/systemd/system/multi-user.target

[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes

[Install]
Alias=default.target

Requires=basic.target means that all units within the basic.target.wants directories (more on the .wants directories later) must be activated first before the current unit (multi-user.target) can be activated. So for now, the control is handed over to basic.target, which in its turn requires sysinit.target.

Requires=sysinit.target

In sysinit.target, there is no Requires= directive specified, but it does have a Wants= directive.

Wants=local-fs.target swap.target

The Wants= directive lists the units that the current unit will try to activate. However, if these units fail to be activated, it won't stop the current unit (in this case sysinit.target) from being activated.

What systemd will do at this point, is to look for .wants directories under /etc/systemd/system/ and (/usr)/lib/systemd/system/ (more on their differences later) with the name of the the specified target. In this case, it finds /usr/lib/systemd/system/local-fs.target.wants/ and activates the units within that directory.

From that point, the control is passed back up the chain again. First back to sysinit.target and activates units within sysinit.target.wants in both /etc/systemd/system and (/usr)/lib/systemd/system directories, then the same for basic.target and finally back to multi-user.target. A great way to visualize how control has been passed from one target to another and which units have been activated in the process, is to use systemctl list-dependencies <name.target>

It is important to note that services in systemd are activated in a parallel manner, whereas in sysvinit was done sequentially (by the number of K- and S- scripts in runlevel directories).

Another note of importance is that there is a difference between the .wants directories in /etc/systemd/system/ and (/usr)/lib/systemd/system/. While the .wants directories in the former is setup by the system and should never be touched by users, the .wants directories in the latter allow the user to manage his own dependencies. This becomes obvious when you enable / disable services to be run upon boot.

Systemd - Enable service at startup

When you install a service (such as nginx in this example), your package manager will create a .service file in the /usr/lib/systemd/system/ directory.

[root@10 vagrant]# ls /usr/lib/systemd/system | grep nginx
nginx-debug.service
nginx.service

To make sure that a service is enabled upon boot, you can use the systemctl enable command:

[root@10 vagrant]# systemctl enable nginx
ln -s '/usr/lib/systemd/system/nginx.service' '/etc/systemd/system/multi-user.target.wants/nginx.service'

What we can see above, is that the systemctl enable command actually creates a symlink from the .wants directory of our default target (in our case multi-user.wants) to /usr/lib/systemd/system/ for our service. In a way a bit comparable to how the scripts in runlevel directories (/etc/rc{number}.d/) in sysvinint were symlinked to scripts in /etc/init.d/.

You can also use systemctl disable <service> to disable a service. In that case, the previously created symlink will be removed.

Systemd - start / stop services

In sysvinit, there were different commands for managing services at startup (chkconfig) and for running/stopping services (service). In systemd, that is all controlled by the systemctl command.

To start a service, use systemctl start <service>

[root@10 vagrant]# systemctl start nginx
[root@10 vagrant]# systemctl status nginx
nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Sun 2016-05-15 14:35:07 UTC; 2s ago
     Docs: http://nginx.org/en/docs/
  Process: 2735 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2744 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 2743 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 2747 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2747 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─2748 nginx: worker process


Systemd - Change targets

In sysvinit, a sys-admin might have to change runlevel to perform administrative tasks. In systemd, since there are no runlevels anymore, you will have to change target:

[root@10 system]# systemctl isolate rescue.target
[root@10 system]# who -r
[root@10 system]# runlevel
3 1 # indicates that the current runlevel is 1 and previous runlevel is 3

Ironically enough, to get the current target, you would still use the old who -r and runlevel commands.

Quick and dirty systemvinit vs systemd vs upstart cheatsheet

Tags: linux, upstart, runlevel, systemctl, service, sysvinit, systemd
Publish Date: 2016-05-13

Quick and dirty cheatsheet

 

systemVinit

systemd

upstart

Start a service

service <name> start

systemctl start <name>

service <name> start

Stop a service

service <name> stop

systemctl stop <name>

service <name> stop

Enable service at startup

chkconfig <name> on

 

chkconfig <name> --level 2,3

systemctl enable <name>

update-rc.d <name> enable

 

update-rc.d <name> enable 2 3

 

 

Disable service at startup

chkconfig <name> off

systemctl disable <name>

update-rc.d <name> disable

Set default runlevel

Edit in /etc/inittab

 

id:<runlevel>:initdefault:

systemctl set-default <name.target>

Edit in /etc/init/rc-sysinit.conf

 

env DEFAULT_RUNLEVEL=2

Get default runlevel

Read from /etc/inittab

 

id:<runlevel>:initdefault:

systemctl get-default

Read from

/etc/init/rc-sysinit.conf

Set current runlevel

init <number>

telinit <number>

systemctl isolate <name.target>

init <number>

telinit <number>

Get current runlevel

runlevel

who -r

runlevel

who -r

runlevel

who -r