Advertisement
Guest User

/etc/grub.d/30_os-prober

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