initcpio-install-systemd 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. add_systemd_unit() {
  3. # Add a systemd unit file to the initcpio image. Hard dependencies on binaries
  4. # and other unit files will be discovered and added.
  5. # $1: path to rules file (or name of rules file)
  6. local unit= rule= entry= key= value= binary= dep=
  7. unit=$(PATH=/usr/lib/systemd/system:/lib/systemd/system type -P "$1")
  8. if [[ -z $unit ]]; then
  9. # complain about not found unit file
  10. return 1
  11. fi
  12. add_file "$unit"
  13. while IFS='=' read -r key values; do
  14. read -ra values <<< "$values"
  15. case $key in
  16. Requires|OnFailure)
  17. # only add hard dependencies (not Wants)
  18. map add_systemd_unit "${values[@]}"
  19. ;;
  20. Exec*)
  21. # do not add binaries unless they are required,
  22. # strip special executable prefixes
  23. case ${values[0]} in
  24. -*) ;;
  25. !!*) add_binary "${values[0]#!!}" ;;
  26. *) add_binary "${values[0]#[@!:+]}" ;;
  27. esac
  28. ;;
  29. esac
  30. done <"$unit"
  31. # preserve reverse soft dependency
  32. for dep in {/usr,}/lib/systemd/system/*.wants/${unit##*/}; do
  33. if [[ -L $dep ]]; then
  34. add_symlink "$dep"
  35. fi
  36. done
  37. # add hard dependencies
  38. if [[ -d $unit.requires ]]; then
  39. for dep in "$unit".requires/*; do
  40. add_systemd_unit ${dep##*/}
  41. done
  42. fi
  43. }
  44. add_systemd_drop_in() {
  45. local unit=$1 dropin_name=$2
  46. mkdir -p "$BUILDROOT/etc/systemd/system/$unit.d"
  47. cat >"$BUILDROOT/etc/systemd/system/$unit.d/$2.conf"
  48. }
  49. build() {
  50. local rules unit
  51. add_binary /usr/bin/kmod /usr/bin/modprobe
  52. add_binary /usr/bin/mount
  53. add_binary /usr/bin/sulogin
  54. add_binary /usr/bin/umount
  55. add_binary /usr/lib/systemd/systemd /init
  56. map add_binary \
  57. /usr/bin/journalctl \
  58. /usr/bin/systemd-tmpfiles \
  59. /usr/lib/systemd/systemd-hibernate-resume \
  60. /usr/lib/systemd/systemd-shutdown \
  61. /usr/lib/systemd/systemd-sulogin-shell \
  62. /usr/lib/systemd/system-generators/systemd-fstab-generator \
  63. /usr/lib/systemd/system-generators/systemd-gpt-auto-generator \
  64. /usr/lib/systemd/system-generators/systemd-hibernate-resume-generator
  65. # udev rules and systemd units
  66. map add_udev_rule "$rules" \
  67. 50-udev-default.rules \
  68. 60-persistent-storage.rules \
  69. 64-btrfs.rules \
  70. 80-drivers.rules \
  71. 99-systemd.rules
  72. map add_systemd_unit \
  73. initrd-cleanup.service \
  74. initrd-fs.target \
  75. initrd-parse-etc.service \
  76. initrd-root-fs.target \
  77. initrd-root-device.target \
  78. initrd-switch-root.service \
  79. initrd-switch-root.target \
  80. initrd-udevadm-cleanup-db.service \
  81. initrd.target \
  82. kmod-static-nodes.service \
  83. local-fs.target \
  84. local-fs-pre.target \
  85. paths.target \
  86. reboot.target \
  87. slices.target \
  88. sockets.target \
  89. swap.target \
  90. systemd-fsck@.service \
  91. systemd-hibernate-resume@.service \
  92. systemd-journald.service \
  93. systemd-journald-audit.socket \
  94. systemd-journald-dev-log.socket \
  95. systemd-modules-load.service \
  96. systemd-tmpfiles-setup-dev.service \
  97. systemd-udev-trigger.service \
  98. systemd-udevd-control.socket \
  99. systemd-udevd-kernel.socket \
  100. systemd-udevd.service \
  101. timers.target \
  102. rescue.target \
  103. emergency.target
  104. add_symlink "/usr/lib/systemd/system/default.target" "initrd.target"
  105. add_symlink "/usr/lib/systemd/system/ctrl-alt-del.target" "reboot.target"
  106. # This code moved to glibc with 2.35-1, so the shared library is just empty.
  107. # Keep it for the time being - but with final file name - for older glibc packages.
  108. # TODO: Remove once settled!
  109. add_binary '/usr/lib/libnss_files.so.2'
  110. printf '%s\n' >"$BUILDROOT/etc/nsswitch.conf" \
  111. 'passwd: files' \
  112. 'group: files' \
  113. 'shadow: files'
  114. echo "root:x:0:0:root:/root:/bin/sh" >"$BUILDROOT/etc/passwd"
  115. echo 'root:*:::::::' >"$BUILDROOT/etc/shadow"
  116. 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"
  117. add_dir "/etc/modules-load.d"
  118. (
  119. . "$_f_config"
  120. set -f
  121. printf '%s\n' ${MODULES[@]} >"$BUILDROOT/etc/modules-load.d/MODULES.conf"
  122. )
  123. }
  124. help() {
  125. cat <<HELPEOF
  126. This will install a basic systemd setup in your initramfs, and is meant to
  127. replace the 'base', 'usr', 'udev' and 'resume' hooks. Other hooks with runtime
  128. components will need to be ported, and will not work as intended. You also may
  129. wish to still include the 'base' hook (before this hook) to ensure that a
  130. rescue shell exists on your initramfs.
  131. HELPEOF
  132. }
  133. # vim: set ft=sh ts=4 sw=4 et: