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