Sunday, January 26, 2014

What Is Systemd and How It Works (Part 3)

Hello everyone,
Today, I continue the systemd discussion, part3. If you haven't read or missed part 1 and 2, you can find it in the following links:
http://ktaraghi.blogspot.ca/2013/11/what-is-systemd-and-how-it-works-part-1.html and http://ktaraghi.blogspot.ca/2013/12/what-is-systemd-and-how-it-works-part-2.html.
In part 3, I would like to talk about special targets and template unit files. systemd has a set of special target files designed for special purposes. you can find a list of special targets in man page, man systemd.special, Figure 1.

                                                                      Figure 1

Some of these special targets are used for on demand services such as printer and bluetooth. So, when a printer is connected and turned on, the printer target becomes active (printer.target). Another example can be sound.target. It's activated when the system detects sound card at boot or it plugs in.
As I said, a full list of special targets are available in manpage including explanations (man systemd.special), however, I tried to explain some them:

basic.target: It loads basic system and boot-up. It pull-in mount points, sockets, and other basic initializations necessary for general purpose daemons.

                                                                          Figure 2

ctrl-alt-del.target: Activated when the user press Ctrl+Alt+Del keys and it is a link to the reboot.target which reboots the system.
default.target: It references to special target to be activated on boot (usually multi-user.target or graphical.target). It's equivalent to run level 3 or 5 in system V.
emergency.target: Starts base system with an emergency shell on the main console.
multi-user.target: Starts up command line interface and it's multi-user (Similar to runlevel 3)
graphical.target: Starts GUI interface (Similar to runlevel 5)
poweroff.target: For shutting down the system.
reboot.target: For rebooting system.
rescue.target: For setting up the base system and a rescue shell
shutdown.target: For terminating the services on system shutdown
sysinit.target: For covering early boot-up scripts
system-update.target: For an offline system update.
exit.target: For shutting down the user service manager


Template unit files


There is a special type of unit file called a template file. A template file name ends with an @ sign. If a corresponding unit file is not found for a service, systemd will check to see if there is template file that can be applied to it. For example, you don't know how many terminals you may use, therefore, they are generated automatically using the getty@.service unit file. Let's take a look at getty@.service: Figure 3

                                                                         Figure 3

Here, since it's template, %I is used to substitute for the actual service name. For example, if the service name is getty@tty2, %I is substituted with tty2.

Finally, systemctl command is used to control the systemd system and service manager. You can still use the "service" command, however, it redirects to systemctl after running "service" command. Figure 4 shows that how we can use the systemctl and service command:

                                                                           Figure 4

And pretty much that's it. Hope it was useful and you enjoyed.
Khosro Taraghi