grub-mkconfig_lib.in 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. # One layer of quotation is eaten by "" and the second by sed; so this turns
  217. # ' into \'.
  218. grub_quote () {
  219. sed "s/'/'\\\\''/g"
  220. }
  221. gettext_quoted () {
  222. gettext "$@" | grub_quote
  223. }
  224. # Run the first argument through gettext, and then pass that and all
  225. # remaining arguments to printf. This is a useful abbreviation and tends to
  226. # be easier to type.
  227. gettext_printf () {
  228. gettext_printf_format="$1"
  229. shift
  230. printf "$(gettext "$gettext_printf_format")" "$@"
  231. }
  232. uses_abstraction () {
  233. device="$1"
  234. old_ifs="$IFS"
  235. IFS='
  236. '
  237. abstraction="`"${grub_probe}" --device ${device} --target=abstraction`"
  238. for module in ${abstraction}; do
  239. if test "x${module}" = "x$2"; then
  240. IFS="$old_ifs"
  241. return 0
  242. fi
  243. done
  244. IFS="$old_ifs"
  245. return 1
  246. }
  247. print_option_help () {
  248. if test x$print_option_help_wc = x; then
  249. if wc -L </dev/null > /dev/null 2>&1; then
  250. print_option_help_wc=-L
  251. elif wc -m </dev/null > /dev/null 2>&1; then
  252. print_option_help_wc=-m
  253. else
  254. print_option_help_wc=-b
  255. fi
  256. fi
  257. if test x$grub_have_fmt = x; then
  258. if fmt -w 40 </dev/null > /dev/null 2>&1; then
  259. grub_have_fmt=y;
  260. else
  261. grub_have_fmt=n;
  262. fi
  263. fi
  264. print_option_help_lead=" $1"
  265. print_option_help_lspace="$(echo "$print_option_help_lead" | wc $print_option_help_wc)"
  266. print_option_help_fill="$((26 - print_option_help_lspace))"
  267. printf "%s" "$print_option_help_lead"
  268. if test $print_option_help_fill -le 0; then
  269. print_option_help_nl=y
  270. echo
  271. else
  272. print_option_help_i=0;
  273. while test $print_option_help_i -lt $print_option_help_fill; do
  274. printf " "
  275. print_option_help_i=$((print_option_help_i+1))
  276. done
  277. print_option_help_nl=n
  278. fi
  279. if test x$grub_have_fmt = xy; then
  280. print_option_help_split="$(echo "$2" | fmt -w 50)"
  281. else
  282. print_option_help_split="$2"
  283. fi
  284. if test x$print_option_help_nl = xy; then
  285. echo "$print_option_help_split" | awk \
  286. '{ print " " $0; }'
  287. else
  288. echo "$print_option_help_split" | awk 'BEGIN { n = 0 }
  289. { if (n == 1) print " " $0; else print $0; n = 1 ; }'
  290. fi
  291. }
  292. grub_fmt () {
  293. if test x$grub_have_fmt = x; then
  294. if fmt -w 40 < /dev/null > /dev/null; then
  295. grub_have_fmt=y;
  296. else
  297. grub_have_fmt=n;
  298. fi
  299. fi
  300. if test x$grub_have_fmt = xy; then
  301. fmt
  302. else
  303. cat
  304. fi
  305. }
  306. grub_tab=" "
  307. grub_add_tab () {
  308. sed -e "s/^/$grub_tab/"
  309. }