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