#!/bin/bash
#
# Make a bootable usb stick with BackTrack: Configure the stick, copy a DVD image
# and install the bootloader.
# See also http://www.knoppix.net/wiki/Bootable_USB_Key , espacially for removing 
# the badware U3 if you have a stick with U3.
# 
# Tested with some 512 MB, 1 GB, 2 GB, 4 GB, 8 GB and 16 GiB sticks.

# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 43):
# Dr. Rolf Freitag (rolf dot freitag at email dot de) wrote this file.
# As long as you retain this notice you can do whatever
# the LGPL (Lesser GNU public License) allows with this stuff.
# If you think this stuff is worth it, you can send me money via
# paypal or if we met some day you can buy me a beer in return.
# ----------------------------------------------------------------------------


# Version 1.1, 2010-05-18, Dr. Rolf Freitag.



# For debugging and first use: Be verbose and stop in case of an error like usb disconnect or dead device.
#set -xe
# Be verbose and stop when a variable isn't set.
set -xu

# Device (see dmesg and/or fdisk -l and correct if necessary or you may lose your data on a HDD/SSD)!!!
device=sdb

# BackTrack image.
imagefile="bt4-final.iso"

echo -n "This script will install a bootable BackTrack on device "
echo -n "$device"
echo "."
echo "Starting in 10 s (press <Ctrl>-C if you want to abort)." 
sleep 10

# Test of stability.
# Comment the following line for fast installation!
#/usr/bin/time badblocks -wsv -o badblocks_0_"$device".txt /dev/"$device"

# Test for speed and badblocks or missing blocks; override previous data.
# The average speed is size/time *0.5 (0.5 because of read and write).
# Comment the following line for fast installation!
#/usr/bin/time badblocks -t random -wsv -o badblocks_1_"$device".txt /dev/"$device"

#clear MBR
dd if=/dev/zero of=/dev/"$device" bs=512 count=1

###############################################

# Partitioning and formatting section: Uncomment only the right section!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# For sticks of size < 1 GiB/GB
# If the stick does not with this geometry, try 0 64 32 for 1 GiB/GB or 0 255 63
#mkdiskimage -1 /dev/"$device" 0 32 32

# For sticks of size = 1 GiB/GB: use zipdisk geometry
#mkdiskimage -1 /dev/"$device" 0 64 32

# For sticks of size > 1 GiB/GB AND <= 2 GiB/GB
#mkdiskimage -F -1 /dev/"$device" 0 128 32

# For sticks > 2 GiB/GB: The common HDD geometry.
mkdiskimage -F -1 /dev/"$device" 0 255 63

sync
sleep 1
#mkdosfs /dev/"$device"1

# prepare mountpoint directories
mkdir -p /mnt/iso
mkdir -p /mnt/"$device"1

# mount the stick
mount -o async,rw,noatime,nodev /dev/"$device"1 /mnt/"$device"1

# Get the DVD image (if not downloded already).
# If the file is already downloaded, you should link it to the acutal 
# directory, e. g. by
# ln -s /home/ftp/images/bt4-final.iso bt4-final.iso
wget -c ftp://ftp.uio.no/linux/backtrack/"$imagefile"

# todo: get the md5sums to check the md5sum of the image file and the files copied
# to the USB stick

# mount the image
mount -o loop "$imagefile" /mnt/iso 

# copy the rest of the image; this takes some minutes!
cp -Lrv /mnt/iso/. /mnt/"$device"1/

# make tmp directory for other files; e. g. for transfering drivers or pictures
mkdir /mnt/"$device"1/tmp

# Install the bootloader into the MBR of the stick.
grub-install --root-directory=/mnt/"$device"1 /dev/"$device"

# unmount the image
umount /mnt/iso

# finish; this can also take some minutes!
sync &
umount /mnt/"$device"1

echo "finished Backtrack usb-stick on device"
echo "$device"
echo "."

# beep finish
/usr/bin/beep -f 500 -l 100 -n -f 1000 -l 100 -n -f 500 -l 100

exit 0

