]> git.proxmox.com Git - grub2.git/blob - util/grub-mkconfig_lib.in
* util/grub-mkconfig_lib.in (gettext_quoted): Add clarifying
[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 return 0
65 }
66
67 convert_system_path_to_grub_path ()
68 {
69 path=$1
70
71 grub_warn "convert_system_path_to_grub_path() is deprecated. Use prepare_grub_to_access_device() instead."
72
73 # abort if GRUB can't access the path
74 if is_path_readable_by_grub ${path} ; then : ; else
75 return 1
76 fi
77
78 if drive=`${grub_probe} -t drive $path` ; then : ; else
79 return 1
80 fi
81
82 if relative_path=`make_system_path_relative_to_its_root $path` ; then : ; else
83 return 1
84 fi
85
86 echo ${drive}${relative_path}
87 }
88
89 save_default_entry ()
90 {
91 if [ "x${GRUB_SAVEDEFAULT}" = "xtrue" ] ; then
92 cat << EOF
93 savedefault
94 EOF
95 fi
96 }
97
98 prepare_grub_to_access_device ()
99 {
100 device=$1
101
102 # Abstraction modules aren't auto-loaded.
103 abstraction="`${grub_probe} --device ${device} --target=abstraction`"
104 for module in ${abstraction} ; do
105 echo "insmod ${module}"
106 done
107
108 partmap="`${grub_probe} --device ${device} --target=partmap`"
109 for module in ${partmap} ; do
110 echo "insmod part_${module}"
111 done
112
113 fs="`${grub_probe} --device ${device} --target=fs`"
114 for module in ${fs} ; do
115 echo "insmod ${module}"
116 done
117
118 # If there's a filesystem UUID that GRUB is capable of identifying, use it;
119 # otherwise set root as per value in device.map.
120 echo "set root='`${grub_probe} --device ${device} --target=drive`'"
121 if fs_uuid="`${grub_probe} --device ${device} --target=fs_uuid 2> /dev/null`" ; then
122 echo "search --no-floppy --fs-uuid --set=root ${fs_uuid}"
123 fi
124 }
125
126 grub_file_is_not_garbage ()
127 {
128 if test -f "$1" ; then
129 case "$1" in
130 *.dpkg-*) return 1 ;; # debian dpkg
131 README*) return 1 ;; # documentation
132 esac
133 else
134 return 1
135 fi
136 return 0
137 }
138
139 version_test_numeric ()
140 {
141 local a=$1
142 local cmp=$2
143 local b=$3
144 if [ "$a" = "$b" ] ; then
145 case $cmp in
146 ge|eq|le) return 0 ;;
147 gt|lt) return 1 ;;
148 esac
149 fi
150 if [ "$cmp" = "lt" ] ; then
151 c=$a
152 a=$b
153 b=$c
154 fi
155 if (echo $a ; echo $b) | sort -n | head -n 1 | grep -qx $b ; then
156 return 0
157 else
158 return 1
159 fi
160 }
161
162 version_test_gt ()
163 {
164 local a=`echo $1 | sed -e "s/[^-]*-//"`
165 local b=`echo $2 | sed -e "s/[^-]*-//"`
166 local cmp=gt
167 if [ "x$b" = "x" ] ; then
168 return 0
169 fi
170 case $a:$b in
171 *.old:*.old) ;;
172 *.old:*) a=`echo -n $a | sed -e s/\.old$//` ; cmp=gt ;;
173 *:*.old) b=`echo -n $b | sed -e s/\.old$//` ; cmp=ge ;;
174 esac
175 version_test_numeric $a $cmp $b
176 return $?
177 }
178
179 version_find_latest ()
180 {
181 local a=""
182 for i in $@ ; do
183 if version_test_gt "$i" "$a" ; then
184 a="$i"
185 fi
186 done
187 echo "$a"
188 }
189
190 # One layer of quotation is eaten by "", the second by sed, and the third by
191 # printf; so this turns ' into \'. Note that you must use the output of
192 # this function in a printf format string.
193 gettext_quoted () {
194 $gettext "$@" | sed "s/'/'\\\\\\\\''/g"
195 }
196
197 # Run the first argument through gettext_quoted, and then pass that and all
198 # remaining arguments to printf. This is a useful abbreviation and tends to
199 # be easier to type.
200 gettext_printf () {
201 local format="$1"
202 shift
203 printf "$(gettext_quoted "$format")" "$@"
204 }
205
206 uses_abstraction () {
207 device=$1
208
209 abstraction="`${grub_probe} --device ${device} --target=abstraction`"
210 for module in ${abstraction}; do
211 if test "x${module}" = "x$2"; then
212 return 0
213 fi
214 done
215 return 1
216 }