grub-mkconfig_lib.in 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. prefix="@prefix@"
  17. exec_prefix="@exec_prefix@"
  18. datarootdir="@datarootdir@"
  19. datadir="@datadir@"
  20. bindir="@bindir@"
  21. sbindir="@sbindir@"
  22. if [ "x$pkgdatadir" = x ]; then
  23. pkgdatadir="${datadir}/@PACKAGE@"
  24. fi
  25. if test "x$grub_probe" = x; then
  26. grub_probe="${sbindir}/@grub_probe@"
  27. fi
  28. if test "x$grub_file" = x; then
  29. grub_file="${bindir}/@grub_file@"
  30. fi
  31. if test "x$grub_mkrelpath" = x; then
  32. grub_mkrelpath="${bindir}/@grub_mkrelpath@"
  33. fi
  34. if which gettext >/dev/null 2>/dev/null; then
  35. :
  36. else
  37. gettext () {
  38. printf "%s" "$@"
  39. }
  40. fi
  41. grub_warn ()
  42. {
  43. echo "$(gettext "Warning:")" "$@" >&2
  44. }
  45. make_system_path_relative_to_its_root ()
  46. {
  47. "${grub_mkrelpath}" "$1"
  48. }
  49. is_path_readable_by_grub ()
  50. {
  51. path="$1"
  52. # abort if path doesn't exist
  53. if test -e "$path" ; then : ;else
  54. return 1
  55. fi
  56. # abort if file is in a filesystem we can't read
  57. if "${grub_probe}" -t fs "$path" > /dev/null 2>&1 ; then : ; else
  58. return 1
  59. fi
  60. # ... or if we can't figure out the abstraction module, for example if
  61. # memberlist fails on an LVM volume group.
  62. if abstractions="`"${grub_probe}" -t abstraction "$path"`" 2> /dev/null ; then
  63. :
  64. else
  65. return 1
  66. fi
  67. if [ x$GRUB_ENABLE_CRYPTODISK = xy ]; then
  68. return 0
  69. fi
  70. for abstraction in $abstractions; do
  71. if [ "x$abstraction" = xcryptodisk ]; then
  72. return 1
  73. fi
  74. done
  75. return 0
  76. }
  77. convert_system_path_to_grub_path ()
  78. {
  79. path="$1"
  80. grub_warn "convert_system_path_to_grub_path() is deprecated. Use prepare_grub_to_access_device() instead."
  81. # abort if GRUB can't access the path
  82. if is_path_readable_by_grub "${path}" ; then : ; else
  83. return 1
  84. fi
  85. if drive="`"${grub_probe}" -t drive "$path"`" ; then : ; else
  86. return 1
  87. fi
  88. if relative_path="`make_system_path_relative_to_its_root "$path"`" ; then : ; else
  89. return 1
  90. fi
  91. echo "${drive}${relative_path}"
  92. }
  93. save_default_entry ()
  94. {
  95. if [ "x${GRUB_SAVEDEFAULT}" = "xtrue" ] ; then
  96. cat << EOF
  97. savedefault
  98. EOF
  99. fi
  100. }
  101. prepare_grub_to_access_device ()
  102. {
  103. old_ifs="$IFS"
  104. IFS='
  105. '
  106. partmap="`"${grub_probe}" --device $@ --target=partmap`"
  107. for module in ${partmap} ; do
  108. case "${module}" in
  109. netbsd | openbsd)
  110. echo "insmod part_bsd";;
  111. *)
  112. echo "insmod part_${module}";;
  113. esac
  114. done
  115. # Abstraction modules aren't auto-loaded.
  116. abstraction="`"${grub_probe}" --device $@ --target=abstraction`"
  117. for module in ${abstraction} ; do
  118. echo "insmod ${module}"
  119. done
  120. fs="`"${grub_probe}" --device $@ --target=fs`"
  121. for module in ${fs} ; do
  122. echo "insmod ${module}"
  123. done
  124. if [ x$GRUB_ENABLE_CRYPTODISK = xy ]; then
  125. for uuid in `"${grub_probe}" --device $@ --target=cryptodisk_uuid`; do
  126. echo "cryptomount -u $uuid"
  127. done
  128. fi
  129. # If there's a filesystem UUID that GRUB is capable of identifying, use it;
  130. # otherwise set root as per value in device.map.
  131. fs_hint="`"${grub_probe}" --device $@ --target=compatibility_hint`"
  132. if [ "x$fs_hint" != x ]; then
  133. echo "set root='$fs_hint'"
  134. fi
  135. if fs_uuid="`"${grub_probe}" --device $@ --target=fs_uuid 2> /dev/null`" ; then
  136. hints="`"${grub_probe}" --device $@ --target=hints_string 2> /dev/null`" || hints=
  137. echo "if [ x\$feature_platform_search_hint = xy ]; then"
  138. echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
  139. echo "else"
  140. echo " search --no-floppy --fs-uuid --set=root ${fs_uuid}"
  141. echo "fi"
  142. fi
  143. IFS="$old_ifs"
  144. }
  145. grub_get_device_id ()
  146. {
  147. old_ifs="$IFS"
  148. IFS='
  149. '
  150. device="$1"
  151. if fs_uuid="`"${grub_probe}" --device ${device} --target=fs_uuid 2> /dev/null`" ; then
  152. echo "$fs_uuid";
  153. else
  154. echo $device |sed 's, ,_,g'
  155. fi
  156. IFS="$old_ifs"
  157. }
  158. grub_file_is_not_garbage ()
  159. {
  160. if test -f "$1" ; then
  161. case "$1" in
  162. *.dpkg-*) return 1 ;; # debian dpkg
  163. *.rpmsave|*.rpmnew) return 1 ;;
  164. README*|*/README*) return 1 ;; # documentation
  165. esac
  166. else
  167. return 1
  168. fi
  169. return 0
  170. }
  171. version_sort ()
  172. {
  173. case $version_sort_sort_has_v in
  174. yes)
  175. LC_ALL=C sort -V;;
  176. no)
  177. LC_ALL=C sort -n;;
  178. *)
  179. if sort -V </dev/null > /dev/null 2>&1; then
  180. version_sort_sort_has_v=yes
  181. LC_ALL=C sort -V
  182. else
  183. version_sort_sort_has_v=no
  184. LC_ALL=C sort -n
  185. fi;;
  186. esac
  187. }
  188. version_test_numeric ()
  189. {
  190. version_test_numeric_a="$1"
  191. version_test_numeric_cmp="$2"
  192. version_test_numeric_b="$3"
  193. if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
  194. case "$version_test_numeric_cmp" in
  195. ge|eq|le) return 0 ;;
  196. gt|lt) return 1 ;;
  197. esac
  198. fi
  199. if [ "$version_test_numeric_cmp" = "lt" ] ; then
  200. version_test_numeric_c="$version_test_numeric_a"
  201. version_test_numeric_a="$version_test_numeric_b"
  202. version_test_numeric_b="$version_test_numeric_c"
  203. fi
  204. if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
  205. return 0
  206. else
  207. return 1
  208. fi
  209. }
  210. version_test_gt ()
  211. {
  212. version_test_gt_a="`echo "$1" | sed -e "s/[^-]*-//"`"
  213. version_test_gt_b="`echo "$2" | sed -e "s/[^-]*-//"`"
  214. version_test_gt_cmp=gt
  215. if [ "x$version_test_gt_b" = "x" ] ; then
  216. return 0
  217. fi
  218. case "$version_test_gt_a:$version_test_gt_b" in
  219. *.old:*.old) ;;
  220. *.old:*) version_test_gt_a="`echo "$version_test_gt_a" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=gt ;;
  221. *:*.old) version_test_gt_b="`echo "$version_test_gt_b" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=ge ;;
  222. esac
  223. version_test_numeric "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
  224. return "$?"
  225. }
  226. version_find_latest ()
  227. {
  228. version_find_latest_a=""
  229. for i in "$@" ; do
  230. if version_test_gt "$i" "$version_find_latest_a" ; then
  231. version_find_latest_a="$i"
  232. fi
  233. done
  234. echo "$version_find_latest_a"
  235. }
  236. # One layer of quotation is eaten by "" and the second by sed; so this turns
  237. # ' into \'.
  238. grub_quote () {
  239. sed "s/'/'\\\\''/g"
  240. }
  241. gettext_quoted () {
  242. gettext "$@" | grub_quote
  243. }
  244. # Run the first argument through gettext, and then pass that and all
  245. # remaining arguments to printf. This is a useful abbreviation and tends to
  246. # be easier to type.
  247. gettext_printf () {
  248. gettext_printf_format="$1"
  249. shift
  250. printf "$(gettext "$gettext_printf_format")" "$@"
  251. }
  252. uses_abstraction () {
  253. device="$1"
  254. old_ifs="$IFS"
  255. IFS='
  256. '
  257. abstraction="`"${grub_probe}" --device ${device} --target=abstraction`"
  258. for module in ${abstraction}; do
  259. if test "x${module}" = "x$2"; then
  260. IFS="$old_ifs"
  261. return 0
  262. fi
  263. done
  264. IFS="$old_ifs"
  265. return 1
  266. }
  267. print_option_help () {
  268. if test x$print_option_help_wc = x; then
  269. if wc -L </dev/null > /dev/null 2>&1; then
  270. print_option_help_wc=-L
  271. elif wc -m </dev/null > /dev/null 2>&1; then
  272. print_option_help_wc=-m
  273. else
  274. print_option_help_wc=-b
  275. fi
  276. fi
  277. if test x$grub_have_fmt = x; then
  278. if fmt -w 40 </dev/null > /dev/null 2>&1; then
  279. grub_have_fmt=y;
  280. else
  281. grub_have_fmt=n;
  282. fi
  283. fi
  284. print_option_help_lead=" $1"
  285. print_option_help_lspace="$(echo "$print_option_help_lead" | wc $print_option_help_wc)"
  286. print_option_help_fill="$((26 - print_option_help_lspace))"
  287. printf "%s" "$print_option_help_lead"
  288. if test $print_option_help_fill -le 0; then
  289. print_option_help_nl=y
  290. echo
  291. else
  292. print_option_help_i=0;
  293. while test $print_option_help_i -lt $print_option_help_fill; do
  294. printf " "
  295. print_option_help_i=$((print_option_help_i+1))
  296. done
  297. print_option_help_nl=n
  298. fi
  299. if test x$grub_have_fmt = xy; then
  300. print_option_help_split="$(echo "$2" | fmt -w 50)"
  301. else
  302. print_option_help_split="$2"
  303. fi
  304. if test x$print_option_help_nl = xy; then
  305. echo "$print_option_help_split" | awk \
  306. '{ print " " $0; }'
  307. else
  308. echo "$print_option_help_split" | awk 'BEGIN { n = 0 }
  309. { if (n == 1) print " " $0; else print $0; n = 1 ; }'
  310. fi
  311. }
  312. grub_fmt () {
  313. if test x$grub_have_fmt = x; then
  314. if fmt -w 40 < /dev/null > /dev/null; then
  315. grub_have_fmt=y;
  316. else
  317. grub_have_fmt=n;
  318. fi
  319. fi
  320. if test x$grub_have_fmt = xy; then
  321. fmt
  322. else
  323. cat
  324. fi
  325. }
  326. grub_tab=" "
  327. grub_add_tab () {
  328. sed -e "s/^/$grub_tab/"
  329. }