grub-mkconfig_lib.in 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # Helper library for grub-mkconfig
  2. # Copyright (C) 2007,2008,2009,2010 Free Software Foundation, Inc.
  3. #
  4. # GRUB is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # GRUB is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  16. transform="@program_transform_name@"
  17. prefix=@prefix@
  18. exec_prefix=@exec_prefix@
  19. datarootdir=@datarootdir@
  20. datadir=@datadir@
  21. bindir=@bindir@
  22. sbindir=@sbindir@
  23. pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
  24. if test "x$grub_probe" = x; then
  25. grub_probe=${sbindir}/`echo burg-probe | sed ${transform}`
  26. fi
  27. if test "x$grub_mkrelpath" = x; then
  28. grub_mkrelpath=${bindir}/`echo burg-mkrelpath | sed ${transform}`
  29. fi
  30. if $(which gettext >/dev/null 2>/dev/null) ; then
  31. gettext="gettext"
  32. else
  33. gettext="echo"
  34. fi
  35. grub_warn ()
  36. {
  37. echo "Warning: $@" >&2
  38. }
  39. make_system_path_relative_to_its_root ()
  40. {
  41. ${grub_mkrelpath} $1
  42. }
  43. is_path_readable_by_grub ()
  44. {
  45. path=$1
  46. # abort if path doesn't exist
  47. if test -e $path ; then : ;else
  48. return 1
  49. fi
  50. # abort if file is in a filesystem we can't read
  51. if ${grub_probe} -t fs $path > /dev/null 2>&1 ; then : ; else
  52. return 1
  53. fi
  54. return 0
  55. }
  56. convert_system_path_to_grub_path ()
  57. {
  58. path=$1
  59. grub_warn "convert_system_path_to_grub_path() is deprecated. Use prepare_grub_to_access_device() instead."
  60. # abort if GRUB can't access the path
  61. if is_path_readable_by_grub ${path} ; then : ; else
  62. return 1
  63. fi
  64. if drive=`${grub_probe} -t drive $path` ; then : ; else
  65. return 1
  66. fi
  67. if relative_path=`make_system_path_relative_to_its_root $path` ; then : ; else
  68. return 1
  69. fi
  70. echo ${drive}${relative_path}
  71. }
  72. save_default_entry ()
  73. {
  74. if [ "x${GRUB_SAVEDEFAULT}" = "xtrue" ] ; then
  75. cat << EOF
  76. savedefault
  77. EOF
  78. fi
  79. }
  80. prepare_grub_to_access_device ()
  81. {
  82. device=$1
  83. # Abstraction modules aren't auto-loaded.
  84. abstraction="`${grub_probe} --device ${device} --target=abstraction`"
  85. for module in ${abstraction} ; do
  86. echo "insmod ${module}"
  87. done
  88. fs="`${grub_probe} --device ${device} --target=fs`"
  89. for module in ${fs} ; do
  90. echo "insmod ${module}"
  91. done
  92. # If there's a filesystem UUID that GRUB is capable of identifying, use it;
  93. # otherwise set root as per value in device.map.
  94. echo "set root='`${grub_probe} --device ${device} --target=drive`'"
  95. if fs_uuid="`${grub_probe} --device ${device} --target=fs_uuid 2> /dev/null`" ; then
  96. echo "search --no-floppy --fs-uuid --set ${fs_uuid}"
  97. fi
  98. }
  99. grub_file_is_not_garbage ()
  100. {
  101. if test -f "$1" ; then
  102. case "$1" in
  103. *.dpkg-*) return 1 ;; # debian dpkg
  104. esac
  105. else
  106. return 1
  107. fi
  108. return 0
  109. }
  110. version_test_numeric ()
  111. {
  112. local a=$1
  113. local cmp=$2
  114. local b=$3
  115. if [ "$a" = "$b" ] ; then
  116. case $cmp in
  117. ge|eq|le) return 0 ;;
  118. gt|lt) return 1 ;;
  119. esac
  120. fi
  121. if [ "$cmp" = "lt" ] ; then
  122. c=$a
  123. a=$b
  124. b=$c
  125. fi
  126. if (echo $a ; echo $b) | sort -n | head -n 1 | grep -qx $b ; then
  127. return 0
  128. else
  129. return 1
  130. fi
  131. }
  132. version_test_gt ()
  133. {
  134. local a=`echo $1 | sed -e "s/[^-]*-//"`
  135. local b=`echo $2 | sed -e "s/[^-]*-//"`
  136. local cmp=gt
  137. if [ "x$b" = "x" ] ; then
  138. return 0
  139. fi
  140. case $a:$b in
  141. *.old:*.old) ;;
  142. *.old:*) a=`echo -n $a | sed -e s/\.old$//` ; cmp=gt ;;
  143. *:*.old) b=`echo -n $b | sed -e s/\.old$//` ; cmp=ge ;;
  144. esac
  145. version_test_numeric $a $cmp $b
  146. return $?
  147. }
  148. version_find_latest ()
  149. {
  150. local a=""
  151. for i in $@ ; do
  152. if version_test_gt "$i" "$a" ; then
  153. a="$i"
  154. fi
  155. done
  156. echo "$a"
  157. }
  158. gettext_quoted () {
  159. $gettext "$@" | sed "s/'/'\\\\''/g"
  160. }
  161. get_auth_option ()
  162. {
  163. local a=
  164. local b=
  165. AUTH_NORMAL=
  166. AUTH_RESCUE=
  167. if [ -n "${GRUB_USERS}" ]; then
  168. a=`echo :${GRUB_USERS}: | sed "s,.*:$1=\([^:]*\):.*,\1,"`
  169. if [ "x$a" = "x:${GRUB_USERS}:" ]; then
  170. a=${GRUB_USERS_ALL}
  171. fi
  172. if [ -n "${a}" ]; then
  173. AUTH_NORMAL="--users \"${a}\" "
  174. fi
  175. b=`echo :${GRUB_USERS}: | sed "s,.*:\*$1=\([^:]*\):.*,\1,"`
  176. if [ "x$b" = "x:${GRUB_USERS}:" ]; then
  177. b=${a}
  178. fi
  179. if [ -n "${b}" ]; then
  180. AUTH_RESCUE="--users \"${b}\" "
  181. fi
  182. fi
  183. }