grub-mkconfig.in 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. GRUB_DEVICE_PARTUUID="`${grub_probe} --device ${GRUB_DEVICE} --target=partuuid 2> /dev/null`" || true
  121. # Device containing our /boot partition. Usually the same as GRUB_DEVICE.
  122. GRUB_DEVICE_BOOT="`${grub_probe} --target=device /boot`"
  123. GRUB_DEVICE_BOOT_UUID="`${grub_probe} --device ${GRUB_DEVICE_BOOT} --target=fs_uuid 2> /dev/null`" || true
  124. # Disable os-prober by default due to security reasons.
  125. GRUB_DISABLE_OS_PROBER="true"
  126. # Filesystem for the device containing our userland. Used for stuff like
  127. # choosing Hurd filesystem module.
  128. GRUB_FS="`${grub_probe} --device ${GRUB_DEVICE} --target=fs 2> /dev/null || echo unknown`"
  129. if [ x"$GRUB_FS" = xunknown ]; then
  130. GRUB_FS="$(stat -f -c %T / || echo unknown)"
  131. fi
  132. # Provide a default set of stock linux early initrd images.
  133. # Define here so the list can be modified in the sourced config file.
  134. if [ "x${GRUB_EARLY_INITRD_LINUX_STOCK}" = "x" ]; then
  135. GRUB_EARLY_INITRD_LINUX_STOCK="intel-uc.img intel-ucode.img amd-uc.img amd-ucode.img early_ucode.cpio microcode.cpio"
  136. fi
  137. if test -f ${sysconfdir}/default/grub ; then
  138. . ${sysconfdir}/default/grub
  139. fi
  140. if [ "x${GRUB_DISABLE_UUID}" = "xtrue" ]; then
  141. if [ -z "${GRUB_DISABLE_LINUX_UUID}" ]; then
  142. GRUB_DISABLE_LINUX_UUID="true"
  143. fi
  144. if [ -z "${GRUB_DISABLE_LINUX_PARTUUID}" ]; then
  145. GRUB_DISABLE_LINUX_PARTUUID="true"
  146. fi
  147. fi
  148. # XXX: should this be deprecated at some point?
  149. if [ "x${GRUB_TERMINAL}" != "x" ] ; then
  150. GRUB_TERMINAL_INPUT="${GRUB_TERMINAL}"
  151. GRUB_TERMINAL_OUTPUT="${GRUB_TERMINAL}"
  152. fi
  153. termoutdefault=0
  154. if [ "x${GRUB_TERMINAL_OUTPUT}" = "x" ]; then
  155. GRUB_TERMINAL_OUTPUT=gfxterm;
  156. termoutdefault=1;
  157. fi
  158. for x in ${GRUB_TERMINAL_OUTPUT}; do
  159. case "x${x}" in
  160. xgfxterm) ;;
  161. xconsole | xserial | xofconsole | xvga_text)
  162. # make sure all our children behave in conformance with ascii..
  163. export LANG=C;;
  164. *) echo "Invalid output terminal \"${GRUB_TERMINAL_OUTPUT}\"" >&2 ; exit 1 ;;
  165. esac
  166. done
  167. GRUB_ACTUAL_DEFAULT="$GRUB_DEFAULT"
  168. if [ "x${GRUB_ACTUAL_DEFAULT}" = "xsaved" ] ; then GRUB_ACTUAL_DEFAULT="`"${grub_editenv}" - list | sed -n '/^saved_entry=/ s,^saved_entry=,,p'`" ; fi
  169. # These are defined in this script, export them here so that user can
  170. # override them.
  171. export GRUB_DEVICE \
  172. GRUB_DEVICE_UUID \
  173. GRUB_DEVICE_PARTUUID \
  174. GRUB_DEVICE_BOOT \
  175. GRUB_DEVICE_BOOT_UUID \
  176. GRUB_DISABLE_OS_PROBER \
  177. GRUB_FS \
  178. GRUB_FONT \
  179. GRUB_PRELOAD_MODULES \
  180. GRUB_ACTUAL_DEFAULT
  181. # These are optional, user-defined variables.
  182. export GRUB_DEFAULT \
  183. GRUB_HIDDEN_TIMEOUT \
  184. GRUB_HIDDEN_TIMEOUT_QUIET \
  185. GRUB_TIMEOUT \
  186. GRUB_TIMEOUT_STYLE \
  187. GRUB_DEFAULT_BUTTON \
  188. GRUB_HIDDEN_TIMEOUT_BUTTON \
  189. GRUB_TIMEOUT_BUTTON \
  190. GRUB_TIMEOUT_STYLE_BUTTON \
  191. GRUB_BUTTON_CMOS_ADDRESS \
  192. GRUB_BUTTON_CMOS_CLEAN \
  193. GRUB_DISTRIBUTOR \
  194. GRUB_CMDLINE_LINUX \
  195. GRUB_CMDLINE_LINUX_DEFAULT \
  196. GRUB_CMDLINE_LINUX_RECOVERY \
  197. GRUB_CMDLINE_XEN \
  198. GRUB_CMDLINE_XEN_DEFAULT \
  199. GRUB_CMDLINE_LINUX_XEN_REPLACE \
  200. GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT \
  201. GRUB_CMDLINE_NETBSD \
  202. GRUB_CMDLINE_NETBSD_DEFAULT \
  203. GRUB_CMDLINE_GNUMACH \
  204. GRUB_TOP_LEVEL \
  205. GRUB_TOP_LEVEL_XEN \
  206. GRUB_TOP_LEVEL_OS_PROBER \
  207. GRUB_EARLY_INITRD_LINUX_CUSTOM \
  208. GRUB_EARLY_INITRD_LINUX_STOCK \
  209. GRUB_TERMINAL_INPUT \
  210. GRUB_TERMINAL_OUTPUT \
  211. GRUB_SERIAL_COMMAND \
  212. GRUB_DISABLE_UUID \
  213. GRUB_DISABLE_LINUX_UUID \
  214. GRUB_DISABLE_LINUX_PARTUUID \
  215. GRUB_DISABLE_RECOVERY \
  216. GRUB_VIDEO_BACKEND \
  217. GRUB_GFXMODE \
  218. GRUB_BACKGROUND \
  219. GRUB_THEME \
  220. GRUB_GFXPAYLOAD_LINUX \
  221. GRUB_INIT_TUNE \
  222. GRUB_SAVEDEFAULT \
  223. GRUB_ENABLE_CRYPTODISK \
  224. GRUB_BADRAM \
  225. GRUB_OS_PROBER_SKIP_LIST \
  226. GRUB_DISABLE_SUBMENU
  227. if test "x${grub_cfg}" != "x"; then
  228. rm -f "${grub_cfg}.new"
  229. oldumask=$(umask); umask 077
  230. exec > "${grub_cfg}.new"
  231. umask $oldumask
  232. fi
  233. gettext "Generating grub configuration file ..." >&2
  234. echo >&2
  235. cat << EOF
  236. #
  237. # DO NOT EDIT THIS FILE
  238. #
  239. # It is automatically generated by $self using templates
  240. # from ${grub_mkconfig_dir} and settings from ${sysconfdir}/default/grub
  241. #
  242. EOF
  243. for i in "${grub_mkconfig_dir}"/* ; do
  244. case "$i" in
  245. # emacsen backup files. FIXME: support other editors
  246. *~) ;;
  247. # emacsen autosave files. FIXME: support other editors
  248. */\#*\#) ;;
  249. *)
  250. if grub_file_is_not_garbage "$i" && test -x "$i" ; then
  251. echo
  252. echo "### BEGIN $i ###"
  253. "$i"
  254. echo "### END $i ###"
  255. fi
  256. ;;
  257. esac
  258. done
  259. if test "x${grub_cfg}" != "x" ; then
  260. if ! ${grub_script_check} ${grub_cfg}.new; then
  261. # TRANSLATORS: %s is replaced by filename
  262. gettext_printf "Syntax errors are detected in generated GRUB config file.
  263. Ensure that there are no errors in /etc/default/grub
  264. and /etc/grub.d/* files or please file a bug report with
  265. %s file attached." "${grub_cfg}.new" >&2
  266. echo >&2
  267. exit 1
  268. else
  269. # none of the children aborted with error, install the new grub.cfg
  270. oldumask=$(umask)
  271. umask 077
  272. cat ${grub_cfg}.new > ${grub_cfg}
  273. umask $oldumask
  274. rm -f ${grub_cfg}.new
  275. fi
  276. fi
  277. gettext "done" >&2
  278. echo >&2