vds89

20_linux_xen

Jan 2nd, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 KB | None | 0 0
  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="/usr"
  21. exec_prefix="/usr"
  22. datarootdir="/usr/share"
  23.  
  24. . "$pkgdatadir/grub-mkconfig_lib"
  25.  
  26. export TEXTDOMAIN=grub
  27. export TEXTDOMAINDIR="${datarootdir}/locale"
  28.  
  29. CLASS="--class gnu-linux --class gnu --class os --class xen"
  30. SUPPORTED_INITS="sysvinit:/lib/sysvinit/init systemd:/lib/systemd/systemd upstart:/sbin/upstart"
  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. # Default to disabling partition uuid support to maintian compatibility with
  53. # older kernels.
  54. GRUB_DISABLE_LINUX_PARTUUID=${GRUB_DISABLE_LINUX_PARTUUID-true}
  55.  
  56. # btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
  57. # and mounting btrfs requires user space scanning, so force UUID in this case.
  58. if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
  59. || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
  60. && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
  61. || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
  62. && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
  63. || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  64. LINUX_ROOT_DEVICE=${GRUB_DEVICE}
  65. elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
  66. || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
  67. LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
  68. else
  69. LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
  70. fi
  71.  
  72. # Allow overriding GRUB_CMDLINE_LINUX and GRUB_CMDLINE_LINUX_DEFAULT.
  73. if [ "${GRUB_CMDLINE_LINUX_XEN_REPLACE}" ]; then
  74. GRUB_CMDLINE_LINUX="${GRUB_CMDLINE_LINUX_XEN_REPLACE}"
  75. fi
  76. if [ "${GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT}" ]; then
  77. GRUB_CMDLINE_LINUX_DEFAULT="${GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT}"
  78. fi
  79.  
  80. case x"$GRUB_FS" in
  81. xbtrfs)
  82. rootsubvol="`make_system_path_relative_to_its_root /`"
  83. rootsubvol="${rootsubvol#/}"
  84. if [ "x${rootsubvol}" != x ]; then
  85. GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
  86. fi;;
  87. xzfs)
  88. rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
  89. bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
  90. LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
  91. ;;
  92. esac
  93.  
  94. title_correction_code=
  95.  
  96. linux_entry ()
  97. {
  98. linux_entry_xsm "$@" false
  99. linux_entry_xsm "$@" true
  100. }
  101. linux_entry_xsm ()
  102. {
  103. os="$1"
  104. version="$2"
  105. xen_version="$3"
  106. type="$4"
  107. args="$5"
  108. xen_args="$6"
  109. xsm="$7"
  110. # If user wants to enable XSM support, make sure there's
  111. # corresponding policy file.
  112. if ${xsm} ; then
  113. xenpolicy="xenpolicy-$xen_version"
  114. if test ! -e "${xen_dirname}/${xenpolicy}" ; then
  115. return
  116. fi
  117. xen_args="$xen_args flask=enforcing"
  118. xen_version="$(gettext_printf "%s (XSM enabled)" "$xen_version")"
  119. # xen_version is used for messages only; actual file is xen_basename
  120. fi
  121. if [ -z "$boot_device_id" ]; then
  122. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  123. fi
  124. if [ x$type != xsimple ] ; then
  125. if [ x$type = xrecovery ] ; then
  126. title="$(gettext_printf "%s, with Xen %s and Linux %s (%s)" "${os}" "${xen_version}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")"
  127. elif [ "${type#init-}" != "$type" ] ; then
  128. title="$(gettext_printf "%s, with Xen %s and Linux %s (%s)" "${os}" "${xen_version}" "${version}" "${type#init-}")"
  129. else
  130. title="$(gettext_printf "%s, with Xen %s and Linux %s" "${os}" "${xen_version}" "${version}")"
  131. fi
  132. replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
  133. if [ x"Xen ${xen_version}>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
  134. quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
  135. title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
  136. 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")"
  137. fi
  138. echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'xen-gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  139. else
  140. title="$(gettext_printf "%s, with Xen hypervisor" "${os}")"
  141. echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'xen-gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  142. fi
  143. if [ x$type != xrecovery ] ; then
  144. save_default_entry | grub_add_tab | sed "s/^/$submenu_indentation/"
  145. fi
  146.  
  147. if [ -z "${prepare_boot_cache}" ]; then
  148. prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
  149. fi
  150. printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  151. xmessage="$(gettext_printf "Loading Xen %s ..." ${xen_version})"
  152. lmessage="$(gettext_printf "Loading Linux %s ..." ${version})"
  153. sed "s/^/$submenu_indentation/" << EOF
  154. echo '$(echo "$xmessage" | grub_quote)'
  155. if [ "\$grub_platform" = "pc" -o "\$grub_platform" = "" ]; then
  156. xen_rm_opts=
  157. else
  158. xen_rm_opts="no-real-mode edd=off"
  159. fi
  160. ${xen_loader} ${rel_xen_dirname}/${xen_basename} placeholder ${xen_args} \${xen_rm_opts}
  161. echo '$(echo "$lmessage" | grub_quote)'
  162. ${module_loader} ${rel_dirname}/${basename} placeholder root=${linux_root_device_thisversion} ro ${args}
  163. EOF
  164. if test -n "${initrd}" ; then
  165. # TRANSLATORS: ramdisk isn't identifier. Should be translated.
  166. message="$(gettext_printf "Loading initial ramdisk ...")"
  167. initrd_path=
  168. for i in ${initrd}; do
  169. initrd_path="${initrd_path} ${rel_dirname}/${i}"
  170. done
  171. sed "s/^/$submenu_indentation/" << EOF
  172. echo '$(echo "$message" | grub_quote)'
  173. ${module_loader} --nounzip $(echo $initrd_path)
  174. EOF
  175. fi
  176. if ${xsm} && test -n "${xenpolicy}" ; then
  177. message="$(gettext_printf "Loading XSM policy ...")"
  178. sed "s/^/$submenu_indentation/" << EOF
  179. echo '$(echo "$message" | grub_quote)'
  180. ${module_loader} ${rel_dirname}/${xenpolicy}
  181. EOF
  182. fi
  183. sed "s/^/$submenu_indentation/" << EOF
  184. }
  185. EOF
  186. }
  187.  
  188. linux_list=
  189. for i in /boot/vmlinu[xz]-* /vmlinu[xz]-* /boot/kernel-*; do
  190. if grub_file_is_not_garbage "$i"; then
  191. basename=$(basename $i)
  192. version=$(echo $basename | sed -e "s,^[^0-9]*-,,g")
  193. dirname=$(dirname $i)
  194. config=
  195. for j in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
  196. if test -e "${j}" ; then
  197. config="${j}"
  198. break
  199. fi
  200. done
  201. 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
  202. fi
  203. done
  204. if [ "x${linux_list}" = "x" ] ; then
  205. exit 0
  206. fi
  207.  
  208. file_is_not_xen_garbage () {
  209. case "$1" in
  210. */xen-syms-*)
  211. return 1;;
  212. */xenpolicy-*)
  213. return 1;;
  214. */*.config)
  215. return 1;;
  216. *)
  217. return 0;;
  218. esac
  219. }
  220.  
  221. xen_list=
  222. for i in /boot/xen*; do
  223. if grub_file_is_not_garbage "$i" && file_is_not_xen_garbage "$i" ; then xen_list="$xen_list $i" ; fi
  224. done
  225. prepare_boot_cache=
  226. boot_device_id=
  227.  
  228. title_correction_code=
  229.  
  230. machine=`uname -m`
  231.  
  232. case "$machine" in
  233. i?86) GENKERNEL_ARCH="x86" ;;
  234. mips|mips64) GENKERNEL_ARCH="mips" ;;
  235. mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
  236. arm*) GENKERNEL_ARCH="arm" ;;
  237. *) GENKERNEL_ARCH="$machine" ;;
  238. esac
  239.  
  240. # Extra indentation to add to menu entries in a submenu. We're not in a submenu
  241. # yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
  242. submenu_indentation=""
  243.  
  244. is_top_level=true
  245.  
  246. while [ "x${xen_list}" != "x" ] ; do
  247. list="${linux_list}"
  248. current_xen=`version_find_latest $xen_list`
  249. xen_basename=`basename ${current_xen}`
  250. xen_dirname=`dirname ${current_xen}`
  251. rel_xen_dirname=`make_system_path_relative_to_its_root $xen_dirname`
  252. xen_version=`echo $xen_basename | sed -e "s,.gz$,,g;s,^xen-,,g"`
  253. if [ -z "$boot_device_id" ]; then
  254. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  255. fi
  256. if [ "x$is_top_level" != xtrue ]; then
  257. echo " submenu '$(gettext_printf "Xen hypervisor, version %s" "${xen_version}" | grub_quote)' \$menuentry_id_option 'xen-hypervisor-$xen_version-$boot_device_id' {"
  258. fi
  259. if ($grub_file --is-arm64-efi $current_xen); then
  260. xen_loader="xen_hypervisor"
  261. module_loader="xen_module"
  262. else
  263. if ($grub_file --is-x86-multiboot2 $current_xen); then
  264. xen_loader="multiboot2"
  265. module_loader="module2"
  266. else
  267. xen_loader="multiboot"
  268. module_loader="module"
  269. fi
  270. fi
  271.  
  272. initrd_early=
  273. for i in ${GRUB_EARLY_INITRD_LINUX_STOCK} \
  274. ${GRUB_EARLY_INITRD_LINUX_CUSTOM}; do
  275. if test -e "${xen_dirname}/${i}" ; then
  276. initrd_early="${initrd_early} ${i}"
  277. fi
  278. done
  279.  
  280. while [ "x$list" != "x" ] ; do
  281. linux=`version_find_latest $list`
  282. gettext_printf "Found linux image: %s\n" "$linux" >&2
  283. basename=`basename $linux`
  284. dirname=`dirname $linux`
  285. rel_dirname=`make_system_path_relative_to_its_root $dirname`
  286. version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
  287. alt_version=`echo $version | sed -e "s,\.old$,,g"`
  288. linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
  289.  
  290. initrd_real=
  291. for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
  292. "initrd-${version}" "initramfs-${version}.img" \
  293. "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
  294. "initrd-${alt_version}" "initramfs-${alt_version}.img" \
  295. "initramfs-genkernel-${version}" \
  296. "initramfs-genkernel-${alt_version}" \
  297. "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
  298. "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}" ; do
  299. if test -e "${dirname}/${i}" ; then
  300. initrd_real="$i"
  301. break
  302. fi
  303. done
  304.  
  305. initrd=
  306. if test -n "${initrd_early}" || test -n "${initrd_real}"; then
  307. initrd="${initrd_early} ${initrd_real}"
  308.  
  309. initrd_display=
  310. for i in ${initrd}; do
  311. initrd_display="${initrd_display} ${dirname}/${i}"
  312. done
  313. gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
  314. fi
  315.  
  316. if test -z "${initrd_real}"; then
  317. # "UUID=" magic is parsed by initrds. Since there's no initrd, it can't work here.
  318. if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] \
  319. || [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then
  320.  
  321. linux_root_device_thisversion=${GRUB_DEVICE}
  322. else
  323. linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
  324. fi
  325. fi
  326.  
  327. # The GRUB_DISABLE_SUBMENU option used to be different than others since it was
  328. # mentioned in the documentation that has to be set to 'y' instead of 'true' to
  329. # enable it. This caused a lot of confusion to users that set the option to 'y',
  330. # 'yes' or 'true'. This was fixed but all of these values must be supported now.
  331. if [ "x${GRUB_DISABLE_SUBMENU}" = xyes ] || [ "x${GRUB_DISABLE_SUBMENU}" = xy ]; then
  332. GRUB_DISABLE_SUBMENU="true"
  333. fi
  334.  
  335. if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
  336. linux_entry "${OS}" "${version}" "${xen_version}" simple \
  337. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
  338.  
  339. submenu_indentation="$grub_tab$grub_tab"
  340.  
  341. if [ -z "$boot_device_id" ]; then
  342. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  343. fi
  344. # TRANSLATORS: %s is replaced with an OS name
  345. echo "submenu '$(gettext_printf "Advanced options for %s (with Xen hypervisor)" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
  346. echo " submenu '$(gettext_printf "Xen hypervisor, version %s" "${xen_version}" | grub_quote)' \$menuentry_id_option 'xen-hypervisor-$xen_version-$boot_device_id' {"
  347. is_top_level=false
  348. fi
  349.  
  350. linux_entry "${OS}" "${version}" "${xen_version}" advanced \
  351. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
  352. for supported_init in ${SUPPORTED_INITS}; do
  353. init_path="${supported_init#*:}"
  354. if [ -x "${init_path}" ] && [ "$(readlink -f /sbin/init)" != "$(readlink -f "${init_path}")" ]; then
  355. linux_entry "${OS}" "${version}" "${xen_version}" "init-${supported_init%%:*}" \
  356. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} init=${init_path}" "${GRUB_CMDLINE_XEN} ${GRUB_CMDLINE_XEN_DEFAULT}"
  357.  
  358. fi
  359. done
  360. if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
  361. linux_entry "${OS}" "${version}" "${xen_version}" recovery \
  362. "single ${GRUB_CMDLINE_LINUX}" "${GRUB_CMDLINE_XEN}"
  363. fi
  364.  
  365. list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
  366. done
  367. if [ x"$is_top_level" != xtrue ]; then
  368. echo ' }'
  369. fi
  370. xen_list=`echo $xen_list | tr ' ' '\n' | fgrep -vx "$current_xen" | tr '\n' ' '`
  371. done
  372.  
  373. # If at least one kernel was found, then we need to
  374. # add a closing '}' for the submenu command.
  375. if [ x"$is_top_level" != xtrue ]; then
  376. echo '}'
  377. fi
  378.  
  379. echo "$title_correction_code"
Advertisement
Add Comment
Please, Sign In to add comment