grub-mkconfig.in 7.9 KB

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