1N/A#! /bin/sh
1N/A# grub-image - Create a GRUB boot filesystem image and tarball
1N/A# Gordon Matzigkeit <gord@fig.org>, 2000-07-25
1N/A#
1N/A# Copyright (C) 2000, 2002 Free Software Foundation, Inc.
1N/A#
1N/A# This file is free software; you can redistribute it and/or modify it
1N/A# under the terms of the GNU General Public License as published by
1N/A# the Free Software Foundation; either version 2 of the License, or
1N/A# (at your option) any later version.
1N/A#
1N/A# This program is distributed in the hope that it will be useful, but
1N/A# WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1N/A# General Public License for more details.
1N/A#
1N/A# You should have received a copy of the GNU General Public License
1N/A# along with this program; if not, write to the Free Software
1N/A# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1N/A
1N/Aprefix=@prefix@
1N/Aexec_prefix=@exec_prefix@
1N/Asbindir=@sbindir@
1N/Alibdir=@libdir@
1N/APACKAGE=@PACKAGE@
1N/Ahost_cpu=@host_cpu@
1N/Ahost_os=@host_os@
1N/Ahost_vendor=@host_vendor@
1N/Acontext=${host_cpu}-${host_vendor}
1N/Apkglibdir=${libdir}/${PACKAGE}/${context}
1N/A
1N/Amke2fs=`which mke2fs`
1N/A
1N/Aprogname=`echo "$0" | sed 's%^.*/%%'`
1N/Athisdir=`echo "$0" | sed 's%/[^/]*$%%'`
1N/Atest "X$thisdir" = "X$0" && thisdir=.
1N/A
1N/A# See if we were invoked from within the build directory, and if so,
1N/A# use the built files rather than the installed ones.
1N/Aif test -f $thisdir/../stage2/stage2; then
1N/A grub_shell="$thisdir/../grub/grub"
1N/A stage1dir="$thisdir/../stage1"
1N/A stage2dir="$thisdir/../stage2"
1N/Aelse
1N/A grub_shell=${sbindir}/grub
1N/A stage1dir="$pkglibdir"
1N/A stage2dir="$pkglibdir"
1N/Afi
1N/A
1N/A# Exit on any error.
1N/Aset -e
1N/A
1N/A# Get GRUB's version from the Grub shell, since we use the
1N/A# installed files.
1N/AVERSION=`$grub_shell --version | sed -e 's/^.* \([0-9.]*\).*$/\1/'`
1N/Atest "X$VERSION" != X
1N/A
1N/Abootdir=${PACKAGE}-${VERSION}-${context}
1N/Aimage=$bootdir.ext2fs
1N/A
1N/A# Create the tarball.
1N/Aif test ! -f $bootdir.tar.gz; then
1N/A echo "# Creating \`$bootdir.tar.gz'"
1N/A mkdir -p $bootdir/boot/grub
1N/A cp -p $stage1dir/stage1 $stage2dir/*_stage1_5 $stage2dir/stage2 \
1N/A $bootdir/boot/grub
1N/A test ! -f menu.lst || cp -p menu.lst $bootdir/boot/grub
1N/A trap "rm -f $bootdir.tar.gz" 0
1N/A GZIP=-9 tar -zcf $bootdir.tar.gz $bootdir
1N/A trap '' 0
1N/A rm -rf $bootdir
1N/Afi
1N/A
1N/A# Create a new filesystem image of the specified size.
1N/Aif test ! -f $image; then
1N/A tarsize=`zcat $bootdir.tar.gz | wc -c`
1N/A
1N/A # Add about 30% (20% overhead plus 10% breathing room), and convert
1N/A # to kilobytes. This factor was determined empirically.
1N/A SIZE=`expr $tarsize \* 130 / 100 / 1024`k
1N/A echo "# Creating $SIZE disk image \`$image'"
1N/A trap "rm -f $image" 0
1N/A dd if=/dev/zero of=$image bs=$SIZE count=1 >/dev/null
1N/A $mke2fs -F $image
1N/A trap '' 0
1N/Afi
1N/A
1N/A
1N/A# Attempt to mount the image.
1N/Aecho "# Mounting \`$image'"
1N/Atest -d $bootdir || mkdir $bootdir
1N/Acase "$host_os" in
1N/Agnu*)
1N/A settrans -a $bootdir /hurd/ext2fs $image
1N/A umount="settrans -a $bootdir"
1N/A ;;
1N/A
1N/Alinux*)
1N/A # This requires running as root, and using the loop device.
1N/A i=0
1N/A while test -e /dev/loop$i; do
1N/A if /sbin/losetup /dev/loop$i $image; then
1N/A break
1N/A fi
1N/A i=`expr $i + 1`
1N/A done
1N/A
1N/A # Silly losetup doesn't report an error!
1N/A mount /dev/loop$i $bootdir
1N/A umount="umount $bootdir && /sbin/losetup -d /dev/loop$i && trap '' 0"
1N/A ;;
1N/A
1N/A*)
1N/A echo "$progname: Mounting \`$image' under \`$host_os' is not supported" 1>&2
1N/A exit 1
1N/A ;;
1N/Aesac
1N/Atrap "$umount" 0
1N/A
1N/A# Extract our tarball into the image, then unmount it.
1N/Aecho "# Copying files into \`$image':"
1N/Atar -zxvf $bootdir.tar.gz
1N/A
1N/Aecho "# \`$image' usage:"
1N/Adf $bootdir
1N/Aeval $umount
1N/Armdir $bootdir || :
1N/A
1N/A# Use the GRUB shell to properly set up GRUB on the image.
1N/Aecho "# Installing GRUB in \`$image'"
1N/Acat <<EOF | $grub_shell --batch --device-map=/dev/null
1N/Adevice (fd0) $image
1N/Aroot (fd0)
1N/Ainstall /boot/grub/stage1 (fd0) /boot/grub/stage2
1N/Aquit
1N/AEOF
1N/A
1N/Aexit 0