40grub2 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. . /usr/share/os-prober/common.sh
  3. set -e
  4. partition="$1"
  5. bootpart="$2"
  6. mpoint="$3"
  7. type="$4"
  8. found_item=0
  9. entry_result () {
  10. if [ "$ignore_item" = 0 ] && \
  11. [ -n "$kernel" ] && \
  12. [ -e "$mpoint/$kernel" ]; then
  13. result "$rootpart:$bootpart:$title:$kernel:$initrd:$parameters"
  14. found_item=1
  15. fi
  16. kernel=""
  17. parameters=""
  18. initrd=""
  19. title=""
  20. ignore_item=0
  21. }
  22. parse_grub_menu () {
  23. mpoint="$1"
  24. rootpart="$2"
  25. bootpart="$3"
  26. kernel=""
  27. parameters=""
  28. initrd=""
  29. title=""
  30. ignore_item=0
  31. while read line; do
  32. debug "parsing: $line"
  33. set -f
  34. set -- $line
  35. set +f
  36. case "$1" in
  37. menuentry)
  38. entry_result
  39. shift 1
  40. # The double-quoted string is the title.
  41. # Make sure to look at the text of the line
  42. # before 'set' mangled it.
  43. title="$(echo "$line" | sed -n 's/[^"]*"\(.*\)".*/\1/p' | sed 's/://g')"
  44. if [ -z "$title" ]; then
  45. # ... or single-quoted? Be careful
  46. # to handle constructions like
  47. # 'foo'\''bar' (which expands to
  48. # foo'bar, as in shell), and to
  49. # handle multiple single-quoted
  50. # strings on the same line.
  51. title="$(echo "$line" | sed -n "s/[^']*'\(\([^']\|'\\\\''\)*\)'.*/\1/p" | sed "s/'\\\\''/'/; s/://g")"
  52. fi
  53. if [ -z "$title" ]; then
  54. ignore_item=1
  55. elif echo "$title" | grep -q '(on /dev/[^)]*)$'; then
  56. log "Skipping entry '$title':"
  57. log "appears to be an automatic reference taken from another menu.lst"
  58. ignore_item=1
  59. fi
  60. ;;
  61. linux)
  62. # Hack alert: sed off any (hdn,n) but
  63. # assume the kernel is on the same
  64. # partition.
  65. kernel="$(echo "$2" | sed 's/(.*)//')"
  66. shift 2
  67. parameters="$@"
  68. # Systems with a separate /boot will not have
  69. # the path to the kernel in grub.cfg.
  70. if [ "$partition" != "$bootpart" ]; then
  71. kernel="/boot$kernel"
  72. fi
  73. ;;
  74. initrd)
  75. initrd="$(echo "$2" | sed 's/(.*)//')"
  76. # Initrd same.
  77. if [ "$partition" != "$bootpart" ]; then
  78. initrd="/boot$initrd"
  79. fi
  80. ;;
  81. "}")
  82. entry_result
  83. ;;
  84. esac
  85. done
  86. entry_result
  87. }
  88. if [ -e "$mpoint/boot/grub/grub.cfg" ] && \
  89. ([ ! -e "$mpoint/boot/grub/menu.lst" ] || \
  90. [ "$mpoint/boot/grub/grub.cfg" -nt "$mpoint/boot/grub/menu.lst" ]); then
  91. debug "parsing grub.cfg"
  92. parse_grub_menu "$mpoint" "$partition" "$bootpart" < "$mpoint/boot/grub/grub.cfg"
  93. elif [ -e "$mpoint/boot/grub2/grub.cfg" ]; then
  94. debug "parsing grub.cfg"
  95. parse_grub_menu "$mpoint" "$partition" "$bootpart" < "$mpoint/boot/grub2/grub.cfg"
  96. fi
  97. if [ "$found_item" = 0 ]; then
  98. exit 1
  99. else
  100. exit 0
  101. fi