grub-install.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #! /bin/sh
  2. # Install GRUB on your drive.
  3. # Copyright (C) 1999,2000,2001,2002,2003,2004,2005,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. # Initialize some variables.
  18. transform="@program_transform_name@"
  19. prefix=@prefix@
  20. exec_prefix=@exec_prefix@
  21. sbindir=@sbindir@
  22. bindir=@bindir@
  23. libdir=@libdir@
  24. PACKAGE_NAME=@PACKAGE_NAME@
  25. PACKAGE_TARNAME=@PACKAGE_TARNAME@
  26. PACKAGE_VERSION=@PACKAGE_VERSION@
  27. target_cpu=@target_cpu@
  28. platform=@platform@
  29. host_os=@host_os@
  30. font=@datadir@/@PACKAGE_TARNAME@/ascii.pf2
  31. pkglibdir=${libdir}/`echo ${PACKAGE_TARNAME}/${target_cpu}-${platform} | sed ${transform}`
  32. localedir=@datadir@/locale
  33. grub_setup=${sbindir}/`echo burg-setup | sed ${transform}`
  34. if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] || [ "${target_cpu}-${platform}" = "mips-yeeloong" ] ; then
  35. grub_mkimage=${bindir}/`echo burg-mkimage | sed ${transform}`
  36. else
  37. grub_mkimage=${bindir}/`echo burg-mkelfimage | sed ${transform}`
  38. fi
  39. grub_mkdevicemap=${sbindir}/`echo burg-mkdevicemap | sed ${transform}`
  40. grub_probe=${sbindir}/`echo burg-probe | sed ${transform}`
  41. grub_editenv=${bindir}/`echo burg-editenv | sed ${transform}`
  42. rootdir=
  43. grub_prefix=`echo /boot/burg | sed ${transform}`
  44. modules=
  45. install_device=
  46. no_floppy=
  47. force_lba=
  48. recheck=no
  49. debug=no
  50. if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
  51. disk_module=biosdisk
  52. elif [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
  53. disk_module=
  54. else
  55. disk_module=ata
  56. fi
  57. # Usage: usage
  58. # Print the usage.
  59. usage () {
  60. cat <<EOF
  61. Usage: burg-install [OPTION] install_device
  62. Install GRUB on your drive.
  63. -h, --help print this message and exit
  64. -v, --version print the version information and exit
  65. --modules=MODULES pre-load specified modules MODULES
  66. --root-directory=DIR install GRUB images under the directory DIR
  67. instead of the root directory
  68. --burg-setup=FILE use FILE as burg-setup
  69. --burg-mkimage=FILE use FILE as burg-mkimage
  70. --burg-mkdevicemap=FILE use FILE as burg-mkdevicemap
  71. --burg-probe=FILE use FILE as burg-probe
  72. --no-floppy do not probe any floppy drive
  73. --recheck probe a device map even if it already exists
  74. --force install even if problems are detected
  75. --alt alternative setup mode
  76. EOF
  77. if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
  78. cat <<EOF
  79. --disk-module=MODULE disk module to use
  80. EOF
  81. fi
  82. if [ "${target_cpu}-${platform}" = "mips-yeeloong" ] ; then
  83. cat <<EOF
  84. --font=FILE font file to use
  85. EOF
  86. fi
  87. cat <<EOF
  88. INSTALL_DEVICE can be a BURG device name or a system device filename.
  89. burg-install copies BURG images into /boot/burg (or /burg on NetBSD and
  90. OpenBSD), and uses burg-setup to install grub into the boot sector.
  91. If the --root-directory option is used, then burg-install will copy
  92. images into the operating system installation rooted at that directory.
  93. Report bugs to <bean123ch@gmail.com>.
  94. EOF
  95. }
  96. # Check the arguments.
  97. for option in "$@"; do
  98. case "$option" in
  99. -h | --help)
  100. usage
  101. exit 0 ;;
  102. -v | --version)
  103. echo "burg-install (${PACKAGE_NAME} ${PACKAGE_VERSION})"
  104. exit 0 ;;
  105. --modules=*)
  106. modules=`echo "$option" | sed 's/--modules=//'` ;;
  107. --font=*)
  108. font=`echo "$option" | sed 's/--font=//'` ;;
  109. --root-directory=*)
  110. rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
  111. --burg-setup=*)
  112. grub_setup=`echo "$option" | sed 's/--burg-setup=//'` ;;
  113. --burg-mkimage=*)
  114. grub_mkimage=`echo "$option" | sed 's/--burg-mkimage=//'` ;;
  115. --burg-mkdevicemap=*)
  116. grub_mkdevicemap=`echo "$option" | sed 's/--burg-mkdevicemap=//'` ;;
  117. --burg-probe=*)
  118. grub_probe=`echo "$option" | sed 's/--burg-probe=//'` ;;
  119. --no-floppy)
  120. no_floppy="--no-floppy" ;;
  121. --recheck)
  122. recheck=yes ;;
  123. --disk-module=*)
  124. if [ "${target_cpu}-${platform}" = "i386-pc" ] ; then
  125. disk_module=`echo "$option" | sed 's/--disk-module=//'`
  126. fi ;;
  127. # This is an undocumented feature...
  128. --debug)
  129. debug=yes ;;
  130. -f | --force)
  131. setup_force="--force" ;;
  132. --alt)
  133. setup_alt="--alt" ;;
  134. -*)
  135. echo "Unrecognized option \`$option'" 1>&2
  136. usage
  137. exit 1
  138. ;;
  139. *)
  140. if test "x$install_device" != x; then
  141. echo "More than one install_devices?" 1>&2
  142. usage
  143. exit 1
  144. fi
  145. install_device="${option}" ;;
  146. esac
  147. done
  148. # for make_system_path_relative_to_its_root()
  149. . ${libdir}/burg/burg-mkconfig_lib
  150. if test "x$install_device" = x && test "${target_cpu}-${platform}" != "mips-yeeloong"; then
  151. echo "install_device not specified." 1>&2
  152. usage
  153. exit 1
  154. fi
  155. # If the debugging feature is enabled, print commands.
  156. setup_verbose=
  157. if test $debug = yes; then
  158. set -x
  159. setup_verbose="--verbose"
  160. fi
  161. # Initialize these directories here, since ROOTDIR was initialized.
  162. case "$host_os" in
  163. netbsd* | openbsd*)
  164. # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
  165. # instead of /boot/grub.
  166. grub_prefix=`echo /burg | sed ${transform}`
  167. bootdir=${rootdir}
  168. ;;
  169. *)
  170. # Use /boot/grub by default.
  171. bootdir=${rootdir}/boot
  172. ;;
  173. esac
  174. grubdir=${bootdir}/`echo burg | sed ${transform}`
  175. device_map=${grubdir}/device.map
  176. grub_probe="${grub_probe} --device-map=${device_map}"
  177. # Check if GRUB is installed.
  178. if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
  179. set $grub_setup dummy
  180. if test -f "$1"; then
  181. :
  182. else
  183. echo "$1: Not found." 1>&2
  184. exit 1
  185. fi
  186. fi
  187. set $grub_mkimage dummy
  188. if test -f "$1"; then
  189. :
  190. else
  191. echo "$1: Not found." 1>&2
  192. exit 1
  193. fi
  194. set $grub_mkdevicemap dummy
  195. if test -f "$1"; then
  196. :
  197. else
  198. echo "$1: Not found." 1>&2
  199. exit 1
  200. fi
  201. # Create the GRUB directory if it is not present.
  202. mkdir -p "$grubdir" || exit 1
  203. # If --recheck is specified, remove the device map, if present.
  204. if test $recheck = yes; then
  205. rm -f $device_map
  206. fi
  207. # Create the device map file if it is not present.
  208. if test -f "$device_map"; then
  209. :
  210. else
  211. # Create a safe temporary file.
  212. test -n "$mklog" && log_file=`$mklog`
  213. $grub_mkdevicemap --device-map=$device_map $no_floppy || exit 1
  214. fi
  215. # Make sure that there is no duplicated entry.
  216. tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
  217. | sort | uniq -d | sed -n 1p`
  218. if test -n "$tmp"; then
  219. echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
  220. exit 1
  221. fi
  222. # Copy the GRUB images to the GRUB directory.
  223. for file in ${grubdir}/*.mod ${grubdir}/*.lst ${grubdir}/*.img ${grubdir}/efiemu??.o; do
  224. if test -f $file && [ "`basename $file`" != menu.lst ]; then
  225. rm -f $file || exit 1
  226. fi
  227. done
  228. for file in ${pkglibdir}/*.mod ${pkglibdir}/*.lst; do
  229. cp -f $file ${grubdir} || exit 1
  230. done
  231. if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
  232. for file in ${pkglibdir}/*.img ${pkglibdir}/efiemu??.o; do
  233. if test -f $file; then
  234. cp -f $file ${grubdir} || exit 1
  235. fi
  236. done
  237. fi
  238. # Copy gettext files
  239. mkdir -p ${grubdir}/locale/
  240. for dir in ${localedir}/*; do
  241. if test -f "$dir/LC_MESSAGES/grub.mo"; then
  242. cp -f "$dir/LC_MESSAGES/grub.mo" "${grubdir}/locale/${dir##*/}.mo"
  243. fi
  244. done
  245. # Write device to a variable so we don't have to traverse /dev every time.
  246. grub_device=`$grub_probe --target=device ${grubdir}` || exit 1
  247. if ! test -f ${grubdir}/burgenv; then
  248. $grub_editenv ${grubdir}/burgenv create
  249. fi
  250. # Create the core image. First, auto-detect the filesystem module.
  251. fs_module=`$grub_probe --target=fs --device ${grub_device}`
  252. if test "x$fs_module" = x -a "x$modules" = x; then
  253. echo "Auto-detection of a filesystem module failed." 1>&2
  254. echo "Please specify the module with the option \`--modules' explicitly." 1>&2
  255. exit 1
  256. fi
  257. # Then the partition map module. In order to support partition-less media,
  258. # this command is allowed to fail (--target=fs already grants us that the
  259. # filesystem will be accessible).
  260. partmap_module=
  261. for x in `$grub_probe --target=partmap --device ${grub_device} 2> /dev/null`; do
  262. partmap_module="$partmap_module part_$x";
  263. done
  264. # Device abstraction module, if any (lvm, raid).
  265. devabstraction_module=`$grub_probe --target=abstraction --device ${grub_device}`
  266. # The order in this list is critical. Be careful when modifying it.
  267. modules="$modules $disk_module"
  268. modules="$modules $fs_module $partmap_module $devabstraction_module"
  269. relative_grubdir=`make_system_path_relative_to_its_root ${grubdir}` || exit 1
  270. if [ "x${relative_grubdir}" = "x" ] ; then
  271. relative_grubdir=/
  272. fi
  273. prefix_drive=
  274. config_opt=
  275. if [ "x${devabstraction_module}" = "x" ] ; then
  276. if [ x"${install_device}" != x ]; then
  277. if echo "${install_device}" | grep -qx "(.*)" ; then
  278. install_drive="${install_device}"
  279. else
  280. install_drive="`$grub_probe --target=drive --device ${install_device}`" || exit 1
  281. fi
  282. install_drive="`echo ${install_drive} | sed -e s/,[0-9]*[a-z]*//g`"
  283. fi
  284. grub_drive="`$grub_probe --target=drive --device ${grub_device}`" || exit 1
  285. # Strip partition number
  286. grub_drive="`echo ${grub_drive} | sed -e s/,[0-9]*[a-z]*//g`"
  287. if [ "$disk_module" = ata ] ; then
  288. # generic method (used on coreboot and ata mod)
  289. uuid="`$grub_probe --target=fs_uuid --device ${grub_device}`"
  290. if [ "x${uuid}" = "x" ] ; then
  291. echo "UUID needed with ata mod, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
  292. exit 1
  293. fi
  294. echo "search.fs_uuid ${uuid} root " > ${grubdir}/load.cfg
  295. echo 'set prefix=($root)'"${relative_grubdir}" >> ${grubdir}/load.cfg
  296. config_opt="-c ${grubdir}/load.cfg "
  297. modules="$modules search_fs_uuid"
  298. elif [ "x${grub_drive}" != "x${install_drive}" ] ; then
  299. uuid="`$grub_probe --target=fs_uuid --device ${grub_device}`"
  300. if [ "x${uuid}" = "x" ] ; then
  301. echo "You attempted a cross-disk install, but the filesystem containing ${grubdir} does not support UUIDs." 1>&2
  302. exit 1
  303. fi
  304. echo "search.fs_uuid ${uuid} root " > ${grubdir}/load.cfg
  305. echo 'set prefix=($root)'"${relative_grubdir}" >> ${grubdir}/load.cfg
  306. config_opt="-c ${grubdir}/load.cfg "
  307. modules="$modules search_fs_uuid"
  308. fi
  309. else
  310. prefix_drive=`$grub_probe --target=drive --device ${grub_device}` || exit 1
  311. fi
  312. if [ "${target_cpu}-${platform}" = "i386-pc" ] || [ "${target_cpu}-${platform}" = "sparc64-ieee1275" ] ; then
  313. $grub_mkimage ${config_opt} --output=${grubdir}/core.img --prefix=${prefix_drive}${relative_grubdir} $modules || exit 1
  314. # Now perform the installation.
  315. $grub_setup ${setup_verbose} ${setup_force} ${setup_alt} --directory=${grubdir} --device-map=${device_map} \
  316. ${install_device} || exit 1
  317. elif [ "${target_cpu}-${platform}" = "mips-yeeloong" ] ; then
  318. $grub_mkimage ${config_opt} -f ${font} -d ${pkglibdir} -O elf --output=/boot/burg.elf --prefix=${prefix_drive}${relative_grubdir} $modules || exit 1
  319. else
  320. $grub_mkimage ${config_opt} -d ${pkglibdir} --output=/boot/multiboot.img --prefix=${prefix_drive}${relative_grubdir} $modules || exit 1
  321. fi
  322. echo "Installation finished. No error reported."
  323. # Bye.
  324. exit 0