]> git.proxmox.com Git - grub2.git/blob - util/grub.d/10_linux.in
37b5180f32e0af7c33bc82b60ffeb0090d6eb380
[grub2.git] / util / grub.d / 10_linux.in
1 #! /bin/sh
2 set -e
3
4 # grub-mkconfig helper script.
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 ubuntu_recovery="@UBUNTU_RECOVERY@"
24
25 . "$pkgdatadir/grub-mkconfig_lib"
26
27 export TEXTDOMAIN=@PACKAGE@
28 export TEXTDOMAINDIR="@localedir@"
29
30 CLASS="--class gnu-linux --class gnu --class os"
31
32 if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
33 OS=GNU/Linux
34 else
35 OS="${GRUB_DISTRIBUTOR} GNU/Linux"
36 CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
37 fi
38
39 # loop-AES arranges things so that /dev/loop/X can be our root device, but
40 # the initrds that Linux uses don't like that.
41 case ${GRUB_DEVICE} in
42 /dev/loop/*|/dev/loop[0-9])
43 GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
44 # We can't cope with devices loop-mounted from files here.
45 case ${GRUB_DEVICE} in
46 /dev/*) ;;
47 *) exit 0 ;;
48 esac
49 ;;
50 esac
51
52 # btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
53 # and mounting btrfs requires user space scanning, so force UUID in this case.
54 if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
55 || ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
56 || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
57 LINUX_ROOT_DEVICE=${GRUB_DEVICE}
58 else
59 LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
60 fi
61
62 case x"$GRUB_FS" in
63 xbtrfs)
64 rootsubvol="`make_system_path_relative_to_its_root /`"
65 rootsubvol="${rootsubvol#/}"
66 if [ "x${rootsubvol}" != x ]; then
67 GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
68 fi;;
69 xzfs)
70 rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
71 bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
72 LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs}"
73 ;;
74 esac
75
76 title_correction_code=
77
78 if [ -x /lib/recovery-mode/recovery-menu ]; then
79 GRUB_CMDLINE_LINUX_RECOVERY=recovery
80 else
81 GRUB_CMDLINE_LINUX_RECOVERY=single
82 fi
83 if [ "$ubuntu_recovery" = 1 ]; then
84 GRUB_CMDLINE_LINUX_RECOVERY="$GRUB_CMDLINE_LINUX_RECOVERY nomodeset"
85 fi
86
87 linux_entry ()
88 {
89 os="$1"
90 version="$2"
91 type="$3"
92 args="$4"
93
94 if [ -z "$boot_device_id" ]; then
95 boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
96 fi
97 if [ x$type != xsimple ] ; then
98 case $type in
99 recovery)
100 title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
101 *)
102 title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
103 esac
104 if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
105 replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
106 quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
107 title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
108 grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
109 fi
110 echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
111 else
112 echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
113 fi
114 if [ x$type != xrecovery ] ; then
115 save_default_entry | grub_add_tab
116 fi
117
118 # Use ELILO's generic "efifb" when it's known to be available.
119 # FIXME: We need an interface to select vesafb in case efifb can't be used.
120 if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
121 echo " load_video" | sed "s/^/$submenu_indentation/"
122 else
123 if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
124 echo " load_video" | sed "s/^/$submenu_indentation/"
125 fi
126 if [ "$ubuntu_recovery" = 0 ] || [ x$type != xrecovery ]; then
127 echo " set gfxpayload=$GRUB_GFXPAYLOAD_LINUX" | sed "s/^/$submenu_indentation/"
128 fi
129 fi
130
131 echo " insmod gzio" | sed "s/^/$submenu_indentation/"
132
133 if [ x$dirname = x/ ]; then
134 if [ -z "${prepare_root_cache}" ]; then
135 prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
136 fi
137 printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
138 else
139 if [ -z "${prepare_boot_cache}" ]; then
140 prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
141 fi
142 printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
143 fi
144 message="$(gettext_printf "Loading Linux %s ..." ${version})"
145 sed "s/^/$submenu_indentation/" << EOF
146 echo '$(echo "$message" | grub_quote)'
147 linux ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
148 EOF
149 if test -n "${initrd}" ; then
150 # TRANSLATORS: ramdisk isn't identifier. Should be translated.
151 message="$(gettext_printf "Loading initial ramdisk ...")"
152 sed "s/^/$submenu_indentation/" << EOF
153 echo '$(echo "$message" | grub_quote)'
154 initrd ${rel_dirname}/${initrd}
155 EOF
156 fi
157 sed "s/^/$submenu_indentation/" << EOF
158 }
159 EOF
160 }
161
162 machine=`uname -m`
163 case "x$machine" in
164 xi?86 | xx86_64)
165 list=
166 for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-* ; do
167 if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
168 done ;;
169 *)
170 list=
171 for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* ; do
172 if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
173 done ;;
174 esac
175
176 case "$machine" in
177 i?86) GENKERNEL_ARCH="x86" ;;
178 mips|mips64) GENKERNEL_ARCH="mips" ;;
179 mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
180 arm*) GENKERNEL_ARCH="arm" ;;
181 *) GENKERNEL_ARCH="$machine" ;;
182 esac
183
184 prepare_boot_cache=
185 prepare_root_cache=
186 boot_device_id=
187 title_correction_code=
188
189 # Extra indentation to add to menu entries in a submenu. We're not in a submenu
190 # yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
191 submenu_indentation=""
192
193 is_top_level=true
194 while [ "x$list" != "x" ] ; do
195 linux=`version_find_latest $list`
196 gettext_printf "Found linux image: %s\n" "$linux" >&2
197 basename=`basename $linux`
198 dirname=`dirname $linux`
199 rel_dirname=`make_system_path_relative_to_its_root $dirname`
200 version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
201 alt_version=`echo $version | sed -e "s,\.old$,,g"`
202 linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
203
204 initrd=
205 for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
206 "initrd-${version}" "initramfs-${version}.img" \
207 "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
208 "initrd-${alt_version}" "initramfs-${alt_version}.img" \
209 "initramfs-genkernel-${version}" \
210 "initramfs-genkernel-${alt_version}" \
211 "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
212 "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
213 if test -e "${dirname}/${i}" ; then
214 initrd="$i"
215 break
216 fi
217 done
218
219 config=
220 for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
221 if test -e "${i}" ; then
222 config="${i}"
223 break
224 fi
225 done
226
227 initramfs=
228 if test -n "${config}" ; then
229 initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
230 fi
231
232 if test -n "${initrd}" ; then
233 gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
234 elif test -z "${initramfs}" ; then
235 # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs. Since there's
236 # no initrd or builtin initramfs, it can't work here.
237 linux_root_device_thisversion=${GRUB_DEVICE}
238 fi
239
240 if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
241 linux_entry "${OS}" "${version}" simple \
242 "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
243
244 submenu_indentation="$grub_tab"
245
246 if [ -z "$boot_device_id" ]; then
247 boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
248 fi
249 # TRANSLATORS: %s is replaced with an OS name
250 echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
251 is_top_level=false
252 fi
253
254 linux_entry "${OS}" "${version}" advanced \
255 "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
256 if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
257 linux_entry "${OS}" "${version}" recovery \
258 "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
259 fi
260
261 list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
262 done
263
264 # If at least one kernel was found, then we need to
265 # add a closing '}' for the submenu command.
266 if [ x"$is_top_level" != xtrue ]; then
267 echo '}'
268 fi
269
270 echo "$title_correction_code"