Advertisement
Guest User

Grub for low-latency (Ubuntu Studio 20.04)

a guest
Aug 15th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.90 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. # Ubuntustudio customization that keeps the latest lowlatency kernel
  21. # as the first menu item and default boot even if there is a newer
  22. # generic or other kernel. Often the updated generic kernel is
  23. # released a few days earlier than the lowlatency kernel
  24.  
  25. prefix="/usr"
  26. exec_prefix="/usr"
  27. datarootdir="/usr/share"
  28. ubuntu_recovery="1"
  29. quiet_boot="1"
  30. quick_boot="1"
  31. gfxpayload_dynamic="1"
  32. vt_handoff="1"
  33.  
  34. . "$pkgdatadir/grub-mkconfig_lib"
  35.  
  36. export TEXTDOMAIN=grub
  37. export TEXTDOMAINDIR="${datarootdir}/locale"
  38.  
  39. # only 09_lowlatency or 10_linux should run, since we got this far
  40. # 10_linux should not run next
  41. if [ -x /etc/grub.d/10_linux ]; then
  42. chmod -x /etc/grub.d/10_linux
  43. fi
  44.  
  45. CLASS="--class gnu-linux --class gnu --class os"
  46. SUPPORTED_INITS="sysvinit:/lib/sysvinit/init systemd:/lib/systemd/systemd upstart:/sbin/upstart"
  47.  
  48. if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  49. OS=GNU/Linux
  50. else
  51. case ${GRUB_DISTRIBUTOR} in
  52. Ubuntu|Kubuntu)
  53. OS="${GRUB_DISTRIBUTOR}"
  54. ;;
  55. *)
  56. OS="${GRUB_DISTRIBUTOR} GNU/Linux"
  57. ;;
  58. esac
  59. CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
  60. fi
  61.  
  62. # loop-AES arranges things so that /dev/loop/X can be our root device, but
  63. # the initrds that Linux uses don't like that.
  64. case ${GRUB_DEVICE} in
  65. /dev/loop/*|/dev/loop[0-9])
  66. GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  67. # We can't cope with devices loop-mounted from files here.
  68. case ${GRUB_DEVICE} in
  69. /dev/*) ;;
  70. *) exit 0 ;;
  71. esac
  72. ;;
  73. esac
  74.  
  75. # btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
  76. # and mounting btrfs requires user space scanning, so force UUID in this case.
  77. if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
  78. || ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
  79. || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  80. LINUX_ROOT_DEVICE=${GRUB_DEVICE}
  81. else
  82. LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
  83. fi
  84.  
  85. case x"$GRUB_FS" in
  86. xbtrfs)
  87. rootsubvol="`make_system_path_relative_to_its_root /`"
  88. rootsubvol="${rootsubvol#/}"
  89. if [ "x${rootsubvol}" != x ]; then
  90. GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
  91. fi;;
  92. xzfs)
  93. rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
  94. bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
  95. LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
  96. ;;
  97. esac
  98.  
  99. title_correction_code=
  100.  
  101. if [ -x /lib/recovery-mode/recovery-menu ]; then
  102. GRUB_CMDLINE_LINUX_RECOVERY=recovery
  103. else
  104. GRUB_CMDLINE_LINUX_RECOVERY=single
  105. fi
  106. if [ "$ubuntu_recovery" = 1 ]; then
  107. GRUB_CMDLINE_LINUX_RECOVERY="$GRUB_CMDLINE_LINUX_RECOVERY nomodeset"
  108. fi
  109.  
  110. if [ "$vt_handoff" = 1 ]; then
  111. for word in $GRUB_CMDLINE_LINUX_DEFAULT; do
  112. if [ "$word" = splash ]; then
  113. GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT \$vt_handoff"
  114. fi
  115. done
  116. fi
  117.  
  118. if [ x"$GRUB_FORCE_PARTUUID" != x ]; then
  119. echo "set partuuid=${GRUB_FORCE_PARTUUID}"
  120. fi
  121.  
  122. linux_entry ()
  123. {
  124. os="$1"
  125. version="$2"
  126. type="$3"
  127. args="$4"
  128.  
  129. if [ -z "$boot_device_id" ]; then
  130. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  131. fi
  132. if [ x$type != xsimple ] ; then
  133. case $type in
  134. recovery)
  135. title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")" ;;
  136. init-*)
  137. title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "${type#init-}")" ;;
  138. *)
  139. title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
  140. esac
  141. if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
  142. replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
  143. quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
  144. title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
  145. 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")"
  146. fi
  147. echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  148. else
  149. echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  150. fi
  151. if [ "$quick_boot" = 1 ]; then
  152. echo " recordfail" | sed "s/^/$submenu_indentation/"
  153. fi
  154. if [ x$type != xrecovery ] ; then
  155. save_default_entry | grub_add_tab
  156. fi
  157.  
  158. # Use ELILO's generic "efifb" when it's known to be available.
  159. # FIXME: We need an interface to select vesafb in case efifb can't be used.
  160. if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
  161. echo " load_video" | sed "s/^/$submenu_indentation/"
  162. else
  163. if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
  164. echo " load_video" | sed "s/^/$submenu_indentation/"
  165. fi
  166. fi
  167. if ([ "$ubuntu_recovery" = 0 ] || [ x$type != xrecovery ]) && \
  168. ([ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 1 ]); then
  169. echo " gfxmode \$linux_gfx_mode" | sed "s/^/$submenu_indentation/"
  170. fi
  171.  
  172. echo " insmod gzio" | sed "s/^/$submenu_indentation/"
  173. echo " if [ x\$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi" | sed "s/^/$submenu_indentation/"
  174.  
  175. if [ x$dirname = x/ ]; then
  176. if [ -z "${prepare_root_cache}" ]; then
  177. prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
  178. fi
  179. printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
  180. else
  181. if [ -z "${prepare_boot_cache}" ]; then
  182. prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
  183. fi
  184. printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  185. fi
  186. if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
  187. message="$(gettext_printf "Loading Linux %s ..." ${version})"
  188. sed "s/^/$submenu_indentation/" << EOF
  189. echo '$(echo "$message" | grub_quote)'
  190. EOF
  191. fi
  192. if test -d /sys/firmware/efi && test -e "${linux}.efi.signed"; then
  193. sed "s/^/$submenu_indentation/" << EOF
  194. linux ${rel_dirname}/${basename}.efi.signed root=${linux_root_device_thisversion} ro ${args}
  195. EOF
  196. else
  197. # We have initrd and PARTUUID is set - we try to boot without initrd, and fallback to using it
  198. # if it fails.
  199. # "panic=-1" means "on panic reboot immediately". "panic=0" disables the reboot behavior.
  200. if [ x"$GRUB_FORCE_PARTUUID" != x ]; then
  201. linux_root_device_thisversion="PARTUUID=${GRUB_FORCE_PARTUUID}"
  202. fi
  203. message="$(gettext_printf "Loading initial ramdisk ...")"
  204. if test -n "${initrd}" && [ x"$GRUB_FORCE_PARTUUID" != x ]; then
  205. sed "s/^/$submenu_indentation/" << EOF
  206. if [ "\${initrdfail}" = 1 ]; then
  207. linux ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
  208. EOF
  209. if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
  210. sed "s/^/$submenu_indentation/" << EOF
  211. echo '$(echo "$message" | grub_quote)'
  212. EOF
  213. fi
  214.  
  215. sed "s/^/$submenu_indentation/" << EOF
  216. initrd ${rel_dirname}/${initrd}
  217. else
  218. linux ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args} panic=-1
  219. fi
  220. initrdfail
  221. EOF
  222. else
  223. # We don't have initrd or we don't want to set PARTUUID. Don't try initrd-less boot with fallback.
  224. sed "s/^/$submenu_indentation/" << EOF
  225. linux ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
  226. EOF
  227. if test -n "${initrd}"; then
  228. # We do have initrd - let's use it at boot.
  229. # TRANSLATORS: ramdisk isn't identifier. Should be translated.
  230. if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
  231. sed "s/^/$submenu_indentation/" << EOF
  232. echo '$(echo "$message" | grub_quote)'
  233. EOF
  234. fi
  235. sed "s/^/$submenu_indentation/" << EOF
  236. initrd ${rel_dirname}/${initrd}
  237. EOF
  238. fi
  239. fi
  240. fi
  241. sed "s/^/$submenu_indentation/" << EOF
  242. }
  243. EOF
  244. }
  245.  
  246. machine=`uname -m`
  247. case "x$machine" in
  248. xi?86 | xx86_64)
  249. list=
  250. for i in /boot/vmlinuz-*lowlatency /vmlinuz-*lowlatency /boot/kernel-*lowlatency ; do
  251. if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
  252. done
  253. list2=
  254. for i in /boot/vmlinuz-*generic /vmlinuz-*generic /boot/kernel-*generic ; do
  255. if grub_file_is_not_garbage "$i" ; then list2="$list2 $i" ; fi
  256. done ;;
  257. *)
  258. list=
  259. for i in /boot/vmlinuz-*lowlatency /boot/vmlinux-*lowlatency /vmlinuz-*lowlatency /vmlinux-*lowlatency /boot/kernel-*lowlatency ; do
  260. if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
  261. done
  262. list2=
  263. for i in /boot/vmlinuz-*generic /boot/vmlinux-*generic /vmlinuz-*generic /vmlinux-*generic /boot/kernel-*generic ; do
  264. if grub_file_is_not_garbage "$i" ; then list2="$list2 $i" ; fi
  265. done ;;
  266. esac
  267.  
  268. case "$machine" in
  269. i?86) GENKERNEL_ARCH="x86" ;;
  270. mips|mips64) GENKERNEL_ARCH="mips" ;;
  271. mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
  272. arm*) GENKERNEL_ARCH="arm" ;;
  273. *) GENKERNEL_ARCH="$machine" ;;
  274. esac
  275.  
  276. prepare_boot_cache=
  277. prepare_root_cache=
  278. boot_device_id=
  279. title_correction_code=
  280.  
  281. cat << 'EOF'
  282. function gfxmode {
  283. set gfxpayload="${1}"
  284. EOF
  285. if [ "$vt_handoff" = 1 ]; then
  286. cat << 'EOF'
  287. if [ "${1}" = "keep" ]; then
  288. set vt_handoff=vt.handoff=1
  289. else
  290. set vt_handoff=
  291. fi
  292. EOF
  293. fi
  294. cat << EOF
  295. }
  296. EOF
  297.  
  298. # Use ELILO's generic "efifb" when it's known to be available.
  299. # FIXME: We need an interface to select vesafb in case efifb can't be used.
  300. if [ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 0 ]; then
  301. echo "set linux_gfx_mode=$GRUB_GFXPAYLOAD_LINUX"
  302. else
  303. cat << EOF
  304. if [ "\${recordfail}" != 1 ]; then
  305. if [ -e \${prefix}/gfxblacklist.txt ]; then
  306. if hwmatch \${prefix}/gfxblacklist.txt 3; then
  307. if [ \${match} = 0 ]; then
  308. set linux_gfx_mode=keep
  309. else
  310. set linux_gfx_mode=text
  311. fi
  312. else
  313. set linux_gfx_mode=text
  314. fi
  315. else
  316. set linux_gfx_mode=keep
  317. fi
  318. else
  319. set linux_gfx_mode=text
  320. fi
  321. EOF
  322. fi
  323. cat << EOF
  324. export linux_gfx_mode
  325. EOF
  326.  
  327. # Extra indentation to add to menu entries in a submenu. We're not in a submenu
  328. # yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
  329. submenu_indentation=""
  330.  
  331. is_top_level=true
  332. is_generic_top=true
  333. if [ "x$list" = "x" ]; then
  334. is_top_level=false
  335. generic_is_default=true
  336. fi
  337. if [ "x$list2" = "x" ]; then
  338. is_generic_top=false
  339. fi
  340. while [ "x$list $list2" != "x" ] ; do
  341. if [ "x$is_generic_top" = xtrue ] && [ "x$is_top_level" != xtrue ]; then
  342. linux=`version_find_latest $list2`
  343. case $linux in
  344. *.efi.signed)
  345. # We handle these in linux_entry.
  346. list2=`echo $list2 | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '`
  347. continue
  348. ;;
  349. esac
  350. else
  351. linux=`version_find_latest $list`
  352. if [ "x$linux" = "x" ]; then
  353. break
  354. fi
  355. case $linux in
  356. *.efi.signed)
  357. # We handle these in linux_entry.
  358. list=`echo $list | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '`
  359. continue
  360. ;;
  361. esac
  362. fi
  363. gettext_printf "Found linux image: %s\n" "$linux" >&2
  364. basename=`basename $linux`
  365. dirname=`dirname $linux`
  366. rel_dirname=`make_system_path_relative_to_its_root $dirname`
  367. version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
  368. alt_version=`echo $version | sed -e "s,\.old$,,g"`
  369. linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
  370.  
  371. initrd=
  372. for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
  373. "initrd-${version}" "initramfs-${version}.img" \
  374. "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
  375. "initrd-${alt_version}" "initramfs-${alt_version}.img" \
  376. "initramfs-genkernel-${version}" \
  377. "initramfs-genkernel-${alt_version}" \
  378. "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
  379. "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
  380. if test -e "${dirname}/${i}" ; then
  381. initrd="$i"
  382. break
  383. fi
  384. done
  385.  
  386. config=
  387. for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
  388. if test -e "${i}" ; then
  389. config="${i}"
  390. break
  391. fi
  392. done
  393.  
  394. initramfs=
  395. if test -n "${config}" ; then
  396. initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
  397. fi
  398.  
  399. if test -n "${initrd}" ; then
  400. gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
  401. elif test -z "${initramfs}" ; then
  402. # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs. Since there's
  403. # no initrd or builtin initramfs, it can't work here.
  404. linux_root_device_thisversion=${GRUB_DEVICE}
  405. fi
  406.  
  407. if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
  408. linux_entry "${OS} (lowlatency)" "${version}" simple \
  409. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  410.  
  411. if [ -z "$boot_device_id" ]; then
  412. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  413. fi
  414. if [ "x$is_generic_top" != xtrue ]; then
  415. submenu_indentation="$grub_tab"
  416. # TRANSLATORS: %s is replaced with an OS name
  417. echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
  418. fi
  419. is_top_level=false
  420. continue
  421. fi
  422.  
  423. if [ "x$is_generic_top" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
  424. linux_entry "${OS} (generic)" "${version}" simple \
  425. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  426.  
  427. submenu_indentation="$grub_tab"
  428. list="$list $list2"
  429.  
  430. if [ -z "$boot_device_id" ]; then
  431. boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  432. fi
  433. # TRANSLATORS: %s is replaced with an OS name
  434. echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
  435. is_generic_top=false
  436. continue
  437.  
  438. fi
  439.  
  440. linux_entry "${OS}" "${version}" advanced \
  441. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  442. for supported_init in ${SUPPORTED_INITS}; do
  443. init_path="${supported_init#*:}"
  444. if [ -x "${init_path}" ] && [ "$(readlink -f /sbin/init)" != "$(readlink -f "${init_path}")" ]; then
  445. linux_entry "${OS}" "${version}" "init-${supported_init%%:*}" \
  446. "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} init=${init_path}"
  447. fi
  448. done
  449. if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
  450. linux_entry "${OS}" "${version}" recovery \
  451. "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
  452. fi
  453.  
  454. list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
  455. done
  456.  
  457. # If at least one kernel was found, then we need to
  458. # add a closing '}' for the submenu command.
  459. if [ x"$is_top_level" != xtrue ]; then
  460. echo '}'
  461. fi
  462.  
  463. echo "$title_correction_code"
  464.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement