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