initcpio-install-systemd 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. # do not add binaries unless they are required,
  59. # strip special executable prefixes
  60. case ${values[0]} in
  61. -*) ;;
  62. !!*) add_binary "${values[0]#!!}" ;;
  63. *) add_binary "${values[0]#[@!:+]}" ;;
  64. esac
  65. ;;
  66. esac
  67. done <"$unit"
  68. # preserve reverse soft dependency
  69. for dep in {/usr,}/lib/systemd/system/*.wants/${unit##*/}; do
  70. if [[ -L $dep ]]; then
  71. add_symlink "$dep"
  72. fi
  73. done
  74. # add hard dependencies
  75. if [[ -d $unit.requires ]]; then
  76. for dep in "$unit".requires/*; do
  77. add_systemd_unit ${dep##*/}
  78. done
  79. fi
  80. }
  81. add_systemd_drop_in() {
  82. local unit=$1 dropin_name=$2
  83. mkdir -p "$BUILDROOT/etc/systemd/system/$unit.d"
  84. cat >"$BUILDROOT/etc/systemd/system/$unit.d/$2.conf"
  85. }
  86. build() {
  87. local rules unit
  88. add_binary /usr/bin/kmod /usr/bin/modprobe
  89. add_binary /usr/bin/mount
  90. add_binary /usr/bin/sulogin
  91. add_binary /usr/bin/umount
  92. add_binary /usr/lib/systemd/systemd /init
  93. map add_binary \
  94. /usr/bin/journalctl \
  95. /usr/bin/systemd-tmpfiles \
  96. /usr/lib/systemd/systemd-hibernate-resume \
  97. /usr/lib/systemd/systemd-shutdown \
  98. /usr/lib/systemd/systemd-sulogin-shell \
  99. /usr/lib/systemd/system-generators/systemd-fstab-generator \
  100. /usr/lib/systemd/system-generators/systemd-gpt-auto-generator \
  101. /usr/lib/systemd/system-generators/systemd-hibernate-resume-generator
  102. # udev rules and systemd units
  103. map add_udev_rule "$rules" \
  104. 50-udev-default.rules \
  105. 60-persistent-storage.rules \
  106. 64-btrfs.rules \
  107. 80-drivers.rules \
  108. 99-systemd.rules
  109. map add_systemd_unit \
  110. initrd-cleanup.service \
  111. initrd-fs.target \
  112. initrd-parse-etc.service \
  113. initrd-root-fs.target \
  114. initrd-root-device.target \
  115. initrd-switch-root.service \
  116. initrd-switch-root.target \
  117. initrd-udevadm-cleanup-db.service \
  118. initrd.target \
  119. kmod-static-nodes.service \
  120. local-fs.target \
  121. local-fs-pre.target \
  122. paths.target \
  123. reboot.target \
  124. slices.target \
  125. sockets.target \
  126. swap.target \
  127. systemd-fsck@.service \
  128. systemd-hibernate-resume@.service \
  129. systemd-journald.service \
  130. systemd-journald-audit.socket \
  131. systemd-journald-dev-log.socket \
  132. systemd-modules-load.service \
  133. systemd-tmpfiles-setup-dev.service \
  134. systemd-udev-trigger.service \
  135. systemd-udevd-control.socket \
  136. systemd-udevd-kernel.socket \
  137. systemd-udevd.service \
  138. timers.target \
  139. rescue.target \
  140. emergency.target
  141. add_symlink "/usr/lib/systemd/system/default.target" "initrd.target"
  142. add_symlink "/usr/lib/systemd/system/ctrl-alt-del.target" "reboot.target"
  143. add_binary "$(readlink -f /usr/lib/libnss_files.so)"
  144. printf '%s\n' >"$BUILDROOT/etc/nsswitch.conf" \
  145. 'passwd: files' \
  146. 'group: files' \
  147. 'shadow: files'
  148. echo "root:x:0:0:root:/root:/bin/sh" >"$BUILDROOT/etc/passwd"
  149. echo 'root:*:::::::' >"$BUILDROOT/etc/shadow"
  150. getent group root audio disk input kmem kvm lp optical render storage tty uucp video | awk -F: ' { print $1 ":x:" $3 ":" }' >"$BUILDROOT/etc/group"
  151. add_dir "/etc/modules-load.d"
  152. (
  153. . "$_f_config"
  154. set -f
  155. printf '%s\n' ${MODULES[@]} >"$BUILDROOT/etc/modules-load.d/MODULES.conf"
  156. )
  157. }
  158. help() {
  159. cat <<HELPEOF
  160. This will install a basic systemd setup in your initramfs, and is meant to
  161. replace the 'base', 'usr', 'udev' and 'resume' hooks. Other hooks with runtime
  162. components will need to be ported, and will not work as intended. You also may
  163. wish to still include the 'base' hook (before this hook) to ensure that a
  164. rescue shell exists on your initramfs.
  165. HELPEOF
  166. }
  167. # vim: set ft=sh ts=4 sw=4 et: