grub-mkconfig_lib 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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="/usr"
  17. exec_prefix="/usr"
  18. datarootdir="/usr/share"
  19. datadir="${datarootdir}"
  20. bindir="${exec_prefix}/bin"
  21. sbindir="${exec_prefix}/sbin"
  22. if [ "x$pkgdatadir" = x ]; then
  23. pkgdatadir="${datadir}/grub"
  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. loop_file=
  116. case $1 in
  117. /dev/loop/*|/dev/loop[0-9])
  118. grub_loop_device="${1#/dev/}"
  119. loop_file=`losetup "$1" | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  120. case $loop_file in
  121. /dev/*) ;;
  122. *)
  123. loop_device="$1"
  124. shift
  125. set -- `"${grub_probe}" --target=device "${loop_file}"` "$@" || return 0
  126. ;;
  127. esac
  128. ;;
  129. esac
  130. # Abstraction modules aren't auto-loaded.
  131. abstraction="`"${grub_probe}" --device $@ --target=abstraction`"
  132. for module in ${abstraction} ; do
  133. echo "insmod ${module}"
  134. done
  135. fs="`"${grub_probe}" --device $@ --target=fs`"
  136. for module in ${fs} ; do
  137. echo "insmod ${module}"
  138. done
  139. if [ x$GRUB_ENABLE_CRYPTODISK = xy ]; then
  140. for uuid in `"${grub_probe}" --device $@ --target=cryptodisk_uuid`; do
  141. echo "cryptomount -u $uuid"
  142. done
  143. fi
  144. # If there's a filesystem UUID that GRUB is capable of identifying, use it;
  145. # otherwise set root as per value in device.map.
  146. fs_hint="`"${grub_probe}" --device $@ --target=compatibility_hint`"
  147. if [ "x$fs_hint" != x ]; then
  148. echo "set root='$fs_hint'"
  149. fi
  150. if fs_uuid="`"${grub_probe}" --device $@ --target=fs_uuid 2> /dev/null`" ; then
  151. hints="`"${grub_probe}" --device $@ --target=hints_string 2> /dev/null`" || hints=
  152. echo "if [ x\$feature_platform_search_hint = xy ]; then"
  153. echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
  154. echo "else"
  155. echo " search --no-floppy --fs-uuid --set=root ${fs_uuid}"
  156. echo "fi"
  157. fi
  158. IFS="$old_ifs"
  159. if [ "x${loop_file}" != x ]; then
  160. loop_mountpoint="$(awk '"'${loop_file}'" ~ "^"$2 && $2 != "/" { print $2 }' /proc/mounts | tail -n1)"
  161. if [ "x${loop_mountpoint}" != x ]; then
  162. echo "loopback ${grub_loop_device} ${loop_file#$loop_mountpoint}"
  163. echo "set root=(${grub_loop_device})"
  164. fi
  165. fi
  166. }
  167. grub_get_device_id ()
  168. {
  169. old_ifs="$IFS"
  170. IFS='
  171. '
  172. device="$1"
  173. if fs_uuid="`"${grub_probe}" --device ${device} --target=fs_uuid 2> /dev/null`" ; then
  174. echo "$fs_uuid";
  175. else
  176. echo $device |sed 's, ,_,g'
  177. fi
  178. IFS="$old_ifs"
  179. }
  180. grub_file_is_not_garbage ()
  181. {
  182. if test -f "$1" ; then
  183. case "$1" in
  184. *.dpkg-*) return 1 ;; # debian dpkg
  185. *.rpmsave|*.rpmnew) return 1 ;;
  186. README*|*/README*) return 1 ;; # documentation
  187. *.sig) return 1 ;; # signatures
  188. esac
  189. else
  190. return 1
  191. fi
  192. return 0
  193. }
  194. version_sort ()
  195. {
  196. case $version_sort_sort_has_v in
  197. yes)
  198. LC_ALL=C sort -V;;
  199. no)
  200. LC_ALL=C sort -n;;
  201. *)
  202. if sort -V </dev/null > /dev/null 2>&1; then
  203. version_sort_sort_has_v=yes
  204. LC_ALL=C sort -V
  205. else
  206. version_sort_sort_has_v=no
  207. LC_ALL=C sort -n
  208. fi;;
  209. esac
  210. }
  211. version_test_numeric ()
  212. {
  213. version_test_numeric_a="$1"
  214. version_test_numeric_cmp="$2"
  215. version_test_numeric_b="$3"
  216. if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
  217. case "$version_test_numeric_cmp" in
  218. ge|eq|le) return 0 ;;
  219. gt|lt) return 1 ;;
  220. esac
  221. fi
  222. if [ "$version_test_numeric_cmp" = "lt" ] ; then
  223. version_test_numeric_c="$version_test_numeric_a"
  224. version_test_numeric_a="$version_test_numeric_b"
  225. version_test_numeric_b="$version_test_numeric_c"
  226. fi
  227. if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | version_sort | head -n 1 | grep -qx "$version_test_numeric_b" ; then
  228. return 0
  229. else
  230. return 1
  231. fi
  232. }
  233. version_test_gt ()
  234. {
  235. version_test_gt_sedexp="s/[^-]*-//;s/[._-]\(pre\|rc\|test\|git\|old\|trunk\)/~\1/g"
  236. version_test_gt_a="`echo "$1" | sed -e "$version_test_gt_sedexp"`"
  237. version_test_gt_b="`echo "$2" | sed -e "$version_test_gt_sedexp"`"
  238. version_test_gt_cmp=gt
  239. if [ "x$version_test_gt_b" = "x" ] ; then
  240. return 0
  241. fi
  242. case "$version_test_gt_a:$version_test_gt_b" in
  243. *.old:*.old) ;;
  244. *.old:*) version_test_gt_a="`echo "$version_test_gt_a" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=gt ;;
  245. *:*.old) version_test_gt_b="`echo "$version_test_gt_b" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=ge ;;
  246. esac
  247. dpkg --compare-versions "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
  248. return "$?"
  249. }
  250. version_find_latest ()
  251. {
  252. version_find_latest_a=""
  253. for i in "$@" ; do
  254. if version_test_gt "$i" "$version_find_latest_a" ; then
  255. version_find_latest_a="$i"
  256. fi
  257. done
  258. echo "$version_find_latest_a"
  259. }
  260. # One layer of quotation is eaten by "" and the second by sed; so this turns
  261. # ' into \'.
  262. grub_quote () {
  263. sed "s/'/'\\\\''/g"
  264. }
  265. gettext_quoted () {
  266. gettext "$@" | grub_quote
  267. }
  268. # Run the first argument through gettext, and then pass that and all
  269. # remaining arguments to printf. This is a useful abbreviation and tends to
  270. # be easier to type.
  271. gettext_printf () {
  272. gettext_printf_format="$1"
  273. shift
  274. printf "$(gettext "$gettext_printf_format")" "$@"
  275. }
  276. uses_abstraction () {
  277. device="$1"
  278. old_ifs="$IFS"
  279. IFS='
  280. '
  281. abstraction="`"${grub_probe}" --device ${device} --target=abstraction`"
  282. for module in ${abstraction}; do
  283. if test "x${module}" = "x$2"; then
  284. IFS="$old_ifs"
  285. return 0
  286. fi
  287. done
  288. IFS="$old_ifs"
  289. return 1
  290. }
  291. print_option_help () {
  292. if test x$print_option_help_wc = x; then
  293. if wc -L </dev/null > /dev/null 2>&1; then
  294. print_option_help_wc=-L
  295. elif wc -m </dev/null > /dev/null 2>&1; then
  296. print_option_help_wc=-m
  297. else
  298. print_option_help_wc=-b
  299. fi
  300. fi
  301. if test x$grub_have_fmt = x; then
  302. if fmt -w 40 </dev/null > /dev/null 2>&1; then
  303. grub_have_fmt=y;
  304. else
  305. grub_have_fmt=n;
  306. fi
  307. fi
  308. print_option_help_lead=" $1"
  309. print_option_help_lspace="$(echo "$print_option_help_lead" | wc $print_option_help_wc)"
  310. print_option_help_fill="$((26 - print_option_help_lspace))"
  311. printf "%s" "$print_option_help_lead"
  312. if test $print_option_help_fill -le 0; then
  313. print_option_help_nl=y
  314. echo
  315. else
  316. print_option_help_i=0;
  317. while test $print_option_help_i -lt $print_option_help_fill; do
  318. printf " "
  319. print_option_help_i=$((print_option_help_i+1))
  320. done
  321. print_option_help_nl=n
  322. fi
  323. if test x$grub_have_fmt = xy; then
  324. print_option_help_split="$(echo "$2" | fmt -w 50)"
  325. else
  326. print_option_help_split="$2"
  327. fi
  328. if test x$print_option_help_nl = xy; then
  329. echo "$print_option_help_split" | awk \
  330. '{ print " " $0; }'
  331. else
  332. echo "$print_option_help_split" | awk 'BEGIN { n = 0 }
  333. { if (n == 1) print " " $0; else print $0; n = 1 ; }'
  334. fi
  335. }
  336. grub_fmt () {
  337. if test x$grub_have_fmt = x; then
  338. if fmt -w 40 < /dev/null > /dev/null; then
  339. grub_have_fmt=y;
  340. else
  341. grub_have_fmt=n;
  342. fi
  343. fi
  344. if test x$grub_have_fmt = xy; then
  345. fmt
  346. else
  347. cat
  348. fi
  349. }
  350. grub_tab=" "
  351. grub_add_tab () {
  352. sed -e "s/^/$grub_tab/"
  353. }