]> git.proxmox.com Git - grub2.git/blob - util/grub-mkconfig.in
merge mainline into net
[grub2.git] / util / grub-mkconfig.in
1 #! /bin/sh
2 set -e
3
4 # Generate grub.cfg by inspecting /boot contents.
5 # Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
6 #
7 # GRUB is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # GRUB is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
20 transform="@program_transform_name@"
21
22 prefix=@prefix@
23 exec_prefix=@exec_prefix@
24 sbindir=@sbindir@
25 libdir=@libdir@
26 sysconfdir=@sysconfdir@
27 PACKAGE_NAME=@PACKAGE_NAME@
28 PACKAGE_VERSION=@PACKAGE_VERSION@
29 host_os=@host_os@
30 datarootdir=@datarootdir@
31 datadir=@datadir@
32 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
33 grub_cfg=""
34 grub_mkconfig_dir=${sysconfdir}/grub.d
35
36 self=`basename $0`
37
38 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
39 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
40
41 GRUB_PREFIX=`echo '/@bootdirname@/@grubdirname@' | sed "s,//*,/,g"`
42
43 # Usage: usage
44 # Print the usage.
45 usage () {
46 cat <<EOF
47 Usage: $self [OPTION]
48 Generate a grub config file
49
50 -o, --output=FILE output generated config to FILE [default=stdout]
51 -h, --help print this message and exit
52 -v, --version print the version information and exit
53
54 Report bugs to <bug-grub@gnu.org>.
55 EOF
56 }
57
58 argument () {
59 opt=$1
60 shift
61
62 if test $# -eq 0; then
63 echo "$0: option requires an argument -- '$opt'" 1>&2
64 exit 1
65 fi
66 echo $1
67 }
68
69 # Check the arguments.
70 while test $# -gt 0
71 do
72 option=$1
73 shift
74
75 case "$option" in
76 -h | --help)
77 usage
78 exit 0 ;;
79 -v | --version)
80 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
81 exit 0 ;;
82 -o | --output)
83 grub_cfg=`argument $option "$@"`; shift;;
84 --output=*)
85 grub_cfg=`echo "$option" | sed 's/--output=//'`
86 ;;
87 -*)
88 echo "Unrecognized option \`$option'" 1>&2
89 usage
90 exit 1
91 ;;
92 # Explicitly ignore non-option arguments, for compatibility.
93 esac
94 done
95
96 . ${libdir}/grub/grub-mkconfig_lib
97
98 if [ "x$EUID" = "x" ] ; then
99 EUID=`id -u`
100 fi
101
102 if [ "$EUID" != 0 ] ; then
103 root=f
104 case "`uname 2>/dev/null`" in
105 CYGWIN*)
106 # Cygwin: Assume root if member of admin group
107 for g in `id -G 2>/dev/null` ; do
108 case $g in
109 0|544) root=t ;;
110 esac
111 done ;;
112 esac
113 if [ $root != t ] ; then
114 echo "$self: You must run this as root" >&2
115 exit 1
116 fi
117 fi
118
119 set $grub_mkdevicemap dummy
120 if test -f "$1"; then
121 :
122 else
123 echo "$1: Not found." 1>&2
124 exit 1
125 fi
126
127 set $grub_probe dummy
128 if test -f "$1"; then
129 :
130 else
131 echo "$1: Not found." 1>&2
132 exit 1
133 fi
134
135 mkdir -p ${GRUB_PREFIX}
136
137 if test -e ${GRUB_PREFIX}/device.map ; then : ; else
138 ${grub_mkdevicemap}
139 fi
140
141 # Device containing our userland. Typically used for root= parameter.
142 GRUB_DEVICE="`${grub_probe} --target=device /`"
143 GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
144
145 # Device containing our /boot partition. Usually the same as GRUB_DEVICE.
146 GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
147 GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
148
149 # Filesystem for the device containing our userland. Used for stuff like
150 # choosing Hurd filesystem module.
151 GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
152
153 if test -f ${sysconfdir}/default/grub ; then
154 . ${sysconfdir}/default/grub
155 fi
156
157 # XXX: should this be deprecated at some point?
158 if [ "x${GRUB_TERMINAL}" != "x" ] ; then
159 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
160 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
161 fi
162
163 termoutdefault=0
164 if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
165 GRUB_TERMINAL_OUTPUT=gfxterm;
166 termoutdefault=1;
167 fi
168
169 for x in ${GRUB_TERMINAL_OUTPUT}; do
170 if [ "x${x}" = "xgfxterm" ]; then
171 # If this platform supports gfxterm, try to use it.
172 if ! test -e ${GRUB_PREFIX}/gfxterm.mod ; then
173 if [ "x$termoutdefault" != "x1" ]; then
174 echo "gfxterm isn't available on your platform" >&2 ; exit 1
175 fi
176 GRUB_TERMINAL_OUTPUT=
177 break;
178 fi
179 if [ ! -s "${GRUB_PREFIX}/video.lst" ] ; then
180 if [ "x$termoutdefault" != "x1" ]; then
181 echo "No suitable backend could be found for gfxterm." >&2 ; exit 1
182 fi
183 GRUB_TERMINAL_OUTPUT=
184 fi
185 if [ -n "$GRUB_FONT" ] ; then
186 if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
187 GRUB_FONT_PATH=${GRUB_FONT}
188 else
189 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
190 exit 1
191 fi
192 else
193 for dir in ${pkgdatadir} ${GRUB_PREFIX} /usr/share/grub ; do
194 for basename in unicode unifont ascii; do
195 path="${dir}/${basename}.pf2"
196 if is_path_readable_by_grub ${path} > /dev/null ; then
197 GRUB_FONT_PATH=${path}
198 else
199 continue
200 fi
201 if [ "${basename}" = "ascii" ] ; then
202 # make sure all our children behave in conformance with ascii..
203 export LANG=C
204 fi
205 break 2
206 done
207 done
208 fi
209 if [ -z "${GRUB_FONT_PATH}" ] ; then
210 if [ "x$termoutdefault" != "x1" ]; then
211 echo "No font for gfxterm found." >&2 ; exit 1
212 fi
213 GRUB_TERMINAL_OUTPUT=
214 fi
215 fi
216 done
217
218 for x in ${GRUB_TERMINAL_OUTPUT}; do
219 case "x${x}" in
220 xgfxterm) ;;
221 xconsole | xserial | xofconsole)
222 # make sure all our children behave in conformance with ascii..
223 export LANG=C;;
224 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
225 esac
226 done
227
228 # These are defined in this script, export them here so that user can
229 # override them.
230 export GRUB_DEVICE \
231 GRUB_DEVICE_UUID \
232 GRUB_DEVICE_BOOT \
233 GRUB_DEVICE_BOOT_UUID \
234 GRUB_FS \
235 GRUB_FONT_PATH \
236 GRUB_PRELOAD_MODULES \
237 GRUB_PREFIX
238
239 # These are optional, user-defined variables.
240 export GRUB_DEFAULT \
241 GRUB_HIDDEN_TIMEOUT \
242 GRUB_HIDDEN_TIMEOUT_QUIET \
243 GRUB_TIMEOUT \
244 GRUB_DEFAULT_BUTTON \
245 GRUB_HIDDEN_TIMEOUT_BUTTON \
246 GRUB_TIMEOUT_BUTTON \
247 GRUB_BUTTON_CMOS_ADDRESS \
248 GRUB_BUTTON_CMOS_CLEAN \
249 GRUB_DISTRIBUTOR \
250 GRUB_CMDLINE_LINUX \
251 GRUB_CMDLINE_LINUX_DEFAULT \
252 GRUB_CMDLINE_XEN \
253 GRUB_CMDLINE_XEN_DEFAULT \
254 GRUB_CMDLINE_NETBSD \
255 GRUB_CMDLINE_NETBSD_DEFAULT \
256 GRUB_TERMINAL_INPUT \
257 GRUB_TERMINAL_OUTPUT \
258 GRUB_SERIAL_COMMAND \
259 GRUB_DISABLE_LINUX_UUID \
260 GRUB_DISABLE_RECOVERY \
261 GRUB_VIDEO_BACKEND \
262 GRUB_GFXMODE \
263 GRUB_BACKGROUND \
264 GRUB_THEME \
265 GRUB_GFXPAYLOAD_LINUX \
266 GRUB_DISABLE_OS_PROBER \
267 GRUB_INIT_TUNE \
268 GRUB_SAVEDEFAULT \
269 GRUB_BADRAM
270
271 if test "x${grub_cfg}" != "x"; then
272 rm -f ${grub_cfg}.new
273 exec > ${grub_cfg}.new
274
275 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
276 # firmware implementations (e.g. OFW or EFI).
277 chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
278 This means that if the generated config contains a password it is readable by everyone"
279 fi
280 echo "Generating grub.cfg ..." >&2
281
282 cat << EOF
283 #
284 # DO NOT EDIT THIS FILE
285 #
286 # It is automatically generated by $self using templates
287 # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
288 #
289 EOF
290
291 for i in ${grub_mkconfig_dir}/* ; do
292 case "$i" in
293 # emacsen backup files. FIXME: support other editors
294 *~) ;;
295 *)
296 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
297 echo
298 echo "### BEGIN $i ###"
299 "$i"
300 echo "### END $i ###"
301 fi
302 ;;
303 esac
304 done
305
306 if test "x${grub_cfg}" != "x" ; then
307 if ! grub-script-check ${grub_cfg}.new; then
308 echo "Syntax errors are detected in generated GRUB config file." >&2
309 echo "Ensure that there are no errors in /etc/default/grub" >&2
310 echo "and /etc/grub.d/* files or please file a bug report with" >&2
311 echo "${grub_cfg}.new file attached." >&2
312 else
313 # none of the children aborted with error, install the new grub.cfg
314 mv -f ${grub_cfg}.new ${grub_cfg}
315 fi
316 fi
317
318 echo "done" >&2