Making bootable USB keys
Samuel Martín Moro
faust64 at gmail.com
Thu Sep 3 15:35:56 UTC 2009
Hello
I'm having some troubles, trying to create bootable USB keys.
I found (freebsd-hackers ML archives) a script, supposed to create the
bootable image from my iso file.
But, it still don't boot... (I may do it wrong)
In details:
-We distribute a FreeBSD (4.7, 5.4, 6.2 and 7.2) "custom" server.
-We burn our install CD (and, in a few, our USB sticks) on a Ferdora 9
(sorry...)
-USB sticks must contain a FAT32 partition (we'ld like to provide doc for
windows users)
Well, my english isn't so great... so I'll post my code (more
understandable)
--maker.sh--
#!/bin/sh
ISO_DIR=/r00t
ISO_PFIX=r00t
VERSION=5.9.3.0
ISO_FILE=$ISO_DIR/$ISO_PFIX-$VERSION.img
DEVICE=
TMPDOC=/mnt/tmpdoc
DOCDIR=/root/samuel/docdir
ERR=
SFX=
MBR=/root/samuel/mbr
BT1=/root/samuel/boot1
BT2=/root/samuel/boot2
if [ -e "$1" ]; then
DEVICE=$1
elif [ "$1" -a -e "/dev/$1" ]; then
DEVICE=/dev/$1
elif [ "$1" ]; then
echo "$0: incorrect device specified" >&2
exit
else
echo "$0: must specify device" >&2
exit
fi
for i in `mount | cut -d ' ' -f 1`
do
if [ "`echo $i | grep $DEVICE`" ]; then
echo "$0: $i already mounted" >&2
echo " umount it manually or choose an other drive" >&2
exit
fi
done
if [ -e "$TMPDOC" -a -d "$TMPDOC" ]; then
echo "$0: removing $TMPDOC directory" >&2
rm -rf $TMPDOC
elif [ -e "$TMPDOC" ]; then
mv $TMPDOC $TMPDOC.old
echo "$0: moved $TMPDOC to $TMPDOC.old" >&2
fi
mkdir $TMPDOC
if [ "$2" ]; then
echo $2 | grep "\.img$" >/dev/null || SFX=".img"
fi
if [ -e "$2$SFX" ]; then
ISO_FILE=$2$SFX
elif [ "$2" -a -e "$ISO_DIR/$2$SFX" ]; then
ISO_FILE=$ISO_DIR/$2
elif [ "$2" -a -e "$ISO_DIR/$ISO_PFIX-$2$SFX" ]; then
ISO_FILE="$ISO_DIR/$ISO_PFX-$2$SFX"
else
echo "$0: will use default file \`$ISO_FILE'" >&2
echo " as system image source" >&2
fi
if [ -e "$ISO_FILE" ]; then
MSize=`ls -l $ISO_FILE | awk '{print $5}'`
else
echo "$0: $ISO_FILE doesn't exist!" >&2
rm -rf $TMPDOC
exit
fi
if [ -z "$MSize" -o "$MSize" -lt 1 ]; then
echo "$0: bad image size (size=$MSize)" >&2
rm -rf $TMPDOC
exit
fi
while :
do
echo " [ Working on $DEVICE ]"
echo -n " determining device geometry "
infos=`fdisk -l $DEVICE 2>/dev/null | grep "[0-9]* heads"`
ident=`fdisk -l $DEVICE 2>/dev/null | awk '/Disk identifier/{print $3}'`
csz=`fdisk -l $DEVICE 2>/dev/null | awk '/Units = cylinders /{print
$7}'`
eval `echo $infos | awk '{print "hpc=" $1 " sec=" $3 " cyl=" $5}'`
if [ -z "$hpc" -o -z "$sec" -o -z "$cyl" -o -z "$csz" ]; then
echo " [ FAIL ]"
echo "$0: can't get infos for device $DEVICE" >&2
rm -rf $TMPDOC
exit
fi
echo " [ OK ]"
echo -n " initializing partition table "
# dd if=/dev/zero of=$DEVICE bs=$csz count=1 >/dev/null 2>&1
dd if=$BT1 of=$DEVICE >/dev/null 2>&1
round=128
tocyl=`expr $hpc '*' $sec '*' $csz`
ret=`expr $MSize % $tocyl`
MSize=`expr $MSize / $tocyl`
test "$ret" -eq "0" || MSize=`expr $MSize + 1`
s2len=$MSize
s2off=`expr $cyl - $s2len - 1`
s1len=`expr $s2off - 1`
s1off=1
sfdisk -DLqf $DEVICE >/dev/null 2>&1 <<EOF
$s1off $s1len b
$s2off $s2len a5 *
EOF
echo " [ OK ]"
echo -n " formatting FAT32 partition "
dd if=/dev/zero of=${DEVICE}1 bs=$csz count=1 >/dev/null 2>&1
mkdosfs -i 42424242 -n "Docs" -F 32 ${DEVICE}1 >/dev/null 2>&1
mount -t vfat ${DEVICE}1 $TMPDOC || ERR=1
if [ "$ERR" ]; then
echo " [ FAIL ]"
echo "$0: unable to mount ${DEVICE}1 on $TMPDOC"
rm -rf $TMPDOC
exit
fi
echo " [ OK ]"
echo -n " copying documentation files "
cp -rp $DOCDIR/* $TMPDOC/ >/dev/null 2>&1 || ERR=2
if [ "$ERR" ]; then
echo " [ FAIL ]"
echo "$0: unable to copy doc files"
ERR=
fi
umount ${DEVICE}1
echo " [ OK ]"
echo -n " copying system "
dd if=$ISO_FILE of=${DEVICE}2 status=noxfer >/dev/null 2>&1
echo " [ OK ]"
mbrsig $DEVICE 2>&1 | awk '{print " marking device with serial " $3
}'
echo " [ Device ready! ]"
echo ""
echo -n "<?> Create new USB key ? [Y/N] : " && read i
test "$i" = "Y" -o "$i" = "y" -o "$i" = "O" -o "$i" = "o" || i=
test -z "$i" && echo " [ leaving ]" && break
echo " Please, remove current USB key, insert new one and press enter"
read i
done
rmdir $TMPDOC
--EOF--
So, this is a "USB stick generator" I'm working on.
It seems to work. (I've not tested everything, but the basis is OK)
The stick is correctly parted.
The documentation is copied.
My only problem is that it still don't wan't to boot...
At the beginning, I was trying to paste my ISO file directly in ${DEVICE}2
Then, I found the following shell script, which is supposed to make my
bootable image from my ISO file
I changed 2/3 things, but some of you may recognize it anyway:
--ISOtoIMG.sh--
#!/bin/sh
MAKEFS=makefs
MKLABEL=bsdlabel
BSDTAR=tar
DD="dd status=noxfer"
make_freebsd_image()
{
local tree=$1
local imagefile=$2
local boot1=${tree}/boot/boot1
local boot2=${tree}/boot/boot2
echo "convert tree $tree image $imagefile"
${MAKEFS} -t ffs -o bsize=4096 -o fsize=512 -f 50 ${imagefile} ${tree}
>/dev/null 2>&1
${MKLABEL} -w -f ${imagefile} auto >/dev/null 2>&1
${MKLABEL} -f ${imagefile} 2>/dev/null | sed -e '/ c:/{p;s/c:/a:/;}' | \
${MKLABEL} -R -f ${imagefile} /dev/stdin >/dev/null 2>&1
${DD} if=${boot1} of=${imagefile} conv=notrunc >/dev/null 2>&1
${DD} if=${boot2} iseek=1 ibs=276 2>/dev/null | \
${DD} of=${imagefile} oseek=1 obs=788 conv=notrunc >/dev/null 2>&1
}
extract_image()
{
[ -f $1 ] || return
local tmp="${tree}.tree"
[ -e ${tmp} ] && rm -rf ${tmp}
mkdir -p $tmp
echo "extracting $tree in $tmp"
(cd $tmp && ${BSDTAR} xf $tree)
tree=$tmp
}
if [ -z "$1" ]; then
echo "$0: usage" >&2
echo " $0 {ISO_input} | {system_root IMG_output}" >&2
exit
fi
tree=`realpath $1`
[ "$2" ] && image=`realpath $2` || image=`echo $tree | sed "s/.iso/.img/"`
extract_image $tree
make_freebsd_image $tree $image
[ -d "$tmp" ] && (chmod -R +w $tmp && rm -rf $tmp)
--EOF--
This seems to work, too...
I'm just surprised:
root at granit:~/samuel# l -h /r00t/r00t-5.9.3.0*
-rw-r--r-- 1 root root 566M 2009-09-03 15:29 /r00t/r00t-5.9.3.0.img
-rw-r--r-- 1 root root 526M 2009-08-08 06:58 /r00t/r00t-5.9.3.0.iso
new file is 40M heavier than our iso image...
Also, in the first script, I tried to do the first dd (initializing
${DEVICE}) with:
- if=$MBR
- if=$BT1
- if=$BT2
- if=/dev/zero
none of that worked...
So.
Does someone understand what am I doing wrong?!
Thanks for you help!
Samuel Martín Moro
CamTrace
{EPITECH.} tek4
More information about the freebsd-questions
mailing list