Skip to content
gurucp edited this page Aug 5, 2016 · 10 revisions

#Let us Learn Systemd

1. What is systemd?

The systemd is the new system and service manager in Linux mainly from Fedora 18 and now adopted by many major Linux distro. The Systemd replaces the Upstart init daemon while providing backward compatibility for legacy System V ( Run Control scripts - RC ) service scripts.

The "systemd" offers the following benefits over init:

  • Services are started in parallel wherever possible using socket-based activation and D-Bus.

  • Daemons can be started on demand.

  • Processes are tracked using control groups (cgroups).

  • Snapshotting of the system state and restoration of the system state from a snapshot is supported.

  • Mount points can be configured as systemd targets.

The "systemd" is the first process that starts after the system boots, and is the final process that is running when the system shuts down. systemd controls the final stages of booting and prepares the system for use. systemd also speeds up booting by loading services concurrently.

SystemD Components

1.1 Linux Boot Process and "systemd"

When a Linux system boots, it performs the following operations:

  1. The computer's BIOS performs a power-on self-test (POST), and then locates and initializes any peripheral devices including the hard disk.

  2. The BIOS reads the Master Boot Record (MBR) into memory from the boot device. The MBR stores information about the organization of partitions on that device. On a computer with x86 architecture, the MBR occupies the first 512 bytes of the boot device. The first 446 bytes contain boot code that points to the boot loader program, which can be on the same device or on another device. The next 64 bytes contain the partition table. The final two bytes are the boot signature, which is used for error detection (MBR = 512 Bytes. 446 (Boot code) + 64 (Partition Table) + 2 (Signature)= 512.

Master Boot Record

  1. The default boot loader program used on most of the Linux is GRUB 2, which stands for GRand Unified Bootloader version 2. The boot loader loads the vmlinuz kernel image file into memory and extracts the contents of the initramfs image file into a temporary, memory-based file system (tmpfs).

  2. The kernel loads the driver modules from the initramfs file system that are needed to access the root file system.

  3. The kernel starts the systemd process with a process ID of 1 (PID 1). systemd is the ancestor of all processes on a system. systemd reads its configuration from files in the /etc/systemd directory. The /etc/systemd/system.conf file controls how systemd handles system initialization.

Example:

 [root@gcpadman-laptop ~]# ps 1
 PID TTY      STAT   TIME COMMAND
 1 ?        Ss     0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
  1. systemd reads the file linked by /etc/systemd/system/default.target, for example /usr/lib/systemd/system/multi-user.target, to determine the default system target. The system target file defines the services that systemd starts. systemd brings the system to the state defined by the system target, performing system initialization tasks such as:

Example:

    [root@gcpadman-laptop ~]# ls -l /etc/systemd/system/default.target
    lrwxrwxrwx. 1 root root 36 Jul 25 12:24 /etc/systemd/system/default.target -> /lib/systemd/system/graphical.target
  • Setting the host name.

  • Initializing the network.

  • Initializing SELinux based on its configuration.

  • Printing a welcome banner.

  • Initializing the system hardware based on kernel boot arguments.

  • Mounting the file systems, including virtual file systems such as the /proc file system.

  • Cleaning up directories in /var.

  • Starting swapping.

1.2 Fundamentals of systemd

###Note: Check this inittab file. This means no more inittab and only it's dead body present.

    [root@gcpadman-laptop ~]# cat /etc/inittab
    # inittab is no longer used.
    #
    # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
    #
    # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
    #
    # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
    #
    # multi-user.target: analogous to runlevel 3
    # graphical.target: analogous to runlevel 5
    #
    # To view current default target, run:
    # systemctl get-default
    #
    # To set a default target, run:
    # systemctl set-default TARGET.target

Now let us try to understand this by doing some reverse engineering rather then just reading. Please take example of any disabled service ( I have taken cups ):

    [root@gcpadman-laptop ~]# systemctl list-unit-files --type service | grep cups
    cups-browsed.service                        disabled
    cups.service                                disabled

Let me try to enable this service using systemctl and carefully check the output;

    [root@gcpadman-laptop ~]# systemctl enable cups.service
    Created symlink from /etc/systemd/system/printer.target.wants/cups.service to /usr/lib/systemd/system/cups.service.
    Created symlink from /etc/systemd/system/sockets.target.wants/cups.socket to /usr/lib/systemd/system/cups.socket.
    Created symlink from /etc/systemd/system/multi-user.target.wants/cups.path to /usr/lib/systemd/system/cups.path.

So systemctl created below 3 soft links for me..!!

    [root@gcpadman-laptop sockets.target.wants]# ls -l /etc/systemd/system/*/cups*
    lrwxrwxrwx 1 root root 33 Aug  4 21:25 /etc/systemd/system/multi-user.target.wants/cups.path -> /usr/lib/systemd/system/cups.path
    lrwxrwxrwx 1 root root 36 Aug  4 21:25 /etc/systemd/system/printer.target.wants/cups.service -> /usr/lib/systemd/system/cups.service
    lrwxrwxrwx 1 root root 35 Aug  4 21:25 /etc/systemd/system/sockets.target.wants/cups.socket -> /usr/lib/systemd/system/cups.socket

When /etc/inittab itself is not available then their must not be any run levels !!!! that is 100% right, looks like someone copied this concept from Solaris 10 where they use SMF concept to manage system runlevel and to manage services.

Systemd has replaced runlevels with target. Let us understand important targets;

    [root@gcpadman-laptop ~]# systemctl list-units --type target --all
    UNIT                   LOAD      ACTIVE   SUB    DESCRIPTION
    basic.target           loaded    active   active Basic System
    bluetooth.target       loaded    active   active Bluetooth
    cryptsetup.target      loaded    active   active Encrypted Volumes
    emergency.target       loaded    inactive dead   Emergency Mode
    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
    network-pre.target     loaded    inactive dead   Network (Pre)
    network.target         loaded    active   active Network
    nfs-client.target      loaded    active   active NFS client services
    nss-lookup.target      loaded    inactive dead   Host and Network Name Lookups
    nss-user-lookup.target loaded    active   active User and Group Name Lookups
    paths.target           loaded    active   active Paths
    remote-fs-pre.target   loaded    active   active Remote File Systems (Pre)
    remote-fs.target       loaded    active   active Remote File Systems
    rescue.target          loaded    inactive dead   Rescue Mode
    shutdown.target        loaded    inactive dead   Shutdown
    slices.target          loaded    active   active Slices
    sockets.target         loaded    active   active Sockets
    sound.target           loaded    active   active Sound Card
    swap.target            loaded    active   active Swap
    sysinit.target         loaded    active   active System Initialization
    ● syslog.target          not-found inactive dead   syslog.target
    time-sync.target       loaded    inactive dead   System Time Synchronized
    timers.target          loaded    active   active Timers
    umount.target          loaded    inactive dead   Unmount All Filesystems

After seeing the above example I feel for a sysadmin below are some of the important targets;

  • basic.target
  • emergency.target
  • graphical.target
  • multi-user.target
  • rescue.target
  • shutdown.target
  • sysinit.target
Clone this wiki locally