grub-mkconfig 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #! /bin/sh
  2. set -e
  3. # Generate grub.cfg by inspecting /boot contents.
  4. # Copyright (C) 2006,2007,2008,2009,2010 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. datarootdir="/share"
  19. prefix=""
  20. exec_prefix=""
  21. sbindir="/sbin"
  22. bindir="/bin"
  23. sysconfdir="/etc"
  24. PACKAGE_NAME=GRUB
  25. PACKAGE_VERSION=2.02~rc2
  26. host_os=gnu-linux
  27. datadir="/"
  28. if [ "x$pkgdatadir" = x ]; then
  29. pkgdatadir="${datadir}/@PACKAGE@"
  30. fi
  31. # export it for scripts
  32. export pkgdatadir
  33. grub_cfg=""
  34. grub_mkconfig_dir="${sysconfdir}"/grub.d
  35. self=`basename $0`
  36. grub_probe="${sbindir}/@grub_probe@"
  37. grub_file="${bindir}/@grub_file@"
  38. grub_editenv="${bindir}/@grub_editenv@"
  39. grub_script_check="${bindir}/@grub_script_check@"
  40. export TEXTDOMAIN=@PACKAGE@
  41. export TEXTDOMAINDIR="@localedir@"
  42. . "${pkgdatadir}/grub-mkconfig_lib"
  43. # Usage: usage
  44. # Print the usage.
  45. usage () {
  46. gettext_printf "Usage: %s [OPTION]\n" "$self"
  47. gettext "Generate a grub config file"; echo
  48. echo
  49. print_option_help "-o, --output=$(gettext FILE)" "$(gettext "output generated config to FILE [default=stdout]")"
  50. print_option_help "-h, --help" "$(gettext "print this message and exit")"
  51. print_option_help "-v, --version" "$(gettext "print the version information and exit")"
  52. echo
  53. gettext "Report bugs to <bug-grub@gnu.org>."; echo
  54. }
  55. argument () {
  56. opt=$1
  57. shift
  58. if test $# -eq 0; then
  59. gettext_printf "%s: option requires an argument -- \`%s'\n" "$self" "$opt" 1>&2
  60. exit 1
  61. fi
  62. echo $1
  63. }
  64. # Check the arguments.
  65. while test $# -gt 0
  66. do
  67. option=$1
  68. shift
  69. case "$option" in
  70. -h | --help)
  71. usage
  72. exit 0 ;;
  73. -V | --version)
  74. echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
  75. exit 0 ;;
  76. -o | --output)
  77. grub_cfg=`argument $option "$@"`; shift;;
  78. --output=*)
  79. grub_cfg=`echo "$option" | sed 's/--output=//'`
  80. ;;
  81. -*)
  82. gettext_printf "Unrecognized option \`%s'\n" "$option" 1>&2
  83. usage
  84. exit 1
  85. ;;
  86. # Explicitly ignore non-option arguments, for compatibility.
  87. esac
  88. done
  89. if [ "x$EUID" = "x" ] ; then
  90. EUID=`id -u`
  91. fi
  92. if [ "$EUID" != 0 ] ; then
  93. root=f
  94. case "`uname 2>/dev/null`" in
  95. CYGWIN*)
  96. # Cygwin: Assume root if member of admin group
  97. for g in `id -G 2>/dev/null` ; do
  98. case $g in
  99. 0|544) root=t ;;
  100. esac
  101. done ;;
  102. esac
  103. if [ $root != t ] ; then
  104. gettext_printf "%s: You must run this as root\n" "$self" >&2
  105. exit 1
  106. fi
  107. fi
  108. set $grub_probe dummy
  109. if test -f "$1"; then
  110. :
  111. else
  112. gettext_printf "%s: Not found.\n" "$1" 1>&2
  113. exit 1
  114. fi
  115. # Device containing our userland. Typically used for root= parameter.
  116. GRUB_DEVICE="`${grub_probe} --target=device /`"
  117. GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
  118. # Device containing our /boot partition. Usually the same as GRUB_DEVICE.
  119. GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
  120. GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
  121. # Filesystem for the device containing our userland. Used for stuff like
  122. # choosing Hurd filesystem module.
  123. GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
  124. if [ x"$GRUB_FS" = xunknown ]; then
  125. GRUB_FS="$(stat -f --printf=%T / || echo unknown)"
  126. fi
  127. if test -f ${sysconfdir}/default/grub ; then
  128. . ${sysconfdir}/default/grub
  129. fi
  130. # XXX: should this be deprecated at some point?
  131. if [ "x${GRUB_TERMINAL}" != "x" ] ; then
  132. GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
  133. GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
  134. fi
  135. termoutdefault=0
  136. if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
  137. GRUB_TERMINAL_OUTPUT=gfxterm;
  138. termoutdefault=1;
  139. fi
  140. for x in ${GRUB_TERMINAL_OUTPUT}; do
  141. case "x${x}" in
  142. xgfxterm) ;;
  143. xconsole | xserial | xofconsole | xvga_text)
  144. # make sure all our children behave in conformance with ascii..
  145. export LANG=C;;
  146. *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
  147. esac
  148. done
  149. GRUB_ACTUAL_DEFAULT="$GRUB_DEFAULT"
  150. if [ "x${GRUB_ACTUAL_DEFAULT}" = "xsaved" ] ; then GRUB_ACTUAL_DEFAULT="`"${grub_editenv}" - list | sed -n '/^saved_entry=/ s,^saved_entry=,,p'`" ; fi
  151. # These are defined in this script, export them here so that user can
  152. # override them.
  153. export GRUB_DEVICE \
  154. GRUB_DEVICE_UUID \
  155. GRUB_DEVICE_BOOT \
  156. GRUB_DEVICE_BOOT_UUID \
  157. GRUB_FS \
  158. GRUB_FONT \
  159. GRUB_PRELOAD_MODULES \
  160. GRUB_ACTUAL_DEFAULT
  161. # These are optional, user-defined variables.
  162. export GRUB_DEFAULT \
  163. GRUB_HIDDEN_TIMEOUT \
  164. GRUB_HIDDEN_TIMEOUT_QUIET \
  165. GRUB_TIMEOUT \
  166. GRUB_TIMEOUT_STYLE \
  167. GRUB_DEFAULT_BUTTON \
  168. GRUB_HIDDEN_TIMEOUT_BUTTON \
  169. GRUB_TIMEOUT_BUTTON \
  170. GRUB_TIMEOUT_STYLE_BUTTON \
  171. GRUB_BUTTON_CMOS_ADDRESS \
  172. GRUB_BUTTON_CMOS_CLEAN \
  173. GRUB_DISTRIBUTOR \
  174. GRUB_CMDLINE_LINUX \
  175. GRUB_CMDLINE_LINUX_DEFAULT \
  176. GRUB_CMDLINE_XEN \
  177. GRUB_CMDLINE_XEN_DEFAULT \
  178. GRUB_CMDLINE_LINUX_XEN_REPLACE \
  179. GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT \
  180. GRUB_CMDLINE_NETBSD \
  181. GRUB_CMDLINE_NETBSD_DEFAULT \
  182. GRUB_CMDLINE_GNUMACH \
  183. GRUB_TERMINAL_INPUT \
  184. GRUB_TERMINAL_OUTPUT \
  185. GRUB_SERIAL_COMMAND \
  186. GRUB_DISABLE_LINUX_UUID \
  187. GRUB_DISABLE_RECOVERY \
  188. GRUB_VIDEO_BACKEND \
  189. GRUB_GFXMODE \
  190. GRUB_BACKGROUND \
  191. GRUB_THEME \
  192. GRUB_GFXPAYLOAD_LINUX \
  193. GRUB_DISABLE_OS_PROBER \
  194. GRUB_INIT_TUNE \
  195. GRUB_SAVEDEFAULT \
  196. GRUB_ENABLE_CRYPTODISK \
  197. GRUB_BADRAM \
  198. GRUB_OS_PROBER_SKIP_LIST \
  199. GRUB_DISABLE_SUBMENU
  200. if test "x${grub_cfg}" != "x"; then
  201. rm -f "${grub_cfg}.new"
  202. oldumask=$(umask); umask 077
  203. exec > "${grub_cfg}.new"
  204. umask $oldumask
  205. fi
  206. gettext "Generating grub configuration file ..." >&2
  207. echo >&2
  208. cat << EOF
  209. #
  210. # DO NOT EDIT THIS FILE
  211. #
  212. # It is automatically generated by $self using templates
  213. # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
  214. #
  215. EOF
  216. for i in "${grub_mkconfig_dir}"/* ; do
  217. case "$i" in
  218. # emacsen backup files. FIXME: support other editors
  219. *~) ;;
  220. # emacsen autosave files. FIXME: support other editors
  221. */\#*\#) ;;
  222. *)
  223. if grub_file_is_not_garbage "$i" && test -x "$i" ; then
  224. echo
  225. echo "### BEGIN $i ###"
  226. "$i"
  227. echo "### END $i ###"
  228. fi
  229. ;;
  230. esac
  231. done
  232. if test "x${grub_cfg}" != "x" ; then
  233. if ! ${grub_script_check} ${grub_cfg}.new; then
  234. # TRANSLATORS: %s is replaced by filename
  235. gettext_printf "Syntax errors are detected in generated GRUB config file.
  236. Ensure that there are no errors in /etc/default/grub
  237. and /etc/grub.d/* files or please file a bug report with
  238. %s file attached." "${grub_cfg}.new" >&2
  239. echo >&2
  240. exit 1
  241. else
  242. # none of the children aborted with error, install the new grub.cfg
  243. mv -f ${grub_cfg}.new ${grub_cfg}
  244. fi
  245. fi
  246. gettext "done" >&2
  247. echo >&2