xref: /titanic_44/usr/src/grub/grub-0.97/util/grub-image.in (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
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
21prefix=@prefix@
22exec_prefix=@exec_prefix@
23sbindir=@sbindir@
24libdir=@libdir@
25PACKAGE=@PACKAGE@
26host_cpu=@host_cpu@
27host_os=@host_os@
28host_vendor=@host_vendor@
29context=${host_cpu}-${host_vendor}
30pkglibdir=${libdir}/${PACKAGE}/${context}
31
32mke2fs=`which mke2fs`
33
34progname=`echo "$0" | sed 's%^.*/%%'`
35thisdir=`echo "$0" | sed 's%/[^/]*$%%'`
36test "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.
40if test -f $thisdir/../stage2/stage2; then
41  grub_shell="$thisdir/../grub/grub"
42  stage1dir="$thisdir/../stage1"
43  stage2dir="$thisdir/../stage2"
44else
45  grub_shell=${sbindir}/grub
46  stage1dir="$pkglibdir"
47  stage2dir="$pkglibdir"
48fi
49
50# Exit on any error.
51set -e
52
53# Get GRUB's version from the Grub shell, since we use the
54# installed files.
55VERSION=`$grub_shell --version | sed -e 's/^.* \([0-9.]*\).*$/\1/'`
56test "X$VERSION" != X
57
58bootdir=${PACKAGE}-${VERSION}-${context}
59image=$bootdir.ext2fs
60
61# Create the tarball.
62if 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
72fi
73
74# Create a new filesystem image of the specified size.
75if 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
86fi
87
88
89# Attempt to mount the image.
90echo "# Mounting \`$image'"
91test -d $bootdir || mkdir $bootdir
92case "$host_os" in
93gnu*)
94  settrans -a $bootdir /hurd/ext2fs $image
95  umount="settrans -a $bootdir"
96  ;;
97
98linux*)
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  ;;
117esac
118trap "$umount" 0
119
120# Extract our tarball into the image, then unmount it.
121echo "# Copying files into \`$image':"
122tar -zxvf $bootdir.tar.gz
123
124echo "# \`$image' usage:"
125df $bootdir
126eval $umount
127rmdir $bootdir || :
128
129# Use the GRUB shell to properly set up GRUB on the image.
130echo "# Installing GRUB in \`$image'"
131cat <<EOF | $grub_shell --batch --device-map=/dev/null
132device (fd0) $image
133root (fd0)
134install /boot/grub/stage1 (fd0) /boot/grub/stage2
135quit
136EOF
137
138exit 0
139