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