grub-image.in 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #! /bin/sh
  2. # grub-image - Create a GRUB boot filesystem image and tarball
  3. # Gordon Matzigkeit <gord@fig.org>, 2000-07-25
  4. #
  5. # Copyright (C) 2000, 2002 Free Software Foundation, Inc.
  6. #
  7. # This file is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. prefix=@prefix@
  21. exec_prefix=@exec_prefix@
  22. sbindir=@sbindir@
  23. libdir=@libdir@
  24. PACKAGE=@PACKAGE@
  25. host_cpu=@host_cpu@
  26. host_os=@host_os@
  27. host_vendor=@host_vendor@
  28. context=${host_cpu}-${host_vendor}
  29. pkglibdir=${libdir}/${PACKAGE}/${context}
  30. mke2fs=`which mke2fs`
  31. progname=`echo "$0" | sed 's%^.*/%%'`
  32. thisdir=`echo "$0" | sed 's%/[^/]*$%%'`
  33. test "X$thisdir" = "X$0" && thisdir=.
  34. # See if we were invoked from within the build directory, and if so,
  35. # use the built files rather than the installed ones.
  36. if test -f $thisdir/../stage2/stage2; then
  37. grub_shell="$thisdir/../grub/grub"
  38. stage1dir="$thisdir/../stage1"
  39. stage2dir="$thisdir/../stage2"
  40. else
  41. grub_shell=${sbindir}/grub
  42. stage1dir="$pkglibdir"
  43. stage2dir="$pkglibdir"
  44. fi
  45. # Exit on any error.
  46. set -e
  47. # Get GRUB's version from the Grub shell, since we use the
  48. # installed files.
  49. VERSION=`$grub_shell --version | sed -e 's/^.* \([0-9.]*\).*$/\1/'`
  50. test "X$VERSION" != X
  51. bootdir=${PACKAGE}-${VERSION}-${context}
  52. image=$bootdir.ext2fs
  53. # Create the tarball.
  54. if test ! -f $bootdir.tar.gz; then
  55. echo "# Creating \`$bootdir.tar.gz'"
  56. mkdir -p $bootdir/boot/grub
  57. cp -p $stage1dir/stage1 $stage2dir/*_stage1_5 $stage2dir/stage2 \
  58. $bootdir/boot/grub
  59. test ! -f menu.lst || cp -p menu.lst $bootdir/boot/grub
  60. trap "rm -f $bootdir.tar.gz" 0
  61. GZIP=-9 tar -zcf $bootdir.tar.gz $bootdir
  62. trap '' 0
  63. rm -rf $bootdir
  64. fi
  65. # Create a new filesystem image of the specified size.
  66. if test ! -f $image; then
  67. tarsize=`zcat $bootdir.tar.gz | wc -c`
  68. # Add about 30% (20% overhead plus 10% breathing room), and convert
  69. # to kilobytes. This factor was determined empirically.
  70. SIZE=`expr $tarsize \* 130 / 100 / 1024`k
  71. echo "# Creating $SIZE disk image \`$image'"
  72. trap "rm -f $image" 0
  73. dd if=/dev/zero of=$image bs=$SIZE count=1 >/dev/null
  74. $mke2fs -F $image
  75. trap '' 0
  76. fi
  77. # Attempt to mount the image.
  78. echo "# Mounting \`$image'"
  79. test -d $bootdir || mkdir $bootdir
  80. case "$host_os" in
  81. gnu*)
  82. settrans -a $bootdir /hurd/ext2fs $image
  83. umount="settrans -a $bootdir"
  84. ;;
  85. linux*)
  86. # This requires running as root, and using the loop device.
  87. i=0
  88. while test -e /dev/loop$i; do
  89. if /sbin/losetup /dev/loop$i $image; then
  90. break
  91. fi
  92. i=`expr $i + 1`
  93. done
  94. # Silly losetup doesn't report an error!
  95. mount /dev/loop$i $bootdir
  96. umount="umount $bootdir && /sbin/losetup -d /dev/loop$i && trap '' 0"
  97. ;;
  98. *)
  99. echo "$progname: Mounting \`$image' under \`$host_os' is not supported" 1>&2
  100. exit 1
  101. ;;
  102. esac
  103. trap "$umount" 0
  104. # Extract our tarball into the image, then unmount it.
  105. echo "# Copying files into \`$image':"
  106. tar -zxvf $bootdir.tar.gz
  107. echo "# \`$image' usage:"
  108. df $bootdir
  109. eval $umount
  110. rmdir $bootdir || :
  111. # Use the GRUB shell to properly set up GRUB on the image.
  112. echo "# Installing GRUB in \`$image'"
  113. cat <<EOF | $grub_shell --batch --device-map=/dev/null
  114. device (fd0) $image
  115. root (fd0)
  116. install /boot/grub/stage1 (fd0) /boot/grub/stage2
  117. quit
  118. EOF
  119. exit 0