90-loaderentry.install 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env bash
  2. # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
  3. # ex: ts=8 sw=4 sts=4 et filetype=sh
  4. COMMAND="$1"
  5. KERNEL_VERSION="$2"
  6. ENTRY_DIR_ABS="$3"
  7. KERNEL_IMAGE="$4"
  8. INITRD_OPTIONS_START="5"
  9. if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
  10. exit 0
  11. fi
  12. if ! [[ -d "$ENTRY_DIR_ABS" ]]; then
  13. exit 0
  14. fi
  15. MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
  16. ENTRY_DIR="/$MACHINE_ID/$KERNEL_VERSION"
  17. BOOT_ROOT=${ENTRY_DIR_ABS%$ENTRY_DIR}
  18. if [[ $COMMAND == remove ]]; then
  19. rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
  20. rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
  21. exit 0
  22. fi
  23. if ! [[ $COMMAND == add ]]; then
  24. exit 1
  25. fi
  26. if ! [[ $KERNEL_IMAGE ]]; then
  27. exit 1
  28. fi
  29. if [[ -f /etc/os-release ]]; then
  30. . /etc/os-release
  31. elif [[ -f /usr/lib/os-release ]]; then
  32. . /usr/lib/os-release
  33. fi
  34. if ! [[ $PRETTY_NAME ]]; then
  35. PRETTY_NAME="Linux $KERNEL_VERSION"
  36. fi
  37. if [[ -f /etc/kernel/cmdline ]]; then
  38. read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
  39. elif [[ -f /usr/lib/kernel/cmdline ]]; then
  40. read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
  41. else
  42. declare -a BOOT_OPTIONS
  43. read -r -d '' -a line < /proc/cmdline
  44. for i in "${line[@]}"; do
  45. [[ "${i#initrd=*}" != "$i" ]] && continue
  46. [[ "${i#BOOT_IMAGE=*}" != "$i" ]] && continue
  47. BOOT_OPTIONS+=("$i")
  48. done
  49. fi
  50. if [[ -f /etc/kernel/tries ]]; then
  51. read -r TRIES </etc/kernel/tries
  52. if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
  53. echo "/etc/kernel/tries does not contain an integer." >&2
  54. exit 1
  55. fi
  56. LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+$TRIES.conf"
  57. else
  58. LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
  59. fi
  60. cp "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" &&
  61. chown root:root "$ENTRY_DIR_ABS/linux" &&
  62. chmod 0644 "$ENTRY_DIR_ABS/linux" || {
  63. echo "Could not copy '$KERNEL_IMAGE to '$ENTRY_DIR_ABS/linux'." >&2
  64. exit 1
  65. }
  66. INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
  67. for initrd in "${INITRD_OPTIONS[@]}"; do
  68. if [[ -f "${initrd}" ]]; then
  69. initrd_basename="$(basename ${initrd})"
  70. [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
  71. echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
  72. cp "${initrd}" "$ENTRY_DIR_ABS/${initrd_basename}" &&
  73. chown root:root "$ENTRY_DIR_ABS/${initrd_basename}" &&
  74. chmod 0644 "$ENTRY_DIR_ABS/${initrd_basename}" || {
  75. echo "Could not copy '${initrd}' to '$ENTRY_DIR_ABS/${initrd_basename}'." >&2
  76. exit 1
  77. }
  78. fi
  79. done
  80. # If no initrd option is supplied, fallback to "initrd" which is
  81. # the name used by dracut when generating it in its kernel-install hook
  82. [[ ${#INITRD_OPTIONS[@]} == 0 ]] && INITRD_OPTIONS=( initrd )
  83. mkdir -p "${LOADER_ENTRY%/*}" || {
  84. echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
  85. exit 1
  86. }
  87. [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
  88. echo "Creating $LOADER_ENTRY"
  89. {
  90. echo "title $PRETTY_NAME"
  91. echo "version $KERNEL_VERSION"
  92. echo "machine-id $MACHINE_ID"
  93. echo "options ${BOOT_OPTIONS[*]}"
  94. echo "linux $ENTRY_DIR/linux"
  95. for initrd in "${INITRD_OPTIONS[@]}"; do
  96. [[ -f $ENTRY_DIR_ABS/$(basename ${initrd}) ]] && \
  97. echo "initrd $ENTRY_DIR/$(basename ${initrd})"
  98. done
  99. :
  100. } > "$LOADER_ENTRY" || {
  101. echo "Could not create loader entry '$LOADER_ENTRY'." >&2
  102. exit 1
  103. }
  104. exit 0