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