daily pastebin goal
83%
SHARE
TWEET

Untitled

a guest Dec 18th, 2012 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/sh
  2. set -e
  3.  
  4. # Generate grub.cfg by inspecting /boot contents.
  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. transform="s,x,x,"
  21. prefix="/usr"
  22. exec_prefix="${prefix}"
  23. datarootdir="${prefix}/share"
  24.  
  25. prefix="/usr"
  26. exec_prefix="${prefix}"
  27. sbindir="${exec_prefix}/sbin"
  28. bindir="${exec_prefix}/bin"
  29. sysconfdir="/etc"
  30. PACKAGE_NAME=GRUB
  31. PACKAGE_VERSION=1.99-21ubuntu3.1
  32. host_os=linux-gnu
  33. datadir="${datarootdir}"
  34. pkgdatadir="${datadir}/`echo grub | sed "${transform}"`"
  35. grub_cfg=""
  36. grub_mkconfig_dir="${sysconfdir}"/grub.d
  37.  
  38. self=`basename $0`
  39. grub_probe="${sbindir}/`echo grub-probe | sed "${transform}"`"
  40. grub_script_check="${bindir}/`echo grub-script-check | sed "${transform}"`"
  41.  
  42. GRUB_PREFIX=`echo '/boot/grub' | sed "s,//*,/,g"`
  43.  
  44. # Usage: usage
  45. # Print the usage.
  46. usage () {
  47.     cat <<EOF
  48. Usage: $self [OPTION]
  49. Generate a grub config file
  50.  
  51.   -o, --output=FILE       output generated config to FILE [default=stdout]
  52.   -h, --help              print this message and exit
  53.   -v, --version           print the version information and exit
  54.  
  55. Report bugs to <bug-grub@gnu.org>.
  56. EOF
  57. }
  58.  
  59. argument () {
  60.   opt=$1
  61.   shift
  62.  
  63.   if test $# -eq 0; then
  64.       echo "$0: option requires an argument -- '$opt'" 1>&2
  65.       exit 1
  66.   fi
  67.   echo $1
  68. }
  69.  
  70. # Check the arguments.
  71. while test $# -gt 0
  72. do
  73.     option=$1
  74. :
  75.  shift
  76.  
  77.     case "$option" in
  78.     -h | --help)
  79.         usage
  80.         exit 0 ;;
  81.     -v | --version)
  82.         echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
  83.         exit 0 ;;
  84.     -o | --output)
  85.         grub_cfg=`argument $option "$@"`; shift;;
  86.     --output=*)
  87.         grub_cfg=`echo "$option" | sed 's/--output=//'`
  88.         ;;
  89.     -*)
  90.         echo "Unrecognized option \`$option'" 1>&2
  91.         usage
  92.         exit 1
  93.         ;;
  94.     # Explicitly ignore non-option arguments, for compatibility.
  95.     esac
  96. done
  97.  
  98. . "${datadir}/grub/grub-mkconfig_lib"
  99.  
  100. if [ "x$EUID" = "x" ] ; then
  101.   EUID=`id -u`
  102. fi
  103.  
  104. if [ "$EUID" != 0 ] ; then
  105.   root=f
  106.   case "`uname 2>/dev/null`" in
  107.     CYGWIN*)
  108.       # Cygwin: Assume root if member of admin group
  109.       for g in `id -G 2>/dev/null` ; do
  110.         case $g in
  111.           0|544) root=t ;;
  112.         esac
  113.       done ;;
  114. esac
  115.   if [ $root != t ] ; then
  116.     echo "$self: You must run this as root" >&2
  117.     exit 1
  118.   fi
  119. fi
  120.  
  121. set $grub_probe dummy
  122. if test -f "$1"; then
  123.     :
  124. else
  125.     echo "$1: Not found." 1>&2
  126.     exit 1
  127. fi
  128.  
  129. mkdir -p ${GRUB_PREFIX}
  130.  
  131. # Device containing our userland.  Typically used for root= parameter.
  132. GRUB_DEVICE="`${grub_probe} --target=device /`"
  133. GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
  134.  
  135. # Device containing our /boot partition.  Usually the same as GRUB_DEVICE.
  136. GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
  137. GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
  138.  
  139. # Filesystem for the device containing our userland.  Used for stuff like
  140. # choosing Hurd filesystem module.
  141. GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
  142.  
  143. if test -f ${sysconfdir}/default/grub ; then
  144.   . ${sysconfdir}/default/grub
  145. fi
  146.  
  147. # XXX: should this be deprecated at some point?
  148. if [ "x${GRUB_TERMINAL}" != "x" ] ; then
  149.   GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
  150.   GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
  151. fi
  152.  
  153. termoutdefault=0
  154. if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
  155.     GRUB_TERMINAL_OUTPUT=gfxterm;
  156.     termoutdefault=1;
  157. fi
  158.  
  159. for x in ${GRUB_TERMINAL_OUTPUT}; do
  160.     if [ "x${x}" = "xgfxterm" ]; then
  161.         if [ -n "$GRUB_FONT" ] ; then
  162.             if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
  163.                 GRUB_FONT_PATH=${GRUB_FONT}
  164.             else
  165.                 echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
  166.                 exit 1
  167.             fi
  168.         else
  169.             for dir in ${pkgdatadir} ${GRUB_PREFIX} /usr/share/grub ; do
  170.                 for basename in unicode unifont ascii; do
  171.                     path="${dir}/${basename}.pf2"
  172.                     if is_path_readable_by_grub ${path} > /dev/null ; then
  173.                         GRUB_FONT_PATH=${path}
  174.                     else
  175.                         continue
  176.                     fi
  177.                     if [ "${basename}" = "ascii" ] ; then
  178.                         # make sure all our children behave in conformance with ascii..
  179.                         export LANG=C
  180.                     fi
  181.                     break 2
  182.                 done
  183.             done
  184.         fi
  185.         if [ -z "${GRUB_FONT_PATH}" ] ; then
  186.             if [ "x$termoutdefault" != "x1" ]; then
  187.                 echo "No font for gfxterm found." >&2 ; exit 1
  188.             fi
  189.             GRUB_TERMINAL_OUTPUT=
  190.         fi
  191.     fi
  192. done
  193.  
  194. for x in ${GRUB_TERMINAL_OUTPUT}; do
  195.     case "x${x}" in
  196.         xgfxterm) ;;
  197.         xconsole | xserial | xofconsole)
  198.             # make sure all our children behave in conformance with ascii..
  199.             export LANG=C;;
  200.         *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
  201.     esac
  202. done
  203.  
  204. # These are defined in this script, export them here so that user can
  205. # override them.
  206. export GRUB_DEVICE \
  207.   GRUB_DEVICE_UUID \
  208.   GRUB_DEVICE_BOOT \
  209.   GRUB_DEVICE_BOOT_UUID \
  210.   GRUB_FS \
  211.   GRUB_FONT_PATH \
  212.   GRUB_PRELOAD_MODULES \
  213.   GRUB_PREFIX
  214.  
  215. # These are optional, user-defined variables.
  216. export GRUB_DEFAULT \
  217.   GRUB_HIDDEN_TIMEOUT \
  218.   GRUB_HIDDEN_TIMEOUT_QUIET \
  219.   GRUB_TIMEOUT \
  220.   GRUB_DEFAULT_BUTTON \
  221.   GRUB_HIDDEN_TIMEOUT_BUTTON \
  222.   GRUB_TIMEOUT_BUTTON \
  223.   GRUB_BUTTON_CMOS_ADDRESS \
  224.   GRUB_BUTTON_CMOS_CLEAN \
  225.   GRUB_DISTRIBUTOR \
  226.   GRUB_CMDLINE_LINUX \
  227.   GRUB_CMDLINE_LINUX_DEFAULT \
  228.   GRUB_CMDLINE_XEN \
  229.   GRUB_CMDLINE_XEN_DEFAULT \
  230.   GRUB_CMDLINE_LINUX_XEN_REPLACE \
  231.   GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT \
  232.   GRUB_CMDLINE_NETBSD \
  233.   GRUB_CMDLINE_NETBSD_DEFAULT \
  234.   GRUB_CMDLINE_GNUMACH \
  235.   GRUB_TERMINAL_INPUT \
  236.   GRUB_TERMINAL_OUTPUT \
  237.   GRUB_SERIAL_COMMAND \
  238.   GRUB_DISABLE_LINUX_UUID \
  239.   GRUB_DISABLE_RECOVERY \
  240.   GRUB_VIDEO_BACKEND \
  241.   GRUB_GFXMODE \
  242.   GRUB_BACKGROUND \
  243.   GRUB_THEME \
  244.   GRUB_GFXPAYLOAD_LINUX \
  245.   GRUB_DISABLE_OS_PROBER \
  246.   GRUB_INIT_TUNE \
  247.   GRUB_SAVEDEFAULT \
  248.   GRUB_BADRAM
  249.  
  250. if test "x${grub_cfg}" != "x"; then
  251.   rm -f ${grub_cfg}.new
  252.   exec > ${grub_cfg}.new
  253.  
  254.   # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
  255.   # firmware implementations (e.g. OFW or EFI).
  256.   chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
  257.   This means that if the generated config contains a password it is readable by everyone"
  258. fi
  259. echo "Generating grub.cfg ..." >&2
  260.  
  261. cat << EOF
  262. #
  263. # DO NOT EDIT THIS FILE
  264. #
  265. # It is automatically generated by $self using templates
  266. # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
  267. #
  268. EOF
  269.  
  270. for i in ${grub_mkconfig_dir}/* ; do
  271.   case "$i" in
  272.     # emacsen backup files. FIXME: support other editors
  273.     *~) ;;
  274.     # emacsen autosave files. FIXME: support other editors
  275.     \#*\#) ;;
  276.     *)
  277.       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
  278.         echo
  279.         echo "### BEGIN $i ###"
  280.         "$i"
  281.         echo "### END $i ###"
  282.       fi
  283.     ;;
  284.   esac
  285. done
  286.  
  287. if [ "x${grub_cfg}" != "x" ] && ! grep "^password " ${grub_cfg}.new >/dev/null; then
  288.   chmod 444 ${grub_cfg}.new || true
  289. fi
  290.  
  291. if test "x${grub_cfg}" != "x" ; then
  292.   if ! ${grub_script_check} ${grub_cfg}.new; then
  293.     echo "Syntax errors are detected in generated GRUB config file." >&2
  294.     echo "Ensure that there are no errors in /etc/default/grub" >&2
  295.     echo "and /etc/grub.d/* files or please file a bug report with" >&2
  296.     echo "${grub_cfg}.new file attached." >&2
  297.   else
  298.     # none of the children aborted with error, install the new grub.cfg
  299.     mv -f ${grub_cfg}.new ${grub_cfg}
  300.   fi
  301. fi
  302.  
  303. echo "done" >&2
RAW Paste Data
Top