summarylogtreecommitdiffstats
path: root/initcpio-install-systemd
blob: d59900c35b51f87bc7793cfb1942a2fca7de7a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash

add_systemd_unit() {
    # Add a systemd unit file to the initcpio image. Hard dependencies on binaries
    # and other unit files will be discovered and added.
    #   $1: path to rules file (or name of rules file)

    local unit= rule= entry= key= value= binary= dep=

    unit=$(PATH=/usr/lib/systemd/system:/lib/systemd/system type -P "$1")
    if [[ -z $unit ]]; then
        # complain about not found unit file
        return 1
    fi

    add_file "$unit"

    while IFS='=' read -r key values; do
        read -ra values <<< "$values"

        case $key in
            Requires|OnFailure)
                # only add hard dependencies (not Wants)
                map add_systemd_unit "${values[@]}"
                ;;
            Exec*)
                # do not add binaries unless they are required,
                # strip special executable prefixes
                case ${values[0]} in
                    -*)  ;;
                    !!*) add_binary "${values[0]#!!}" ;;
                    *)   add_binary "${values[0]#[@!:+]}" ;;
                esac
                ;;
        esac

    done <"$unit"

    # preserve reverse soft dependency
    for dep in {/usr,}/lib/systemd/system/*.wants/${unit##*/}; do
        if [[ -L $dep ]]; then
            add_symlink "$dep"
        fi
    done

    # add hard dependencies
    if [[ -d $unit.requires ]]; then
        for dep in "$unit".requires/*; do
            add_systemd_unit ${dep##*/}
        done
    fi
}

add_systemd_drop_in() {
    local unit=$1 dropin_name=$2

    mkdir -p "$BUILDROOT/etc/systemd/system/$unit.d"
    cat >"$BUILDROOT/etc/systemd/system/$unit.d/$2.conf"
}

build() {
    local rules unit

    add_binary /usr/bin/kmod /usr/bin/modprobe
    add_binary /usr/bin/mount
    add_binary /usr/bin/sulogin
    add_binary /usr/bin/umount
    add_binary /usr/lib/systemd/systemd /init

    map add_binary \
        /usr/bin/journalctl \
        /usr/bin/systemd-tmpfiles \
        /usr/lib/systemd/systemd-hibernate-resume \
        /usr/lib/systemd/systemd-shutdown \
        /usr/lib/systemd/systemd-sulogin-shell \
        /usr/lib/systemd/system-generators/systemd-fstab-generator \
        /usr/lib/systemd/system-generators/systemd-gpt-auto-generator \
        /usr/lib/systemd/system-generators/systemd-hibernate-resume-generator

    # udev rules
    map add_udev_rule "$rules" \
        50-udev-default.rules \
        60-persistent-storage.rules \
        64-btrfs.rules \
        80-drivers.rules \
        99-systemd.rules

    # systemd units
    map add_systemd_unit \
        initrd-cleanup.service \
        initrd-fs.target \
        initrd-parse-etc.service \
        initrd-root-fs.target \
        initrd-root-device.target \
        initrd-switch-root.service \
        initrd-switch-root.target \
        initrd-udevadm-cleanup-db.service \
        initrd.target \
        kmod-static-nodes.service \
        local-fs.target \
        local-fs-pre.target \
        paths.target \
        reboot.target \
        slices.target \
        sockets.target \
        swap.target \
        systemd-battery-check.service \
        systemd-fsck@.service \
        systemd-journald-audit.socket \
        systemd-journald-dev-log.socket \
        systemd-journald.service \
        systemd-modules-load.service \
        systemd-pcrphase-initrd.service \
        systemd-tmpfiles-setup-dev.service \
        systemd-udevd-control.socket \
        systemd-udevd-kernel.socket \
        systemd-udevd.service \
        systemd-udev-trigger.service \
        systemd-vconsole-setup.service \
        timers.target \
        rescue.target \
        emergency.target

    # add libraries dlopen()ed by tpm2-util
    for LIB in tss2-{esys,rc,mu,tcti-'*'}; do
        for FILE in $(find /usr/lib/ -maxdepth 1 -name "lib${LIB}.so*"); do
            if [[ -L "${FILE}" ]]; then
                add_symlink "${FILE}"
            else
                add_binary "${FILE}"
            fi
        done
    done

    add_symlink "/usr/lib/systemd/system/default.target" "initrd.target"
    add_symlink "/usr/lib/systemd/system/ctrl-alt-del.target" "reboot.target"

    printf '%s\n' >"$BUILDROOT/etc/nsswitch.conf" \
        'passwd: files' \
        'group: files' \
        'shadow: files'

    echo "root:x:0:0:root:/root:/bin/sh" >"$BUILDROOT/etc/passwd"
    echo 'root:*:::::::' >"$BUILDROOT/etc/shadow"
    getent group root audio disk input kmem kvm lp optical render sgx storage tty uucp video | awk -F: ' { print $1 ":x:" $3 ":" }' >"$BUILDROOT/etc/group"

    add_dir "/etc/modules-load.d"
    (
      . "$_f_config"
      set -f
      printf '%s\n' ${MODULES[@]} >"$BUILDROOT/etc/modules-load.d/MODULES.conf"
    )
}

help() {
    cat <<HELPEOF
This will install a basic systemd setup in your initramfs, and is meant to
replace the 'base', 'usr', 'udev' and 'resume' hooks. Other hooks with runtime
components will need to be ported, and will not work as intended. You also may
wish to still include the 'base' hook (before this hook) to ensure that a
rescue shell exists on your initramfs.
HELPEOF
}

# vim: set ft=sh ts=4 sw=4 et: