]> git.proxmox.com Git - grub2.git/blame - util/grub-mkconfig.in
Fix an infinite loop in grub-mkconfig
[grub2.git] / util / grub-mkconfig.in
CommitLineData
1246efeb
CW
1#! /bin/sh
2set -e
ebd97f6e 3
4# Generate grub.cfg by inspecting /boot contents.
d500ed12 5# Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
ebd97f6e 6#
5a79f472 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
ebd97f6e 10# (at your option) any later version.
11#
5a79f472 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.
ebd97f6e 16#
17# You should have received a copy of the GNU General Public License
5a79f472 18# along with GRUB. If not, see <http://www.gnu.org/licenses/>.
ebd97f6e 19
48b391e9
VS
20prefix="@prefix@"
21exec_prefix="@exec_prefix@"
22datarootdir="@datarootdir@"
23
24prefix="@prefix@"
25exec_prefix="@exec_prefix@"
26sbindir="@sbindir@"
27bindir="@bindir@"
28sysconfdir="@sysconfdir@"
0ea7c4f9
GS
29PACKAGE_NAME=@PACKAGE_NAME@
30PACKAGE_VERSION=@PACKAGE_VERSION@
38e55e90 31host_os=@host_os@
48b391e9 32datadir="@datadir@"
c8d9ead6 33if [ "x$pkgdatadir" = x ]; then
2311c5ca 34 pkgdatadir="${datadir}/@PACKAGE@"
c8d9ead6 35fi
18ade780 36grub_cfg=""
48b391e9 37grub_mkconfig_dir="${sysconfdir}"/grub.d
2e610d62 38
0ea7c4f9
GS
39self=`basename $0`
40
fd49ceb3 41grub_probe="${sbindir}/@grub_probe@"
ec824e0f 42grub_file="${bindir}/@grub_file@"
fd49ceb3
CW
43grub_editenv="${bindir}/@grub_editenv@"
44grub_script_check="${bindir}/@grub_script_check@"
ebd97f6e 45
9cc3ee5c
VS
46export TEXTDOMAIN=@PACKAGE@
47export TEXTDOMAINDIR="@localedir@"
48
c8d9ead6 49. "${pkgdatadir}/grub-mkconfig_lib"
a32d5c71 50
68807e5f 51# Usage: usage
52# Print the usage.
53usage () {
a32d5c71
VS
54 gettext_printf "Usage: %s [OPTION]\n" "$self"
55 gettext "Generate a grub config file"; echo
56 echo
9f9d3f69
VS
57 print_option_help "-o, --output=$(gettext FILE)" "$(gettext "output generated config to FILE [default=stdout]")"
58 print_option_help "-h, --help" "$(gettext "print this message and exit")"
59 print_option_help "-v, --version" "$(gettext "print the version information and exit")"
a32d5c71
VS
60 echo
61 gettext "Report bugs to <bug-grub@gnu.org>."; echo
68807e5f 62}
63
b02c7c8f
BC
64argument () {
65 opt=$1
66 shift
67
68 if test $# -eq 0; then
67811a6f 69 gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
b02c7c8f
BC
70 exit 1
71 fi
72 echo $1
73}
74
68807e5f 75# Check the arguments.
7a64e28d
BC
76while test $# -gt 0
77do
78 option=$1
79 shift
80
68807e5f 81 case "$option" in
82 -h | --help)
83 usage
84 exit 0 ;;
85 -v | --version)
0ea7c4f9 86 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
68807e5f 87 exit 0 ;;
7a64e28d 88 -o | --output)
b02c7c8f 89 grub_cfg=`argument $option "$@"`; shift;;
880fc3c4 90 --output=*)
18ade780 91 grub_cfg=`echo "$option" | sed 's/--output=//'`
b39f9d20 92 ;;
68807e5f 93 -*)
a32d5c71 94 gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
68807e5f 95 usage
96 exit 1
97 ;;
044e2e60 98 # Explicitly ignore non-option arguments, for compatibility.
68807e5f 99 esac
100done
101
a20dfe95
CW
102if fgrep -qs '${GRUB_PREFIX}/video.lst' "${grub_mkconfig_dir}/00_header"; then
103 echo "GRUB >= 2.00 has been unpacked but not yet configured." >&2
104 echo "grub-mkconfig will not work until the upgrade is complete." >&2
105 echo "It should run later as part of configuring the new GRUB packages." >&2
106 exit 0
107fi
108
991477f8 109if [ "x$EUID" = "x" ] ; then
110 EUID=`id -u`
ebd97f6e 111fi
112
991477f8 113if [ "$EUID" != 0 ] ; then
b609876d 114 root=f
115 case "`uname 2>/dev/null`" in
116 CYGWIN*)
117 # Cygwin: Assume root if member of admin group
118 for g in `id -G 2>/dev/null` ; do
119 case $g in
120 0|544) root=t ;;
121 esac
122 done ;;
123 esac
124 if [ $root != t ] ; then
a32d5c71 125 gettext_printf "%s: You must run this as root\n" "$self" >&2
b609876d 126 exit 1
127 fi
ebd97f6e 128fi
129
2e610d62 130set $grub_probe dummy
131if test -f "$1"; then
132 :
133else
a32d5c71 134 gettext_print "%s: Not found.\n" "$1" 1>&2
2e610d62 135 exit 1
ebd97f6e 136fi
137
cc85c3c3 138# Device containing our userland. Typically used for root= parameter.
faf4a65e 139GRUB_DEVICE="`${grub_probe} --target=device /`"
69ba137e 140GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
62191274 141
142# Device containing our /boot partition. Usually the same as GRUB_DEVICE.
faf4a65e 143GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
69ba137e 144GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
57f96397 145
146# Filesystem for the device containing our userland. Used for stuff like
147# choosing Hurd filesystem module.
99fd620d 148GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
57f96397 149
faf4a65e
VS
150if [ x"$GRUB_FS" = xunknown ]; then
151 GRUB_FS="$(stat -f --printf=%T / || echo unknown)"
4c4ea9c7
VS
152fi
153
ebd97f6e 154if test -f ${sysconfdir}/default/grub ; then
155 . ${sysconfdir}/default/grub
156fi
b7b657b6
CW
157for x in ${sysconfdir}/default/grub.d/*.cfg ; do
158 if [ -e "${x}" ]; then
159 . "${x}"
160 fi
161done
ebd97f6e 162
651c29b7 163# XXX: should this be deprecated at some point?
164if [ "x${GRUB_TERMINAL}" != "x" ] ; then
165 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
166 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
167fi
168
d5631db0 169termoutdefault=0
38e55e90 170if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
d5631db0
VS
171 GRUB_TERMINAL_OUTPUT=gfxterm;
172 termoutdefault=1;
173fi
174
d5631db0
VS
175for x in ${GRUB_TERMINAL_OUTPUT}; do
176 case "x${x}" in
177 xgfxterm) ;;
608b647a 178 xconsole | xserial | xofconsole | xvga_text)
d5631db0
VS
179 # make sure all our children behave in conformance with ascii..
180 export LANG=C;;
181 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
182 esac
183done
6193defe 184
0cdc126c
VS
185GRUB_ACTUAL_DEFAULT="$GRUB_DEFAULT"
186
187if [ "x${GRUB_ACTUAL_DEFAULT}" = "xsaved" ] ; then GRUB_ACTUAL_DEFAULT="`"${grub_editenv}" - list | sed -n '/^saved_entry=/ s,^saved_entry=,,p'`" ; fi
188
41ab038c
CW
189if [ "x${GRUB_RECOVERY_TITLE}" = "x" ]; then
190 GRUB_RECOVERY_TITLE="recovery mode"
191fi
192
0cdc126c 193
57f96397 194# These are defined in this script, export them here so that user can
195# override them.
9a10df16 196export GRUB_DEVICE \
197 GRUB_DEVICE_UUID \
198 GRUB_DEVICE_BOOT \
199 GRUB_DEVICE_BOOT_UUID \
200 GRUB_FS \
274416e8 201 GRUB_FONT \
0cdc126c
VS
202 GRUB_PRELOAD_MODULES \
203 GRUB_ACTUAL_DEFAULT
57f96397 204
205# These are optional, user-defined variables.
9a10df16 206export GRUB_DEFAULT \
207 GRUB_HIDDEN_TIMEOUT \
208 GRUB_HIDDEN_TIMEOUT_QUIET \
209 GRUB_TIMEOUT \
8f236c14 210 GRUB_TIMEOUT_STYLE \
76e6d0d7
VS
211 GRUB_DEFAULT_BUTTON \
212 GRUB_HIDDEN_TIMEOUT_BUTTON \
213 GRUB_TIMEOUT_BUTTON \
53cc63bf 214 GRUB_TIMEOUT_STYLE_BUTTON \
76e6d0d7 215 GRUB_BUTTON_CMOS_ADDRESS \
eaf41b25 216 GRUB_BUTTON_CMOS_CLEAN \
9a10df16 217 GRUB_DISTRIBUTOR \
218 GRUB_CMDLINE_LINUX \
219 GRUB_CMDLINE_LINUX_DEFAULT \
19dcc490
VS
220 GRUB_CMDLINE_XEN \
221 GRUB_CMDLINE_XEN_DEFAULT \
f35fa3a6
CW
222 GRUB_CMDLINE_LINUX_XEN_REPLACE \
223 GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT \
0d2c20c6
GS
224 GRUB_CMDLINE_NETBSD \
225 GRUB_CMDLINE_NETBSD_DEFAULT \
ac96441c 226 GRUB_CMDLINE_GNUMACH \
9a10df16 227 GRUB_TERMINAL_INPUT \
228 GRUB_TERMINAL_OUTPUT \
229 GRUB_SERIAL_COMMAND \
230 GRUB_DISABLE_LINUX_UUID \
219b3564 231 GRUB_DISABLE_RECOVERY \
e4311a9f 232 GRUB_VIDEO_BACKEND \
5174302b 233 GRUB_GFXMODE \
d7c43ba1 234 GRUB_BACKGROUND \
eb1c9594 235 GRUB_THEME \
65a533e7 236 GRUB_GFXPAYLOAD_LINUX \
0a39de87 237 GRUB_DISABLE_OS_PROBER \
a0ca21c2 238 GRUB_INIT_TUNE \
d500ed12 239 GRUB_SAVEDEFAULT \
20a40940 240 GRUB_ENABLE_CRYPTODISK \
323de05b 241 GRUB_BADRAM \
55e706c9 242 GRUB_OS_PROBER_SKIP_LIST \
4b19cd97 243 GRUB_DISABLE_SUBMENU \
41ab038c
CW
244 GRUB_RECORDFAIL_TIMEOUT \
245 GRUB_RECOVERY_TITLE
57f96397 246
18ade780 247if test "x${grub_cfg}" != "x"; then
055f856f
MW
248 rm -f "${grub_cfg}.new"
249 oldumask=$(umask); umask 077
250 exec > "${grub_cfg}.new"
251 umask $oldumask
18ade780 252fi
ca120e31 253gettext "Generating grub configuration file ..." >&2
a32d5c71 254echo >&2
ebd97f6e 255
256cat << EOF
257#
258# DO NOT EDIT THIS FILE
259#
0ea7c4f9 260# It is automatically generated by $self using templates
f6fd460a 261# from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
ebd97f6e 262#
263EOF
264
ec824e0f
VS
265
266for i in "${grub_mkconfig_dir}"/* ; do
53b052de 267 case "$i" in
ebd97f6e 268 # emacsen backup files. FIXME: support other editors
269 *~) ;;
6d1fa41f 270 # emacsen autosave files. FIXME: support other editors
c137aea0 271 */\#*\#) ;;
ebd97f6e 272 *)
2f80039d 273 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
faf4a65e
VS
274 echo
275 echo "### BEGIN $i ###"
276 "$i"
277 echo "### END $i ###"
ebd97f6e 278 fi
279 ;;
280 esac
281done
282
e3361108
CW
283if [ "x${grub_cfg}" != "x" ] && ! grep "^password" ${grub_cfg}.new >/dev/null; then
284 chmod 444 ${grub_cfg}.new || true
285fi
286
18ade780 287if test "x${grub_cfg}" != "x" ; then
4417aae6 288 if ! ${grub_script_check} ${grub_cfg}.new; then
e7d2559b 289 # TRANSLATORS: %s is replaced by filename
a32d5c71
VS
290 gettext_printf "Syntax errors are detected in generated GRUB config file.
291Ensure that there are no errors in /etc/default/grub
292and /etc/grub.d/* files or please file a bug report with
293%s file attached." "${grub_cfg}.new" >&2
fe4ae213 294 echo >&2
75d8c629 295 else
296 # none of the children aborted with error, install the new grub.cfg
297 mv -f ${grub_cfg}.new ${grub_cfg}
298 fi
18ade780 299fi
ebd97f6e 300
a32d5c71
VS
301gettext "done" >&2
302echo >&2