Wednesday, December 18, 2019

Selecting systemd target

A systemd target is a set of systemd units  that should be started to reach desired state.
Important target are listed below.

Target
Purpose
graphical.target
System supports multiple user,graphical and test-based logins
multi-user.target
System support multiple users, text-based logins only.
rescue.target
Sulogin prompt, Basic system initilzation completed
emergency.target
Sulogin prompt, initramfs pivot complete and system root mounted on / read-only

It is possible for a target to be part of another target. For eg, the graphical.target include multi-user.target, which in turn depends on basic-target and others.
This dependencies can be viewed using the following command
[root@server1 ~]# systemctl list-dependencies graphical.target  | grep target
graphical.target
● └─multi-user.target
●   ├─basic.target
●   │ ├─paths.target
●   │ ├─slices.target
●   │ ├─sockets.target
●   │ ├─sysinit.target
●   │ │ ├─cryptsetup.target
●   │ │ ├─local-fs.target
●   │ │ └─swap.target
●   │ └─timers.target
●   ├─getty.target
●   ├─nfs-client.target
●   │ └─remote-fs-pre.target
●   └─remote-fs.target
●     └─nfs-client.target
●       └─remote-fs-pre.target

An overview of all available target can viewed with
[root@server1 ~]# systemctl list-units --type=target --all
  UNIT                   LOAD      ACTIVE   SUB    DESCRIPTION
  basic.target           loaded    active   active Basic System
  cryptsetup.target      loaded    active   active Encrypted Volumes
  emergency.target       loaded    inactive dead   Emergency Mode
  final.target           loaded    inactive dead   Final Step
  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
  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
  ....
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

30 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

An overview of all targets installed on disk can be viewed with.
[root@server1 ~]# systemctl list-unit-files --type=target
UNIT FILE                 STATE
anaconda.target           static
basic.target              static
bluetooth.target          static
cryptsetup-pre.target     static
cryptsetup.target         static
ctrl-alt-del.target       disabled
default.target            enabled
emergency.target          static
final.target              static
getty.target              static
graphical.target          enabled
halt.target               disabled
hibernate.target          static
..
60 unit files listed.

Selecting a target at run-time.
On running system , administrator can choose to switch to a different target using the systemctl isolate TARGET command.
[root@server1 ~]# systemctl isolate graphical.target
Not all target can be isolated . Only target that have AllowIsolate=yes set in their unit file can be isolated.

Set the default target
When the system starts, and control is passed over to systemd from the initramfs, systemd will try to activate the default.target . Normally the default.target will be symbolic link to either graphical.target or multi-user.target

Systemctl provide two command to manage the link. get-default and set-default
[root@server1 ~]# systemctl get-default
graphical.target
[root@server1 ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@server1 ~]#  systemctl get-default
multi-user.target

Selecting a different target at boot time.
To select a different target at boot time, a special option can be appended to the kernel command line from the boot loader. systemd.unit= .
For eg to boot the system into a resuce shell, pass the following option at the interactive boot loader menu
systemd.unit=rescue.target
To use this method  of selecting a different target. Use the following procedure for RHEL 7
1. (Re)boot the machine
2. Interrupt the boot loader menu countdown by pressing any key
3. Move the cursor to the entry to be started
4. Press e to edit the current entry
5. Move the cursor to the in that starts with linux16. This is kernel command line.
6 Append systemd.unit=rescue.target
7. Ctrl+x to boot with these changes.

Diagnose and repair systemd boot issues.
If there are problem during the starting of services, there few tools available to System Administrator that can help with debugging and/or troubleshooting.

Early Debug shell
By running systemctl enable debug-shell.service, a root shell will be spawned on TTY9(Ctrl+alt+F9) early during the boot sequence. This shell is automatically logged in as root so that administrator can use some of the other debugging tools while the system is still booting.

Note:  Disable the debug-shell.service service when debugging is completed, as it leaves unauthenticated root shell open to anyone with local console access.

Emergency and rescue shell

Append the following into kernel command line from the boot loader
systemd.unit=rescue.target
systemd.unit=emergency.target 

The system will span into a special rescue or emergency shell instead of starting normally. Both of these shell require root password.

The emeregency target keeps the root file system mounted in read-only mode.
The rescue target waits for sysinit.target to complete first so that more of the system will be initialized, for eg logging, file system etc.

Exiting from these shell will continue with the regular boot process.

Stuck Jobs
During startup, systemd spawns a number of jobs. if some of these jobs cannot be completed , they will block other jobs from running.
To inspect the current job list systemctl list-jobs command is used.
Any jobs list as running must completed before the jobs listed as waiting can continue.
[root@server1 ~]# systemctl list-jobs
No jobs running.

No comments: