grub-mkconfig.in 7.2 KB

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