17c478bd9Sstevel@tonic-gate#! /bin/sh 27c478bd9Sstevel@tonic-gate# grub-image - Create a GRUB boot filesystem image and tarball 37c478bd9Sstevel@tonic-gate# Gordon Matzigkeit <gord@fig.org>, 2000-07-25 47c478bd9Sstevel@tonic-gate# 57c478bd9Sstevel@tonic-gate# Copyright (C) 2000, 2002 Free Software Foundation, Inc. 67c478bd9Sstevel@tonic-gate# 77c478bd9Sstevel@tonic-gate# This file is free software; you can redistribute it and/or modify it 87c478bd9Sstevel@tonic-gate# under the terms of the GNU General Public License as published by 97c478bd9Sstevel@tonic-gate# the Free Software Foundation; either version 2 of the License, or 107c478bd9Sstevel@tonic-gate# (at your option) any later version. 117c478bd9Sstevel@tonic-gate# 127c478bd9Sstevel@tonic-gate# This program is distributed in the hope that it will be useful, but 137c478bd9Sstevel@tonic-gate# WITHOUT ANY WARRANTY; without even the implied warranty of 147c478bd9Sstevel@tonic-gate# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 157c478bd9Sstevel@tonic-gate# General Public License for more details. 167c478bd9Sstevel@tonic-gate# 177c478bd9Sstevel@tonic-gate# You should have received a copy of the GNU General Public License 187c478bd9Sstevel@tonic-gate# along with this program; if not, write to the Free Software 197c478bd9Sstevel@tonic-gate# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gateprefix=@prefix@ 227c478bd9Sstevel@tonic-gateexec_prefix=@exec_prefix@ 237c478bd9Sstevel@tonic-gatesbindir=@sbindir@ 247c478bd9Sstevel@tonic-gatelibdir=@libdir@ 257c478bd9Sstevel@tonic-gatePACKAGE=@PACKAGE@ 267c478bd9Sstevel@tonic-gatehost_cpu=@host_cpu@ 277c478bd9Sstevel@tonic-gatehost_os=@host_os@ 287c478bd9Sstevel@tonic-gatehost_vendor=@host_vendor@ 297c478bd9Sstevel@tonic-gatecontext=${host_cpu}-${host_vendor} 307c478bd9Sstevel@tonic-gatepkglibdir=${libdir}/${PACKAGE}/${context} 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gatemke2fs=`which mke2fs` 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gateprogname=`echo "$0" | sed 's%^.*/%%'` 357c478bd9Sstevel@tonic-gatethisdir=`echo "$0" | sed 's%/[^/]*$%%'` 367c478bd9Sstevel@tonic-gatetest "X$thisdir" = "X$0" && thisdir=. 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate# See if we were invoked from within the build directory, and if so, 397c478bd9Sstevel@tonic-gate# use the built files rather than the installed ones. 407c478bd9Sstevel@tonic-gateif test -f $thisdir/../stage2/stage2; then 417c478bd9Sstevel@tonic-gate grub_shell="$thisdir/../grub/grub" 427c478bd9Sstevel@tonic-gate stage1dir="$thisdir/../stage1" 437c478bd9Sstevel@tonic-gate stage2dir="$thisdir/../stage2" 447c478bd9Sstevel@tonic-gateelse 457c478bd9Sstevel@tonic-gate grub_shell=${sbindir}/grub 467c478bd9Sstevel@tonic-gate stage1dir="$pkglibdir" 477c478bd9Sstevel@tonic-gate stage2dir="$pkglibdir" 487c478bd9Sstevel@tonic-gatefi 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate# Exit on any error. 517c478bd9Sstevel@tonic-gateset -e 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate# Get GRUB's version from the Grub shell, since we use the 547c478bd9Sstevel@tonic-gate# installed files. 557c478bd9Sstevel@tonic-gateVERSION=`$grub_shell --version | sed -e 's/^.* \([0-9.]*\).*$/\1/'` 567c478bd9Sstevel@tonic-gatetest "X$VERSION" != X 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gatebootdir=${PACKAGE}-${VERSION}-${context} 597c478bd9Sstevel@tonic-gateimage=$bootdir.ext2fs 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate# Create the tarball. 627c478bd9Sstevel@tonic-gateif test ! -f $bootdir.tar.gz; then 637c478bd9Sstevel@tonic-gate echo "# Creating \`$bootdir.tar.gz'" 647c478bd9Sstevel@tonic-gate mkdir -p $bootdir/boot/grub 657c478bd9Sstevel@tonic-gate cp -p $stage1dir/stage1 $stage2dir/*_stage1_5 $stage2dir/stage2 \ 667c478bd9Sstevel@tonic-gate $bootdir/boot/grub 677c478bd9Sstevel@tonic-gate test ! -f menu.lst || cp -p menu.lst $bootdir/boot/grub 687c478bd9Sstevel@tonic-gate trap "rm -f $bootdir.tar.gz" 0 697c478bd9Sstevel@tonic-gate GZIP=-9 tar -zcf $bootdir.tar.gz $bootdir 707c478bd9Sstevel@tonic-gate trap '' 0 717c478bd9Sstevel@tonic-gate rm -rf $bootdir 727c478bd9Sstevel@tonic-gatefi 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate# Create a new filesystem image of the specified size. 757c478bd9Sstevel@tonic-gateif test ! -f $image; then 767c478bd9Sstevel@tonic-gate tarsize=`zcat $bootdir.tar.gz | wc -c` 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate # Add about 30% (20% overhead plus 10% breathing room), and convert 797c478bd9Sstevel@tonic-gate # to kilobytes. This factor was determined empirically. 807c478bd9Sstevel@tonic-gate SIZE=`expr $tarsize \* 130 / 100 / 1024`k 817c478bd9Sstevel@tonic-gate echo "# Creating $SIZE disk image \`$image'" 827c478bd9Sstevel@tonic-gate trap "rm -f $image" 0 837c478bd9Sstevel@tonic-gate dd if=/dev/zero of=$image bs=$SIZE count=1 >/dev/null 847c478bd9Sstevel@tonic-gate $mke2fs -F $image 857c478bd9Sstevel@tonic-gate trap '' 0 867c478bd9Sstevel@tonic-gatefi 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate# Attempt to mount the image. 907c478bd9Sstevel@tonic-gateecho "# Mounting \`$image'" 917c478bd9Sstevel@tonic-gatetest -d $bootdir || mkdir $bootdir 927c478bd9Sstevel@tonic-gatecase "$host_os" in 937c478bd9Sstevel@tonic-gategnu*) 947c478bd9Sstevel@tonic-gate settrans -a $bootdir /hurd/ext2fs $image 957c478bd9Sstevel@tonic-gate umount="settrans -a $bootdir" 967c478bd9Sstevel@tonic-gate ;; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gatelinux*) 997c478bd9Sstevel@tonic-gate # This requires running as root, and using the loop device. 1007c478bd9Sstevel@tonic-gate i=0 1017c478bd9Sstevel@tonic-gate while test -e /dev/loop$i; do 1027c478bd9Sstevel@tonic-gate if /sbin/losetup /dev/loop$i $image; then 1037c478bd9Sstevel@tonic-gate break 1047c478bd9Sstevel@tonic-gate fi 1057c478bd9Sstevel@tonic-gate i=`expr $i + 1` 1067c478bd9Sstevel@tonic-gate done 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate # Silly losetup doesn't report an error! 1097c478bd9Sstevel@tonic-gate mount /dev/loop$i $bootdir 1107c478bd9Sstevel@tonic-gate umount="umount $bootdir && /sbin/losetup -d /dev/loop$i && trap '' 0" 1117c478bd9Sstevel@tonic-gate ;; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate*) 1147c478bd9Sstevel@tonic-gate echo "$progname: Mounting \`$image' under \`$host_os' is not supported" 1>&2 1157c478bd9Sstevel@tonic-gate exit 1 1167c478bd9Sstevel@tonic-gate ;; 1177c478bd9Sstevel@tonic-gateesac 1187c478bd9Sstevel@tonic-gatetrap "$umount" 0 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate# Extract our tarball into the image, then unmount it. 1217c478bd9Sstevel@tonic-gateecho "# Copying files into \`$image':" 1227c478bd9Sstevel@tonic-gatetar -zxvf $bootdir.tar.gz 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gateecho "# \`$image' usage:" 1257c478bd9Sstevel@tonic-gatedf $bootdir 1267c478bd9Sstevel@tonic-gateeval $umount 1277c478bd9Sstevel@tonic-gatermdir $bootdir || : 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate# Use the GRUB shell to properly set up GRUB on the image. 1307c478bd9Sstevel@tonic-gateecho "# Installing GRUB in \`$image'" 1317c478bd9Sstevel@tonic-gatecat <<EOF | $grub_shell --batch --device-map=/dev/null 1327c478bd9Sstevel@tonic-gatedevice (fd0) $image 1337c478bd9Sstevel@tonic-gateroot (fd0) 1347c478bd9Sstevel@tonic-gateinstall /boot/grub/stage1 (fd0) /boot/grub/stage2 1357c478bd9Sstevel@tonic-gatequit 1367c478bd9Sstevel@tonic-gateEOF 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gateexit 0 139