]> git.proxmox.com Git - grub2.git/blob - util/grub-mkconfig.in
merge trunk
[grub2.git] / util / grub-mkconfig.in
1 #! /bin/sh
2 set -e
3
4 # Generate grub.cfg by inspecting /boot contents.
5 # Copyright (C) 2006,2007,2008,2009,2010 Free Software Foundation, Inc.
6 #
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
10 # (at your option) any later version.
11 #
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.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
20 transform="@program_transform_name@"
21
22 prefix=@prefix@
23 exec_prefix=@exec_prefix@
24 sbindir=@sbindir@
25 bindir=@bindir@
26 libdir=@libdir@
27 sysconfdir=@sysconfdir@
28 PACKAGE_NAME=@PACKAGE_NAME@
29 PACKAGE_VERSION=@PACKAGE_VERSION@
30 host_os=@host_os@
31 datarootdir=@datarootdir@
32 datadir=@datadir@
33 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
34 grub_cfg=""
35 grub_mkconfig_dir=${sysconfdir}/grub.d
36
37 self=`basename $0`
38
39 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed "${transform}"`
40 grub_probe=${sbindir}/`echo grub-probe | sed "${transform}"`
41 grub_script_check="${bindir}/`echo grub-script-check | sed "${transform}"`"
42
43 GRUB_PREFIX=`echo '/@bootdirname@/@grubdirname@' | sed "s,//*,/,g"`
44
45 # Usage: usage
46 # Print the usage.
47 usage () {
48 cat <<EOF
49 Usage: $self [OPTION]
50 Generate a grub config file
51
52 -o, --output=FILE output generated config to FILE [default=stdout]
53 -h, --help print this message and exit
54 -v, --version print the version information and exit
55
56 Report bugs to <bug-grub@gnu.org>.
57 EOF
58 }
59
60 argument () {
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
71 # Check the arguments.
72 while test $# -gt 0
73 do
74 option=$1
75 shift
76
77 case "$option" in
78 -h | --help)
79 usage
80 exit 0 ;;
81 -v | --version)
82 echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
83 exit 0 ;;
84 -o | --output)
85 grub_cfg=`argument $option "$@"`; shift;;
86 --output=*)
87 grub_cfg=`echo "$option" | sed 's/--output=//'`
88 ;;
89 -*)
90 echo "Unrecognized option \`$option'" 1>&2
91 usage
92 exit 1
93 ;;
94 # Explicitly ignore non-option arguments, for compatibility.
95 esac
96 done
97
98 . ${libdir}/grub/grub-mkconfig_lib
99
100 if [ "x$EUID" = "x" ] ; then
101 EUID=`id -u`
102 fi
103
104 if [ "$EUID" != 0 ] ; then
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 "$self: You must run this as root" >&2
117 exit 1
118 fi
119 fi
120
121 set $grub_mkdevicemap dummy
122 if test -f "$1"; then
123 :
124 else
125 echo "$1: Not found." 1>&2
126 exit 1
127 fi
128
129 set $grub_probe dummy
130 if test -f "$1"; then
131 :
132 else
133 echo "$1: Not found." 1>&2
134 exit 1
135 fi
136
137 mkdir -p ${GRUB_PREFIX}
138
139 if test -e ${GRUB_PREFIX}/device.map ; then : ; else
140 ${grub_mkdevicemap}
141 fi
142
143 # Device containing our userland. Typically used for root= parameter.
144 GRUB_DEVICE="`${grub_probe} --target=device /`"
145 GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
146
147 # Device containing our /boot partition. Usually the same as GRUB_DEVICE.
148 GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
149 GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
150
151 # Filesystem for the device containing our userland. Used for stuff like
152 # choosing Hurd filesystem module.
153 GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
154
155 if test -f ${sysconfdir}/default/grub ; then
156 . ${sysconfdir}/default/grub
157 fi
158
159 # XXX: should this be deprecated at some point?
160 if [ "x${GRUB_TERMINAL}" != "x" ] ; then
161 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
162 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
163 fi
164
165 termoutdefault=0
166 if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
167 GRUB_TERMINAL_OUTPUT=gfxterm;
168 termoutdefault=1;
169 fi
170
171 for x in ${GRUB_TERMINAL_OUTPUT}; do
172 if [ "x${x}" = "xgfxterm" ]; then
173 if [ -n "$GRUB_FONT" ] ; then
174 if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
175 GRUB_FONT_PATH=${GRUB_FONT}
176 else
177 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
178 exit 1
179 fi
180 else
181 for dir in ${pkgdatadir} ${GRUB_PREFIX} /usr/share/grub ; do
182 for basename in unicode unifont ascii; do
183 path="${dir}/${basename}.pf2"
184 if is_path_readable_by_grub ${path} > /dev/null ; then
185 GRUB_FONT_PATH=${path}
186 else
187 continue
188 fi
189 if [ "${basename}" = "ascii" ] ; then
190 # make sure all our children behave in conformance with ascii..
191 export LANG=C
192 fi
193 break 2
194 done
195 done
196 fi
197 if [ -z "${GRUB_FONT_PATH}" ] ; then
198 if [ "x$termoutdefault" != "x1" ]; then
199 echo "No font for gfxterm found." >&2 ; exit 1
200 fi
201 GRUB_TERMINAL_OUTPUT=
202 fi
203 fi
204 done
205
206 for x in ${GRUB_TERMINAL_OUTPUT}; do
207 case "x${x}" in
208 xgfxterm) ;;
209 xconsole | xserial | xofconsole)
210 # make sure all our children behave in conformance with ascii..
211 export LANG=C;;
212 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
213 esac
214 done
215
216 # These are defined in this script, export them here so that user can
217 # override them.
218 export GRUB_DEVICE \
219 GRUB_DEVICE_UUID \
220 GRUB_DEVICE_BOOT \
221 GRUB_DEVICE_BOOT_UUID \
222 GRUB_FS \
223 GRUB_FONT_PATH \
224 GRUB_PRELOAD_MODULES \
225 GRUB_PREFIX
226
227 # These are optional, user-defined variables.
228 export GRUB_DEFAULT \
229 GRUB_HIDDEN_TIMEOUT \
230 GRUB_HIDDEN_TIMEOUT_QUIET \
231 GRUB_TIMEOUT \
232 GRUB_DEFAULT_BUTTON \
233 GRUB_HIDDEN_TIMEOUT_BUTTON \
234 GRUB_TIMEOUT_BUTTON \
235 GRUB_BUTTON_CMOS_ADDRESS \
236 GRUB_BUTTON_CMOS_CLEAN \
237 GRUB_DISTRIBUTOR \
238 GRUB_CMDLINE_LINUX \
239 GRUB_CMDLINE_LINUX_DEFAULT \
240 GRUB_CMDLINE_XEN \
241 GRUB_CMDLINE_XEN_DEFAULT \
242 GRUB_CMDLINE_NETBSD \
243 GRUB_CMDLINE_NETBSD_DEFAULT \
244 GRUB_TERMINAL_INPUT \
245 GRUB_TERMINAL_OUTPUT \
246 GRUB_SERIAL_COMMAND \
247 GRUB_DISABLE_LINUX_UUID \
248 GRUB_DISABLE_RECOVERY \
249 GRUB_VIDEO_BACKEND \
250 GRUB_GFXMODE \
251 GRUB_BACKGROUND \
252 GRUB_THEME \
253 GRUB_GFXPAYLOAD_LINUX \
254 GRUB_DISABLE_OS_PROBER \
255 GRUB_INIT_TUNE \
256 GRUB_SAVEDEFAULT \
257 GRUB_BADRAM
258
259 if test "x${grub_cfg}" != "x"; then
260 rm -f ${grub_cfg}.new
261 exec > ${grub_cfg}.new
262
263 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
264 # firmware implementations (e.g. OFW or EFI).
265 chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
266 This means that if the generated config contains a password it is readable by everyone"
267 fi
268 echo "Generating grub.cfg ..." >&2
269
270 cat << EOF
271 #
272 # DO NOT EDIT THIS FILE
273 #
274 # It is automatically generated by $self using templates
275 # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
276 #
277 EOF
278
279 for i in ${grub_mkconfig_dir}/* ; do
280 case "$i" in
281 # emacsen backup files. FIXME: support other editors
282 *~) ;;
283 # emacsen autosave files. FIXME: support other editors
284 \#*\#) ;;
285 *)
286 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
287 echo
288 echo "### BEGIN $i ###"
289 "$i"
290 echo "### END $i ###"
291 fi
292 ;;
293 esac
294 done
295
296 if test "x${grub_cfg}" != "x" ; then
297 if ! ${grub_script_check} ${grub_cfg}.new; then
298 echo "Syntax errors are detected in generated GRUB config file." >&2
299 echo "Ensure that there are no errors in /etc/default/grub" >&2
300 echo "and /etc/grub.d/* files or please file a bug report with" >&2
301 echo "${grub_cfg}.new file attached." >&2
302 else
303 # none of the children aborted with error, install the new grub.cfg
304 mv -f ${grub_cfg}.new ${grub_cfg}
305 fi
306 fi
307
308 echo "done" >&2