#### Beyond Linux® From Scratch (systemd Edition) - Version r13.0-491

### Chapter 12. System Utilities

- Prev

  Sysstat-12.7.9

- Next

  UDisks-2.11.1

- Up
- Home

# Systemd-260.1

## Introduction to systemd

While systemd was installed when building LFS, there are many features provided by the package that were not included in the initial installation because Linux-PAM was not yet installed. The systemd package needs to be rebuilt to provide a working systemd-logind service, which provides many additional features for dependent packages.

### Note

Development versions of BLFS may not build or run some packages properly if LFS or dependencies have been updated since the most recent stable versions of the books.

### Package Information

- Download (HTTP): https://github.com/systemd/systemd/archive/v260.1/systemd-260.1.tar.gz

- Download MD5 sum: 8a54e8e7736f5a047c990ab570dae7f7

- Download size: 17 MB

- Estimated disk space required: 420 MB (with tests)

- Estimated build time: 1.3 SBU (with tests using 4 cores)

### systemd Dependencies

#### Recommended

### Note

Linux-PAM-1.7.2 is not strictly required to build systemd, but the main reason to rebuild systemd in BLFS (it's already built in LFS anyway) is for the systemd-logind daemon and the `pam_systemd.so` PAM module. Linux-PAM-1.7.2 is required for them. All packages in BLFS book with a dependency on systemd expect that it has been rebuilt with Linux-PAM-1.7.2.

Linux-PAM-1.7.2 and Polkit-127 (runtime)

#### Optional

btrfs-progs-6.19.1, cURL-8.19.0, cryptsetup-2.8.6, git-2.53.0, iptables-1.8.12, libarchive-3.8.7, libidn2-2.3.8, libpwquality-1.4.5, libseccomp-2.6.0, libxkbcommon-1.13.1, make-ca-1.16.1, p11-kit-0.26.2, qemu-11.0.0, libqrencode-4.1.1, rsync-3.4.2, sphinx-9.1.0, Valgrind-3.26.0, zsh-5.9 (for the zsh completions), AppArmor, audit-userspace, bash-completion, jekyll, kexec-tools, libbpf, libdw, libfido2, libmicrohttpd, pefile, pyelftools, quota-tools, rpm, SELinux, systemtap, tpm2-tss and Xen

#### Optional (to rebuild the manual pages)

docbook-xml-4.5, docbook-xsl-nons-1.79.2, libxslt-1.1.45, and lxml-6.1.0 (to build the index of systemd manual pages)

Editor Notes: https://wiki.linuxfromscratch.org/blfs/wiki/Logind

## Installation of systemd

### Warning

If a previous version of systemd has been installed, remove a service that will generate errors on following boots. As the `root` user:

    rm -fv /usr/lib/systemd/system/systemd-update-utmp-runlevel.service

Remove two unneeded groups, `render` and `sgx`, from the default udev rules:

    sed -i -e 's/GROUP="render"/GROUP="video"/' \
           -e 's/GROUP="sgx", //' rules.d/50-udev-default.rules.in

Rebuild systemd by running the following commands:

    mkdir build &&
    cd    build &&

    meson setup ..                 \
          --prefix=/usr            \
          --buildtype=release      \
          -D default-dnssec=no     \
          -D firstboot=false       \
          -D install-tests=false   \
          -D ldconfig=false        \
          -D man=auto              \
          -D sysusers=false        \
          -D rpmmacrosdir=no       \
          -D homed=disabled        \
          -D mode=release          \
          -D pam=enabled           \
          -D pamconfdir=/etc/pam.d \
          -D dev-kvm-mode=0660     \
          -D nobody-group=nogroup  \
          -D sysupdate=disabled    \
          -D ukify=disabled        \
          -D docdir=/usr/share/doc/systemd-260.1 &&

    ninja

### Note

For the best test results, make sure you run the test suite from a system that is booted by the same systemd version you are rebuilding.

To test the results, issue: ninja test. The test named `test-stat-util` is known to fail if some kernel features are not enabled. If the test suite is run as the `root` user, some other tests may fail because they depend on various kernel configuration options.

Now, as the `root` user:

    ninja install

## Command Explanations

`--buildtype=release`: Specify a buildtype suitable for stable releases of the package, as the default may produce unoptimized binaries.

`-D pamconfdir=/etc/pam.d`: Forces the PAM files to be installed in /etc/pam.d rather than /usr/lib/pam.d.

`-D homed=disabled`: Removes a daemon that does not offer any use under a traditional BLFS configuration, especially using accounts created with useradd. To enable systemd-homed, first ensure that you have cryptsetup-2.8.6 and libpwquality-1.4.5 installed, and then change “disabled” to “enabled” in the above meson setup command.

`-D ukify=disabled`: Removes a script for combining a kernel, an initramfs, and a kernel command line etc. into an UEFI application which can be loaded by the UEFI firmware to start the embedded Linux kernel. It's not needed for booting a BLFS system with UEFI. Additionally, it requires the pefile Python module at runtime, so if it's enabled but pefile is not installed, one test for it will fail in the test suite. To enable systemd-ukify, install the pefile module and then change “disabled” to “enabled” in the above meson setup command.

## Configuring systemd

The `/etc/pam.d/system-session` file needs to be modified and a new file needs to be created in order for systemd-logind to work correctly. Run the following commands as the `root` user:

    grep 'pam_systemd' /etc/pam.d/system-session ||
    cat >> /etc/pam.d/system-session << "EOF"
    # Begin Systemd addition

    session  required    pam_loginuid.so
    session  optional    pam_systemd.so

    # End Systemd addition
    EOF

    cat > /etc/pam.d/systemd-user << "EOF"
    # Begin /etc/pam.d/systemd-user

    account  required    pam_access.so
    account  include     system-account

    session  required    pam_env.so
    session  required    pam_limits.so
    session  required    pam_loginuid.so
    session  optional    pam_keyinit.so force revoke
    session  optional    pam_systemd.so

    auth     required    pam_deny.so
    password required    pam_deny.so

    # End /etc/pam.d/systemd-user
    EOF

As the `root` user, replace the running systemd manager (the init process) with the systemd executable newly built and installed:

    systemctl daemon-reexec

A desktop environment often runs as a group of systemd user services. Those services are spawned by the systemd per-user instance, instead of the login shell. To ensure those services get the environment variables we've set via `/etc/profile.d`, as the `root` user, install a systemd environment generator to dump all the relevant exported environment variables into the systemd per-user instance when the instance starts to run:

    install -vdm755 /etc/systemd/user-environment-generators &&
    cat > /etc/systemd/user-environment-generators/50-profile.sh << "EOF" &&
    #!/usr/bin/env -S -i /usr/bin/bash
    # SPDX-License-Identifier: MIT

    . /etc/profile

    # Systemd should have already set a better value for them.
    unset XDG_RUNTIME_DIR
    for i in $(locale); do
      unset ${i%=*}
    done

    # Some shell magic that we don't want to expose.
    unset SHLVL

    # Systemd does not want to pass functions to the environment
    for i in $(declare -pF | awk '{print $3}'); do
      unset -f $i
    done

    python3 << _EOF
    import os
    for var in os.environ:
      # Simply unsetting them in shell does not work.
      if var in ['LC_CTYPE', '_']:
        continue

      print(var + '=' + os.environ[var])
    _EOF
    EOF

    chmod -v 755 /etc/systemd/user-environment-generators/50-profile.sh

Read systemd.environment-generator(7) for details about the systemd environment generators. Note that if you've edited the content of `/etc/profile.d` when a desktop environment is running, you need to sync the change into the systemd per-user instance:

    systemctl --user unset-environment \
      $(/etc/systemd/user-environment-generators/50-profile.sh | sed 's/=.*//')
    systemctl --user daemon-reload

And then log out and log in again to sync the change into the desktop environment. The systemctl --user unset-environment command is needed on some desktop environments which write the environment variables into the runtime environment variable pool of the per-user systemd instance because the content of that pool would override the setting from the generator.

### Important

Now ensure Shadow-4.19.4 has been already rebuilt with Linux-PAM-1.7.2 support first, then logout, and login again. This will ensure that the running login session is registered with systemd-logind, and that a per-user systemd instance is running for each user owning a login session. Many BLFS packages listing systemd as a dependency need the systemd-logind integration and/or a running per-user systemd instance at runtime.

### Warning

If you are upgrading from a previous version of systemd and an initrd is used for system boot, you should generate a new initrd before rebooting the system.

## Contents

A list of the installed files, along with their short descriptions can be found at ../../../../lfs/view/systemd/chapter08/systemd.html#contents-systemd.

Listed below are the newly installed programs along with short descriptions.

Installed Programs: homectl (optional), systemd-cryptenroll (if cryptsetup-2.8.6 is installed), and systemd-cryptsetup (if cryptsetup-2.8.6 is installed),

### Short Descriptions

homectl | is a tool to create, remove, change, or inspect a home directory managed by systemd-homed; note that it's useless for the classic UNIX users and home directories which we are using in LFS/BLFS book
systemd-cryptenroll | Is used to enroll or remove a system from full disk encryption, as well as set and query private keys and recovery keys
systemd-cryptsetup | Attaches or removes an encrypted block device
`pam_systemd.so` | is a PAM module used to register user sessions with the systemd login manager, systemd-logind

- Prev

  Sysstat-12.7.9

- Next

  UDisks-2.11.1

- Up
- Home