17c478bd9Sstevel@tonic-gate#!/bin/sh
27c478bd9Sstevel@tonic-gate# MaKe a Bootable IMAGE --- 1.44, 2.88 and El Torito no-emulation mode
37c478bd9Sstevel@tonic-gate# C) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>
47c478bd9Sstevel@tonic-gate# C) 2001,2002,2003 Robert Millan <robertmh@gnu.org>
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate# This program is free software; you can redistribute it and/or modify
87c478bd9Sstevel@tonic-gate# it under the terms of the GNU General Public License as published by
97c478bd9Sstevel@tonic-gate# the Free Software Foundation; either version 2, or (at your option)
107c478bd9Sstevel@tonic-gate# any later version.
117c478bd9Sstevel@tonic-gate#
127c478bd9Sstevel@tonic-gate# This program is distributed in the hope that it will be useful,
137c478bd9Sstevel@tonic-gate# but WITHOUT ANY WARRANTY; without even the implied warranty of
147c478bd9Sstevel@tonic-gate# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157c478bd9Sstevel@tonic-gate# GNU 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, you can either send email to this
197c478bd9Sstevel@tonic-gate# program's maintainer or write to: The Free Software Foundation,
207c478bd9Sstevel@tonic-gate# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
217c478bd9Sstevel@tonic-gate
227c478bd9Sstevel@tonic-gate# $Id: mkbimage,v 1.19 2004/07/21 14:43:04 robertmh Exp $
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate# Global variables
257c478bd9Sstevel@tonic-gatetarfile=
267c478bd9Sstevel@tonic-gatedir=
277c478bd9Sstevel@tonic-gatefs= #file system type
287c478bd9Sstevel@tonic-gatedecompress=
297c478bd9Sstevel@tonic-gateimage_type=
307c478bd9Sstevel@tonic-gateuname=`uname -s`
317c478bd9Sstevel@tonic-gatePATH=/sbin:$PATH
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate# You can set GRUB_PATH  if you need to use a specially located GRUB.
347c478bd9Sstevel@tonic-gate# This MUST end by a '/'!
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate#----------------------------DON'T CHANGE: INTERNALS
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gateblock_size=512
407c478bd9Sstevel@tonic-gatecylinders=
417c478bd9Sstevel@tonic-gateheads=
427c478bd9Sstevel@tonic-gatesectors=
437c478bd9Sstevel@tonic-gatecyl_size=
447c478bd9Sstevel@tonic-gatetype_option=
457c478bd9Sstevel@tonic-gategeo_option=
467c478bd9Sstevel@tonic-gateimage=
477c478bd9Sstevel@tonic-gatebk_120=$((2 * 15 * 80))
487c478bd9Sstevel@tonic-gatebk_144=$((2 * 18 * 80))
497c478bd9Sstevel@tonic-gatebk_288=$((2 * 36 * 80))
507c478bd9Sstevel@tonic-gatebk_160=$((2 * 20 * 80))
517c478bd9Sstevel@tonic-gatebk_168=$((2 * 21 * 80))
527c478bd9Sstevel@tonic-gatebk_174=$((2 * 21 * 83))
537c478bd9Sstevel@tonic-gatelo_options=
547c478bd9Sstevel@tonic-gatedevice_map=
557c478bd9Sstevel@tonic-gatemkfs_options=
567c478bd9Sstevel@tonic-gatedebug=
577c478bd9Sstevel@tonic-gatestage2_os_name=
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate# Name by which this script was invoked.
607c478bd9Sstevel@tonic-gateprogram=`echo "$0" | sed -e 's/[^\/]*\///g'`
617c478bd9Sstevel@tonic-gateversion_number='$Revision: 1.19 $'
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gateusage="
647c478bd9Sstevel@tonic-gateUsage: $program [-hVF] [-t TYPE] [-d DIRECTORY] [-s FS_TYPE] -f TAR_FILE
657c478bd9Sstevel@tonic-gateMake a Bootable IMAGE using GRUB as a bootloader
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gateOptions:
687c478bd9Sstevel@tonic-gate Actions:
697c478bd9Sstevel@tonic-gate -d DIRECTORY [default CWD]
707c478bd9Sstevel@tonic-gate	    Directory where the boot.image and the partition subdirectories
717c478bd9Sstevel@tonic-gate	    are/will be created
727c478bd9Sstevel@tonic-gate -f TAR_FILE
737c478bd9Sstevel@tonic-gate	    Name of the tar file containing the filesystem to install. Can
747c478bd9Sstevel@tonic-gate	    be a pure tar file [.tar] or a compressed tar file
757c478bd9Sstevel@tonic-gate	    [.tar.gz|.tar.bz2]
767c478bd9Sstevel@tonic-gate -s FS_TYPE
777c478bd9Sstevel@tonic-gate	    Type of the file system to create on the virtual disk. Choices
787c478bd9Sstevel@tonic-gate	    are:
797c478bd9Sstevel@tonic-gate		ext2 on GNU [default is ext2]
807c478bd9Sstevel@tonic-gate		ext2, minix or msdos on GNU/Linux [default is ext2]
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate -t TYPE
837c478bd9Sstevel@tonic-gate	    Type of the image to create. Choices are '1.20', '1.44', '1.60',
847c478bd9Sstevel@tonic-gate	    '1.68', '1.74', '2.88' or 'hd' [default is hd]
857c478bd9Sstevel@tonic-gate -F
867c478bd9Sstevel@tonic-gate	    Force to set the set_dpt flag (unnecessary 99% of the time! Be
877c478bd9Sstevel@tonic-gate	    careful!
887c478bd9Sstevel@tonic-gate Informations:
897c478bd9Sstevel@tonic-gate -D
907c478bd9Sstevel@tonic-gate	    turn Debugging on [xtrace]
917c478bd9Sstevel@tonic-gate -h|--help
927c478bd9Sstevel@tonic-gate	    display this Help and exit
937c478bd9Sstevel@tonic-gate -V|--version
947c478bd9Sstevel@tonic-gate	    display Version information and exit
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gateCopyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
977c478bd9Sstevel@tonic-gateCopyright (c) 2001,2002 Robert Millan <zeratul2@wanadoo.es>.
987c478bd9Sstevel@tonic-gateGPLed."
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gateversion="mkbimage $version_number
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gateWritten by Thierry Laronde and Robert Millan.
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gateCopyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
1057c478bd9Sstevel@tonic-gateCopyright (c) 2001,2002,2003 Robert Millan <zeratul2@wanadoo.es>.
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gateThis is free software under the GPL version 2 or later; see the source for
1087c478bd9Sstevel@tonic-gatecopying conditions.  There is NO warranty, not even for MERCHANTABILITY or
1097c478bd9Sstevel@tonic-gateFITNESS FOR A PARTICULAR PURPOSE."
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate# Functions
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gateerror ()
1147c478bd9Sstevel@tonic-gate{
1157c478bd9Sstevel@tonic-gate	case $1 in
1167c478bd9Sstevel@tonic-gate	bug) echo "This is a bug!";
1177c478bd9Sstevel@tonic-gate		echo "$usage";;
1187c478bd9Sstevel@tonic-gate	option) echo "Unknow option"; echo "$usage";;
1197c478bd9Sstevel@tonic-gate	missing_argument) echo "You must give an argument to the option!";
1207c478bd9Sstevel@tonic-gate		echo "$usage";;
1217c478bd9Sstevel@tonic-gate	missing_option) echo "You must indicate at least one option!";
1227c478bd9Sstevel@tonic-gate		echo "$usage";;
1237c478bd9Sstevel@tonic-gate	must_be_root) echo "You must be root! (or install e2tools/mtools)";;
1247c478bd9Sstevel@tonic-gate	unknown_fs)	if [ $uname = Linux ];
1257c478bd9Sstevel@tonic-gate				then echo "The GNU/Linux supported fs are: ext2, minix or msdos!";
1267c478bd9Sstevel@tonic-gate			elif [ $uname = GNU ];
1277c478bd9Sstevel@tonic-gate				then echo "The GNU supported fs is ext2!";
1287c478bd9Sstevel@tonic-gate			fi;;
1297c478bd9Sstevel@tonic-gate	unknown_format) echo "The tar file must be .tar|.tar.gz|.tar.bz2!";;
1307c478bd9Sstevel@tonic-gate	wont_fit) echo "The files won't fit on the selected type of media!";;
1317c478bd9Sstevel@tonic-gate	wrong_directory) echo "Directory inexistant or not given!";
1327c478bd9Sstevel@tonic-gate		echo "$usage";;
1337c478bd9Sstevel@tonic-gate	wrong_file) echo "File inexistant or empty!";
1347c478bd9Sstevel@tonic-gate		echo "$usage";;
1357c478bd9Sstevel@tonic-gate	wrong_type) echo "The type specified is not a valid one!";
1367c478bd9Sstevel@tonic-gate		echo "$usage";;
1377c478bd9Sstevel@tonic-gate	esac
1387c478bd9Sstevel@tonic-gate	exit 1
1397c478bd9Sstevel@tonic-gate}
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate# create a filesystem of type $fs in $image with offset $offset
1427c478bd9Sstevel@tonic-gatemkbimage_mkfs ()
1437c478bd9Sstevel@tonic-gate{
1447c478bd9Sstevel@tonic-gate  case $offset in
1457c478bd9Sstevel@tonic-gate    0)  lo_options="";;
1467c478bd9Sstevel@tonic-gate    *) lo_options="-o $offset";;
1477c478bd9Sstevel@tonic-gate  esac
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate  if [ "$offset" = "0" ] ; then
1507c478bd9Sstevel@tonic-gate    mkfs.$fs -F $image
1517c478bd9Sstevel@tonic-gate  elif [ `id -u` = "0" ] ; then
1527c478bd9Sstevel@tonic-gate    losetup $lo_options /dev/loop1 $image
1537c478bd9Sstevel@tonic-gate    mkfs.$fs /dev/loop1
1547c478bd9Sstevel@tonic-gate    losetup -d /dev/loop1
1557c478bd9Sstevel@tonic-gate  else
1567c478bd9Sstevel@tonic-gate    error must_be_root
1577c478bd9Sstevel@tonic-gate  fi
1587c478bd9Sstevel@tonic-gate}
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate# copy ${image}1/* to ${image}:/, assuming ${image} contains a filesystem
1617c478bd9Sstevel@tonic-gate# of type $fs in offset $offset
1627c478bd9Sstevel@tonic-gatemkbimage_cp ()
1637c478bd9Sstevel@tonic-gate{
1647c478bd9Sstevel@tonic-gate  case $offset in
1657c478bd9Sstevel@tonic-gate    0)  lo_options="";;
1667c478bd9Sstevel@tonic-gate    *)  lo_options="-o $offset";;
1677c478bd9Sstevel@tonic-gate  esac
1687c478bd9Sstevel@tonic-gate  case $fs in
1697c478bd9Sstevel@tonic-gate    ext2)
1707c478bd9Sstevel@tonic-gate      cp="e2cp";
1717c478bd9Sstevel@tonic-gate      mkdir="e2mkdir";;
1727c478bd9Sstevel@tonic-gate    vfat)
1737c478bd9Sstevel@tonic-gate      cp="mcopy";
1747c478bd9Sstevel@tonic-gate      mkdir="mmd";;
1757c478bd9Sstevel@tonic-gate    *)
1767c478bd9Sstevel@tonic-gate      cp="";
1777c478bd9Sstevel@tonic-gate      mkdir="";;
1787c478bd9Sstevel@tonic-gate  esac
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate  if [ "$offset" = 0 ] && which $cp > /dev/null ; then
1817c478bd9Sstevel@tonic-gate    for dir in $(cd ${image}1 && find -type d) ; do
1827c478bd9Sstevel@tonic-gate	$mkdir ${image}:$dir
1837c478bd9Sstevel@tonic-gate    done
1847c478bd9Sstevel@tonic-gate    for file in $(cd ${image}1 && find -type f) ; do
1857c478bd9Sstevel@tonic-gate	$cp ${image}1/$file ${image}:$file
1867c478bd9Sstevel@tonic-gate    done
1877c478bd9Sstevel@tonic-gate  elif [ "`id -u`" = "0" ] ; then
1887c478bd9Sstevel@tonic-gate    losetup $lo_options /dev/loop1 $image
1897c478bd9Sstevel@tonic-gate    mkdir ${image}.mnt
1907c478bd9Sstevel@tonic-gate    mount -t $fs /dev/loop1 ${image}.mnt
1917c478bd9Sstevel@tonic-gate    cp -a ${image}1/* ${image}.mnt/ && sync
1927c478bd9Sstevel@tonic-gate    umount ${image}.mnt
1937c478bd9Sstevel@tonic-gate    rmdir ${image}.mnt
1947c478bd9Sstevel@tonic-gate    losetup -d /dev/loop1
1957c478bd9Sstevel@tonic-gate  else
1967c478bd9Sstevel@tonic-gate    error must_be_root
1977c478bd9Sstevel@tonic-gate  fi
1987c478bd9Sstevel@tonic-gate}
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate#**********************************************************************
2017c478bd9Sstevel@tonic-gate#                          MAIN PROGRAM                               *
2027c478bd9Sstevel@tonic-gate#**********************************************************************
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate#---------------------- Getting the options
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate[ $# -eq 0 ] && error missing_option;
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gatewhile [ $# -gt 0 ]; do
2097c478bd9Sstevel@tonic-gate	case "$1" in
2107c478bd9Sstevel@tonic-gate		-d) shift;
2117c478bd9Sstevel@tonic-gate			dir="$1";
2127c478bd9Sstevel@tonic-gate			[ ! -d "$1" ] && error wrong_directory;;
2137c478bd9Sstevel@tonic-gate		-f) shift;
2147c478bd9Sstevel@tonic-gate			tarfile="$1";
2157c478bd9Sstevel@tonic-gate			[ -z "$tarfile" ] && error missing_argument;;
2167c478bd9Sstevel@tonic-gate		-s) shift;
2177c478bd9Sstevel@tonic-gate			fs="$1";;
2187c478bd9Sstevel@tonic-gate		-t) shift;
2197c478bd9Sstevel@tonic-gate			image_type="$1";;
2207c478bd9Sstevel@tonic-gate		-F) geo_option="-F";;
2217c478bd9Sstevel@tonic-gate		-D) debug="-v";
2227c478bd9Sstevel@tonic-gate		    set -x;;
2237c478bd9Sstevel@tonic-gate		-h|--help) echo "$usage"; exit 0;;
2247c478bd9Sstevel@tonic-gate		-V|--version) echo "$version"; exit 0;;
2257c478bd9Sstevel@tonic-gate	 	*) error option ;;
2267c478bd9Sstevel@tonic-gate	esac
2277c478bd9Sstevel@tonic-gateshift
2287c478bd9Sstevel@tonic-gatedone
2297c478bd9Sstevel@tonic-gate#---------------------- Sanity checks
2307c478bd9Sstevel@tonic-gate[ ! "$tarfile" ] && error missing_argument;
2317c478bd9Sstevel@tonic-gate[ ! -s "$tarfile" ] && error wrong_file;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gateif [ ! "$image_type" ]; then
2347c478bd9Sstevel@tonic-gate	image_type=hd;
2357c478bd9Sstevel@tonic-gateelif [ "$image_type" != "1.20" ] && [ "$image_type" != "1.44" ] \
2367c478bd9Sstevel@tonic-gate  && [ "$image_type" != "1.60" ] && [ "$image_type" != "1.68" ] \
2377c478bd9Sstevel@tonic-gate  && [ "$image_type" != "2.88" ] && [ "$image_type" != "1.74" ] \
2387c478bd9Sstevel@tonic-gate  && [ "$image_type" != "hd" ] && [ "$image_type" != "1.60" ] ; then
2397c478bd9Sstevel@tonic-gate  error wrong_type ;
2407c478bd9Sstevel@tonic-gatefi
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate[ ! "$fs" ] && fs=ext2
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate# Carlo Contavalli reported that I [TL] have forgotten to specify the
2457c478bd9Sstevel@tonic-gate# partition ID for sfdisk to correctly fill the partition table (ext2 is the
2467c478bd9Sstevel@tonic-gate# default on Linux, so this worked in this case...). This is fixed below.
2477c478bd9Sstevel@tonic-gatecase "$fs" in
2487c478bd9Sstevel@tonic-gate	ext2) mkfs_options="-m 0";
2497c478bd9Sstevel@tonic-gate		  part_id="83";; # This is the default
2507c478bd9Sstevel@tonic-gate#	ufs)	if [ $uname = Linux ];
2517c478bd9Sstevel@tonic-gate#		then error unknown_fs;
2527c478bd9Sstevel@tonic-gate#		fi;;
2537c478bd9Sstevel@tonic-gate	minix)	if [ $uname = GNU ];
2547c478bd9Sstevel@tonic-gate		then error unknown_fs;
2557c478bd9Sstevel@tonic-gate		else
2567c478bd9Sstevel@tonic-gate			mkfs_options="-v"; # Minix version 2
2577c478bd9Sstevel@tonic-gate			part_id="81";
2587c478bd9Sstevel@tonic-gate		fi;;
2597c478bd9Sstevel@tonic-gate	msdos)	if [ $uname = GNU ];
2607c478bd9Sstevel@tonic-gate		then error unknown_fs;
2617c478bd9Sstevel@tonic-gate		else
2627c478bd9Sstevel@tonic-gate			mkfs_options="-f 1 -F 12"; # the smallest...
2637c478bd9Sstevel@tonic-gate			part_id="1";
2647c478bd9Sstevel@tonic-gate		fi;;
2657c478bd9Sstevel@tonic-gate	*) error unknown_fs;;
2667c478bd9Sstevel@tonic-gateesac
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate# What type of tar file has been given ?
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gatesuffix=`echo "$tarfile" | sed -n 's/^.*\.\([targbz2]\{2,3\}\)$/\1/p'`
2717c478bd9Sstevel@tonic-gatecase "$suffix" in
2727c478bd9Sstevel@tonic-gate	tar) decompress="cat";;
2737c478bd9Sstevel@tonic-gate	gz) decompress="gunzip -c";;
2747c478bd9Sstevel@tonic-gate	bz2) decompress="bunzip2 -c";;
2757c478bd9Sstevel@tonic-gate	*) error unknown_format;;
2767c478bd9Sstevel@tonic-gateesac
2777c478bd9Sstevel@tonic-gate#---------------------- Initializations
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate[ ! "$dir" ] && dir=`pwd`
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gateimage=$dir/$image_type.image
2827c478bd9Sstevel@tonic-gatedevice_map=$dir/device.map
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate# First, find the size of the tar file in block_size.
2857c478bd9Sstevel@tonic-gatefile_size=`$decompress $tarfile | wc -c | tr -d ' '`
2867c478bd9Sstevel@tonic-gatefile_size=$(($file_size / $block_size + 1))
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate# Increase in order to be sure that with a fs there will be enough
2897c478bd9Sstevel@tonic-gate# room (trying 110%)
2907c478bd9Sstevel@tonic-gatefile_size=$(($file_size + $file_size / 10))
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gatecase "$image_type" in
2937c478bd9Sstevel@tonic-gate  hd) heads=16;
2947c478bd9Sstevel@tonic-gate    sectors=63;
2957c478bd9Sstevel@tonic-gate    cyl_size=$((16 * 63));
2967c478bd9Sstevel@tonic-gate    # Create the minimum number of cylinders. At the moment, we leave
2977c478bd9Sstevel@tonic-gate    # some space by rounding everything up by adding 1 cylinder, plus
2987c478bd9Sstevel@tonic-gate    # another one for MBR + reserved track.
2997c478bd9Sstevel@tonic-gate    cylinders=$(($file_size / $cyl_size + 2));;
3007c478bd9Sstevel@tonic-gate  1.20) [ $file_size -ge $bk_120 ] && error wont_fit;
3017c478bd9Sstevel@tonic-gate    heads=2;
3027c478bd9Sstevel@tonic-gate    sectors=15;
3037c478bd9Sstevel@tonic-gate    cyl_size=$((2 * 15));
3047c478bd9Sstevel@tonic-gate    cylinders=80;;
3057c478bd9Sstevel@tonic-gate  1.44) [ $file_size -ge $bk_144 ] && error wont_fit;
3067c478bd9Sstevel@tonic-gate    heads=2;
3077c478bd9Sstevel@tonic-gate    sectors=18;
3087c478bd9Sstevel@tonic-gate    cyl_size=$((2 * 18));
3097c478bd9Sstevel@tonic-gate    cylinders=80;;
3107c478bd9Sstevel@tonic-gate  1.60) [ $file_size -ge $bk_160 ] && error wont_fit;
3117c478bd9Sstevel@tonic-gate    heads=2;
3127c478bd9Sstevel@tonic-gate    sectors=20;
3137c478bd9Sstevel@tonic-gate    cyl_size=$((2 * 20));
3147c478bd9Sstevel@tonic-gate    cylinders=80;
3157c478bd9Sstevel@tonic-gate    geo_option="-F";;
3167c478bd9Sstevel@tonic-gate  1.68) [ $file_size -ge $bk_168 ] && error wont_fit;
3177c478bd9Sstevel@tonic-gate    heads=2;
3187c478bd9Sstevel@tonic-gate    sectors=21;
3197c478bd9Sstevel@tonic-gate    cyl_size=$((2 * 21));
3207c478bd9Sstevel@tonic-gate    cylinders=80;;
3217c478bd9Sstevel@tonic-gate  1.74) [ $file_size -ge $bk_174 ] && error wont_fit;
3227c478bd9Sstevel@tonic-gate    heads=2;
3237c478bd9Sstevel@tonic-gate    sectors=21;
3247c478bd9Sstevel@tonic-gate    cyl_size=$((2 * 21));
3257c478bd9Sstevel@tonic-gate    cylinders=83;;
3267c478bd9Sstevel@tonic-gate  2.88) [ $file_size -ge $bk_288 ] && error wont_fit;
3277c478bd9Sstevel@tonic-gate    heads=2;
3287c478bd9Sstevel@tonic-gate    sectors=36;
3297c478bd9Sstevel@tonic-gate    cyl_size=$((2 * 36));
3307c478bd9Sstevel@tonic-gate    cylinders=80;;
3317c478bd9Sstevel@tonic-gate  *) error bug;;
3327c478bd9Sstevel@tonic-gateesac
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gatetype_option="-t $image_type"
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate# We start by creating a virtual disk which size is the number of
3377c478bd9Sstevel@tonic-gate# cylinders of $cyl_size mandatory to put the files stocked in the $tarfile
3387c478bd9Sstevel@tonic-gate# Create the empty virtual disk
3397c478bd9Sstevel@tonic-gatedd if=/dev/zero of=$image bs=$block_size count=$(($cyl_size * $cylinders))
3407c478bd9Sstevel@tonic-gate
3417c478bd9Sstevel@tonic-gate# We then format the virtual disk
3427c478bd9Sstevel@tonic-gate# NOTE: the El Torito specification wants only one partition. So we
3437c478bd9Sstevel@tonic-gate# create the first, and the remaining 3 entries are empty.
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gateif [ "$image_type" = "hd" ]; then
3467c478bd9Sstevel@tonic-gate  sfdisk -C $cylinders -H $heads -S $sectors -D $image<<EOT
3477c478bd9Sstevel@tonic-gate,,$part_id,*,0,1,1
3487c478bd9Sstevel@tonic-gate
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gateEOT
3517c478bd9Sstevel@tonic-gate  offset="$(($sectors * $block_size))"
3527c478bd9Sstevel@tonic-gate  type_option=
3537c478bd9Sstevel@tonic-gateelse
3547c478bd9Sstevel@tonic-gate  offset="0"
3557c478bd9Sstevel@tonic-gatefi
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate# It's time now to create the filesystem on the first partition.
3587c478bd9Sstevel@tonic-gatemkbimage_mkfs
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate# then untar the files
3617c478bd9Sstevel@tonic-gate[ ! -e ${image}1 ] || { echo "${image}1 exists, please remove it first"; exit 1;}
3627c478bd9Sstevel@tonic-gatemkdir -p ${image}1
3637c478bd9Sstevel@tonic-gate$decompress $tarfile | tar -C ${image}1 $debug -xf -
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate# copy the untarred files into the filesystem image
3667c478bd9Sstevel@tonic-gatemkbimage_cp
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate#We verify that the stage2 exists and we search the name
3697c478bd9Sstevel@tonic-gatestage2_os_name=`find ${image}1 -name stage2 -type f`
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate[ -r "$stage2_os_name" ] || { echo "I can't find stage2!"; exit 1;}
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate#------------------------- GRUB stuff
3747c478bd9Sstevel@tonic-gateif [ "$image_type" = "hd" ]; then
3757c478bd9Sstevel@tonic-gate	device='(hd0)'
3767c478bd9Sstevel@tonic-gate	root='(hd0,0)'
3777c478bd9Sstevel@tonic-gateelse
3787c478bd9Sstevel@tonic-gate	device='(fd0)'
3797c478bd9Sstevel@tonic-gate	root='(fd0)'
3807c478bd9Sstevel@tonic-gatefi
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gatecat<<EOT >$device_map
3837c478bd9Sstevel@tonic-gate$device	${image}
3847c478bd9Sstevel@tonic-gateEOT
3857c478bd9Sstevel@tonic-gate
3867c478bd9Sstevel@tonic-gate${GRUB_PATH}grub --device-map=$device_map --batch<<EOT
3877c478bd9Sstevel@tonic-gategeometry $device $cylinders $heads $sectors
3887c478bd9Sstevel@tonic-gateroot $root
3897c478bd9Sstevel@tonic-gatesetup $device
3907c478bd9Sstevel@tonic-gategeometry $geo_option -w $type_option $device $cylinders $heads $sectors
3917c478bd9Sstevel@tonic-gateEOT
3927c478bd9Sstevel@tonic-gate
3937c478bd9Sstevel@tonic-gateecho "-------------------WHAT'S NEXT?-------------------------------------"
3947c478bd9Sstevel@tonic-gateecho
3957c478bd9Sstevel@tonic-gate
3967c478bd9Sstevel@tonic-gatecat <<EOF
3977c478bd9Sstevel@tonic-gateIf you have created an image aimed to a floppy, then something like:
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gatedd if=<type>.image of=/dev/fd0[u<size>] bs=512
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gatewill be more than enough... if you have formated the floppy correctly
4027c478bd9Sstevel@tonic-gateusing \`superformat' to be found in \`fdutils' package.
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gateFor El Torito floppy emulation :
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gatemkisofs -b <image> -c boot.catalog -o raw.iso <dir>
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gateAnd for El Torito Hard Disk emulation:
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gatemkisofs -b <image> -hard-disk-boot -c boot.catalog -o raw.iso <dir> 
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gateEnjoy!
4137c478bd9Sstevel@tonic-gateEOF
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gaterm -rf ${image}1
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gateexit 0
418