net-misc/hoppscotch-bin: bump version to 24.12.1.0 (#6084)
[gentoo-zh.git] / sys-kernel / mkinitcpio / files / initcpio-install-systemd
blobda822f8a612618ab4c464c7990a8b1b8a582b651
1 #!/bin/bash
3 add_udev_rule() {
4 # Add an udev rules file to the initcpio image. Dependencies on binaries
5 # will be discovered and added.
6 # $1: path to rules file (or name of rules file)
8 local rules="$1" rule= key= value= binary=
10 if [[ ${rules:0:1} != '/' ]]; then
11 rules=$(PATH=/usr/lib/udev/rules.d:/lib/udev/rules.d type -P "$1")
13 if [[ -z $rules ]]; then
14 # complain about not found rules
15 return 1
18 add_file "$rules" /usr/lib/udev/rules.d/"${rules##*/}"
20 while IFS=, read -ra rule; do
21 # skip empty lines, comments
22 [[ -z $rule || $rule = @(+([[:space:]])|#*) ]] && continue
24 for pair in "${rule[@]}"; do
25 IFS=' =' read -r key value <<< "$pair"
26 case $key in
27 RUN@({program}|+)|IMPORT{program}|ENV{REMOVE_CMD})
28 # strip quotes
29 binary=${value//[\"\']/}
30 # just take the first word as the binary name
31 binary=${binary%% *}
32 [[ ${binary:0:1} == '$' ]] && continue
33 if [[ ${binary:0:1} != '/' ]]; then
34 binary=$(PATH=/usr/lib/udev:/lib/udev type -P "$binary")
36 add_binary "$binary"
38 esac
39 done
40 done <"$rules"
43 add_systemd_unit() {
44 # Add a systemd unit file to the initcpio image. Hard dependencies on binaries
45 # and other unit files will be discovered and added.
46 # $1: path to rules file (or name of rules file)
48 local unit= rule= entry= key= value= binary= dep=
50 unit=$(PATH=/usr/lib/systemd/system:/lib/systemd/system:/usr/lib64/systemd/system:/lib64/systemd/system type -P "$1")
51 if [[ -z $unit ]]; then
52 # complain about not found unit file
53 return 1
56 add_file "$unit"
58 while IFS='=' read -r key values; do
59 read -ra values <<< "$values"
61 case $key in
62 Requires|OnFailure)
63 # only add hard dependencies (not Wants)
64 map add_systemd_unit "${values[@]}"
66 Exec*)
67 # do not add binaries unless they are required,
68 # strip special executable prefixes
69 case ${values[0]} in
70 -*) ;;
71 !!*) add_binary "${values[0]#!!}" ;;
72 *) add_binary "${values[0]#[@!:+]}" ;;
73 esac
75 esac
77 done <"$unit"
79 # preserve reverse soft dependency
80 for dep in {/usr,}/lib/systemd/system/*.wants/${unit##*/}; do
81 if [[ -L $dep ]]; then
82 add_symlink "$dep"
84 done
86 # add hard dependencies
87 if [[ -d $unit.requires ]]; then
88 for dep in "$unit".requires/*; do
89 add_systemd_unit ${dep##*/}
90 done
94 add_systemd_drop_in() {
95 local unit=$1 dropin_name=$2
97 mkdir -p "$BUILDROOT/etc/systemd/system/$unit.d"
98 cat >"$BUILDROOT/etc/systemd/system/$unit.d/$2.conf"
101 build() {
102 local rules unit
104 local libdir
106 if [ -e /usr/lib64 ] ; then
107 libdir="lib64"
108 else
109 libdir="lib"
112 add_binary /usr/lib/initcpio/busybox /usr/bin/busybox
113 add_symlink "/bin" /usr/bin
115 for applet in $(/usr/lib/initcpio/busybox --list | grep -v busybox); do
116 add_symlink "/usr/bin/$applet" /usr/bin/busybox
117 done
119 add_binary /usr/bin/sulogin
120 add_binary /usr/lib/systemd/systemd /init
121 add_file /etc/os-release
122 add_file /etc/machine-id
123 add_file /etc/machine-info
125 map add_binary \
126 /usr/bin/systemd-tmpfiles \
127 /usr/lib/systemd/systemd-executor \
128 /usr/lib/systemd/systemd-hibernate-resume \
129 /usr/lib/systemd/systemd-shutdown \
130 /usr/lib/systemd/systemd-sulogin-shell \
131 /usr/lib/systemd/system-generators/systemd-fstab-generator \
132 /usr/lib/systemd/system-generators/systemd-gpt-auto-generator \
133 /usr/lib/systemd/system-generators/systemd-hibernate-resume-generator
135 # udev rules and systemd units
136 map add_udev_rule "$rules" \
137 50-udev-default.rules \
138 60-persistent-storage.rules \
139 64-btrfs.rules \
140 80-drivers.rules \
141 99-systemd.rules
143 map add_systemd_unit \
144 initrd-cleanup.service \
145 initrd-fs.target \
146 initrd-parse-etc.service \
147 initrd-root-fs.target \
148 initrd-root-device.target \
149 initrd-switch-root.service \
150 initrd-switch-root.target \
151 initrd.target \
152 local-fs.target \
153 local-fs-pre.target \
154 paths.target \
155 reboot.target \
156 slices.target \
157 sockets.target \
158 systemd-fsck@.service \
159 systemd-hibernate-resume@.service \
160 systemd-modules-load.service \
161 systemd-udev-trigger.service \
162 systemd-udevd-control.socket \
163 systemd-udevd-kernel.socket \
164 systemd-udevd.service \
165 timers.target \
166 rescue.target \
167 emergency.target
169 add_symlink "/usr/lib/systemd/system/default.target" "initrd.target"
170 add_symlink "/usr/lib/systemd/system/ctrl-alt-del.target" "reboot.target"
172 add_binary "$(readlink -f /usr/$libdir/libnss_files.so.2)"
173 add_binary "$(readlink -f /usr/$libdir/libnss_systemd.so.2)"
174 printf '%s\n' >"$BUILDROOT/etc/nsswitch.conf" \
175 'passwd: files systemd' \
176 'group: files [SUCCESS=merge] systemd' \
177 'shadow: files [UNAVAIL=return] systemd'
179 echo "root:x:0:0:root:/root:/bin/sh" >"$BUILDROOT/etc/passwd"
180 echo 'root::18684::::::' >"$BUILDROOT/etc/shadow"
181 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"
183 for sysuserfile in $(ls /usr/lib/sysusers.d/) ; do
184 add_file /usr/lib/sysusers.d/${sysuserfile}
185 done
187 add_dir "/etc/modules-load.d"
189 . "$_f_config"
190 set -f
191 printf '%s\n' ${MODULES[@]} >"$BUILDROOT/etc/modules-load.d/MODULES.conf"
194 add_dir /etc/systemd
195 echo -e "[Manager]\\nDefaultStandardOutput=inherit" > $BUILDROOT/etc/systemd/system.conf
198 help() {
199 cat <<HELPEOF
200 This will install a basic systemd setup in your initramfs, and is meant to
201 replace the 'base', 'usr', 'udev' and 'resume' hooks. Other hooks with runtime
202 components will need to be ported, and will not work as intended. You also may
203 wish to still include the 'base' hook (before this hook) to ensure that a
204 rescue shell exists on your initramfs.
205 HELPEOF
208 # vim: set ft=sh ts=4 sw=4 et: