barjac

grub2-mkconfig (labels)

Oct 27th, 2010
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.65 KB | None | 0 0
  1. #! /bin/sh -e
  2.  
  3. # Generate grub.cfg by inspecting /boot contents.
  4. # Copyright (C) 2006,2007,2008,2009 Free Software Foundation, Inc.
  5. #
  6. # GRUB is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # GRUB is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  18.  
  19. transform="s&^&&;s,grub,grub2,"
  20.  
  21. prefix=/usr
  22. exec_prefix=/usr
  23. sbindir=/usr/sbin
  24. libdir=/usr/lib
  25. sysconfdir=/etc
  26. package_version=1.98
  27. datarootdir=${prefix}/share
  28. datadir=/usr/share
  29. pkgdatadir=${datadir}/`echo grub | sed "${transform}"`
  30. grub_prefix=`echo /boot/grub | sed ${transform}`
  31. grub_cfg=""
  32. grub_mkconfig_dir=${sysconfdir}/grub.d
  33.  
  34. grub_mkdevicemap=${sbindir}/`echo grub-mkdevicemap | sed ${transform}`
  35. grub_probe=${sbindir}/`echo grub-probe | sed ${transform}`
  36.  
  37. # Usage: usage
  38. # Print the usage.
  39. usage () {
  40.     cat <<EOF
  41. Usage: $0 [OPTION]
  42. Generate a grub config file
  43.  
  44.   -o, --output=FILE       output generated config to FILE [default=stdout]
  45.   -l, --root_label        use a label for the root device if possible        
  46.   -h, --help              print this message and exit
  47.   -v, --version           print the version information and exit
  48.  
  49. Report bugs to <bug-grub@gnu.org>.
  50. EOF
  51. }
  52.  
  53. # Check the arguments.
  54. for option in "$@"; do
  55.     case "$option" in
  56.     -h | --help)
  57.     usage
  58.     exit 0 ;;
  59.     -v | --version)
  60.     echo "$0 (GNU GRUB ${package_version})"
  61.     exit 0 ;;
  62.     -l | --label)
  63.         shift
  64.     GRUB_USE_LABEL="true"
  65.     ;;
  66.     -o)
  67.     shift
  68.     grub_cfg=$1
  69.     ;;
  70.     --output=*)
  71.     grub_cfg=`echo "$option" | sed 's/--output=//'`
  72.     ;;
  73.          -*)
  74.     echo "Unrecognized option \`$option'" 1>&2
  75.     usage
  76.     exit 1
  77.     ;;
  78.    esac
  79. done
  80.  
  81. . ${libdir}/grub/grub-mkconfig_lib
  82.  
  83. if [ "x$EUID" = "x" ] ; then
  84.  EUID=`id -u`
  85. fi
  86.  
  87. if [ "$EUID" != 0 ] ; then
  88.  root=f
  89.  case "`uname 2>/dev/null`" in
  90.    CYGWIN*)
  91.      # Cygwin: Assume root if member of admin group
  92.      for g in `id -G 2>/dev/null` ; do
  93.     case $g in
  94.       0|544) root=t ;;
  95.     esac
  96.      done ;;
  97.  esac
  98.  if [ $root != t ] ; then
  99.    echo "$0: You must run this as root" >&2
  100.    exit 1
  101.  fi
  102. fi
  103.  
  104. set $grub_mkdevicemap dummy
  105. if test -f "$1"; then
  106.    :
  107. else
  108.    echo "$1: Not found." 1>&2
  109.    exit 1
  110. fi
  111.  
  112. set $grub_probe dummy
  113. if test -f "$1"; then
  114.    :
  115. else
  116.    echo "$1: Not found." 1>&2
  117.    exit 1
  118. fi
  119.  
  120. mkdir -p ${grub_prefix}
  121.  
  122. if test -e ${grub_prefix}/device.map ; then : ; else
  123.  ${grub_mkdevicemap}
  124. fi
  125.  
  126. # Device containing our userland.  Typically used for root= parameter.
  127. GRUB_DEVICE="`${grub_probe} --target=device /`"
  128. GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
  129. # In 1.98 "grub-probe --target=fs_label" is not supported so using blkid  (bcj)
  130. # GRUB_DEVICE_LABEL="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2> /dev/null`" || true  (bcj)
  131. if blkid ${GRUB_DEVICE}| grep -q "LABEL=";then
  132. GRUB_DEVICE_LABEL=$(blkid ${GRUB_DEVICE}| grep "LABEL=" |tr -s 'LABEL=' '*' | cut -d* -f2 | cut -d'"' -f2)
  133. fi
  134. # Device containing our /boot partition.  Usually the same as GRUB_DEVICE.
  135. GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
  136. GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
  137.  
  138. # Filesystem for the device containing our userland.  Used for stuff like
  139. # choosing Hurd filesystem module.
  140. GRUB_FS="`${grub_probe} --target=fs / 2> /dev/null || echo unknown`"
  141.  
  142. if test -f ${sysconfdir}/default/grub ; then
  143.   . ${sysconfdir}/default/grub
  144. fi
  145.  
  146. # XXX: should this be deprecated at some point?
  147. if [ "x${GRUB_TERMINAL}" != "x" ] ; then
  148.   GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
  149.   GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
  150. fi
  151.  
  152. case x${GRUB_TERMINAL_OUTPUT} in
  153.   x | xgfxterm)
  154.     # If this platform supports gfxterm, try to use it.
  155.     if test -e ${grub_prefix}/gfxterm.mod ; then
  156.       # FIXME: this should do something smarter than just loading first
  157.       # video backend.
  158.       GRUB_VIDEO_BACKEND=$(head -n 1 ${grub_prefix}/video.lst || true)
  159.       if [ -n "${GRUB_VIDEO_BACKEND}" ] ; then
  160.         GRUB_TERMINAL_OUTPUT=gfxterm
  161.       elif [ "${GRUB_TERMINAL_OUTPUT}" = "gfxterm" ] ; then
  162.         echo "No suitable backend could be found for gfxterm." >&2 ; exit 1
  163.       fi
  164.     fi
  165.   ;;
  166.   xconsole | xserial | xofconsole) ;;
  167.   *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
  168. esac
  169.  
  170. # check for terminals that require fonts
  171. case ${GRUB_TERMINAL_OUTPUT} in
  172.   gfxterm)
  173.     if [ -n "$GRUB_FONT" ] ; then
  174.       if is_path_readable_by_grub ${GRUB_FONT} > /dev/null ; then
  175.         GRUB_FONT_PATH=${GRUB_FONT}
  176.     else
  177.       echo "No such font or not readable by grub: ${GRUB_FONT}" >&2
  178.       exit 1
  179.     fi
  180.     else
  181.       for dir in ${pkgdatadir} /boot/grub /usr/share/grub ; do
  182.         for basename in unicode unifont ascii; do
  183.       path="${dir}/${basename}.pf2"
  184.           if is_path_readable_by_grub ${path} > /dev/null ; then
  185.         GRUB_FONT_PATH=${path}
  186.       else
  187.         continue
  188.       fi
  189.       if [ "${basename}" = "ascii" ] ; then
  190.         # make sure all our children behave in conformance with ascii..
  191.         export LANG=C
  192.       fi
  193.       break 2
  194.         done
  195.       done
  196.     fi
  197.     if [ -z "${GRUB_FONT_PATH}" ] ; then
  198.       # fallback to the native terminal for this platform
  199.       unset GRUB_TERMINAL_OUTPUT
  200.     fi
  201.   ;;
  202.   *)
  203.     # make sure all our children behave in conformance with ascii..
  204.     export LANG=C
  205. esac
  206.  
  207. # These are defined in this script, export them here so that user can
  208. # override them.
  209. export GRUB_DEVICE \
  210.   GRUB_DEVICE_UUID \
  211.   GRUB_DEVICE_LABEL \
  212.   GRUB_USE_LABEL \
  213.   GRUB_DEVICE_BOOT \
  214.   GRUB_DEVICE_BOOT_UUID \
  215.   GRUB_FS \
  216.   GRUB_FONT_PATH \
  217.   GRUB_PRELOAD_MODULES \
  218.   GRUB_VIDEO_BACKEND
  219.  
  220. # These are optional, user-defined variables.
  221. export GRUB_DEFAULT \
  222.   GRUB_HIDDEN_TIMEOUT \
  223.   GRUB_HIDDEN_TIMEOUT_QUIET \
  224.   GRUB_TIMEOUT \
  225.   GRUB_DISTRIBUTOR \
  226.   GRUB_CMDLINE_LINUX \
  227.   GRUB_CMDLINE_LINUX_DEFAULT \
  228.   GRUB_TERMINAL_INPUT \
  229.   GRUB_TERMINAL_OUTPUT \
  230.   GRUB_SERIAL_COMMAND \
  231.   GRUB_DISABLE_LINUX_UUID \
  232.   GRUB_DISABLE_LINUX_RECOVERY \
  233.   GRUB_GFXMODE \
  234.   GRUB_THEME \
  235.   GRUB_GFXPAYLOAD_LINUX \
  236.   GRUB_DISABLE_OS_PROBER \
  237.   GRUB_INIT_TUNE \
  238.   GRUB_SAVEDEFAULT
  239.  
  240.  
  241.  
  242. if test "x${grub_cfg}" != "x"; then
  243.   rm -f ${grub_cfg}.new
  244.   exec > ${grub_cfg}.new
  245.  
  246.   # Allow this to fail, since /boot/grub/ might need to be fatfs to support some
  247.   # firmware implementations (e.g. OFW or EFI).
  248.   chmod 400 ${grub_cfg}.new || grub_warn "Could not make ${grub_cfg}.new readable by only root.\
  249.  This means that if the generated config contains a password it is readable by everyone"
  250. fi
  251. echo "Generating grub.cfg ..." >&2
  252.  
  253. cat << EOF
  254. #
  255. # DO NOT EDIT THIS FILE
  256. #
  257. # It is automatically generated by $0 using templates
  258. # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
  259. #
  260. EOF
  261.  
  262. for i in ${grub_mkconfig_dir}/* ; do
  263.   case "$i" in
  264.     # emacsen backup files. FIXME: support other editors
  265.     *~) ;;
  266.     *)
  267.       if grub_file_is_not_garbage "$i" && test -x "$i" ; then
  268.         echo
  269.         echo "### BEGIN $i ###"
  270.         "$i"
  271.         echo "### END $i ###"
  272.       fi
  273.     ;;
  274.   esac
  275. done
  276.  
  277. if test "x${grub_cfg}" != "x" ; then
  278.   # none of the children aborted with error, install the new grub.cfg
  279.   mv -f ${grub_cfg}.new ${grub_cfg}
  280. fi
  281.  
  282. echo "done" >&2
Add Comment
Please, Sign In to add comment