mkbimage revision 1
f743002678eb67b99bbc29fee116b65d9530fec0wrowe#!/bin/sh
80833bb9a1bf25dcf19e814438a4b311d2e1f4cffuankg# MaKe a Bootable IMAGE --- 1.44, 2.88 and El Torito no-emulation mode
6736c640e65e06990ef33af71ee81fac4df4ff5fjim# C) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>
7025945569c7fbb49f2e64e0d26f861d22a54675jorton# C) 2001,2002,2003 Robert Millan <robertmh@gnu.org>
7025945569c7fbb49f2e64e0d26f861d22a54675jorton
7025945569c7fbb49f2e64e0d26f861d22a54675jorton
7025945569c7fbb49f2e64e0d26f861d22a54675jorton# This program is free software; you can redistribute it and/or modify
e20c6ecbd465cd7dabb44acea6afafc7925f3a03sf# it under the terms of the GNU General Public License as published by
12b987b969f03ef98d9175a53d849ab62f5684fecovener# the Free Software Foundation; either version 2, or (at your option)
12b987b969f03ef98d9175a53d849ab62f5684fecovener# any later version.
4c21a9f062e187b87c9cec726023f0fc086008f2sf#
4c21a9f062e187b87c9cec726023f0fc086008f2sf# This program is distributed in the hope that it will be useful,
4c21a9f062e187b87c9cec726023f0fc086008f2sf# but WITHOUT ANY WARRANTY; without even the implied warranty of
4c21a9f062e187b87c9cec726023f0fc086008f2sf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e20c6ecbd465cd7dabb44acea6afafc7925f3a03sf# GNU General Public License for more details.
e20c6ecbd465cd7dabb44acea6afafc7925f3a03sf#
7025945569c7fbb49f2e64e0d26f861d22a54675jorton# You should have received a copy of the GNU General Public License
f5b3f41700a107b9df2b3c5a1cc3e5ea775fd8fesf# along with this program; if not, you can either send email to this
f5b3f41700a107b9df2b3c5a1cc3e5ea775fd8fesf# program's maintainer or write to: The Free Software Foundation,
f5b3f41700a107b9df2b3c5a1cc3e5ea775fd8fesf# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
ec7851f543720a66e94295707116b4bc91961030sf
ec7851f543720a66e94295707116b4bc91961030sf# $Id: mkbimage,v 1.19 2004/07/21 14:43:04 robertmh Exp $
ec7851f543720a66e94295707116b4bc91961030sf
ef056d09d39412b9f7d9b71f48ff3d00222833f6humbedooh# Global variables
ef056d09d39412b9f7d9b71f48ff3d00222833f6humbedoohtarfile=
ef056d09d39412b9f7d9b71f48ff3d00222833f6humbedoohdir=
304257c2b0d71c1e4cf3c5c819bb6b60e7c82d51sffs= #file system type
304257c2b0d71c1e4cf3c5c819bb6b60e7c82d51sfdecompress=
304257c2b0d71c1e4cf3c5c819bb6b60e7c82d51sfimage_type=
304257c2b0d71c1e4cf3c5c819bb6b60e7c82d51sfuname=`uname -s`
a9383516f09ba627694a14d533f55628426f5418jortonPATH=/usr/sbin:$PATH
a9383516f09ba627694a14d533f55628426f5418jorton
a9383516f09ba627694a14d533f55628426f5418jorton# You can set GRUB_PATH if you need to use a specially located GRUB.
099c357f282d4aebf2b32264f7dce6ffc0497c37sf# This MUST end by a '/'!
099c357f282d4aebf2b32264f7dce6ffc0497c37sf
099c357f282d4aebf2b32264f7dce6ffc0497c37sf
099c357f282d4aebf2b32264f7dce6ffc0497c37sf#----------------------------DON'T CHANGE: INTERNALS
7e51e049b88b11a1f0d9203f3b70b0a5de06f56ejorton
7e51e049b88b11a1f0d9203f3b70b0a5de06f56ejortonblock_size=512
7e51e049b88b11a1f0d9203f3b70b0a5de06f56ejortoncylinders=
d9b079a6dd66d36313be56f859c8c61153146527sfheads=
d9b079a6dd66d36313be56f859c8c61153146527sfsectors=
d9b079a6dd66d36313be56f859c8c61153146527sfcyl_size=
50f8c80eb4d3989ebf3f1341aeef4d2d302af162sftype_option=
50f8c80eb4d3989ebf3f1341aeef4d2d302af162sfgeo_option=
4bb0a88a01fb7b494bb02a8b881b5eab0308bda6sfimage=
50f8c80eb4d3989ebf3f1341aeef4d2d302af162sfbk_120=$((2 * 15 * 80))
295ba2e25e562ee52c7bf96808cf9d486ebfb325sfbk_144=$((2 * 18 * 80))
295ba2e25e562ee52c7bf96808cf9d486ebfb325sfbk_288=$((2 * 36 * 80))
295ba2e25e562ee52c7bf96808cf9d486ebfb325sfbk_160=$((2 * 20 * 80))
3104923cb51cf5826c97368d1f0d5202c352cd4fsfbk_168=$((2 * 21 * 80))
3104923cb51cf5826c97368d1f0d5202c352cd4fsfbk_174=$((2 * 21 * 83))
7b395e4e878c28a4784919cfd2e704ddd14a3390jortonlo_options=
7b395e4e878c28a4784919cfd2e704ddd14a3390jortondevice_map=
7b395e4e878c28a4784919cfd2e704ddd14a3390jortonmkfs_options=
7b395e4e878c28a4784919cfd2e704ddd14a3390jortondebug=
536e48c08d674acac5d44929318f2ad928edc361jortonstage2_os_name=
536e48c08d674acac5d44929318f2ad928edc361jorton
e81785da447b469da66f218b3f0244aab507958djorton# Name by which this script was invoked.
e81785da447b469da66f218b3f0244aab507958djortonprogram=`echo "$0" | sed -e 's/[^\/]*\///g'`
424b4e796cde9df955948c354cb4df6080a92d4esfversion_number='$Revision: 1.19 $'
424b4e796cde9df955948c354cb4df6080a92d4esf
424b4e796cde9df955948c354cb4df6080a92d4esfusage="
5cc1d035c9293c4d64b2344b5eac35edd1bd4537rpluemUsage: $program [-hVF] [-t TYPE] [-d DIRECTORY] [-s FS_TYPE] -f TAR_FILE
5cc1d035c9293c4d64b2344b5eac35edd1bd4537rpluemMake a Bootable IMAGE using GRUB as a bootloader
5cc1d035c9293c4d64b2344b5eac35edd1bd4537rpluem
5cc1d035c9293c4d64b2344b5eac35edd1bd4537rpluemOptions:
3e4e54d4e3fc0123c63d57aa84ac7ad7a8c73ff8jorton Actions:
3e4e54d4e3fc0123c63d57aa84ac7ad7a8c73ff8jorton -d DIRECTORY [default CWD]
3e4e54d4e3fc0123c63d57aa84ac7ad7a8c73ff8jorton Directory where the boot.image and the partition subdirectories
00f53c09b10014571b639d5e9df993fb188220a6sf are/will be created
00f53c09b10014571b639d5e9df993fb188220a6sf -f TAR_FILE
00f53c09b10014571b639d5e9df993fb188220a6sf Name of the tar file containing the filesystem to install. Can
00f53c09b10014571b639d5e9df993fb188220a6sf be a pure tar file [.tar] or a compressed tar file
459eaf0826f995b73a0dc066f59ea10d2824e72dsf [.tar.gz|.tar.bz2]
459eaf0826f995b73a0dc066f59ea10d2824e72dsf -s FS_TYPE
459eaf0826f995b73a0dc066f59ea10d2824e72dsf Type of the file system to create on the virtual disk. Choices
459eaf0826f995b73a0dc066f59ea10d2824e72dsf are:
53e9b27aba029b18be814df40bcf6f0428771d1efuankg ext2 on GNU [default is ext2]
53e9b27aba029b18be814df40bcf6f0428771d1efuankg ext2, minix or msdos on GNU/Linux [default is ext2]
53e9b27aba029b18be814df40bcf6f0428771d1efuankg
53e9b27aba029b18be814df40bcf6f0428771d1efuankg -t TYPE
53e9b27aba029b18be814df40bcf6f0428771d1efuankg Type of the image to create. Choices are '1.20', '1.44', '1.60',
21da42a6b8f551ef603bd06356d3bf71d6d0c21dsf '1.68', '1.74', '2.88' or 'hd' [default is hd]
21da42a6b8f551ef603bd06356d3bf71d6d0c21dsf -F
21da42a6b8f551ef603bd06356d3bf71d6d0c21dsf Force to set the set_dpt flag (unnecessary 99% of the time! Be
2876a591f17b0c6ed0cf28da929643965e67c953sf careful!
2876a591f17b0c6ed0cf28da929643965e67c953sf Informations:
2876a591f17b0c6ed0cf28da929643965e67c953sf -D
2876a591f17b0c6ed0cf28da929643965e67c953sf turn Debugging on [xtrace]
38eb64d29a17648e108852de42f23335aecaa8f8sf -h|--help
976f49eaf81ffe917272e4f1834115dec06962b0sf display this Help and exit
38eb64d29a17648e108852de42f23335aecaa8f8sf -V|--version
5a1f28ac93914b4bae892c69ac4b3e670bc10da8nd display Version information and exit
5a1f28ac93914b4bae892c69ac4b3e670bc10da8nd
5a1f28ac93914b4bae892c69ac4b3e670bc10da8ndCopyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
2ba1586475aa4ec972ca7c19b06d53bf76f0ee7efuankgCopyright (c) 2001,2002 Robert Millan <zeratul2@wanadoo.es>.
2ba1586475aa4ec972ca7c19b06d53bf76f0ee7efuankgGPLed."
c7502880be24058c7fc03771fda61f95bc238339sf
c7502880be24058c7fc03771fda61f95bc238339sfversion="mkbimage $version_number
c7502880be24058c7fc03771fda61f95bc238339sf
ca61ccd0c306c2c72df153688ba1b49f3eceed80sfWritten by Thierry Laronde and Robert Millan.
ca61ccd0c306c2c72df153688ba1b49f3eceed80sf
ca61ccd0c306c2c72df153688ba1b49f3eceed80sfCopyright (c) 2001,2002,2003 Thierry Laronde <tlaronde@polynum.org>.
6bb524f1895f30265a1431afc460977d391cb36bsfCopyright (c) 2001,2002,2003 Robert Millan <zeratul2@wanadoo.es>.
6bb524f1895f30265a1431afc460977d391cb36bsf
ca61ccd0c306c2c72df153688ba1b49f3eceed80sfThis is free software under the GPL version 2 or later; see the source for
6bb524f1895f30265a1431afc460977d391cb36bsfcopying conditions. There is NO warranty, not even for MERCHANTABILITY or
b40bfdb714672f972887f9f1f1e154e00125bb68covenerFITNESS FOR A PARTICULAR PURPOSE."
b40bfdb714672f972887f9f1f1e154e00125bb68covener
b40bfdb714672f972887f9f1f1e154e00125bb68covener# Functions
b40bfdb714672f972887f9f1f1e154e00125bb68covener
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrinerror ()
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin{
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin case $1 in
e6dd71992459d05a676b98b7963423dc5dc1e24aminfrin bug) echo "This is a bug!";
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin echo "$usage";;
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin option) echo "Unknow option"; echo "$usage";;
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin missing_argument) echo "You must give an argument to the option!";
23f1535d6a60817d2846bac0aea230ea475d7dccminfrin echo "$usage";;
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung missing_option) echo "You must indicate at least one option!";
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung echo "$usage";;
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung must_be_root) echo "You must be root! (or install e2tools/mtools)";;
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung unknown_fs) if [ $uname = Linux ];
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung then echo "The GNU/Linux supported fs are: ext2, minix or msdos!";
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung elif [ $uname = GNU ];
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung then echo "The GNU supported fs is ext2!";
ec7520b24cd80d34d82bbcaca153cbb23cc04bc0rjung fi;;
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick unknown_format) echo "The tar file must be .tar|.tar.gz|.tar.bz2!";;
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick wont_fit) echo "The files won't fit on the selected type of media!";;
0827cb14e550f6f65018431c22c2c913631c8f25kbrand wrong_directory) echo "Directory inexistant or not given!";
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick echo "$usage";;
ae600ca541efc686b34f8b1f21bd3d0741d37674covener wrong_file) echo "File inexistant or empty!";
6249dfa569d3b4f1f539665b979a80c6e335d93etrawick echo "$usage";;
cfa64348224b66dd1c9979b809406c4d15b1c137fielding wrong_type) echo "The type specified is not a valid one!";
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim echo "$usage";;
cfa64348224b66dd1c9979b809406c4d15b1c137fielding esac
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim exit 1
cfa64348224b66dd1c9979b809406c4d15b1c137fielding}
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajim
cfa64348224b66dd1c9979b809406c4d15b1c137fielding# create a filesystem of type $fs in $image with offset $offset
74499a117b3b2cd9666715a14f90c0e5d1a4ee8ajimmkbimage_mkfs ()
cfa64348224b66dd1c9979b809406c4d15b1c137fielding{
case $offset in
0) lo_options="";;
*) lo_options="-o $offset";;
esac
if [ "$offset" = "0" ] ; then
mkfs.$fs -F $image
elif [ `id -u` = "0" ] ; then
losetup $lo_options /dev/loop1 $image
mkfs.$fs /dev/loop1
losetup -d /dev/loop1
else
error must_be_root
fi
}
# copy ${image}1/* to ${image}:/, assuming ${image} contains a filesystem
# of type $fs in offset $offset
mkbimage_cp ()
{
case $offset in
0) lo_options="";;
*) lo_options="-o $offset";;
esac
case $fs in
ext2)
cp="e2cp";
mkdir="e2mkdir";;
vfat)
cp="mcopy";
mkdir="mmd";;
*)
cp="";
mkdir="";;
esac
if [ "$offset" = 0 ] && which $cp > /dev/null ; then
for dir in $(cd ${image}1 && find -type d) ; do
$mkdir ${image}:$dir
done
for file in $(cd ${image}1 && find -type f) ; do
$cp ${image}1/$file ${image}:$file
done
elif [ "`id -u`" = "0" ] ; then
losetup $lo_options /dev/loop1 $image
mkdir ${image}.mnt
mount -t $fs /dev/loop1 ${image}.mnt
cp -a ${image}1/* ${image}.mnt/ && sync
umount ${image}.mnt
rmdir ${image}.mnt
losetup -d /dev/loop1
else
error must_be_root
fi
}
#**********************************************************************
# MAIN PROGRAM *
#**********************************************************************
#---------------------- Getting the options
[ $# -eq 0 ] && error missing_option;
while [ $# -gt 0 ]; do
case "$1" in
-d) shift;
dir="$1";
[ ! -d "$1" ] && error wrong_directory;;
-f) shift;
tarfile="$1";
[ -z "$tarfile" ] && error missing_argument;;
-s) shift;
fs="$1";;
-t) shift;
image_type="$1";;
-F) geo_option="-F";;
-D) debug="-v";
set -x;;
-h|--help) echo "$usage"; exit 0;;
-V|--version) echo "$version"; exit 0;;
*) error option ;;
esac
shift
done
#---------------------- Sanity checks
[ ! "$tarfile" ] && error missing_argument;
[ ! -s "$tarfile" ] && error wrong_file;
if [ ! "$image_type" ]; then
image_type=hd;
elif [ "$image_type" != "1.20" ] && [ "$image_type" != "1.44" ] \
&& [ "$image_type" != "1.60" ] && [ "$image_type" != "1.68" ] \
&& [ "$image_type" != "2.88" ] && [ "$image_type" != "1.74" ] \
&& [ "$image_type" != "hd" ] && [ "$image_type" != "1.60" ] ; then
error wrong_type ;
fi
[ ! "$fs" ] && fs=ext2
# Carlo Contavalli reported that I [TL] have forgotten to specify the
# partition ID for sfdisk to correctly fill the partition table (ext2 is the
# default on Linux, so this worked in this case...). This is fixed below.
case "$fs" in
ext2) mkfs_options="-m 0";
part_id="83";; # This is the default
# ufs) if [ $uname = Linux ];
# then error unknown_fs;
# fi;;
minix) if [ $uname = GNU ];
then error unknown_fs;
else
mkfs_options="-v"; # Minix version 2
part_id="81";
fi;;
msdos) if [ $uname = GNU ];
then error unknown_fs;
else
mkfs_options="-f 1 -F 12"; # the smallest...
part_id="1";
fi;;
*) error unknown_fs;;
esac
# What type of tar file has been given ?
suffix=`echo "$tarfile" | sed -n 's/^.*\.\([targbz2]\{2,3\}\)$/\1/p'`
case "$suffix" in
tar) decompress="cat";;
gz) decompress="gunzip -c";;
bz2) decompress="bunzip2 -c";;
*) error unknown_format;;
esac
#---------------------- Initializations
[ ! "$dir" ] && dir=`pwd`
image=$dir/$image_type.image
device_map=$dir/device.map
# First, find the size of the tar file in block_size.
file_size=`$decompress $tarfile | wc -c | tr -d ' '`
file_size=$(($file_size / $block_size + 1))
# Increase in order to be sure that with a fs there will be enough
# room (trying 110%)
file_size=$(($file_size + $file_size / 10))
case "$image_type" in
hd) heads=16;
sectors=63;
cyl_size=$((16 * 63));
# Create the minimum number of cylinders. At the moment, we leave
# some space by rounding everything up by adding 1 cylinder, plus
# another one for MBR + reserved track.
cylinders=$(($file_size / $cyl_size + 2));;
1.20) [ $file_size -ge $bk_120 ] && error wont_fit;
heads=2;
sectors=15;
cyl_size=$((2 * 15));
cylinders=80;;
1.44) [ $file_size -ge $bk_144 ] && error wont_fit;
heads=2;
sectors=18;
cyl_size=$((2 * 18));
cylinders=80;;
1.60) [ $file_size -ge $bk_160 ] && error wont_fit;
heads=2;
sectors=20;
cyl_size=$((2 * 20));
cylinders=80;
geo_option="-F";;
1.68) [ $file_size -ge $bk_168 ] && error wont_fit;
heads=2;
sectors=21;
cyl_size=$((2 * 21));
cylinders=80;;
1.74) [ $file_size -ge $bk_174 ] && error wont_fit;
heads=2;
sectors=21;
cyl_size=$((2 * 21));
cylinders=83;;
2.88) [ $file_size -ge $bk_288 ] && error wont_fit;
heads=2;
sectors=36;
cyl_size=$((2 * 36));
cylinders=80;;
*) error bug;;
esac
type_option="-t $image_type"
# We start by creating a virtual disk which size is the number of
# cylinders of $cyl_size mandatory to put the files stocked in the $tarfile
# Create the empty virtual disk
dd if=/dev/zero of=$image bs=$block_size count=$(($cyl_size * $cylinders))
# We then format the virtual disk
# NOTE: the El Torito specification wants only one partition. So we
# create the first, and the remaining 3 entries are empty.
if [ "$image_type" = "hd" ]; then
sfdisk -C $cylinders -H $heads -S $sectors -D $image<<EOT
,,$part_id,*,0,1,1
EOT
offset="$(($sectors * $block_size))"
type_option=
else
offset="0"
fi
# It's time now to create the filesystem on the first partition.
mkbimage_mkfs
# then untar the files
[ ! -e ${image}1 ] || { echo "${image}1 exists, please remove it first"; exit 1;}
mkdir -p ${image}1
$decompress $tarfile | tar -C ${image}1 $debug -xf -
# copy the untarred files into the filesystem image
mkbimage_cp
#We verify that the stage2 exists and we search the name
stage2_os_name=`find ${image}1 -name stage2 -type f`
[ -r "$stage2_os_name" ] || { echo "I can't find stage2!"; exit 1;}
#------------------------- GRUB stuff
if [ "$image_type" = "hd" ]; then
device='(hd0)'
root='(hd0,0)'
else
device='(fd0)'
root='(fd0)'
fi
cat<<EOT >$device_map
$device ${image}
EOT
${GRUB_PATH}grub --device-map=$device_map --batch<<EOT
geometry $device $cylinders $heads $sectors
root $root
setup $device
geometry $geo_option -w $type_option $device $cylinders $heads $sectors
EOT
echo "-------------------WHAT'S NEXT?-------------------------------------"
echo
cat <<EOF
If you have created an image aimed to a floppy, then something like:
dd if=<type>.image of=/dev/fd0[u<size>] bs=512
will be more than enough... if you have formated the floppy correctly
using \`superformat' to be found in \`fdutils' package.
For El Torito floppy emulation :
mkisofs -b <image> -c boot.catalog -o raw.iso <dir>
And for El Torito Hard Disk emulation:
mkisofs -b <image> -hard-disk-boot -c boot.catalog -o raw.iso <dir>
Enjoy!
EOF
rm -rf ${image}1
exit 0