Home | History | Annotate | Download | only in util
      1 #! /bin/sh
      2 # grub-image - Create a GRUB boot filesystem image and tarball
      3 # Gordon Matzigkeit <gord (at] 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 
     21 prefix=/usr/local
     22 exec_prefix=${prefix}
     23 sbindir=${exec_prefix}/sbin
     24 libdir=${exec_prefix}/lib
     25 PACKAGE=grub
     26 host_cpu=x86_64
     27 host_os=linux-gnu
     28 host_vendor=unknown
     29 context=${host_cpu}-${host_vendor}
     30 pkglibdir=${libdir}/${PACKAGE}/${context}
     31 
     32 mke2fs=`which mke2fs`
     33 
     34 progname=`echo "$0" | sed 's%^.*/%%'`
     35 thisdir=`echo "$0" | sed 's%/[^/]*$%%'`
     36 test "X$thisdir" = "X$0" && thisdir=.
     37 
     38 # See if we were invoked from within the build directory, and if so,
     39 # use the built files rather than the installed ones.
     40 if test -f $thisdir/../stage2/stage2; then
     41   grub_shell="$thisdir/../grub/grub"
     42   stage1dir="$thisdir/../stage1"
     43   stage2dir="$thisdir/../stage2"
     44 else
     45   grub_shell=${sbindir}/grub
     46   stage1dir="$pkglibdir"
     47   stage2dir="$pkglibdir"
     48 fi
     49 
     50 # Exit on any error.
     51 set -e
     52 
     53 # Get GRUB's version from the Grub shell, since we use the
     54 # installed files.
     55 VERSION=`$grub_shell --version | sed -e 's/^.* \([0-9.]*\).*$/\1/'`
     56 test "X$VERSION" != X
     57 
     58 bootdir=${PACKAGE}-${VERSION}-${context}
     59 image=$bootdir.ext2fs
     60 
     61 # Create the tarball.
     62 if test ! -f $bootdir.tar.gz; then
     63   echo "# Creating \`$bootdir.tar.gz'"
     64   mkdir -p $bootdir/boot/grub
     65   cp -p $stage1dir/stage1 $stage2dir/*_stage1_5 $stage2dir/stage2 \
     66     $bootdir/boot/grub
     67   test ! -f menu.lst || cp -p menu.lst $bootdir/boot/grub
     68   trap "rm -f $bootdir.tar.gz" 0
     69   GZIP=-9 tar -zcf $bootdir.tar.gz $bootdir
     70   trap '' 0
     71   rm -rf $bootdir
     72 fi
     73 
     74 # Create a new filesystem image of the specified size.
     75 if test ! -f $image; then
     76   tarsize=`zcat $bootdir.tar.gz | wc -c`
     77 
     78   # Add about 30% (20% overhead plus 10% breathing room), and convert
     79   # to kilobytes.  This factor was determined empirically.
     80   SIZE=`expr $tarsize \* 130 / 100 / 1024`k
     81   echo "# Creating $SIZE disk image \`$image'"
     82   trap "rm -f $image" 0
     83   dd if=/dev/zero of=$image bs=$SIZE count=1 >/dev/null
     84   $mke2fs -F $image
     85   trap '' 0
     86 fi
     87 
     88 
     89 # Attempt to mount the image.
     90 echo "# Mounting \`$image'"
     91 test -d $bootdir || mkdir $bootdir
     92 case "$host_os" in
     93 gnu*)
     94   settrans -a $bootdir /hurd/ext2fs $image
     95   umount="settrans -a $bootdir"
     96   ;;
     97 
     98 linux*)
     99   # This requires running as root, and using the loop device.
    100   i=0
    101   while test -e /dev/loop$i; do
    102     if /sbin/losetup /dev/loop$i $image; then
    103       break
    104     fi
    105     i=`expr $i + 1`
    106   done
    107 
    108   # Silly losetup doesn't report an error!
    109   mount /dev/loop$i $bootdir
    110   umount="umount $bootdir && /sbin/losetup -d /dev/loop$i && trap '' 0"
    111   ;;
    112 
    113 *)
    114   echo "$progname: Mounting \`$image' under \`$host_os' is not supported" 1>&2
    115   exit 1
    116   ;;
    117 esac
    118 trap "$umount" 0
    119 
    120 # Extract our tarball into the image, then unmount it.
    121 echo "# Copying files into \`$image':"
    122 tar -zxvf $bootdir.tar.gz
    123 
    124 echo "# \`$image' usage:"
    125 df $bootdir
    126 eval $umount
    127 rmdir $bootdir || :
    128 
    129 # Use the GRUB shell to properly set up GRUB on the image.
    130 echo "# Installing GRUB in \`$image'"
    131 cat <<EOF | $grub_shell --batch --device-map=/dev/null
    132 device (fd0) $image
    133 root (fd0)
    134 install /boot/grub/stage1 (fd0) /boot/grub/stage2
    135 quit
    136 EOF
    137 
    138 exit 0
    139