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