]> git.proxmox.com Git - grub2.git/blob - util/grub-mkconfig_lib.in
Put recheck back
[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 transform="@program_transform_name@"
18
19 prefix="@prefix@"
20 exec_prefix="@exec_prefix@"
21 datarootdir="@datarootdir@"
22 datadir="@datadir@"
23 bindir="@bindir@"
24 sbindir="@sbindir@"
25 pkgdatadir="${datadir}/`echo "@PACKAGE_TARNAME@" | sed "${transform}"`"
26
27 if test "x$grub_probe" = x; then
28 grub_probe="${sbindir}/`echo grub-probe | sed "${transform}"`"
29 fi
30 if test "x$grub_mkrelpath" = x; then
31 grub_mkrelpath="${bindir}/`echo grub-mkrelpath | sed "${transform}"`"
32 fi
33
34 if $(which gettext >/dev/null 2>/dev/null) ; then
35 gettext="gettext"
36 else
37 gettext="echo"
38 fi
39
40 grub_warn ()
41 {
42 echo "Warning: $@" >&2
43 }
44
45 make_system_path_relative_to_its_root ()
46 {
47 "${grub_mkrelpath}" "$1"
48 }
49
50 is_path_readable_by_grub ()
51 {
52 path="$1"
53
54 # abort if path doesn't exist
55 if test -e "$path" ; then : ;else
56 return 1
57 fi
58
59 # abort if file is in a filesystem we can't read
60 if "${grub_probe}" -t fs "$path" > /dev/null 2>&1 ; then : ; else
61 return 1
62 fi
63
64 # ... or if we can't figure out the abstraction module, for example if
65 # memberlist fails on an LVM volume group.
66 if abstractions="`"${grub_probe}" -t abstraction "$path"`" 2>&1 ; then
67 :
68 else
69 return 1
70 fi
71
72 if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
73 return 0
74 fi
75
76 for abstraction in $abstractions; do
77 if [ "x$abstraction" = xcryptodisk ]; then
78 return 1
79 fi
80 done
81
82 return 0
83 }
84
85 convert_system_path_to_grub_path ()
86 {
87 path="$1"
88
89 grub_warn "convert_system_path_to_grub_path() is deprecated. Use prepare_grub_to_access_device() instead."
90
91 # abort if GRUB can't access the path
92 if is_path_readable_by_grub "${path}" ; then : ; else
93 return 1
94 fi
95
96 if drive="`"${grub_probe}" -t drive "$path"`" ; then : ; else
97 return 1
98 fi
99
100 if relative_path="`make_system_path_relative_to_its_root "$path"`" ; then : ; else
101 return 1
102 fi
103
104 echo "${drive}${relative_path}"
105 }
106
107 save_default_entry ()
108 {
109 if [ "x${GRUB_SAVEDEFAULT}" = "xtrue" ] ; then
110 cat << EOF
111 savedefault
112 EOF
113 fi
114 }
115
116 prepare_grub_to_access_device ()
117 {
118 device="$1"
119
120 partmap="`"${grub_probe}" --device "${device}" --target=partmap`"
121 for module in ${partmap} ; do
122 case "${module}" in
123 netbsd | openbsd)
124 echo "insmod part_bsd";;
125 *)
126 echo "insmod part_${module}";;
127 esac
128 done
129
130 # Abstraction modules aren't auto-loaded.
131 abstraction="`"${grub_probe}" --device "${device}" --target=abstraction`"
132 for module in ${abstraction} ; do
133 echo "insmod ${module}"
134 done
135
136 fs="`"${grub_probe}" --device "${device}" --target=fs`"
137 for module in ${fs} ; do
138 echo "insmod ${module}"
139 done
140
141 if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
142 for uuid in "`"${grub_probe}" --device "${device}" --target=cryptodisk_uuid`"; do
143 echo "cryptomount -u $uuid"
144 done
145 fi
146
147 # If there's a filesystem UUID that GRUB is capable of identifying, use it;
148 # otherwise set root as per value in device.map.
149 echo "set root='`"${grub_probe}" --device "${device}" --target=compatibility_hint`'"
150 if fs_uuid="`"${grub_probe}" --device "${device}" --target=fs_uuid 2> /dev/null`" ; then
151 hints="`"${grub_probe}" --device "${device}" --target=hints_string 2> /dev/null`"
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 }
159
160 grub_file_is_not_garbage ()
161 {
162 if test -f "$1" ; then
163 case "$1" in
164 *.dpkg-*) return 1 ;; # debian dpkg
165 README*|*/README*) return 1 ;; # documentation
166 esac
167 else
168 return 1
169 fi
170 return 0
171 }
172
173 version_test_numeric ()
174 {
175 version_test_numeric_a="$1"
176 version_test_numeric_cmp="$2"
177 version_test_numeric_b="$3"
178 if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
179 case "$version_test_numeric_cmp" in
180 ge|eq|le) return 0 ;;
181 gt|lt) return 1 ;;
182 esac
183 fi
184 if [ "$version_test_numeric_cmp" = "lt" ] ; then
185 version_test_numeric_c="$version_test_numeric_a"
186 version_test_numeric_a="$version_test_numeric_b"
187 version_test_numeric_b="$version_test_numeric_c"
188 fi
189 if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | sort -n | head -n 1 | grep -qx "$version_test_numeric_b" ; then
190 return 0
191 else
192 return 1
193 fi
194 }
195
196 version_test_gt ()
197 {
198 version_test_gt_a="`echo "$1" | sed -e "s/[^-]*-//"`"
199 version_test_gt_b="`echo "$2" | sed -e "s/[^-]*-//"`"
200 version_test_gt_cmp=gt
201 if [ "x$version_test_gt_b" = "x" ] ; then
202 return 0
203 fi
204 case "$version_test_gt_a:$version_test_gt_b" in
205 *.old:*.old) ;;
206 *.old:*) version_test_gt_a="`echo -n "$version_test_gt_a" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=gt ;;
207 *:*.old) version_test_gt_b="`echo -n "$version_test_gt_b" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=ge ;;
208 esac
209 version_test_numeric "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
210 return "$?"
211 }
212
213 version_find_latest ()
214 {
215 version_find_latest_a=""
216 for i in "$@" ; do
217 if version_test_gt "$i" "$version_find_latest_a" ; then
218 version_find_latest_a="$i"
219 fi
220 done
221 echo "$version_find_latest_a"
222 }
223
224 # One layer of quotation is eaten by "", the second by sed, and the third by
225 # printf; so this turns ' into \'. Note that you must use the output of
226 # this function in a printf format string.
227 gettext_quoted () {
228 "$gettext" "$@" | sed "s/'/'\\\\\\\\''/g"
229 }
230
231 # Run the first argument through gettext_quoted, and then pass that and all
232 # remaining arguments to printf. This is a useful abbreviation and tends to
233 # be easier to type.
234 gettext_printf () {
235 gettext_printf_format="$1"
236 shift
237 printf "$(gettext_quoted "$gettext_printf_format")" "$@"
238 }
239
240 uses_abstraction () {
241 device="$1"
242
243 abstraction="`"${grub_probe}" --device "${device}" --target=abstraction`"
244 for module in ${abstraction}; do
245 if test "x${module}" = "x$2"; then
246 return 0
247 fi
248 done
249 return 1
250 }