2 # MaKe a Bootable IMAGE --- 1.44, 2.88 and El Torito no-emulation mode
3 # C) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>
4 # C) 2001,2002,2003 Robert Millan <robertmh@gnu.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, you can either send email to this
19 # program's maintainer or write to: The Free Software Foundation,
20 # Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
33 # You can set GRUB_PATH if you need to use a specially located GRUB.
34 # This MUST end by a '/'!
37 #----------------------------DON'T CHANGE: INTERNALS
47 bk_120
=$
((2 * 15 * 80))
48 bk_144
=$
((2 * 18 * 80))
49 bk_288
=$
((2 * 36 * 80))
50 bk_160
=$
((2 * 20 * 80))
51 bk_168
=$
((2 * 21 * 80))
52 bk_174
=$
((2 * 21 * 83))
59 # Name by which this script was invoked.
60 program
=`echo "$0" | sed -e 's/[^\/]*\///g'`
61 version_number
='$Revision$'
64 Usage: $program [-hVF] [-t TYPE] [-d DIRECTORY] [-s FS_TYPE] -f TAR_FILE
65 Make a Bootable IMAGE using GRUB as a bootloader
69 -d DIRECTORY [default CWD]
70 Directory where the boot.image and the partition subdirectories
73 Name of the tar file containing the filesystem to install. Can
74 be a pure tar file [.tar] or a compressed tar file
77 Type of the file system to create on the virtual disk. Choices
79 ext2 on GNU [default is ext2]
80 ext2, minix or msdos on GNU/Linux [default is ext2]
83 Type of the image to create. Choices are '1.20', '1.44', '1.60',
84 '1.68', '1.74', '2.88' or 'hd' [default is hd]
86 Force to set the set_dpt flag (unnecessary 99% of the time! Be
90 turn Debugging on [xtrace]
92 display this Help and exit
94 display Version information and exit
96 Copyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
97 Copyright (c) 2001,2002 Robert Millan <zeratul2@wanadoo.es>.
100 version
="mkbimage $version_number
102 Written by Thierry Laronde and Robert Millan.
104 Copyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
105 Copyright (c) 2001,2002,2003 Robert Millan <zeratul2@wanadoo.es>.
107 This is free software under the GPL version 2 or later; see the source for
108 copying conditions. There is NO warranty, not even for MERCHANTABILITY or
109 FITNESS FOR A PARTICULAR PURPOSE."
116 bug
) echo "This is a bug!";
118 option
) echo "Unknow option"; echo "$usage";;
119 missing_argument
) echo "You must give an argument to the option!";
121 missing_option
) echo "You must indicate at least one option!";
123 must_be_root
) echo "You must be root! (or install e2tools/mtools)";;
124 unknown_fs
) if [ $uname = Linux
];
125 then echo "The GNU/Linux supported fs are: ext2, minix or msdos!";
126 elif [ $uname = GNU
];
127 then echo "The GNU supported fs is ext2!";
129 unknown_format
) echo "The tar file must be .tar|.tar.gz|.tar.bz2!";;
130 wont_fit
) echo "The files won't fit on the selected type of media!";;
131 wrong_directory
) echo "Directory inexistant or not given!";
133 wrong_file
) echo "File inexistant or empty!";
135 wrong_type
) echo "The type specified is not a valid one!";
141 # create a filesystem of type $fs in $image with offset $offset
146 *) lo_options
="-o $offset";;
149 if [ "$offset" = "0" ] ; then
151 elif [ `id -u` = "0" ] ; then
152 losetup
$lo_options /dev
/loop1
$image
154 losetup
-d /dev
/loop1
160 # copy ${image}1/* to ${image}:/, assuming ${image} contains a filesystem
161 # of type $fs in offset $offset
166 *) lo_options
="-o $offset";;
180 if [ "$offset" = 0 ] && which $cp > /dev
/null
; then
181 for dir
in $
(cd ${image}1 && find -type d
) ; do
184 for file in $
(cd ${image}1 && find -type f
) ; do
185 $cp ${image}1/$file ${image}:$file
187 elif [ "`id -u`" = "0" ] ; then
188 losetup
$lo_options /dev
/loop1
$image
190 mount
-t $fs /dev
/loop1
${image}.mnt
191 cp -a ${image}1/* ${image}.mnt
/ && sync
194 losetup
-d /dev
/loop1
200 #**********************************************************************
202 #**********************************************************************
204 #---------------------- Getting the options
206 [ $# -eq 0 ] && error missing_option
;
208 while [ $# -gt 0 ]; do
212 [ ! -d "$1" ] && error wrong_directory
;;
215 [ -z "$tarfile" ] && error missing_argument
;;
220 -F) geo_option
="-F";;
223 -h|
--help) echo "$usage"; exit 0;;
224 -V|
--version) echo "$version"; exit 0;;
229 #---------------------- Sanity checks
230 [ ! "$tarfile" ] && error missing_argument
;
231 [ ! -s "$tarfile" ] && error wrong_file
;
233 if [ ! "$image_type" ]; then
235 elif [ "$image_type" != "1.20" ] && [ "$image_type" != "1.44" ] \
236 && [ "$image_type" != "1.60" ] && [ "$image_type" != "1.68" ] \
237 && [ "$image_type" != "2.88" ] && [ "$image_type" != "1.74" ] \
238 && [ "$image_type" != "hd" ] && [ "$image_type" != "1.60" ] ; then
242 [ ! "$fs" ] && fs
=ext2
244 # Carlo Contavalli reported that I [TL] have forgotten to specify the
245 # partition ID for sfdisk to correctly fill the partition table (ext2 is the
246 # default on Linux, so this worked in this case...). This is fixed below.
248 ext2
) mkfs_options
="-m 0";
249 part_id
="83";; # This is the default
250 # ufs) if [ $uname = Linux ];
251 # then error unknown_fs;
253 minix
) if [ $uname = GNU
];
254 then error unknown_fs
;
256 mkfs_options
="-v"; # Minix version 2
259 msdos
) if [ $uname = GNU
];
260 then error unknown_fs
;
262 mkfs_options
="-f 1 -F 12"; # the smallest...
265 *) error unknown_fs
;;
268 # What type of tar file has been given ?
270 suffix
=`echo "$tarfile" | sed -n 's/^.*\.\([targbz2]\{2,3\}\)$/\1/p'`
272 tar) decompress
="cat";;
273 gz
) decompress
="gunzip -c";;
274 bz2
) decompress
="bunzip2 -c";;
275 *) error unknown_format
;;
277 #---------------------- Initializations
279 [ ! "$dir" ] && dir
=`pwd`
281 image
=$dir/$image_type.image
282 device_map
=$dir/device.map
284 # First, find the size of the tar file in block_size.
285 file_size
=`$decompress $tarfile | wc -c | tr -d ' '`
286 file_size
=$
(($file_size / $block_size + 1))
288 # Increase in order to be sure that with a fs there will be enough
290 file_size
=$
(($file_size + $file_size / 10))
292 case "$image_type" in
295 cyl_size
=$
((16 * 63));
296 # Create the minimum number of cylinders. At the moment, we leave
297 # some space by rounding everything up by adding 1 cylinder, plus
298 # another one for MBR + reserved track.
299 cylinders
=$
(($file_size / $cyl_size + 2));;
300 1.20) [ $file_size -ge $bk_120 ] && error wont_fit
;
303 cyl_size
=$
((2 * 15));
305 1.44) [ $file_size -ge $bk_144 ] && error wont_fit
;
308 cyl_size
=$
((2 * 18));
310 1.60) [ $file_size -ge $bk_160 ] && error wont_fit
;
313 cyl_size
=$
((2 * 20));
316 1.68) [ $file_size -ge $bk_168 ] && error wont_fit
;
319 cyl_size
=$
((2 * 21));
321 1.74) [ $file_size -ge $bk_174 ] && error wont_fit
;
324 cyl_size
=$
((2 * 21));
326 2.88) [ $file_size -ge $bk_288 ] && error wont_fit
;
329 cyl_size
=$
((2 * 36));
334 type_option
="-t $image_type"
336 # We start by creating a virtual disk which size is the number of
337 # cylinders of $cyl_size mandatory to put the files stocked in the $tarfile
338 # Create the empty virtual disk
339 dd if=/dev
/zero of
=$image bs
=$block_size count
=$
(($cyl_size * $cylinders))
341 # We then format the virtual disk
342 # NOTE: the El Torito specification wants only one partition. So we
343 # create the first, and the remaining 3 entries are empty.
345 if [ "$image_type" = "hd" ]; then
346 sfdisk
-C $cylinders -H $heads -S $sectors -D $image<<EOT
351 offset
="$(($sectors * $block_size))"
357 # It's time now to create the filesystem on the first partition.
360 # then untar the files
361 [ ! -e ${image}1 ] || { echo "${image}1 exists, please remove it first"; exit 1;}
363 $decompress $tarfile |
tar -C ${image}1 $debug -xf -
365 # copy the untarred files into the filesystem image
368 #We verify that the stage2 exists and we search the name
369 stage2_os_name
=`find ${image}1 -name stage2 -type f`
371 [ -r "$stage2_os_name" ] ||
{ echo "I can't find stage2!"; exit 1;}
373 #------------------------- GRUB stuff
374 if [ "$image_type" = "hd" ]; then
382 cat<<EOT >$device_map
386 ${GRUB_PATH}grub
--device-map=$device_map --batch<<EOT
387 geometry $device $cylinders $heads $sectors
390 geometry $geo_option -w $type_option $device $cylinders $heads $sectors
393 echo "-------------------WHAT'S NEXT?-------------------------------------"
397 If you have created an image aimed to a floppy, then something like:
399 dd if=<type>.image of=/dev/fd0[u<size>] bs=512
401 will be more than enough... if you have formated the floppy correctly
402 using \`superformat' to be found in \`fdutils' package.
404 For El Torito floppy emulation :
406 mkisofs -b <image> -c boot.catalog -o raw.iso <dir>
408 And for El Torito Hard Disk emulation:
410 mkisofs -b <image> -hard-disk-boot -c boot.catalog -o raw.iso <dir>