2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / util / grub-mkconfig.in
bloba8c2675181b74cd2b1e8776c7a66720fa7955e0f
1 #! /bin/sh -e
3 # Generate grub.cfg by inspecting /boot contents.
4 # Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
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
9 # (at your option) any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 transform="@program_transform_name@"
21 prefix=@prefix@
22 exec_prefix=@exec_prefix@
23 sbindir=@sbindir@
24 libdir=@libdir@
25 sysconfdir=@sysconfdir@
26 package_version=@PACKAGE_VERSION@
27 datarootdir=@datarootdir@
28 datadir=@datadir@
29 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
30 grub_prefix=`echo /boot/grub | sed ${transform}`
31 grub_cfg=""
32 grub_mkconfig_dir=${sysconfdir}/grub.d
34 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
35 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
37 # Usage: usage
38 # Print the usage.
39 usage () {
40 cat <<EOF
41 Usage: $0 [OPTION]
42 Generate a grub config file
44 -o, --output=FILE output generated config to FILE [default=stdout]
45 -h, --help print this message and exit
46 -v, --version print the version information and exit
48 Report bugs to <bug-grub@gnu.org>.
49 EOF
52 # Check the arguments.
53 for option in "$@"; do
54 case "$option" in
55 -h | --help)
56 usage
57 exit 0 ;;
58 -v | --version)
59 echo "$0 (GNU GRUB ${package_version})"
60 exit 0 ;;
61 -o)
62 shift
63 grub_cfg=$1
65 --output=*)
66 grub_cfg=`echo "$option" | sed 's/--output=//'`
68 -*)
69 echo "Unrecognized option \`$option'" 1>&2
70 usage
71 exit 1
73 esac
74 done
76 . ${libdir}/grub/grub-mkconfig_lib
78 if [ "x$EUID" = "x" ] ; then
79 EUID=`id -u`
82 if [ "$EUID" != 0 ] ; then
83 root=f
84 case "`uname 2>/dev/null`" in
85 CYGWIN*)
86 # Cygwin: Assume root if member of admin group
87 for g in `id -G 2>/dev/null` ; do
88 case $g in
89 0|544) root=t ;;
90 esac
91 done ;;
92 esac
93 if [ $root != t ] ; then
94 echo "$0: You must run this as root" >&2
95 exit 1
99 set $grub_mkdevicemap dummy
100 if test -f "$1"; then
102 else
103 echo "$1: Not found." 1>&2
104 exit 1
107 set $grub_probe dummy
108 if test -f "$1"; then
110 else
111 echo "$1: Not found." 1>&2
112 exit 1
115 mkdir -p ${grub_prefix}
117 if test -e ${grub_prefix}/device.map ; then : ; else
118 ${grub_mkdevicemap}
121 # Device containing our userland. Typically used for root= parameter.
122 GRUB_DEVICE="`${grub_probe} --target=device /`"
123 GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
125 # Device containing our /boot partition. Usually the same as GRUB_DEVICE.
126 GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
127 GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
129 # Filesystem for the device containing our userland. Used for stuff like
130 # choosing Hurd filesystem module.
131 GRUB_FS="`${grub_probe} --target=fs / 2> /dev/null || echo unknown`"
133 if test -f ${sysconfdir}/default/grub ; then
134 . ${sysconfdir}/default/grub
137 # XXX: should this be deprecated at some point?
138 if [ "x${GRUB_TERMINAL}" != "x" ] ; then
139 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
140 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
143 case x${GRUB_TERMINAL_OUTPUT} in
144 x | xgfxterm)
145 # If this platform supports gfxterm, try to use it.
146 if test -e ${grub_prefix}/gfxterm.mod ; then
147 GRUB_VIDEO_BACKEND=
148 for i in vbe ; do
149 if test -e ${grub_prefix}/$i.mod ; then
150 GRUB_VIDEO_BACKEND=$i
151 break
153 done
154 if [ -n "${GRUB_VIDEO_BACKEND}" ] ; then
155 GRUB_TERMINAL_OUTPUT=gfxterm
156 elif [ "${GRUB_TERMINAL_OUTPUT}" = "gfxterm" ] ; then
157 echo "No suitable backend could be found for gfxterm." >&2 ; exit 1
161 xconsole | xserial | xofconsole) ;;
162 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
163 esac
165 # check for terminals that require fonts
166 case ${GRUB_TERMINAL_OUTPUT} in
167 gfxterm)
168 if [ -n "$GRUB_FONT" ] ; then
169 if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
170 GRUB_FONT_PATH=${GRUB_FONT}
171 else
172 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
173 exit 1
175 else
176 for dir in ${pkgdatadir} /boot/grub /usr/share/grub ; do
177 for basename in unicode unifont ascii; do
178 path="${dir}/${basename}.pf2"
179 if is_path_readable_by_grub ${path} > /dev/null ; then
180 GRUB_FONT_PATH=${path}
181 else
182 continue
184 if [ "${basename}" = "ascii" ] ; then
185 # make sure all our children behave in conformance with ascii..
186 export LANG=C
188 break 2
189 done
190 done
192 if [ -z "${GRUB_FONT_PATH}" ] ; then
193 # fallback to the native terminal for this platform
194 unset GRUB_TERMINAL_OUTPUT
198 # make sure all our children behave in conformance with ascii..
199 export LANG=C
200 esac
202 # These are defined in this script, export them here so that user can
203 # override them.
204 export GRUB_DEVICE \
205 GRUB_DEVICE_UUID \
206 GRUB_DEVICE_BOOT \
207 GRUB_DEVICE_BOOT_UUID \
208 GRUB_FS \
209 GRUB_FONT_PATH \
210 GRUB_PRELOAD_MODULES \
211 GRUB_VIDEO_BACKEND
213 # These are optional, user-defined variables.
214 export GRUB_DEFAULT \
215 GRUB_HIDDEN_TIMEOUT \
216 GRUB_HIDDEN_TIMEOUT_QUIET \
217 GRUB_TIMEOUT \
218 GRUB_DISTRIBUTOR \
219 GRUB_CMDLINE_LINUX \
220 GRUB_CMDLINE_LINUX_DEFAULT \
221 GRUB_TERMINAL_INPUT \
222 GRUB_TERMINAL_OUTPUT \
223 GRUB_SERIAL_COMMAND \
224 GRUB_DISABLE_LINUX_UUID \
225 GRUB_DISABLE_LINUX_RECOVERY \
226 GRUB_GFXMODE \
227 GRUB_DISABLE_OS_PROBER
229 if test "x${grub_cfg}" != "x"; then
230 rm -f ${grub_cfg}.new
231 exec > ${grub_cfg}.new
233 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
234 # firmware implementations (e.g. OFW or EFI).
235 chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
236 This means that if the generated config contains a password it is readable by everyone"
238 echo "Generating grub.cfg ..." >&2
240 cat << EOF
242 # DO NOT EDIT THIS FILE
244 # It is automatically generated by $0 using templates
245 # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
249 for i in ${grub_mkconfig_dir}/* ; do
250 case "$i" in
251 # emacsen backup files. FIXME: support other editors
252 *~) ;;
254 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
255 echo
256 echo "### BEGIN $i ###"
257 "$i"
258 echo "### END $i ###"
261 esac
262 done
264 if test "x${grub_cfg}" != "x" ; then
265 # none of the children aborted with error, install the new grub.cfg
266 mv -f ${grub_cfg}.new ${grub_cfg}
269 echo "done" >&2