]> git.proxmox.com Git - grub2.git/blob - util/grub-mkconfig.in
94cd2e199ffe866d17857e3bde014a00fa479477
[grub2.git] / util / grub-mkconfig.in
1 #! /bin/sh -e
2
3 # Generate grub.cfg by inspecting /boot contents.
4 # Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
5 #
6 # GRUB is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # GRUB is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18
19 transform="@program_transform_name@"
20
21 prefix=@prefix@
22 exec_prefix=@exec_prefix@
23 sbindir=@sbindir@
24 libdir=@libdir@
25 sysconfdir=@sysconfdir@
26 package_version=@PACKAGE_VERSION@
27 datarootdir=@datarootdir@
28 datadir=@datadir@
29 pkgdatadir=${datadir}/`echo @PACKAGE_TARNAME@ | sed "${transform}"`
30 grub_prefix=`echo /boot/grub | sed ${transform}`
31 grub_cfg=""
32 grub_mkconfig_dir=${sysconfdir}/grub.d
33
34 grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
35 grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
36
37 # Usage: usage
38 # Print the usage.
39 usage () {
40 cat <<EOF
41 Usage: $0 [OPTION]
42 Generate a grub config file
43
44 -o, --output=FILE output generated config to FILE [default=stdout]
45 -h, --help print this message and exit
46 -v, --version print the version information and exit
47
48 Report bugs to <bug-grub@gnu.org>.
49 EOF
50 }
51
52 # Check the arguments.
53 for option in "$@"; do
54 case "$option" in
55 -h | --help)
56 usage
57 exit 0 ;;
58 -v | --version)
59 echo "$0 (GNU GRUB ${package_version})"
60 exit 0 ;;
61 -o)
62 shift
63 grub_cfg=$1
64 ;;
65 --output=*)
66 grub_cfg=`echo "$option" | sed 's/--output=//'`
67 ;;
68 -*)
69 echo "Unrecognized option \`$option'" 1>&2
70 usage
71 exit 1
72 ;;
73 esac
74 done
75
76 . ${libdir}/grub/grub-mkconfig_lib
77
78 if [ "x$EUID" = "x" ] ; then
79 EUID=`id -u`
80 fi
81
82 if [ "$EUID" != 0 ] ; then
83 root=f
84 case "`uname 2>/dev/null`" in
85 CYGWIN*)
86 # Cygwin: Assume root if member of admin group
87 for g in `id -G 2>/dev/null` ; do
88 case $g in
89 0|544) root=t ;;
90 esac
91 done ;;
92 esac
93 if [ $root != t ] ; then
94 echo "$0: You must run this as root" >&2
95 exit 1
96 fi
97 fi
98
99 set $grub_mkdevicemap dummy
100 if test -f "$1"; then
101 :
102 else
103 echo "$1: Not found." 1>&2
104 exit 1
105 fi
106
107 set $grub_probe dummy
108 if test -f "$1"; then
109 :
110 else
111 echo "$1: Not found." 1>&2
112 exit 1
113 fi
114
115 mkdir -p ${grub_prefix}
116
117 if test -e ${grub_prefix}/device.map ; then : ; else
118 ${grub_mkdevicemap}
119 fi
120
121 # Device containing our userland. Typically used for root= parameter.
122 GRUB_DEVICE="`${grub_probe} --target=device /`"
123 GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
124
125 # Device containing our /boot partition. Usually the same as GRUB_DEVICE.
126 GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
127 GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
128
129 # Filesystem for the device containing our userland. Used for stuff like
130 # choosing Hurd filesystem module.
131 GRUB_FS="`${grub_probe} --target=fs / 2> /dev/null || echo unknown`"
132
133 if test -f ${sysconfdir}/default/grub ; then
134 . ${sysconfdir}/default/grub
135 fi
136
137 # XXX: should this be deprecated at some point?
138 if [ "x${GRUB_TERMINAL}" != "x" ] ; then
139 GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
140 GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
141 fi
142
143 termoutdefault=0
144 if [ "x${GRUB_TERMINAL_OUTPUT}" == x ]; then
145 GRUB_TERMINAL_OUTPUT=gfxterm;
146 termoutdefault=1;
147 fi
148
149 for x in ${GRUB_TERMINAL_OUTPUT}; do
150 if [ x${x} == xgfxterm ]; then
151 # If this platform supports gfxterm, try to use it.
152 if ! test -e ${grub_prefix}/gfxterm.mod ; then
153 if [ "x$termoutdefault" != "x1" ]; then
154 echo "gfxterm isn't available on your platform" >&2 ; exit 1
155 fi
156 GRUB_TERMINAL_OUTPUT=
157 break;
158 fi
159 # FIXME: this should do something smarter than just loading first
160 # video backend.
161 GRUB_VIDEO_BACKEND=$(head -n 1 ${grub_prefix}/video.lst || true)
162 if [ -z "${GRUB_VIDEO_BACKEND}" ] ; then
163 if [ "x$termoutdefault" != "x1" ]; then
164 echo "No suitable backend could be found for gfxterm." >&2 ; exit 1
165 fi
166 GRUB_TERMINAL_OUTPUT=
167 fi
168 if [ -n "$GRUB_FONT" ] ; then
169 if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
170 GRUB_FONT_PATH=${GRUB_FONT}
171 else
172 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
173 exit 1
174 fi
175 else
176 for dir in ${pkgdatadir} /boot/grub /usr/share/grub ; do
177 for basename in unicode unifont ascii; do
178 path="${dir}/${basename}.pf2"
179 if is_path_readable_by_grub ${path} > /dev/null ; then
180 GRUB_FONT_PATH=${path}
181 else
182 continue
183 fi
184 if [ "${basename}" = "ascii" ] ; then
185 # make sure all our children behave in conformance with ascii..
186 export LANG=C
187 fi
188 break 2
189 done
190 done
191 fi
192 if [ -z "${GRUB_FONT_PATH}" ] ; then
193 if [ "x$termoutdefault" != "x1" ]; then
194 echo "No font for gfxterm found." >&2 ; exit 1
195 fi
196 GRUB_TERMINAL_OUTPUT=
197 fi
198 fi
199 done
200
201 for x in ${GRUB_TERMINAL_OUTPUT}; do
202 case "x${x}" in
203 xgfxterm) ;;
204 xconsole | xserial | xofconsole)
205 # make sure all our children behave in conformance with ascii..
206 export LANG=C;;
207 *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
208 esac
209 done
210
211 # These are defined in this script, export them here so that user can
212 # override them.
213 export GRUB_DEVICE \
214 GRUB_DEVICE_UUID \
215 GRUB_DEVICE_BOOT \
216 GRUB_DEVICE_BOOT_UUID \
217 GRUB_FS \
218 GRUB_FONT_PATH \
219 GRUB_PRELOAD_MODULES \
220 GRUB_VIDEO_BACKEND
221
222 # These are optional, user-defined variables.
223 export GRUB_DEFAULT \
224 GRUB_HIDDEN_TIMEOUT \
225 GRUB_HIDDEN_TIMEOUT_QUIET \
226 GRUB_TIMEOUT \
227 GRUB_DISTRIBUTOR \
228 GRUB_CMDLINE_LINUX \
229 GRUB_CMDLINE_LINUX_DEFAULT \
230 GRUB_TERMINAL_INPUT \
231 GRUB_TERMINAL_OUTPUT \
232 GRUB_SERIAL_COMMAND \
233 GRUB_DISABLE_LINUX_UUID \
234 GRUB_DISABLE_LINUX_RECOVERY \
235 GRUB_GFXMODE \
236 GRUB_THEME \
237 GRUB_GFXPAYLOAD_LINUX \
238 GRUB_DISABLE_OS_PROBER \
239 GRUB_INIT_TUNE \
240 GRUB_SAVEDEFAULT
241
242 if test "x${grub_cfg}" != "x"; then
243 rm -f ${grub_cfg}.new
244 exec > ${grub_cfg}.new
245
246 # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
247 # firmware implementations (e.g. OFW or EFI).
248 chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
249 This means that if the generated config contains a password it is readable by everyone"
250 fi
251 echo "Generating grub.cfg ..." >&2
252
253 cat << EOF
254 #
255 # DO NOT EDIT THIS FILE
256 #
257 # It is automatically generated by $0 using templates
258 # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
259 #
260 EOF
261
262 for i in ${grub_mkconfig_dir}/* ; do
263 case "$i" in
264 # emacsen backup files. FIXME: support other editors
265 *~) ;;
266 *)
267 if grub_file_is_not_garbage "$i" && test -x "$i" ; then
268 echo
269 echo "### BEGIN $i ###"
270 "$i"
271 echo "### END $i ###"
272 fi
273 ;;
274 esac
275 done
276
277 if test "x${grub_cfg}" != "x" ; then
278 # none of the children aborted with error, install the new grub.cfg
279 mv -f ${grub_cfg}.new ${grub_cfg}
280 fi
281
282 echo "done" >&2