xref: /titanic_51/usr/src/grub/grub-0.97/util/mkbimage (revision 1b8adde7ba7d5e04395c141c5400dc2cffd7d809)
1*1b8adde7SWilliam Kucharski#!/bin/sh
2*1b8adde7SWilliam Kucharski# MaKe a Bootable IMAGE --- 1.44, 2.88 and El Torito no-emulation mode
3*1b8adde7SWilliam Kucharski# C) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>
4*1b8adde7SWilliam Kucharski# C) 2001,2002,2003 Robert Millan <robertmh@gnu.org>
5*1b8adde7SWilliam Kucharski
6*1b8adde7SWilliam Kucharski
7*1b8adde7SWilliam Kucharski# This program is free software; you can redistribute it and/or modify
8*1b8adde7SWilliam Kucharski# it under the terms of the GNU General Public License as published by
9*1b8adde7SWilliam Kucharski# the Free Software Foundation; either version 2, or (at your option)
10*1b8adde7SWilliam Kucharski# any later version.
11*1b8adde7SWilliam Kucharski#
12*1b8adde7SWilliam Kucharski# This program is distributed in the hope that it will be useful,
13*1b8adde7SWilliam Kucharski# but WITHOUT ANY WARRANTY; without even the implied warranty of
14*1b8adde7SWilliam Kucharski# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*1b8adde7SWilliam Kucharski# GNU General Public License for more details.
16*1b8adde7SWilliam Kucharski#
17*1b8adde7SWilliam Kucharski# You should have received a copy of the GNU General Public License
18*1b8adde7SWilliam Kucharski# along with this program; if not, you can either send email to this
19*1b8adde7SWilliam Kucharski# program's maintainer or write to: The Free Software Foundation,
20*1b8adde7SWilliam Kucharski# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
21*1b8adde7SWilliam Kucharski
22*1b8adde7SWilliam Kucharski# $Id: mkbimage,v 1.19 2004/07/21 14:43:04 robertmh Exp $
23*1b8adde7SWilliam Kucharski
24*1b8adde7SWilliam Kucharski# Global variables
25*1b8adde7SWilliam Kucharskitarfile=
26*1b8adde7SWilliam Kucharskidir=
27*1b8adde7SWilliam Kucharskifs= #file system type
28*1b8adde7SWilliam Kucharskidecompress=
29*1b8adde7SWilliam Kucharskiimage_type=
30*1b8adde7SWilliam Kucharskiuname=`uname -s`
31*1b8adde7SWilliam KucharskiPATH=/sbin:$PATH
32*1b8adde7SWilliam Kucharski
33*1b8adde7SWilliam Kucharski# You can set GRUB_PATH  if you need to use a specially located GRUB.
34*1b8adde7SWilliam Kucharski# This MUST end by a '/'!
35*1b8adde7SWilliam Kucharski
36*1b8adde7SWilliam Kucharski
37*1b8adde7SWilliam Kucharski#----------------------------DON'T CHANGE: INTERNALS
38*1b8adde7SWilliam Kucharski
39*1b8adde7SWilliam Kucharskiblock_size=512
40*1b8adde7SWilliam Kucharskicylinders=
41*1b8adde7SWilliam Kucharskiheads=
42*1b8adde7SWilliam Kucharskisectors=
43*1b8adde7SWilliam Kucharskicyl_size=
44*1b8adde7SWilliam Kucharskitype_option=
45*1b8adde7SWilliam Kucharskigeo_option=
46*1b8adde7SWilliam Kucharskiimage=
47*1b8adde7SWilliam Kucharskibk_120=$((2 * 15 * 80))
48*1b8adde7SWilliam Kucharskibk_144=$((2 * 18 * 80))
49*1b8adde7SWilliam Kucharskibk_288=$((2 * 36 * 80))
50*1b8adde7SWilliam Kucharskibk_160=$((2 * 20 * 80))
51*1b8adde7SWilliam Kucharskibk_168=$((2 * 21 * 80))
52*1b8adde7SWilliam Kucharskibk_174=$((2 * 21 * 83))
53*1b8adde7SWilliam Kucharskilo_options=
54*1b8adde7SWilliam Kucharskidevice_map=
55*1b8adde7SWilliam Kucharskimkfs_options=
56*1b8adde7SWilliam Kucharskidebug=
57*1b8adde7SWilliam Kucharskistage2_os_name=
58*1b8adde7SWilliam Kucharski
59*1b8adde7SWilliam Kucharski# Name by which this script was invoked.
60*1b8adde7SWilliam Kucharskiprogram=`echo "$0" | sed -e 's/[^\/]*\///g'`
61*1b8adde7SWilliam Kucharskiversion_number='$Revision: 1.19 $'
62*1b8adde7SWilliam Kucharski
63*1b8adde7SWilliam Kucharskiusage="
64*1b8adde7SWilliam KucharskiUsage: $program [-hVF] [-t TYPE] [-d DIRECTORY] [-s FS_TYPE] -f TAR_FILE
65*1b8adde7SWilliam KucharskiMake a Bootable IMAGE using GRUB as a bootloader
66*1b8adde7SWilliam Kucharski
67*1b8adde7SWilliam KucharskiOptions:
68*1b8adde7SWilliam Kucharski Actions:
69*1b8adde7SWilliam Kucharski -d DIRECTORY [default CWD]
70*1b8adde7SWilliam Kucharski	    Directory where the boot.image and the partition subdirectories
71*1b8adde7SWilliam Kucharski	    are/will be created
72*1b8adde7SWilliam Kucharski -f TAR_FILE
73*1b8adde7SWilliam Kucharski	    Name of the tar file containing the filesystem to install. Can
74*1b8adde7SWilliam Kucharski	    be a pure tar file [.tar] or a compressed tar file
75*1b8adde7SWilliam Kucharski	    [.tar.gz|.tar.bz2]
76*1b8adde7SWilliam Kucharski -s FS_TYPE
77*1b8adde7SWilliam Kucharski	    Type of the file system to create on the virtual disk. Choices
78*1b8adde7SWilliam Kucharski	    are:
79*1b8adde7SWilliam Kucharski		ext2 on GNU [default is ext2]
80*1b8adde7SWilliam Kucharski		ext2, minix or msdos on GNU/Linux [default is ext2]
81*1b8adde7SWilliam Kucharski
82*1b8adde7SWilliam Kucharski -t TYPE
83*1b8adde7SWilliam Kucharski	    Type of the image to create. Choices are '1.20', '1.44', '1.60',
84*1b8adde7SWilliam Kucharski	    '1.68', '1.74', '2.88' or 'hd' [default is hd]
85*1b8adde7SWilliam Kucharski -F
86*1b8adde7SWilliam Kucharski	    Force to set the set_dpt flag (unnecessary 99% of the time! Be
87*1b8adde7SWilliam Kucharski	    careful!
88*1b8adde7SWilliam Kucharski Informations:
89*1b8adde7SWilliam Kucharski -D
90*1b8adde7SWilliam Kucharski	    turn Debugging on [xtrace]
91*1b8adde7SWilliam Kucharski -h|--help
92*1b8adde7SWilliam Kucharski	    display this Help and exit
93*1b8adde7SWilliam Kucharski -V|--version
94*1b8adde7SWilliam Kucharski	    display Version information and exit
95*1b8adde7SWilliam Kucharski
96*1b8adde7SWilliam KucharskiCopyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
97*1b8adde7SWilliam KucharskiCopyright (c) 2001,2002 Robert Millan <zeratul2@wanadoo.es>.
98*1b8adde7SWilliam KucharskiGPLed."
99*1b8adde7SWilliam Kucharski
100*1b8adde7SWilliam Kucharskiversion="mkbimage $version_number
101*1b8adde7SWilliam Kucharski
102*1b8adde7SWilliam KucharskiWritten by Thierry Laronde and Robert Millan.
103*1b8adde7SWilliam Kucharski
104*1b8adde7SWilliam KucharskiCopyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
105*1b8adde7SWilliam KucharskiCopyright (c) 2001,2002,2003 Robert Millan <zeratul2@wanadoo.es>.
106*1b8adde7SWilliam Kucharski
107*1b8adde7SWilliam KucharskiThis is free software under the GPL version 2 or later; see the source for
108*1b8adde7SWilliam Kucharskicopying conditions.  There is NO warranty, not even for MERCHANTABILITY or
109*1b8adde7SWilliam KucharskiFITNESS FOR A PARTICULAR PURPOSE."
110*1b8adde7SWilliam Kucharski
111*1b8adde7SWilliam Kucharski# Functions
112*1b8adde7SWilliam Kucharski
113*1b8adde7SWilliam Kucharskierror ()
114*1b8adde7SWilliam Kucharski{
115*1b8adde7SWilliam Kucharski	case $1 in
116*1b8adde7SWilliam Kucharski	bug) echo "This is a bug!";
117*1b8adde7SWilliam Kucharski		echo "$usage";;
118*1b8adde7SWilliam Kucharski	option) echo "Unknow option"; echo "$usage";;
119*1b8adde7SWilliam Kucharski	missing_argument) echo "You must give an argument to the option!";
120*1b8adde7SWilliam Kucharski		echo "$usage";;
121*1b8adde7SWilliam Kucharski	missing_option) echo "You must indicate at least one option!";
122*1b8adde7SWilliam Kucharski		echo "$usage";;
123*1b8adde7SWilliam Kucharski	must_be_root) echo "You must be root! (or install e2tools/mtools)";;
124*1b8adde7SWilliam Kucharski	unknown_fs)	if [ $uname = Linux ];
125*1b8adde7SWilliam Kucharski				then echo "The GNU/Linux supported fs are: ext2, minix or msdos!";
126*1b8adde7SWilliam Kucharski			elif [ $uname = GNU ];
127*1b8adde7SWilliam Kucharski				then echo "The GNU supported fs is ext2!";
128*1b8adde7SWilliam Kucharski			fi;;
129*1b8adde7SWilliam Kucharski	unknown_format) echo "The tar file must be .tar|.tar.gz|.tar.bz2!";;
130*1b8adde7SWilliam Kucharski	wont_fit) echo "The files won't fit on the selected type of media!";;
131*1b8adde7SWilliam Kucharski	wrong_directory) echo "Directory inexistant or not given!";
132*1b8adde7SWilliam Kucharski		echo "$usage";;
133*1b8adde7SWilliam Kucharski	wrong_file) echo "File inexistant or empty!";
134*1b8adde7SWilliam Kucharski		echo "$usage";;
135*1b8adde7SWilliam Kucharski	wrong_type) echo "The type specified is not a valid one!";
136*1b8adde7SWilliam Kucharski		echo "$usage";;
137*1b8adde7SWilliam Kucharski	esac
138*1b8adde7SWilliam Kucharski	exit 1
139*1b8adde7SWilliam Kucharski}
140*1b8adde7SWilliam Kucharski
141*1b8adde7SWilliam Kucharski# create a filesystem of type $fs in $image with offset $offset
142*1b8adde7SWilliam Kucharskimkbimage_mkfs ()
143*1b8adde7SWilliam Kucharski{
144*1b8adde7SWilliam Kucharski  case $offset in
145*1b8adde7SWilliam Kucharski    0)  lo_options="";;
146*1b8adde7SWilliam Kucharski    *) lo_options="-o $offset";;
147*1b8adde7SWilliam Kucharski  esac
148*1b8adde7SWilliam Kucharski
149*1b8adde7SWilliam Kucharski  if [ "$offset" = "0" ] ; then
150*1b8adde7SWilliam Kucharski    mkfs.$fs -F $image
151*1b8adde7SWilliam Kucharski  elif [ `id -u` = "0" ] ; then
152*1b8adde7SWilliam Kucharski    losetup $lo_options /dev/loop1 $image
153*1b8adde7SWilliam Kucharski    mkfs.$fs /dev/loop1
154*1b8adde7SWilliam Kucharski    losetup -d /dev/loop1
155*1b8adde7SWilliam Kucharski  else
156*1b8adde7SWilliam Kucharski    error must_be_root
157*1b8adde7SWilliam Kucharski  fi
158*1b8adde7SWilliam Kucharski}
159*1b8adde7SWilliam Kucharski
160*1b8adde7SWilliam Kucharski# copy ${image}1/* to ${image}:/, assuming ${image} contains a filesystem
161*1b8adde7SWilliam Kucharski# of type $fs in offset $offset
162*1b8adde7SWilliam Kucharskimkbimage_cp ()
163*1b8adde7SWilliam Kucharski{
164*1b8adde7SWilliam Kucharski  case $offset in
165*1b8adde7SWilliam Kucharski    0)  lo_options="";;
166*1b8adde7SWilliam Kucharski    *)  lo_options="-o $offset";;
167*1b8adde7SWilliam Kucharski  esac
168*1b8adde7SWilliam Kucharski  case $fs in
169*1b8adde7SWilliam Kucharski    ext2)
170*1b8adde7SWilliam Kucharski      cp="e2cp";
171*1b8adde7SWilliam Kucharski      mkdir="e2mkdir";;
172*1b8adde7SWilliam Kucharski    vfat)
173*1b8adde7SWilliam Kucharski      cp="mcopy";
174*1b8adde7SWilliam Kucharski      mkdir="mmd";;
175*1b8adde7SWilliam Kucharski    *)
176*1b8adde7SWilliam Kucharski      cp="";
177*1b8adde7SWilliam Kucharski      mkdir="";;
178*1b8adde7SWilliam Kucharski  esac
179*1b8adde7SWilliam Kucharski
180*1b8adde7SWilliam Kucharski  if [ "$offset" = 0 ] && which $cp > /dev/null ; then
181*1b8adde7SWilliam Kucharski    for dir in $(cd ${image}1 && find -type d) ; do
182*1b8adde7SWilliam Kucharski	$mkdir ${image}:$dir
183*1b8adde7SWilliam Kucharski    done
184*1b8adde7SWilliam Kucharski    for file in $(cd ${image}1 && find -type f) ; do
185*1b8adde7SWilliam Kucharski	$cp ${image}1/$file ${image}:$file
186*1b8adde7SWilliam Kucharski    done
187*1b8adde7SWilliam Kucharski  elif [ "`id -u`" = "0" ] ; then
188*1b8adde7SWilliam Kucharski    losetup $lo_options /dev/loop1 $image
189*1b8adde7SWilliam Kucharski    mkdir ${image}.mnt
190*1b8adde7SWilliam Kucharski    mount -t $fs /dev/loop1 ${image}.mnt
191*1b8adde7SWilliam Kucharski    cp -a ${image}1/* ${image}.mnt/ && sync
192*1b8adde7SWilliam Kucharski    umount ${image}.mnt
193*1b8adde7SWilliam Kucharski    rmdir ${image}.mnt
194*1b8adde7SWilliam Kucharski    losetup -d /dev/loop1
195*1b8adde7SWilliam Kucharski  else
196*1b8adde7SWilliam Kucharski    error must_be_root
197*1b8adde7SWilliam Kucharski  fi
198*1b8adde7SWilliam Kucharski}
199*1b8adde7SWilliam Kucharski
200*1b8adde7SWilliam Kucharski#**********************************************************************
201*1b8adde7SWilliam Kucharski#                          MAIN PROGRAM                               *
202*1b8adde7SWilliam Kucharski#**********************************************************************
203*1b8adde7SWilliam Kucharski
204*1b8adde7SWilliam Kucharski#---------------------- Getting the options
205*1b8adde7SWilliam Kucharski
206*1b8adde7SWilliam Kucharski[ $# -eq 0 ] && error missing_option;
207*1b8adde7SWilliam Kucharski
208*1b8adde7SWilliam Kucharskiwhile [ $# -gt 0 ]; do
209*1b8adde7SWilliam Kucharski	case "$1" in
210*1b8adde7SWilliam Kucharski		-d) shift;
211*1b8adde7SWilliam Kucharski			dir="$1";
212*1b8adde7SWilliam Kucharski			[ ! -d "$1" ] && error wrong_directory;;
213*1b8adde7SWilliam Kucharski		-f) shift;
214*1b8adde7SWilliam Kucharski			tarfile="$1";
215*1b8adde7SWilliam Kucharski			[ -z "$tarfile" ] && error missing_argument;;
216*1b8adde7SWilliam Kucharski		-s) shift;
217*1b8adde7SWilliam Kucharski			fs="$1";;
218*1b8adde7SWilliam Kucharski		-t) shift;
219*1b8adde7SWilliam Kucharski			image_type="$1";;
220*1b8adde7SWilliam Kucharski		-F) geo_option="-F";;
221*1b8adde7SWilliam Kucharski		-D) debug="-v";
222*1b8adde7SWilliam Kucharski		    set -x;;
223*1b8adde7SWilliam Kucharski		-h|--help) echo "$usage"; exit 0;;
224*1b8adde7SWilliam Kucharski		-V|--version) echo "$version"; exit 0;;
225*1b8adde7SWilliam Kucharski	 	*) error option ;;
226*1b8adde7SWilliam Kucharski	esac
227*1b8adde7SWilliam Kucharskishift
228*1b8adde7SWilliam Kucharskidone
229*1b8adde7SWilliam Kucharski#---------------------- Sanity checks
230*1b8adde7SWilliam Kucharski[ ! "$tarfile" ] && error missing_argument;
231*1b8adde7SWilliam Kucharski[ ! -s "$tarfile" ] && error wrong_file;
232*1b8adde7SWilliam Kucharski
233*1b8adde7SWilliam Kucharskiif [ ! "$image_type" ]; then
234*1b8adde7SWilliam Kucharski	image_type=hd;
235*1b8adde7SWilliam Kucharskielif [ "$image_type" != "1.20" ] && [ "$image_type" != "1.44" ] \
236*1b8adde7SWilliam Kucharski  && [ "$image_type" != "1.60" ] && [ "$image_type" != "1.68" ] \
237*1b8adde7SWilliam Kucharski  && [ "$image_type" != "2.88" ] && [ "$image_type" != "1.74" ] \
238*1b8adde7SWilliam Kucharski  && [ "$image_type" != "hd" ] && [ "$image_type" != "1.60" ] ; then
239*1b8adde7SWilliam Kucharski  error wrong_type ;
240*1b8adde7SWilliam Kucharskifi
241*1b8adde7SWilliam Kucharski
242*1b8adde7SWilliam Kucharski[ ! "$fs" ] && fs=ext2
243*1b8adde7SWilliam Kucharski
244*1b8adde7SWilliam Kucharski# Carlo Contavalli reported that I [TL] have forgotten to specify the
245*1b8adde7SWilliam Kucharski# partition ID for sfdisk to correctly fill the partition table (ext2 is the
246*1b8adde7SWilliam Kucharski# default on Linux, so this worked in this case...). This is fixed below.
247*1b8adde7SWilliam Kucharskicase "$fs" in
248*1b8adde7SWilliam Kucharski	ext2) mkfs_options="-m 0";
249*1b8adde7SWilliam Kucharski		  part_id="83";; # This is the default
250*1b8adde7SWilliam Kucharski#	ufs)	if [ $uname = Linux ];
251*1b8adde7SWilliam Kucharski#		then error unknown_fs;
252*1b8adde7SWilliam Kucharski#		fi;;
253*1b8adde7SWilliam Kucharski	minix)	if [ $uname = GNU ];
254*1b8adde7SWilliam Kucharski		then error unknown_fs;
255*1b8adde7SWilliam Kucharski		else
256*1b8adde7SWilliam Kucharski			mkfs_options="-v"; # Minix version 2
257*1b8adde7SWilliam Kucharski			part_id="81";
258*1b8adde7SWilliam Kucharski		fi;;
259*1b8adde7SWilliam Kucharski	msdos)	if [ $uname = GNU ];
260*1b8adde7SWilliam Kucharski		then error unknown_fs;
261*1b8adde7SWilliam Kucharski		else
262*1b8adde7SWilliam Kucharski			mkfs_options="-f 1 -F 12"; # the smallest...
263*1b8adde7SWilliam Kucharski			part_id="1";
264*1b8adde7SWilliam Kucharski		fi;;
265*1b8adde7SWilliam Kucharski	*) error unknown_fs;;
266*1b8adde7SWilliam Kucharskiesac
267*1b8adde7SWilliam Kucharski
268*1b8adde7SWilliam Kucharski# What type of tar file has been given ?
269*1b8adde7SWilliam Kucharski
270*1b8adde7SWilliam Kucharskisuffix=`echo "$tarfile" | sed -n 's/^.*\.\([targbz2]\{2,3\}\)$/\1/p'`
271*1b8adde7SWilliam Kucharskicase "$suffix" in
272*1b8adde7SWilliam Kucharski	tar) decompress="cat";;
273*1b8adde7SWilliam Kucharski	gz) decompress="gunzip -c";;
274*1b8adde7SWilliam Kucharski	bz2) decompress="bunzip2 -c";;
275*1b8adde7SWilliam Kucharski	*) error unknown_format;;
276*1b8adde7SWilliam Kucharskiesac
277*1b8adde7SWilliam Kucharski#---------------------- Initializations
278*1b8adde7SWilliam Kucharski
279*1b8adde7SWilliam Kucharski[ ! "$dir" ] && dir=`pwd`
280*1b8adde7SWilliam Kucharski
281*1b8adde7SWilliam Kucharskiimage=$dir/$image_type.image
282*1b8adde7SWilliam Kucharskidevice_map=$dir/device.map
283*1b8adde7SWilliam Kucharski
284*1b8adde7SWilliam Kucharski# First, find the size of the tar file in block_size.
285*1b8adde7SWilliam Kucharskifile_size=`$decompress $tarfile | wc -c | tr -d ' '`
286*1b8adde7SWilliam Kucharskifile_size=$(($file_size / $block_size + 1))
287*1b8adde7SWilliam Kucharski
288*1b8adde7SWilliam Kucharski# Increase in order to be sure that with a fs there will be enough
289*1b8adde7SWilliam Kucharski# room (trying 110%)
290*1b8adde7SWilliam Kucharskifile_size=$(($file_size + $file_size / 10))
291*1b8adde7SWilliam Kucharski
292*1b8adde7SWilliam Kucharskicase "$image_type" in
293*1b8adde7SWilliam Kucharski  hd) heads=16;
294*1b8adde7SWilliam Kucharski    sectors=63;
295*1b8adde7SWilliam Kucharski    cyl_size=$((16 * 63));
296*1b8adde7SWilliam Kucharski    # Create the minimum number of cylinders. At the moment, we leave
297*1b8adde7SWilliam Kucharski    # some space by rounding everything up by adding 1 cylinder, plus
298*1b8adde7SWilliam Kucharski    # another one for MBR + reserved track.
299*1b8adde7SWilliam Kucharski    cylinders=$(($file_size / $cyl_size + 2));;
300*1b8adde7SWilliam Kucharski  1.20) [ $file_size -ge $bk_120 ] && error wont_fit;
301*1b8adde7SWilliam Kucharski    heads=2;
302*1b8adde7SWilliam Kucharski    sectors=15;
303*1b8adde7SWilliam Kucharski    cyl_size=$((2 * 15));
304*1b8adde7SWilliam Kucharski    cylinders=80;;
305*1b8adde7SWilliam Kucharski  1.44) [ $file_size -ge $bk_144 ] && error wont_fit;
306*1b8adde7SWilliam Kucharski    heads=2;
307*1b8adde7SWilliam Kucharski    sectors=18;
308*1b8adde7SWilliam Kucharski    cyl_size=$((2 * 18));
309*1b8adde7SWilliam Kucharski    cylinders=80;;
310*1b8adde7SWilliam Kucharski  1.60) [ $file_size -ge $bk_160 ] && error wont_fit;
311*1b8adde7SWilliam Kucharski    heads=2;
312*1b8adde7SWilliam Kucharski    sectors=20;
313*1b8adde7SWilliam Kucharski    cyl_size=$((2 * 20));
314*1b8adde7SWilliam Kucharski    cylinders=80;
315*1b8adde7SWilliam Kucharski    geo_option="-F";;
316*1b8adde7SWilliam Kucharski  1.68) [ $file_size -ge $bk_168 ] && error wont_fit;
317*1b8adde7SWilliam Kucharski    heads=2;
318*1b8adde7SWilliam Kucharski    sectors=21;
319*1b8adde7SWilliam Kucharski    cyl_size=$((2 * 21));
320*1b8adde7SWilliam Kucharski    cylinders=80;;
321*1b8adde7SWilliam Kucharski  1.74) [ $file_size -ge $bk_174 ] && error wont_fit;
322*1b8adde7SWilliam Kucharski    heads=2;
323*1b8adde7SWilliam Kucharski    sectors=21;
324*1b8adde7SWilliam Kucharski    cyl_size=$((2 * 21));
325*1b8adde7SWilliam Kucharski    cylinders=83;;
326*1b8adde7SWilliam Kucharski  2.88) [ $file_size -ge $bk_288 ] && error wont_fit;
327*1b8adde7SWilliam Kucharski    heads=2;
328*1b8adde7SWilliam Kucharski    sectors=36;
329*1b8adde7SWilliam Kucharski    cyl_size=$((2 * 36));
330*1b8adde7SWilliam Kucharski    cylinders=80;;
331*1b8adde7SWilliam Kucharski  *) error bug;;
332*1b8adde7SWilliam Kucharskiesac
333*1b8adde7SWilliam Kucharski
334*1b8adde7SWilliam Kucharskitype_option="-t $image_type"
335*1b8adde7SWilliam Kucharski
336*1b8adde7SWilliam Kucharski# We start by creating a virtual disk which size is the number of
337*1b8adde7SWilliam Kucharski# cylinders of $cyl_size mandatory to put the files stocked in the $tarfile
338*1b8adde7SWilliam Kucharski# Create the empty virtual disk
339*1b8adde7SWilliam Kucharskidd if=/dev/zero of=$image bs=$block_size count=$(($cyl_size * $cylinders))
340*1b8adde7SWilliam Kucharski
341*1b8adde7SWilliam Kucharski# We then format the virtual disk
342*1b8adde7SWilliam Kucharski# NOTE: the El Torito specification wants only one partition. So we
343*1b8adde7SWilliam Kucharski# create the first, and the remaining 3 entries are empty.
344*1b8adde7SWilliam Kucharski
345*1b8adde7SWilliam Kucharskiif [ "$image_type" = "hd" ]; then
346*1b8adde7SWilliam Kucharski  sfdisk -C $cylinders -H $heads -S $sectors -D $image<<EOT
347*1b8adde7SWilliam Kucharski,,$part_id,*,0,1,1
348*1b8adde7SWilliam Kucharski
349*1b8adde7SWilliam Kucharski
350*1b8adde7SWilliam KucharskiEOT
351*1b8adde7SWilliam Kucharski  offset="$(($sectors * $block_size))"
352*1b8adde7SWilliam Kucharski  type_option=
353*1b8adde7SWilliam Kucharskielse
354*1b8adde7SWilliam Kucharski  offset="0"
355*1b8adde7SWilliam Kucharskifi
356*1b8adde7SWilliam Kucharski
357*1b8adde7SWilliam Kucharski# It's time now to create the filesystem on the first partition.
358*1b8adde7SWilliam Kucharskimkbimage_mkfs
359*1b8adde7SWilliam Kucharski
360*1b8adde7SWilliam Kucharski# then untar the files
361*1b8adde7SWilliam Kucharski[ ! -e ${image}1 ] || { echo "${image}1 exists, please remove it first"; exit 1;}
362*1b8adde7SWilliam Kucharskimkdir -p ${image}1
363*1b8adde7SWilliam Kucharski$decompress $tarfile | tar -C ${image}1 $debug -xf -
364*1b8adde7SWilliam Kucharski
365*1b8adde7SWilliam Kucharski# copy the untarred files into the filesystem image
366*1b8adde7SWilliam Kucharskimkbimage_cp
367*1b8adde7SWilliam Kucharski
368*1b8adde7SWilliam Kucharski#We verify that the stage2 exists and we search the name
369*1b8adde7SWilliam Kucharskistage2_os_name=`find ${image}1 -name stage2 -type f`
370*1b8adde7SWilliam Kucharski
371*1b8adde7SWilliam Kucharski[ -r "$stage2_os_name" ] || { echo "I can't find stage2!"; exit 1;}
372*1b8adde7SWilliam Kucharski
373*1b8adde7SWilliam Kucharski#------------------------- GRUB stuff
374*1b8adde7SWilliam Kucharskiif [ "$image_type" = "hd" ]; then
375*1b8adde7SWilliam Kucharski	device='(hd0)'
376*1b8adde7SWilliam Kucharski	root='(hd0,0)'
377*1b8adde7SWilliam Kucharskielse
378*1b8adde7SWilliam Kucharski	device='(fd0)'
379*1b8adde7SWilliam Kucharski	root='(fd0)'
380*1b8adde7SWilliam Kucharskifi
381*1b8adde7SWilliam Kucharski
382*1b8adde7SWilliam Kucharskicat<<EOT >$device_map
383*1b8adde7SWilliam Kucharski$device	${image}
384*1b8adde7SWilliam KucharskiEOT
385*1b8adde7SWilliam Kucharski
386*1b8adde7SWilliam Kucharski${GRUB_PATH}grub --device-map=$device_map --batch<<EOT
387*1b8adde7SWilliam Kucharskigeometry $device $cylinders $heads $sectors
388*1b8adde7SWilliam Kucharskiroot $root
389*1b8adde7SWilliam Kucharskisetup $device
390*1b8adde7SWilliam Kucharskigeometry $geo_option -w $type_option $device $cylinders $heads $sectors
391*1b8adde7SWilliam KucharskiEOT
392*1b8adde7SWilliam Kucharski
393*1b8adde7SWilliam Kucharskiecho "-------------------WHAT'S NEXT?-------------------------------------"
394*1b8adde7SWilliam Kucharskiecho
395*1b8adde7SWilliam Kucharski
396*1b8adde7SWilliam Kucharskicat <<EOF
397*1b8adde7SWilliam KucharskiIf you have created an image aimed to a floppy, then something like:
398*1b8adde7SWilliam Kucharski
399*1b8adde7SWilliam Kucharskidd if=<type>.image of=/dev/fd0[u<size>] bs=512
400*1b8adde7SWilliam Kucharski
401*1b8adde7SWilliam Kucharskiwill be more than enough... if you have formated the floppy correctly
402*1b8adde7SWilliam Kucharskiusing \`superformat' to be found in \`fdutils' package.
403*1b8adde7SWilliam Kucharski
404*1b8adde7SWilliam KucharskiFor El Torito floppy emulation :
405*1b8adde7SWilliam Kucharski
406*1b8adde7SWilliam Kucharskimkisofs -b <image> -c boot.catalog -o raw.iso <dir>
407*1b8adde7SWilliam Kucharski
408*1b8adde7SWilliam KucharskiAnd for El Torito Hard Disk emulation:
409*1b8adde7SWilliam Kucharski
410*1b8adde7SWilliam Kucharskimkisofs -b <image> -hard-disk-boot -c boot.catalog -o raw.iso <dir> 
411*1b8adde7SWilliam Kucharski
412*1b8adde7SWilliam KucharskiEnjoy!
413*1b8adde7SWilliam KucharskiEOF
414*1b8adde7SWilliam Kucharski
415*1b8adde7SWilliam Kucharskirm -rf ${image}1
416*1b8adde7SWilliam Kucharski
417*1b8adde7SWilliam Kucharskiexit 0
418