Advertisement
Guest User

Untitled

a guest
Jan 9th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.21 KB | None | 0 0
  1. #! /bin/sh
  2. set -e
  3.  
  4. # grub-mkconfig helper script.
  5. # Copyright (C) 2006,2007,2008,2009  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. export TEXTDOMAIN=grub
  25. export TEXTDOMAINDIR="${datarootdir}/locale"
  26.  
  27. . "$pkgdatadir/grub-mkconfig_lib"
  28.  
  29. if [ "x${GRUB_DISABLE_OS_PROBER}" = "xtrue" ]; then
  30.   grub_warn "$(gettext_printf "os-prober will not be executed to detect other bootable partitions.\nSystems on them will not be added to the GRUB boot configuration.\nCheck GRUB_DISABLE_OS_PROBER documentation entry.")"
  31.   exit 0
  32. fi
  33.  
  34. if ! command -v os-prober > /dev/null || ! command -v linux-boot-prober > /dev/null ; then
  35.   # missing os-prober and/or linux-boot-prober
  36.   exit 0
  37. fi
  38.  
  39. grub_warn "$(gettext_printf "os-prober will be executed to detect other bootable partitions.\nIts output will be used to detect bootable binaries on them and create new boot entries.")"
  40.  
  41. OSPROBED="`os-prober | tr ' ' '^' | paste -s -d ' '`"
  42. if [ -z "${OSPROBED}" ] ; then
  43.   # empty os-prober output, nothing doing
  44.   exit 0
  45. fi
  46.  
  47. osx_entry() {
  48.     if [ x$2 = x32 ]; then
  49.         # TRANSLATORS: it refers to kernel architecture (32-bit)
  50.     bitstr="$(gettext "(32-bit)")"
  51.    else
  52.        # TRANSLATORS: it refers to kernel architecture (64-bit)
  53.     bitstr="$(gettext "(64-bit)")"
  54.    fi
  55.    # TRANSLATORS: it refers on the OS residing on device %s
  56.    onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  57.        cat << EOF
  58. menuentry '$(echo "${LONGNAME} $bitstr $onstr" | grub_quote)' --class osx --class darwin --class os \$menuentry_id_option 'osprober-xnu-$2-$(grub_get_device_id "${DEVICE}")'  {
  59. EOF
  60.     save_default_entry | grub_add_tab
  61.     prepare_grub_to_access_device ${DEVICE} | grub_add_tab
  62.     cat << EOF
  63.        load_video
  64.        set do_resume=0
  65.        if [ /var/vm/sleepimage -nt10 / ]; then
  66.           if xnu_resume /var/vm/sleepimage; then
  67.             set do_resume=1
  68.           fi
  69.        fi
  70.        if [ \$do_resume = 0 ]; then
  71.           xnu_uuid ${OSXUUID} uuid
  72.           if [ -f /Extra/DSDT.aml ]; then
  73.              acpi -e /Extra/DSDT.aml
  74.           fi
  75.           if [ /kernelcache -nt /System/Library/Extensions ]; then
  76.              $1 /kernelcache boot-uuid=\${uuid} rd=*uuid
  77.           elif [ -f /System/Library/Kernels/kernel ]; then
  78.              $1 /System/Library/Kernels/kernel boot-uuid=\${uuid} rd=*uuid
  79.              xnu_kextdir /System/Library/Extensions
  80.           else
  81.              $1 /mach_kernel boot-uuid=\${uuid} rd=*uuid
  82.              if [ /System/Library/Extensions.mkext -nt /System/Library/Extensions ]; then
  83.                xnu_mkext /System/Library/Extensions.mkext
  84.              else
  85.                xnu_kextdir /System/Library/Extensions
  86.              fi
  87.           fi
  88.           if [ -f /Extra/Extensions.mkext ]; then
  89.              xnu_mkext /Extra/Extensions.mkext
  90.           fi
  91.           if [ -d /Extra/Extensions ]; then
  92.              xnu_kextdir /Extra/Extensions
  93.           fi
  94.           if [ -f /Extra/devprop.bin ]; then
  95.              xnu_devprop_load /Extra/devprop.bin
  96.           fi
  97.           if [ -f /Extra/splash.jpg ]; then
  98.              insmod jpeg
  99.              xnu_splash /Extra/splash.jpg
  100.           fi
  101.           if [ -f /Extra/splash.png ]; then
  102.              insmod png
  103.              xnu_splash /Extra/splash.png
  104.           fi
  105.           if [ -f /Extra/splash.tga ]; then
  106.              insmod tga
  107.              xnu_splash /Extra/splash.tga
  108.           fi
  109.        fi
  110. }
  111. EOF
  112. }
  113.  
  114. used_osprober_linux_ids=
  115.  
  116. for OS in ${OSPROBED} ; do
  117.  DEVICE="`echo ${OS} | cut -d ':' -f 1`"
  118.  LONGNAME="`echo ${OS} | cut -d ':' -f 2 | tr '^' ' '`"
  119.  LABEL="`echo ${OS} | cut -d ':' -f 3 | tr '^' ' '`"
  120.  BOOT="`echo ${OS} | cut -d ':' -f 4`"
  121.  if UUID="`${grub_probe} --target=fs_uuid --device ${DEVICE%@*}`"; then
  122.    EXPUUID="$UUID"
  123.  
  124.    if [ x"${DEVICE#*@}" != x ] ; then
  125.      EXPUUID="${EXPUUID}@${DEVICE#*@}"
  126.    fi
  127.  
  128.    if [ "x${GRUB_OS_PROBER_SKIP_LIST}" != "x" ] && [ "x`echo ${GRUB_OS_PROBER_SKIP_LIST} | grep -i -e '\b'${EXPUUID}'\b'`" != "x" ] ; then
  129.      echo "Skipped ${LONGNAME} on ${DEVICE} by user request." >&2
  130.      continue
  131.    fi
  132.  fi
  133.  
  134.  BTRFS="`echo ${OS} | cut -d ':' -f 5`"
  135.  if [ "x$BTRFS" = "xbtrfs" ]; then
  136.     BTRFSuuid="`echo ${OS} | cut -d ':' -f 6`"
  137.     BTRFSsubvol="`echo ${OS} | cut -d ':' -f 7`"
  138.  fi
  139.  
  140.  if [ -z "${LONGNAME}" ] ; then
  141.    LONGNAME="${LABEL}"
  142.  fi
  143.  
  144.  # os-prober returns text string followed by optional counter
  145.  CLASS="--class $(echo "${LABEL}" | LC_ALL=C sed 's,[[:digit:]]*$,,' | cut -d' ' -f1 | tr 'A-Z' 'a-z' | LC_ALL=C sed 's,[^[:alnum:]_],_,g')"
  146.  
  147.  gettext_printf "Found %s on %s\n" "${LONGNAME}" "${DEVICE}" >&2
  148.  
  149.  case ${BOOT} in
  150.    chain)
  151.  
  152.       onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  153.      cat << EOF
  154. menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' $CLASS --class os \$menuentry_id_option 'osprober-chain-$(grub_get_device_id "${DEVICE}")' {
  155. EOF
  156.      save_default_entry | grub_add_tab
  157.      prepare_grub_to_access_device ${DEVICE} | grub_add_tab
  158.  
  159.      if [ x"`${grub_probe} --device ${DEVICE} --target=partmap`" = xmsdos ]; then
  160.       cat << EOF
  161.     parttool \${root} hidden-
  162. EOF
  163.      fi
  164.  
  165.      case ${LONGNAME} in
  166.     Windows\ Vista*|Windows\ 7*|Windows\ Server\ 2008*)
  167.     ;;
  168.     *)
  169.       cat << EOF
  170.     drivemap -s (hd0) \${root}
  171. EOF
  172.     ;;
  173.      esac
  174.  
  175.      cat <<EOF
  176.     chainloader +1
  177. }
  178. EOF
  179.    ;;
  180.    efi)
  181.  
  182.     EFIPATH=${DEVICE#*@}
  183.     DEVICE=${DEVICE%@*}
  184.     onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  185.      cat << EOF
  186. menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' $CLASS --class os \$menuentry_id_option 'osprober-efi-$(grub_get_device_id "${DEVICE}")' {
  187. EOF
  188.      save_default_entry | sed -e "s/^/\t/"
  189.      prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
  190.  
  191.      cat <<EOF
  192.     chainloader ${EFIPATH}
  193. }
  194. EOF
  195.    ;;
  196.    linux)
  197.      if [ "x$BTRFS" = "xbtrfs" ]; then
  198.         LINUXPROBED="`linux-boot-prober btrfs ${BTRFSuuid} ${BTRFSsubvol}  2> /dev/null | tr ' ' '^' | paste -s -d ' '`"
  199.      else
  200.         LINUXPROBED="`linux-boot-prober ${DEVICE} 2> /dev/null | tr ' ' '^' | paste -s -d ' '`"
  201.      fi
  202.      prepare_boot_cache=
  203.      boot_device_id=
  204.      is_top_level=true
  205.      title_correction_code=
  206.      OS="${LONGNAME}"
  207.  
  208.      for LINUX in ${LINUXPROBED} ; do
  209.        LROOT="`echo ${LINUX} | cut -d ':' -f 1`"
  210.        LBOOT="`echo ${LINUX} | cut -d ':' -f 2`"
  211.        LLABEL="`echo ${LINUX} | cut -d ':' -f 3 | tr '^' ' '`"
  212.        LKERNEL="`echo ${LINUX} | cut -d ':' -f 4`"
  213.        LINITRD="`echo ${LINUX} | cut -d ':' -f 5`"
  214.        LPARAMS="`echo ${LINUX} | cut -d ':' -f 6- | tr '^' ' '`"
  215.  
  216.        if [ -z "${LLABEL}" ] ; then
  217.          LLABEL="${LONGNAME}"
  218.        fi
  219.  
  220.     if [ "${LROOT}" != "${LBOOT}" ]; then
  221.       LKERNEL="${LKERNEL#/boot}"
  222.       LINITRD="${LINITRD#/boot}"
  223.     fi
  224.  
  225.     onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  226.     recovery_params="$(echo "${LPARAMS}" | grep single)" || true
  227.     counter=1
  228.     while echo "$used_osprober_linux_ids" | grep 'osprober-gnulinux-$LKERNEL-${recovery_params}-$counter-$boot_device_id' > /dev/null; do
  229.         counter=$((counter+1));
  230.     done
  231.     if [ -z "$boot_device_id" ]; then
  232.         boot_device_id="$(grub_get_device_id "${DEVICE}")"
  233.     fi
  234.     used_osprober_linux_ids="$used_osprober_linux_ids 'osprober-gnulinux-$LKERNEL-${recovery_params}-$counter-$boot_device_id'"
  235.  
  236.     if [ -z "${prepare_boot_cache}" ]; then
  237.       prepare_boot_cache="$(prepare_grub_to_access_device ${LBOOT} | grub_add_tab)"
  238.     fi
  239.  
  240.     # The GRUB_DISABLE_SUBMENU option used to be different than others since it was
  241.     # mentioned in the documentation that has to be set to 'y' instead of 'true' to
  242.     # enable it. This caused a lot of confusion to users that set the option to 'y',
  243.     # 'yes' or 'true'. This was fixed but all of these values must be supported now.
  244.     if [ "x${GRUB_DISABLE_SUBMENU}" = xyes ] || [ "x${GRUB_DISABLE_SUBMENU}" = xy ]; then
  245.         GRUB_DISABLE_SUBMENU="true"
  246.     fi
  247.  
  248.     if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xtrue ]; then
  249.            cat << EOF
  250. menuentry '$(echo "$OS $onstr" | grub_quote)' $CLASS --class gnu-linux --class gnu --class os \$menuentry_id_option 'osprober-gnulinux-simple-$boot_device_id' {
  251. EOF
  252.         save_default_entry | grub_add_tab
  253.         printf '%s\n' "${prepare_boot_cache}"
  254.         cat <<  EOF
  255.     linux ${LKERNEL} ${LPARAMS}
  256. EOF
  257.            if [ -n "${LINITRD}" ] ; then
  258.          cat << EOF
  259.     initrd ${LINITRD}
  260. EOF
  261.            fi
  262.        cat << EOF
  263. }
  264. EOF
  265.         echo "submenu '$(gettext_printf "Advanced options for %s" "${OS} $onstr" | grub_quote)' \$menuentry_id_option 'osprober-gnulinux-advanced-$boot_device_id' {"
  266.         is_top_level=false
  267.     fi
  268.     title="${LLABEL} $onstr"
  269.        cat << EOF
  270.     menuentry '$(echo "$title" | grub_quote)' --class gnu-linux --class gnu --class os \$menuentry_id_option 'osprober-gnulinux-$LKERNEL-${recovery_params}-$boot_device_id' {
  271. EOF
  272.     save_default_entry | sed -e "s/^/$grub_tab$grub_tab/"
  273.     printf '%s\n' "${prepare_boot_cache}" | grub_add_tab
  274.     cat <<  EOF
  275.         linux ${LKERNEL} ${LPARAMS}
  276. EOF
  277.        if [ -n "${LINITRD}" ] ; then
  278.            cat << EOF
  279.         initrd ${LINITRD}
  280. EOF
  281.        fi
  282.        cat << EOF
  283.     }
  284. EOF
  285.     if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
  286.         replacement_title="$(echo "Advanced options for ${OS} $onstr" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
  287.         quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
  288.         title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
  289.         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")"
  290.     fi
  291.      done
  292.      if [ x"$is_top_level" != xtrue ]; then
  293.       echo '}'
  294.      fi
  295.      echo "$title_correction_code"
  296.    ;;
  297.    macosx)
  298.      if [ "${UUID}" ]; then
  299.     OSXUUID="${UUID}"
  300.     osx_entry xnu_kernel 32
  301.     osx_entry xnu_kernel64 64
  302.      fi
  303.    ;;
  304.    hurd)
  305.      onstr="$(gettext_printf "(on %s)" "${DEVICE}")"
  306.      cat << EOF
  307. menuentry '$(echo "${LONGNAME} $onstr" | grub_quote)' --class hurd --class gnu --class os \$menuentry_id_option 'osprober-gnuhurd-/boot/gnumach.gz-false-$(grub_get_device_id "${DEVICE}")' {
  308. EOF
  309.      save_default_entry | grub_add_tab
  310.      prepare_grub_to_access_device ${DEVICE} | grub_add_tab
  311.      grub_device="`${grub_probe} --device ${DEVICE} --target=drive`"
  312.      mach_device="`echo "${grub_device}" | sed -e 's/(\(hd.*\),msdos\(.*\))/\1s\2/'`"
  313.      grub_fs="`${grub_probe} --device ${DEVICE} --target=fs`"
  314.       case "${grub_fs}" in
  315.     *fs)    hurd_fs="${grub_fs}" ;;
  316.     *)  hurd_fs="${grub_fs}fs" ;;
  317.       esac
  318.       cat << EOF
  319.     multiboot /boot/gnumach.gz root=device:${mach_device}
  320.     module /hurd/${hurd_fs}.static ${hurd_fs} --readonly \\
  321.             --multiboot-command-line='\${kernel-command-line}' \\
  322.             --host-priv-port='\${host-port}' \\
  323.             --device-master-port='\${device-port}' \\
  324.             --exec-server-task='\${exec-task}' -T typed '\${root}' \\
  325.             '\$(task-create)' '\$(task-resume)'
  326.     module /lib/ld.so.1 exec /hurd/exec '\$(exec-task=task-create)'
  327. }
  328. EOF
  329.     ;;
  330.     minix)
  331.       cat << EOF
  332. menuentry "${LONGNAME} (on ${DEVICE}, Multiboot)" {
  333. EOF
  334.          save_default_entry | sed -e "s/^/\t/"
  335.          prepare_grub_to_access_device ${DEVICE} | sed -e "s/^/\t/"
  336.      cat << EOF
  337.     multiboot /boot/image_latest
  338. }
  339. EOF
  340.     ;;
  341.     *)
  342.       # TRANSLATORS: %s is replaced by OS name.
  343.       gettext_printf "%s is not yet supported by grub-mkconfig.\n" "  ${LONGNAME}" >&2
  344.     ;;
  345.   esac
  346. done
  347.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement