]> git.proxmox.com Git - grub2.git/blame - util/grub-mkconfig.in
2010-02-10 Vladimir Serbinenko <phcoder@gmail.com>
[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@
29a6b9e8 27datarootdir=@datarootdir@
b969c52f 28datadir=@datadir@
29pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
ebd97f6e 30grub_prefix=`echo /boot/grub | 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.
53for option in "$@"; do
54 case "$option" in
55 -h | --help)
56 usage
57 exit 0 ;;
58 -v | --version)
880e0a0c 59 echo "$0 (GNU GRUB ${package_version})"
68807e5f 60 exit 0 ;;
18ade780 61 -o)
62 shift
63 grub_cfg=$1
64 ;;
880fc3c4 65 --output=*)
18ade780 66 grub_cfg=`echo "$option" | sed 's/--output=//'`
b39f9d20 67 ;;
68807e5f 68 -*)
69 echo "Unrecognized option \`$option'" 1>&2
70 usage
71 exit 1
72 ;;
73 esac
74done
75
18ade780 76. ${libdir}/grub/grub-mkconfig_lib
42c71976 77
991477f8 78if [ "x$EUID" = "x" ] ; then
79 EUID=`id -u`
ebd97f6e 80fi
81
991477f8 82if [ "$EUID" != 0 ] ; then
b609876d 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
96 fi
ebd97f6e 97fi
98
2e610d62 99set $grub_mkdevicemap dummy
100if test -f "$1"; then
101 :
102else
103 echo "$1: Not found." 1>&2
104 exit 1
ebd97f6e 105fi
106
2e610d62 107set $grub_probe dummy
108if test -f "$1"; then
109 :
110else
111 echo "$1: Not found." 1>&2
112 exit 1
ebd97f6e 113fi
114
2e610d62 115mkdir -p ${grub_prefix}
116
117if test -e ${grub_prefix}/device.map ; then : ; else
74b21bee 118 ${grub_mkdevicemap}
ebd97f6e 119fi
120
cc85c3c3 121# Device containing our userland. Typically used for root= parameter.
69ba137e 122GRUB_DEVICE="`${grub_probe} --target=device /`"
123GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
62191274 124
125# Device containing our /boot partition. Usually the same as GRUB_DEVICE.
69ba137e 126GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
127GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
57f96397 128
129# Filesystem for the device containing our userland. Used for stuff like
130# choosing Hurd filesystem module.
69ba137e 131GRUB_FS="`${grub_probe} --target=fs / 2> /dev/null || echo unknown`"
57f96397 132
ebd97f6e 133if test -f ${sysconfdir}/default/grub ; then
134 . ${sysconfdir}/default/grub
135fi
136
651c29b7 137# XXX: should this be deprecated at some point?
138if [ "x${GRUB_TERMINAL}" != "x" ] ; then
139 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
140 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
141fi
142
143case x${GRUB_TERMINAL_OUTPUT} in
3b76e68b 144 x | xgfxterm)
145 # If this platform supports gfxterm, try to use it.
146 if test -e ${grub_prefix}/gfxterm.mod ; then
dc0c71d9
RM
147 # FIXME: this should do something smarter than just loading first
148 # video backend.
149 GRUB_VIDEO_BACKEND=$(head -n 1 ${grub_prefix}/video.lst || true)
3b76e68b 150 if [ -n "${GRUB_VIDEO_BACKEND}" ] ; then
151 GRUB_TERMINAL_OUTPUT=gfxterm
152 elif [ "${GRUB_TERMINAL_OUTPUT}" = "gfxterm" ] ; then
153 echo "No suitable backend could be found for gfxterm." >&2 ; exit 1
154 fi
155 fi
156 ;;
157 xconsole | xserial | xofconsole) ;;
651c29b7 158 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
6193defe 159esac
160
161# check for terminals that require fonts
651c29b7 162case ${GRUB_TERMINAL_OUTPUT} in
6193defe 163 gfxterm)
a78c8d24 164 if [ -n "$GRUB_FONT" ] ; then
165 if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
166 GRUB_FONT_PATH=${GRUB_FONT}
b969c52f 167 else
a78c8d24 168 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
169 exit 1
b969c52f 170 fi
a78c8d24 171 else
172 for dir in ${pkgdatadir} /boot/grub /usr/share/grub ; do
173 for basename in unicode unifont ascii; do
174 path="${dir}/${basename}.pf2"
175 if is_path_readable_by_grub ${path} > /dev/null ; then
176 GRUB_FONT_PATH=${path}
177 else
178 continue
179 fi
180 if [ "${basename}" = "ascii" ] ; then
181 # make sure all our children behave in conformance with ascii..
182 export LANG=C
183 fi
184 break 2
185 done
b969c52f 186 done
a78c8d24 187 fi
29a6b9e8 188 if [ -z "${GRUB_FONT_PATH}" ] ; then
189 # fallback to the native terminal for this platform
190 unset GRUB_TERMINAL_OUTPUT
191 fi
6193defe 192 ;;
52768e37 193 *)
6193defe 194 # make sure all our children behave in conformance with ascii..
195 export LANG=C
6193defe 196esac
197
57f96397 198# These are defined in this script, export them here so that user can
199# override them.
9a10df16 200export GRUB_DEVICE \
201 GRUB_DEVICE_UUID \
202 GRUB_DEVICE_BOOT \
203 GRUB_DEVICE_BOOT_UUID \
204 GRUB_FS \
205 GRUB_FONT_PATH \
3b76e68b 206 GRUB_PRELOAD_MODULES \
207 GRUB_VIDEO_BACKEND
57f96397 208
209# These are optional, user-defined variables.
9a10df16 210export GRUB_DEFAULT \
211 GRUB_HIDDEN_TIMEOUT \
212 GRUB_HIDDEN_TIMEOUT_QUIET \
213 GRUB_TIMEOUT \
214 GRUB_DISTRIBUTOR \
215 GRUB_CMDLINE_LINUX \
216 GRUB_CMDLINE_LINUX_DEFAULT \
217 GRUB_TERMINAL_INPUT \
218 GRUB_TERMINAL_OUTPUT \
219 GRUB_SERIAL_COMMAND \
220 GRUB_DISABLE_LINUX_UUID \
221 GRUB_DISABLE_LINUX_RECOVERY \
5174302b 222 GRUB_GFXMODE \
eb1c9594 223 GRUB_THEME \
5174302b 224 GRUB_DISABLE_OS_PROBER
57f96397 225
18ade780 226if test "x${grub_cfg}" != "x"; then
227 rm -f ${grub_cfg}.new
228 exec > ${grub_cfg}.new
57f96397 229
18ade780 230 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
231 # firmware implementations (e.g. OFW or EFI).
8aa1541a 232 chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
233 This means that if the generated config contains a password it is readable by everyone"
18ade780 234fi
235echo "Generating grub.cfg ..." >&2
ebd97f6e 236
237cat << EOF
238#
239# DO NOT EDIT THIS FILE
240#
cc85c3c3 241# It is automatically generated by $0 using templates
f6fd460a 242# from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
ebd97f6e 243#
244EOF
245
18ade780 246for i in ${grub_mkconfig_dir}/* ; do
53b052de 247 case "$i" in
ebd97f6e 248 # emacsen backup files. FIXME: support other editors
249 *~) ;;
250 *)
2f80039d 251 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
ebd97f6e 252 echo
253 echo "### BEGIN $i ###"
53b052de 254 "$i"
ebd97f6e 255 echo "### END $i ###"
256 fi
257 ;;
258 esac
259done
260
18ade780 261if test "x${grub_cfg}" != "x" ; then
262 # none of the children aborted with error, install the new grub.cfg
263 mv -f ${grub_cfg}.new ${grub_cfg}
264fi
ebd97f6e 265
266echo "done" >&2