]> git.proxmox.com Git - grub2.git/blob - util/grub-mkconfig_lib.in
* util/grub-mkstandalone.in: Fix help messages. Gettextize.
[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 :
36 else
37 gettext () {
38 echo -n "$@"
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>&1 ; then
69 :
70 else
71 return 1
72 fi
73
74 if [ x$GRUB_CRYPTODISK_ENABLE = 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 device="$1"
121
122 partmap="`"${grub_probe}" --device "${device}" --target=partmap`"
123 for module in ${partmap} ; do
124 case "${module}" in
125 netbsd | openbsd)
126 echo "insmod part_bsd";;
127 *)
128 echo "insmod part_${module}";;
129 esac
130 done
131
132 # Abstraction modules aren't auto-loaded.
133 abstraction="`"${grub_probe}" --device "${device}" --target=abstraction`"
134 for module in ${abstraction} ; do
135 echo "insmod ${module}"
136 done
137
138 fs="`"${grub_probe}" --device "${device}" --target=fs`"
139 for module in ${fs} ; do
140 echo "insmod ${module}"
141 done
142
143 if [ x$GRUB_CRYPTODISK_ENABLE = xy ]; then
144 for uuid in "`"${grub_probe}" --device "${device}" --target=cryptodisk_uuid`"; do
145 echo "cryptomount -u $uuid"
146 done
147 fi
148
149 # If there's a filesystem UUID that GRUB is capable of identifying, use it;
150 # otherwise set root as per value in device.map.
151 fs_hint="`"${grub_probe}" --device "${device}" --target=compatibility_hint`"
152 if [ "x$fs_hint" != x ]; then
153 echo "set root='$fs_hint'"
154 fi
155 if fs_uuid="`"${grub_probe}" --device "${device}" --target=fs_uuid 2> /dev/null`" ; then
156 hints="`"${grub_probe}" --device "${device}" --target=hints_string 2> /dev/null`"
157 echo "if [ x\$feature_platform_search_hint = xy ]; then"
158 echo " search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
159 echo "else"
160 echo " search --no-floppy --fs-uuid --set=root ${fs_uuid}"
161 echo "fi"
162 fi
163 }
164
165 grub_file_is_not_garbage ()
166 {
167 if test -f "$1" ; then
168 case "$1" in
169 *.dpkg-*) return 1 ;; # debian dpkg
170 README*|*/README*) return 1 ;; # documentation
171 esac
172 else
173 return 1
174 fi
175 return 0
176 }
177
178 version_test_numeric ()
179 {
180 version_test_numeric_a="$1"
181 version_test_numeric_cmp="$2"
182 version_test_numeric_b="$3"
183 if [ "$version_test_numeric_a" = "$version_test_numeric_b" ] ; then
184 case "$version_test_numeric_cmp" in
185 ge|eq|le) return 0 ;;
186 gt|lt) return 1 ;;
187 esac
188 fi
189 if [ "$version_test_numeric_cmp" = "lt" ] ; then
190 version_test_numeric_c="$version_test_numeric_a"
191 version_test_numeric_a="$version_test_numeric_b"
192 version_test_numeric_b="$version_test_numeric_c"
193 fi
194 if (echo "$version_test_numeric_a" ; echo "$version_test_numeric_b") | sort -n | head -n 1 | grep -qx "$version_test_numeric_b" ; then
195 return 0
196 else
197 return 1
198 fi
199 }
200
201 version_test_gt ()
202 {
203 version_test_gt_a="`echo "$1" | sed -e "s/[^-]*-//"`"
204 version_test_gt_b="`echo "$2" | sed -e "s/[^-]*-//"`"
205 version_test_gt_cmp=gt
206 if [ "x$version_test_gt_b" = "x" ] ; then
207 return 0
208 fi
209 case "$version_test_gt_a:$version_test_gt_b" in
210 *.old:*.old) ;;
211 *.old:*) version_test_gt_a="`echo -n "$version_test_gt_a" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=gt ;;
212 *:*.old) version_test_gt_b="`echo -n "$version_test_gt_b" | sed -e 's/\.old$//'`" ; version_test_gt_cmp=ge ;;
213 esac
214 version_test_numeric "$version_test_gt_a" "$version_test_gt_cmp" "$version_test_gt_b"
215 return "$?"
216 }
217
218 version_find_latest ()
219 {
220 version_find_latest_a=""
221 for i in "$@" ; do
222 if version_test_gt "$i" "$version_find_latest_a" ; then
223 version_find_latest_a="$i"
224 fi
225 done
226 echo "$version_find_latest_a"
227 }
228
229 # One layer of quotation is eaten by "", the second by sed, and the third by
230 # printf; so this turns ' into \'. Note that you must use the output of
231 # this function in a printf format string.
232 gettext_quoted () {
233 gettext "$@" | sed "s/'/'\\\\\\\\''/g"
234 }
235
236 # Run the first argument through gettext_quoted, and then pass that and all
237 # remaining arguments to printf. This is a useful abbreviation and tends to
238 # be easier to type.
239 gettext_printf () {
240 gettext_printf_format="$1"
241 shift
242 printf "$(gettext_quoted "$gettext_printf_format")" "$@"
243 }
244
245 uses_abstraction () {
246 device="$1"
247
248 abstraction="`"${grub_probe}" --device "${device}" --target=abstraction`"
249 for module in ${abstraction}; do
250 if test "x${module}" = "x$2"; then
251 return 0
252 fi
253 done
254 return 1
255 }