grub-mkconfig_lib.in 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 command -v gettext >/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 [ "x${GRUB_DISABLE_UUID}" != "xtrue" ] && 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. if [ "x$hints" != x ]; then
  138. echo "if [ x\$feature_platform_search_hint = xy ]; then"
  139. echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
  140. echo "else"
  141. echo " search --no-floppy --fs-uuid --set=root ${fs_uuid}"
  142. echo "fi"
  143. else
  144. echo "search --no-floppy --fs-uuid --set=root ${fs_uuid}"
  145. fi
  146. fi
  147. IFS="$old_ifs"
  148. }
  149. grub_get_device_id ()
  150. {
  151. old_ifs="$IFS"
  152. IFS='
  153. '
  154. device="$1"
  155. if [ "x${GRUB_DISABLE_UUID}" != "xtrue" ] && fs_uuid="`"${grub_probe}" --device ${device} --target=fs_uuid 2> /dev/null`" ; then
  156. echo "$fs_uuid";
  157. else
  158. echo $device |sed 's, ,_,g'
  159. fi
  160. IFS="$old_ifs"
  161. }
  162. grub_file_is_not_garbage ()
  163. {
  164. if test -f "$1" ; then
  165. case "$1" in
  166. *.dpkg-*) return 1 ;; # debian dpkg
  167. *.rpmsave|*.rpmnew) return 1 ;;
  168. README*|*/README*) return 1 ;; # documentation
  169. *.sig) return 1 ;; # signatures
  170. esac
  171. else
  172. return 1
  173. fi
  174. return 0
  175. }
  176. version_sort ()
  177. {
  178. case $version_sort_sort_has_v in
  179. yes)
  180. LC_ALL=C sort -V "$@";;
  181. no)
  182. LC_ALL=C sort -n "$@";;
  183. *)
  184. if sort -V </dev/null > /dev/null 2>&1; then
  185. version_sort_sort_has_v=yes
  186. LC_ALL=C sort -V "$@"
  187. else
  188. version_sort_sort_has_v=no
  189. LC_ALL=C sort -n "$@"
  190. fi;;
  191. esac
  192. }
  193. # Given an item as the first argument and a list as the subsequent arguments,
  194. # returns the list with the first argument moved to the front if it exists in
  195. # the list.
  196. grub_move_to_front ()
  197. {
  198. item="$1"
  199. shift
  200. item_found=false
  201. for i in "$@"; do
  202. if [ "x$i" = "x$item" ]; then
  203. item_found=true
  204. fi
  205. done
  206. if [ "x$item_found" = xtrue ]; then
  207. echo "$item"
  208. fi
  209. for i in "$@"; do
  210. if [ "x$i" = "x$item" ]; then
  211. continue
  212. fi
  213. echo "$i"
  214. done
  215. }
  216. version_test_numeric ()
  217. {
  218. grub_warn "version_test_numeric() is deprecated. Use version_sort() instead."
  219. version_test_numeric_a="$1"
  220. version_test_numeric_cmp="$2"
  221. version_test_numeric_b="$3"
  222. if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
  223. case "$version_test_numeric_cmp" in
  224. ge|eq|le) return 0 ;;
  225. gt|lt) return 1 ;;
  226. esac
  227. fi
  228. if [ "$version_test_numeric_cmp" = "lt" ] ; then
  229. version_test_numeric_c="$version_test_numeric_a"
  230. version_test_numeric_a="$version_test_numeric_b"
  231. version_test_numeric_b="$version_test_numeric_c"
  232. fi
  233. if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
  234. return 0
  235. else
  236. return 1
  237. fi
  238. }
  239. version_test_gt ()
  240. {
  241. grub_warn "version_test_gt() is deprecated. Use version_sort() instead."
  242. version_test_gt_a="`echo "$1" | sed -e "s/[^-]*-//"`"
  243. version_test_gt_b="`echo "$2" | sed -e "s/[^-]*-//"`"
  244. version_test_gt_cmp=gt
  245. if [ "x$version_test_gt_b" = "x" ] ; then
  246. return 0
  247. fi
  248. case "$version_test_gt_a:$version_test_gt_b" in
  249. *.old:*.old) ;;
  250. *.old:*) version_test_gt_a="`echo "$version_test_gt_a" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=gt ;;
  251. *:*.old) version_test_gt_b="`echo "$version_test_gt_b" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=ge ;;
  252. esac
  253. version_test_numeric "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
  254. return "$?"
  255. }
  256. version_find_latest ()
  257. {
  258. grub_warn "version_find_latest() is deprecated. Use version_sort() instead."
  259. version_find_latest_a=""
  260. for i in "$@" ; do
  261. if version_test_gt "$i" "$version_find_latest_a" ; then
  262. version_find_latest_a="$i"
  263. fi
  264. done
  265. echo "$version_find_latest_a"
  266. }
  267. # One layer of quotation is eaten by "" and the second by sed; so this turns
  268. # ' into \'.
  269. grub_quote () {
  270. sed "s/'/'\\\\''/g"
  271. }
  272. gettext_quoted () {
  273. gettext "$@" | grub_quote
  274. }
  275. # Run the first argument through gettext, and then pass that and all
  276. # remaining arguments to printf. This is a useful abbreviation and tends to
  277. # be easier to type.
  278. gettext_printf () {
  279. gettext_printf_format="$1"
  280. shift
  281. printf "$(gettext "$gettext_printf_format")" "$@"
  282. }
  283. uses_abstraction () {
  284. device="$1"
  285. old_ifs="$IFS"
  286. IFS='
  287. '
  288. abstraction="`"${grub_probe}" --device ${device} --target=abstraction`"
  289. for module in ${abstraction}; do
  290. if test "x${module}" = "x$2"; then
  291. IFS="$old_ifs"
  292. return 0
  293. fi
  294. done
  295. IFS="$old_ifs"
  296. return 1
  297. }
  298. print_option_help () {
  299. if test x$print_option_help_wc = x; then
  300. if wc -L </dev/null > /dev/null 2>&1; then
  301. print_option_help_wc=-L
  302. elif wc -m </dev/null > /dev/null 2>&1; then
  303. print_option_help_wc=-m
  304. else
  305. print_option_help_wc=-b
  306. fi
  307. fi
  308. if test x$grub_have_fmt = x; then
  309. if fmt -w 40 </dev/null > /dev/null 2>&1; then
  310. grub_have_fmt=y;
  311. else
  312. grub_have_fmt=n;
  313. fi
  314. fi
  315. print_option_help_lead=" $1"
  316. print_option_help_lspace="$(echo "$print_option_help_lead" | wc $print_option_help_wc)"
  317. print_option_help_fill="$((26 - print_option_help_lspace))"
  318. printf "%s" "$print_option_help_lead"
  319. if test $print_option_help_fill -le 0; then
  320. print_option_help_nl=y
  321. echo
  322. else
  323. print_option_help_i=0;
  324. while test $print_option_help_i -lt $print_option_help_fill; do
  325. printf " "
  326. print_option_help_i=$((print_option_help_i+1))
  327. done
  328. print_option_help_nl=n
  329. fi
  330. if test x$grub_have_fmt = xy; then
  331. print_option_help_split="$(echo "$2" | fmt -w 50)"
  332. else
  333. print_option_help_split="$2"
  334. fi
  335. if test x$print_option_help_nl = xy; then
  336. echo "$print_option_help_split" | awk \
  337. '{ print " " $0; }'
  338. else
  339. echo "$print_option_help_split" | awk 'BEGIN { n = 0 }
  340. { if (n == 1) print " " $0; else print $0; n = 1 ; }'
  341. fi
  342. }
  343. grub_fmt () {
  344. if test x$grub_have_fmt = x; then
  345. if fmt -w 40 < /dev/null > /dev/null; then
  346. grub_have_fmt=y;
  347. else
  348. grub_have_fmt=n;
  349. fi
  350. fi
  351. if test x$grub_have_fmt = xy; then
  352. fmt
  353. else
  354. cat
  355. fi
  356. }
  357. grub_tab=" "
  358. grub_add_tab () {
  359. sed -e "s/^/$grub_tab/"
  360. }