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