initcpio-install-systemd 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/bin/bash
  2. strip_quotes() {
  3. local len=${#1} quotes=$'[\'"]' str=${!1}
  4. if [[ ${str:0:1} = ${str: -1} && ${str:0:1} = $quotes ]]; then
  5. printf -v "$1" %s "${str:1:-1}"
  6. fi
  7. }
  8. add_udev_rule() {
  9. # Add an udev rules file to the initcpio image. Dependencies on binaries
  10. # will be discovered and added.
  11. # $1: path to rules file (or name of rules file)
  12. local rules= rule= key= value= binary=
  13. rules=$(PATH=/usr/lib/udev/rules.d:/lib/udev/rules.d type -P "$1")
  14. if [[ -z $rules ]]; then
  15. # complain about not found rules
  16. return 1
  17. fi
  18. add_file "$rules"
  19. while IFS=, read -ra rule; do
  20. # skip empty lines, comments
  21. [[ -z $rule || $rule = @(+([[:space:]])|#*) ]] && continue
  22. for pair in "${rule[@]}"; do
  23. IFS=' =' read -r key value <<< "$pair"
  24. case $key in
  25. RUN@({program}|+)|IMPORT{program}|ENV{REMOVE_CMD})
  26. strip_quotes 'value'
  27. # just take the first word as the binary name
  28. binary=${value%% *}
  29. [[ ${binary:0:1} == '$' ]] && continue
  30. if [[ ${binary:0:1} != '/' ]]; then
  31. binary=$(PATH=/usr/lib/udev:/lib/udev type -P "$binary")
  32. fi
  33. add_binary "$binary"
  34. ;;
  35. esac
  36. done
  37. done <"$rules"
  38. }
  39. add_systemd_unit() {
  40. # Add a systemd unit file to the initcpio image. Hard dependencies on binaries
  41. # and other unit files will be discovered and added.
  42. # $1: path to rules file (or name of rules file)
  43. local unit= rule= entry= key= value= binary= dep=
  44. unit=$(PATH=/usr/lib/systemd/system:/lib/systemd/system type -P "$1")
  45. if [[ -z $unit ]]; then
  46. # complain about not found unit file
  47. return 1
  48. fi
  49. add_file "$unit"
  50. while IFS='=' read -r key values; do
  51. read -ra values <<< "$values"
  52. case $key in
  53. Requires|OnFailure)
  54. # only add hard dependencies (not Wants)
  55. map add_systemd_unit "${values[@]}"
  56. ;;
  57. Exec*)
  58. # don't add binaries unless they are required
  59. if [[ ${values[0]:0:1} != '-' ]]; then
  60. add_binary "${values[0]}"
  61. fi
  62. ;;
  63. esac
  64. done <"$unit"
  65. # preserve reverse soft dependency
  66. for dep in {/usr,}/lib/systemd/system/*.wants/${unit##*/}; do
  67. if [[ -L $dep ]]; then
  68. add_symlink "$dep"
  69. fi
  70. done
  71. # add hard dependencies
  72. if [[ -d $unit.requires ]]; then
  73. for dep in "$unit".requires/*; do
  74. add_systemd_unit ${dep##*/}
  75. done
  76. fi
  77. }
  78. add_systemd_drop_in() {
  79. local unit=$1 dropin_name=$2
  80. mkdir -p "$BUILDROOT/etc/systemd/system/$unit.d"
  81. cat >"$BUILDROOT/etc/systemd/system/$unit.d/$2.conf"
  82. }
  83. build() {
  84. local rules unit
  85. # from base
  86. add_binary /bin/mount
  87. add_binary /usr/bin/kmod /usr/bin/modprobe
  88. add_binary /usr/lib/systemd/systemd /init
  89. add_binary /usr/bin/sulogin
  90. map add_binary \
  91. /usr/bin/systemd-tmpfiles \
  92. /usr/lib/systemd/systemd-hibernate-resume \
  93. /usr/lib/systemd/systemd-shutdown \
  94. /usr/lib/systemd/systemd-sulogin-shell \
  95. /usr/lib/systemd/system-generators/systemd-fstab-generator \
  96. /usr/lib/systemd/system-generators/systemd-gpt-auto-generator \
  97. /usr/lib/systemd/system-generators/systemd-hibernate-resume-generator
  98. # for journalctl in emergency shell
  99. add_binary journalctl
  100. # udev rules and systemd units
  101. map add_udev_rule "$rules" \
  102. 50-udev-default.rules \
  103. 60-persistent-storage.rules \
  104. 64-btrfs.rules \
  105. 80-drivers.rules \
  106. 99-systemd.rules
  107. map add_systemd_unit \
  108. initrd-cleanup.service \
  109. initrd-fs.target \
  110. initrd-parse-etc.service \
  111. initrd-root-fs.target \
  112. initrd-root-device.target \
  113. initrd-switch-root.service \
  114. initrd-switch-root.target \
  115. initrd-udevadm-cleanup-db.service \
  116. initrd.target \
  117. kmod-static-nodes.service \
  118. local-fs.target \
  119. local-fs-pre.target \
  120. paths.target \
  121. reboot.target \
  122. slices.target \
  123. sockets.target \
  124. swap.target \
  125. systemd-fsck@.service \
  126. systemd-hibernate-resume@.service \
  127. systemd-journald.service \
  128. systemd-journald-audit.socket \
  129. systemd-journald-dev-log.socket \
  130. systemd-modules-load.service \
  131. systemd-tmpfiles-setup-dev.service \
  132. systemd-udev-trigger.service \
  133. systemd-udevd-control.socket \
  134. systemd-udevd-kernel.socket \
  135. systemd-udevd.service \
  136. timers.target \
  137. rescue.target \
  138. emergency.target
  139. add_symlink "/usr/lib/systemd/system/default.target" "initrd.target"
  140. add_symlink "/usr/lib/systemd/system/ctrl-alt-del.target" "reboot.target"
  141. add_binary "$(readlink -f /usr/lib/libnss_files.so)"
  142. printf '%s\n' >"$BUILDROOT/etc/nsswitch.conf" \
  143. 'passwd: files' \
  144. 'group: files' \
  145. 'shadow: files'
  146. echo "root:x:0:0:root:/:/bin/sh" >"$BUILDROOT/etc/passwd"
  147. echo "root:x:0:root" >"$BUILDROOT/etc/group"
  148. echo "root::::::::" >"$BUILDROOT/etc/shadow"
  149. add_systemd_drop_in systemd-udevd.service resolve-names <<EOF
  150. [Service]
  151. ExecStart=
  152. ExecStart=/usr/lib/systemd/systemd-udevd --resolve-names=never
  153. EOF
  154. add_dir "/etc/modules-load.d"
  155. (
  156. . "$_f_config"
  157. set -f
  158. printf '%s\n' ${MODULES[@]} >"$BUILDROOT/etc/modules-load.d/MODULES.conf"
  159. )
  160. }
  161. help() {
  162. cat <<HELPEOF
  163. This will install a basic systemd setup in your initramfs, and is meant to
  164. replace the 'base', 'usr', 'udev' and 'resume' hooks. Other hooks with runtime
  165. components will need to be ported, and will not work as intended. You also may
  166. wish to still include the 'base' hook (before this hook) to ensure that a
  167. rescue shell exists on your initramfs.
  168. HELPEOF
  169. }
  170. # vim: set ft=sh ts=4 sw=4 et: