20_linux_xen.in 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #! /bin/sh
  2. set -e
  3. # grub-mkconfig helper script.
  4. # Copyright (C) 2006,2007,2008,2009,2010 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. prefix="@prefix@"
  19. exec_prefix="@exec_prefix@"
  20. datarootdir="@datarootdir@"
  21. . "$pkgdatadir/grub-mkconfig_lib"
  22. export TEXTDOMAIN=@PACKAGE@
  23. export TEXTDOMAINDIR="@localedir@"
  24. CLASS="--class gnu-linux --class gnu --class os --class xen"
  25. if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  26. OS=GNU/Linux
  27. else
  28. OS="${GRUB_DISTRIBUTOR} GNU/Linux"
  29. CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
  30. fi
  31. # loop-AES arranges things so that /dev/loop/X can be our root device, but
  32. # the initrds that Linux uses don't like that.
  33. case ${GRUB_DEVICE} in
  34. /dev/loop/*|/dev/loop[0-9])
  35. GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  36. ;;
  37. esac
  38. # Default to disabling partition uuid support to maintian compatibility with
  39. # older kernels.
  40. GRUB_DISABLE_LINUX_PARTUUID=${GRUB_DISABLE_LINUX_PARTUUID-true}
  41. # btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
  42. # and mounting btrfs requires user space scanning, so force UUID in this case.
  43. if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
  44. || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
  45. && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
  46. || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
  47. && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
  48. || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  49. LINUX_ROOT_DEVICE=${GRUB_DEVICE}
  50. elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
  51. || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
  52. LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
  53. else
  54. LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
  55. fi
  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. case x"$GRUB_FS" in
  64. xbtrfs)
  65. rootsubvol="`make_system_path_relative_to_its_root /`"
  66. rootsubvol="${rootsubvol#/}"
  67. if [ "x${rootsubvol}" != x ]; then
  68. GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
  69. fi;;
  70. xzfs)
  71. rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
  72. bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
  73. LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
  74. ;;
  75. esac
  76. title_correction_code=
  77. linux_entry ()
  78. {
  79. linux_entry_xsm "$@" false
  80. linux_entry_xsm "$@" true
  81. }
  82. linux_entry_xsm ()
  83. {
  84. os="$1"
  85. version="$2"
  86. xen_version="$3"
  87. type="$4"
  88. args="$5"
  89. xen_args="$6"
  90. xsm="$7"
  91. # If user wants to enable XSM support, make sure there's
  92. # corresponding policy file.
  93. if ${xsm} ; then
  94. xenpolicy="xenpolicy-$xen_version"
  95. if test ! -e "${xen_dirname}/${xenpolicy}" ; then
  96. return
  97. fi
  98. xen_args="$xen_args flask=enforcing"
  99. xen_version="$(gettext_printf "%s (XSM enabled)" "$xen_version")"
  100. # xen_version is used for messages only; actual file is xen_basename
  101. fi
  102. if [ -z "$boot_device_id" ]; then
  103. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  104. fi
  105. if [ x$type != xsimple ] ; then
  106. if [ x$type = xrecovery ] ; then
  107. title="$(gettext_printf "%s, with Xen %s and Linux %s (recovery mode)" "${os}" "${xen_version}" "${version}")"
  108. else
  109. title="$(gettext_printf "%s, with Xen %s and Linux %s" "${os}" "${xen_version}" "${version}")"
  110. fi
  111. replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
  112. if [ x"Xen ${xen_version}>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
  113. quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
  114. title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
  115. 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")"
  116. fi
  117. echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'xen-gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  118. else
  119. title="$(gettext_printf "%s, with Xen hypervisor" "${os}")"
  120. echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'xen-gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  121. fi
  122. if [ x$type != xrecovery ] ; then
  123. save_default_entry | grub_add_tab | sed "s/^/$submenu_indentation/"
  124. fi
  125. if [ -z "${prepare_boot_cache}" ]; then
  126. prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
  127. fi
  128. printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  129. xmessage="$(gettext_printf "Loading Xen %s ..." ${xen_version})"
  130. lmessage="$(gettext_printf "Loading Linux %s ..." ${version})"
  131. sed "s/^/$submenu_indentation/" << EOF
  132. echo '$(echo "$xmessage" | grub_quote)'
  133. if [ "\$grub_platform" = "pc" -o "\$grub_platform" = "" ]; then
  134. xen_rm_opts=
  135. else
  136. xen_rm_opts="no-real-mode edd=off"
  137. fi
  138. ${xen_loader} ${rel_xen_dirname}/${xen_basename} placeholder ${xen_args} \${xen_rm_opts}
  139. echo '$(echo "$lmessage" | grub_quote)'
  140. ${module_loader} ${rel_dirname}/${basename} placeholder root=${linux_root_device_thisversion} ro ${args}
  141. EOF
  142. if test -n "${initrd}" ; then
  143. # TRANSLATORS: ramdisk isn't identifier. Should be translated.
  144. message="$(gettext_printf "Loading initial ramdisk ...")"
  145. initrd_path=
  146. for i in ${initrd}; do
  147. initrd_path="${initrd_path} ${rel_dirname}/${i}"
  148. done
  149. sed "s/^/$submenu_indentation/" << EOF
  150. echo '$(echo "$message" | grub_quote)'
  151. ${module_loader} --nounzip $(echo $initrd_path)
  152. EOF
  153. fi
  154. if test -n "${xenpolicy}" ; then
  155. message="$(gettext_printf "Loading XSM policy ...")"
  156. sed "s/^/$submenu_indentation/" << EOF
  157. echo '$(echo "$message" | grub_quote)'
  158. ${module_loader} ${rel_dirname}/${xenpolicy}
  159. EOF
  160. fi
  161. sed "s/^/$submenu_indentation/" << EOF
  162. }
  163. EOF
  164. }
  165. linux_list=
  166. for i in /boot/vmlinu[xz]-* /vmlinu[xz]-* /boot/kernel-*; do
  167. if grub_file_is_not_garbage "$i"; then
  168. basename=$(basename $i)
  169. version=$(echo $basename | sed -e "s,^[^0-9]*-,,g")
  170. dirname=$(dirname $i)
  171. config=
  172. for j in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
  173. if test -e "${j}" ; then
  174. config="${j}"
  175. break
  176. fi
  177. done
  178. 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
  179. fi
  180. done
  181. if [ "x${linux_list}" = "x" ] ; then
  182. exit 0
  183. fi
  184. file_is_not_xen_garbage () {
  185. case "$1" in
  186. */xen-syms-*)
  187. return 1;;
  188. */xenpolicy-*)
  189. return 1;;
  190. */*.config)
  191. return 1;;
  192. *)
  193. return 0;;
  194. esac
  195. }
  196. xen_list=
  197. for i in /boot/xen*; do
  198. if grub_file_is_not_garbage "$i" && file_is_not_xen_garbage "$i" ; then xen_list="$xen_list $i" ; fi
  199. done
  200. prepare_boot_cache=
  201. boot_device_id=
  202. title_correction_code=
  203. machine=`uname -m`
  204. case "$machine" in
  205. i?86) GENKERNEL_ARCH="x86" ;;
  206. mips|mips64) GENKERNEL_ARCH="mips" ;;
  207. mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
  208. arm*) GENKERNEL_ARCH="arm" ;;
  209. *) GENKERNEL_ARCH="$machine" ;;
  210. esac
  211. # Extra indentation to add to menu entries in a submenu. We're not in a submenu
  212. # yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
  213. submenu_indentation=""
  214. is_top_level=true
  215. while [ "x${xen_list}" != "x" ] ; do
  216. list="${linux_list}"
  217. current_xen=`version_find_latest $xen_list`
  218. xen_basename=`basename ${current_xen}`
  219. xen_dirname=`dirname ${current_xen}`
  220. rel_xen_dirname=`make_system_path_relative_to_its_root $xen_dirname`
  221. xen_version=`echo $xen_basename | sed -e "s,.gz$,,g;s,^xen-,,g"`
  222. if [ -z "$boot_device_id" ]; then
  223. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  224. fi
  225. if [ "x$is_top_level" != xtrue ]; then
  226. echo " submenu '$(gettext_printf "Xen hypervisor, version %s" "${xen_version}" | grub_quote)' \$menuentry_id_option 'xen-hypervisor-$xen_version-$boot_device_id' {"
  227. fi
  228. if ($grub_file --is-arm64-efi $current_xen); then
  229. xen_loader="xen_hypervisor"
  230. module_loader="xen_module"
  231. else
  232. if ($grub_file --is-x86-multiboot2 $current_xen); then
  233. xen_loader="multiboot2"
  234. module_loader="module2"
  235. else
  236. xen_loader="multiboot"
  237. module_loader="module"
  238. fi
  239. fi
  240. initrd_early=
  241. for i in ${GRUB_EARLY_INITRD_LINUX_STOCK} \
  242. ${GRUB_EARLY_INITRD_LINUX_CUSTOM}; do
  243. if test -e "${xen_dirname}/${i}" ; then
  244. initrd_early="${initrd_early} ${i}"
  245. fi
  246. done
  247. while [ "x$list" != "x" ] ; do
  248. linux=`version_find_latest $list`
  249. gettext_printf "Found linux image: %s\n" "$linux" >&2
  250. basename=`basename $linux`
  251. dirname=`dirname $linux`
  252. rel_dirname=`make_system_path_relative_to_its_root $dirname`
  253. version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
  254. alt_version=`echo $version | sed -e "s,\.old$,,g"`
  255. linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
  256. initrd_real=
  257. for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
  258. "initrd-${version}" "initramfs-${version}.img" \
  259. "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
  260. "initrd-${alt_version}" "initramfs-${alt_version}.img" \
  261. "initramfs-genkernel-${version}" \
  262. "initramfs-genkernel-${alt_version}" \
  263. "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
  264. "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}" ; do
  265. if test -e "${dirname}/${i}" ; then
  266. initrd_real="$i"
  267. break
  268. fi
  269. done
  270. initrd=
  271. if test -n "${initrd_early}" || test -n "${initrd_real}"; then
  272. initrd="${initrd_early} ${initrd_real}"
  273. initrd_display=
  274. for i in ${initrd}; do
  275. initrd_display="${initrd_display} ${dirname}/${i}"
  276. done
  277. gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
  278. fi
  279. if test -z "${initrd_real}"; then
  280. # "UUID=" magic is parsed by initrds. Since there's no initrd, it can't work here.
  281. if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] \
  282. || [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then
  283. linux_root_device_thisversion=${GRUB_DEVICE}
  284. else
  285. linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
  286. fi
  287. fi
  288. # The GRUB_DISABLE_SUBMENU option used to be different than others since it was
  289. # mentioned in the documentation that has to be set to 'y' instead of 'true' to
  290. # enable it. This caused a lot of confusion to users that set the option to 'y',
  291. # 'yes' or 'true'. This was fixed but all of these values must be supported now.
  292. if [ "x${GRUB_DISABLE_SUBMENU}" = xyes ] || [ "x${GRUB_DISABLE_SUBMENU}" = xy ]; then
  293. GRUB_DISABLE_SUBMENU="true"
  294. fi
  295. if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
  296. linux_entry "${OS}" "${version}" "${xen_version}" simple \
  297. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
  298. submenu_indentation="$grub_tab$grub_tab"
  299. if [ -z "$boot_device_id" ]; then
  300. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  301. fi
  302. # TRANSLATORS: %s is replaced with an OS name
  303. echo "submenu '$(gettext_printf "Advanced options for %s (with Xen hypervisor)" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
  304. echo " submenu '$(gettext_printf "Xen hypervisor, version %s" "${xen_version}" | grub_quote)' \$menuentry_id_option 'xen-hypervisor-$xen_version-$boot_device_id' {"
  305. is_top_level=false
  306. fi
  307. linux_entry "${OS}" "${version}" "${xen_version}" advanced \
  308. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
  309. if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
  310. linux_entry "${OS}" "${version}" "${xen_version}" recovery \
  311. "single ${GRUB_CMDLINE_LINUX}" "${GRUB_CMDLINE_XEN}"
  312. fi
  313. list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
  314. done
  315. if [ x"$is_top_level" != xtrue ]; then
  316. echo ' }'
  317. fi
  318. xen_list=`echo $xen_list | tr ' ' '\n' | fgrep -vx "$current_xen" | tr '\n' ' '`
  319. done
  320. # If at least one kernel was found, then we need to
  321. # add a closing '}' for the submenu command.
  322. if [ x"$is_top_level" != xtrue ]; then
  323. echo '}'
  324. fi
  325. echo "$title_correction_code"