]> git.proxmox.com Git - grub2.git/blame - util/grub-mkconfig.in
* util/grub-mkimage.c (main): Fix format-security warning.
[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
20transform="@program_transform_name@"
48b391e9
VS
21prefix="@prefix@"
22exec_prefix="@exec_prefix@"
23datarootdir="@datarootdir@"
24
25prefix="@prefix@"
26exec_prefix="@exec_prefix@"
27sbindir="@sbindir@"
28bindir="@bindir@"
29sysconfdir="@sysconfdir@"
0ea7c4f9
GS
30PACKAGE_NAME=@PACKAGE_NAME@
31PACKAGE_VERSION=@PACKAGE_VERSION@
38e55e90 32host_os=@host_os@
48b391e9
VS
33datadir="@datadir@"
34pkgdatadir="${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`"
18ade780 35grub_cfg=""
48b391e9 36grub_mkconfig_dir="${sysconfdir}"/grub.d
2e610d62 37
0ea7c4f9
GS
38self=`basename $0`
39
48b391e9 40grub_probe="${sbindir}/`echo grub-probe | sed "${transform}"`"
4417aae6 41grub_script_check="${bindir}/`echo grub-script-check | sed "${transform}"`"
ebd97f6e 42
b9fe6ea2
YB
43GRUB_PREFIX=`echo '/@bootdirname@/@grubdirname@' | sed "s,//*,/,g"`
44
68807e5f 45# Usage: usage
46# Print the usage.
47usage () {
48 cat <<EOF
0ea7c4f9 49Usage: $self [OPTION]
18ade780 50Generate a grub config file
68807e5f 51
18ade780 52 -o, --output=FILE output generated config to FILE [default=stdout]
68807e5f 53 -h, --help print this message and exit
54 -v, --version print the version information and exit
68807e5f 55
56Report bugs to <bug-grub@gnu.org>.
57EOF
58}
59
b02c7c8f
BC
60argument () {
61 opt=$1
62 shift
63
64 if test $# -eq 0; then
65 echo "$0: option requires an argument -- '$opt'" 1>&2
66 exit 1
67 fi
68 echo $1
69}
70
68807e5f 71# Check the arguments.
7a64e28d
BC
72while test $# -gt 0
73do
74 option=$1
75 shift
76
68807e5f 77 case "$option" in
78 -h | --help)
79 usage
80 exit 0 ;;
81 -v | --version)
0ea7c4f9 82 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
68807e5f 83 exit 0 ;;
7a64e28d 84 -o | --output)
b02c7c8f 85 grub_cfg=`argument $option "$@"`; shift;;
880fc3c4 86 --output=*)
18ade780 87 grub_cfg=`echo "$option" | sed 's/--output=//'`
b39f9d20 88 ;;
68807e5f 89 -*)
90 echo "Unrecognized option \`$option'" 1>&2
91 usage
92 exit 1
93 ;;
044e2e60 94 # Explicitly ignore non-option arguments, for compatibility.
68807e5f 95 esac
96done
97
48b391e9 98. "${datadir}/@PACKAGE@/grub-mkconfig_lib"
42c71976 99
991477f8 100if [ "x$EUID" = "x" ] ; then
101 EUID=`id -u`
ebd97f6e 102fi
103
991477f8 104if [ "$EUID" != 0 ] ; then
b609876d 105 root=f
106 case "`uname 2>/dev/null`" in
107 CYGWIN*)
108 # Cygwin: Assume root if member of admin group
109 for g in `id -G 2>/dev/null` ; do
110 case $g in
111 0|544) root=t ;;
112 esac
113 done ;;
114 esac
115 if [ $root != t ] ; then
0ea7c4f9 116 echo "$self: You must run this as root" >&2
b609876d 117 exit 1
118 fi
ebd97f6e 119fi
120
2e610d62 121set $grub_probe dummy
122if test -f "$1"; then
123 :
124else
125 echo "$1: Not found." 1>&2
126 exit 1
ebd97f6e 127fi
128
601c84fd 129mkdir -p ${GRUB_PREFIX}
2e610d62 130
cc85c3c3 131# Device containing our userland. Typically used for root= parameter.
69ba137e 132GRUB_DEVICE="`${grub_probe} --target=device /`"
133GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
62191274 134
135# Device containing our /boot partition. Usually the same as GRUB_DEVICE.
69ba137e 136GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
137GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
57f96397 138
139# Filesystem for the device containing our userland. Used for stuff like
140# choosing Hurd filesystem module.
99fd620d 141GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
57f96397 142
ebd97f6e 143if test -f ${sysconfdir}/default/grub ; then
144 . ${sysconfdir}/default/grub
145fi
146
651c29b7 147# XXX: should this be deprecated at some point?
148if [ "x${GRUB_TERMINAL}" != "x" ] ; then
149 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
150 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
151fi
152
d5631db0 153termoutdefault=0
38e55e90 154if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
d5631db0
VS
155 GRUB_TERMINAL_OUTPUT=gfxterm;
156 termoutdefault=1;
157fi
158
159for x in ${GRUB_TERMINAL_OUTPUT}; do
38e55e90 160 if [ "x${x}" = "xgfxterm" ]; then
d5631db0
VS
161 if [ -n "$GRUB_FONT" ] ; then
162 if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
163 GRUB_FONT_PATH=${GRUB_FONT}
164 else
165 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
166 exit 1
167 fi
b969c52f 168 else
b9fe6ea2 169 for dir in ${pkgdatadir} ${GRUB_PREFIX} /usr/share/grub ; do
d5631db0
VS
170 for basename in unicode unifont ascii; do
171 path="${dir}/${basename}.pf2"
172 if is_path_readable_by_grub ${path} > /dev/null ; then
173 GRUB_FONT_PATH=${path}
174 else
175 continue
176 fi
177 if [ "${basename}" = "ascii" ] ; then
178 # make sure all our children behave in conformance with ascii..
179 export LANG=C
180 fi
181 break 2
182 done
183 done
184 fi
185 if [ -z "${GRUB_FONT_PATH}" ] ; then
186 if [ "x$termoutdefault" != "x1" ]; then
187 echo "No font for gfxterm found." >&2 ; exit 1
188 fi
189 GRUB_TERMINAL_OUTPUT=
b969c52f 190 fi
29a6b9e8 191 fi
d5631db0
VS
192done
193
194for x in ${GRUB_TERMINAL_OUTPUT}; do
195 case "x${x}" in
196 xgfxterm) ;;
197 xconsole | xserial | xofconsole)
198 # make sure all our children behave in conformance with ascii..
199 export LANG=C;;
200 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
201 esac
202done
6193defe 203
57f96397 204# These are defined in this script, export them here so that user can
205# override them.
9a10df16 206export GRUB_DEVICE \
207 GRUB_DEVICE_UUID \
208 GRUB_DEVICE_BOOT \
209 GRUB_DEVICE_BOOT_UUID \
210 GRUB_FS \
211 GRUB_FONT_PATH \
3b76e68b 212 GRUB_PRELOAD_MODULES \
601c84fd 213 GRUB_PREFIX
57f96397 214
215# These are optional, user-defined variables.
9a10df16 216export GRUB_DEFAULT \
217 GRUB_HIDDEN_TIMEOUT \
218 GRUB_HIDDEN_TIMEOUT_QUIET \
219 GRUB_TIMEOUT \
76e6d0d7
VS
220 GRUB_DEFAULT_BUTTON \
221 GRUB_HIDDEN_TIMEOUT_BUTTON \
222 GRUB_TIMEOUT_BUTTON \
223 GRUB_BUTTON_CMOS_ADDRESS \
eaf41b25 224 GRUB_BUTTON_CMOS_CLEAN \
9a10df16 225 GRUB_DISTRIBUTOR \
226 GRUB_CMDLINE_LINUX \
227 GRUB_CMDLINE_LINUX_DEFAULT \
19dcc490
VS
228 GRUB_CMDLINE_XEN \
229 GRUB_CMDLINE_XEN_DEFAULT \
f35fa3a6
CW
230 GRUB_CMDLINE_LINUX_XEN_REPLACE \
231 GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT \
0d2c20c6
GS
232 GRUB_CMDLINE_NETBSD \
233 GRUB_CMDLINE_NETBSD_DEFAULT \
9a10df16 234 GRUB_TERMINAL_INPUT \
235 GRUB_TERMINAL_OUTPUT \
236 GRUB_SERIAL_COMMAND \
237 GRUB_DISABLE_LINUX_UUID \
219b3564 238 GRUB_DISABLE_RECOVERY \
e4311a9f 239 GRUB_VIDEO_BACKEND \
5174302b 240 GRUB_GFXMODE \
d7c43ba1 241 GRUB_BACKGROUND \
eb1c9594 242 GRUB_THEME \
65a533e7 243 GRUB_GFXPAYLOAD_LINUX \
0a39de87 244 GRUB_DISABLE_OS_PROBER \
a0ca21c2 245 GRUB_INIT_TUNE \
d500ed12 246 GRUB_SAVEDEFAULT \
20a40940 247 GRUB_ENABLE_CRYPTODISK \
d500ed12 248 GRUB_BADRAM
57f96397 249
18ade780 250if test "x${grub_cfg}" != "x"; then
251 rm -f ${grub_cfg}.new
252 exec > ${grub_cfg}.new
57f96397 253
18ade780 254 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
255 # firmware implementations (e.g. OFW or EFI).
8aa1541a 256 chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
257 This means that if the generated config contains a password it is readable by everyone"
18ade780 258fi
259echo "Generating grub.cfg ..." >&2
ebd97f6e 260
261cat << EOF
262#
263# DO NOT EDIT THIS FILE
264#
0ea7c4f9 265# It is automatically generated by $self using templates
f6fd460a 266# from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
ebd97f6e 267#
268EOF
269
18ade780 270for i in ${grub_mkconfig_dir}/* ; do
53b052de 271 case "$i" in
ebd97f6e 272 # emacsen backup files. FIXME: support other editors
273 *~) ;;
6d1fa41f
MZ
274 # emacsen autosave files. FIXME: support other editors
275 \#*\#) ;;
ebd97f6e 276 *)
2f80039d 277 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
ebd97f6e 278 echo
279 echo "### BEGIN $i ###"
53b052de 280 "$i"
ebd97f6e 281 echo "### END $i ###"
282 fi
283 ;;
284 esac
285done
286
18ade780 287if test "x${grub_cfg}" != "x" ; then
4417aae6 288 if ! ${grub_script_check} ${grub_cfg}.new; then
75d8c629 289 echo "Syntax errors are detected in generated GRUB config file." >&2
290 echo "Ensure that there are no errors in /etc/default/grub" >&2
291 echo "and /etc/grub.d/* files or please file a bug report with" >&2
292 echo "${grub_cfg}.new file attached." >&2
293 else
294 # none of the children aborted with error, install the new grub.cfg
295 mv -f ${grub_cfg}.new ${grub_cfg}
296 fi
18ade780 297fi
ebd97f6e 298
299echo "done" >&2