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