rescue-usb/usbcreator.sh

173 lines
5.2 KiB
Bash

#!/bin/bash
#Check for root
if [[ $EUID -ne "0" ]]
then
echo "You must be root - try: sudo $0 or sudo bash $0"
exit 1
fi
#check for wipefs
command -v wipefs >/dev/null 2>&1 || { echo >&2 "I require wipefs but it's not installed. Aborting."; exit 1; }
script="$0"
basename="$(dirname $script)"
DIR="/home/temp/rescueusb"
#Warn user that this will detroit the things
echo "This will destroy all data and partitions on the device name you enter without further warning!!"
#get device name
echo "Please enter the device you wish to use (/dev/sdX):"
read dev
device=${dev}1
echo "Unmounting $device Partitions..."
for part in $dev*
do umount $part > /dev/null 2>&1
done
#umount $device
DD="${device:0:7}"
if [ "$DD" == "/dev/sd" ];
then
echo ""
else
echo "Bad device - must start with /dev/sd"
exit 1
fi
if [ ${device:8:1} == 0 ]
then
echo "Bad Partition! should end in the number 1 or 2 - e.g. /dev/sdc1"
exit1
fi
#format device
echo "Partitioning: $dev"
read -p "Proceed with partitioning (y/n): " opinion2
if [[ $opinion2 == 'y' ]] || [[ $opinion2 == 'yes' ]]
then
#umount $device > /dev/null 2>&1
label="RESCUE-USB"
##Remove existing data/partitions
echo "Removing existing partitions and data from $dev..."
#dd if=/dev/zero of=$dev bs=512 count=1 conv=notrunc > /dev/null 2>&1
wipefs -a $dev > /dev/null 2>&1
##Create partition table
echo "Creating partition table..."
parted $dev --script -- mklabel msdos > /dev/null 2>&1
##Create partitions
echo "Creating partitions on $device..."
parted $dev --script -- mkpart primary 0% 90% > /dev/null 2>&1
parted $dev --script -- mkpart primary 91% 100% > /dev/null 2>&1
#Create /home/temp if it doesn't exist
echo "Creating working directory of $DIR..."
mkdir -p $DIR
#Fetch Easy2Boot
echo "Fetching Easy2Boot..."
wget -O $DIR/E2B.zip "http://files.easy2boot.com/200003511-5bf395cedc/Easy2Boot_v1.A9.zip" > /dev/null 2>&1
#Extract it
echo "Extracting Easy2Boot..."
unzip -d $DIR/ $DIR/E2B.zip > /dev/null 2>&1
#Remove the zip
echo "Removing Easy2Boot zip..."
rm -f $DIR/E2B.zip > /dev/null 2>&1
echo "Formatting primary partition..."
#Run E2B's fmt.sh
/sbin/mkfs.vfat -F32 -n "$label" $device > /dev/null 2>&1
ptn=${device: -1}
echo Making partition "$ptn" active using parted...
sudo parted -s ${device%?} set $ptn boot on > /dev/null 2>&1
sleep 2
sync
sleep 5
echo "Mounting $device as /mnt/myusb"
mkdir -p /mnt/myusb 2>/dev/null
mount $device /mnt/myusb > /dev/null 2>&1
echo "Installing grub4dos to MBR"
chmod +rwx $DIR/_ISO/docs/linux_utils/bootlace.com > /dev/null 2>&1
$DIR/_ISO/docs/linux_utils/bootlace.com --time-out=0 ${device%?} > /dev/null 2>&1
echo "Installing grub4dos to PBR"
$DIR/_ISO/docs/linux_utils/bootlace.com --floppy=1 $device > /dev/null 2>&1
echo "Copying $DIR to /mnt/myusb..."
cp -r -i $DIR/* /mnt/myusb > /dev/null 2>&1
#Fetch ISOs to device
echo "Copying ISOs from internet to the USB..."
pushd $DIR
wget -O $DIR/chntpw.zip http://pogostick.net/~pnh/ntpasswd/cd140201.zip > /dev/null 2>&1
unzip chntpw.zip
cp cd140201.iso /mnt/myusb/_ISO/MAINMENU/chntpw.iso
wget -O $DIR/clonezilla.iso https://downloads.sourceforge.net/project/clonezilla/clonezilla_live_stable/2.6.0-37/clonezilla-live-2.6.0-37-amd64.iso > /dev/null 2>&1
cp clonezilla.iso /mnt/myusb/_ISO/MAINMENU/
wget -O $DIR/supergrub2disk.iso https://downloads.sourceforge.net/project/supergrub2/2.02s10/super_grub2_disk_2.02s10/super_grub2_disk_hybrid_2.02s10.iso > /dev/null 2>&1
cp supergrub2disk.iso /mnt/myusb/_ISO/MAINMENU/
wget -O $DIR/systemrescuecd.iso https://osdn.net/projects/systemrescuecd/storage/releases/6.0.2/systemrescuecd-6.0.2.iso > /dev/null 2>&1
popd > /dev/null 2>&1
pushd /mnt/myusb/_ISO/MAINMENU/ > /dev/null 2>&1
wget -nd -e robots=off -r --no-parent /mnt/myusb/_ISO/MAINMENU/ http://files.tfmm.co/isos/ --reject index.htm* > /dev/null 2>&1
mv acti-new.iso acronis-new.iso
mv acti-2012.iso acronis-2012.iso
mv WPEF.iso WinPE_Fixer.iso
popd > /dev/null 2>&1
#Use E2B's defragger
echo "Defragmenting the drive and ISOs..."
chmod +x $DIR/_ISO/docs/linux_utils/defragfs > /dev/null 2>&1
$DIR/_ISO/docs/linux_utils/defragfs /mnt/myusb -f > /dev/null 2>&1
#mount WinPE iso to copy /tools dir
echo "Mounting WinPE ISO and copying tools directory..."
mkdir $DIR/isomount > /dev/null 2>&1
mount -o loop /mnt/myusb/_ISO/MAINMENU/WinPE_Fixer.iso $DIR/isomount > /dev/null 2>&1
#copy tools dir
cp -r $DIR/isomount/tools /mnt/myusb/ > /dev/null 2>&1
#unmount ISO
umount $DIR/isomount > /dev/null 2>&1
#Get files to Update Menu settings
echo "Placing menu config files..."
cp resources/menu_defaults.txt /mnt/myusb/_ISO/menu_defaults.txt > /dev/null 2>&1
cp resources/MyE2B.cfg /mnt/myusb/_ISO/MyE2B.cfg > /dev/null 2>&1
#Remove working dir
echo "Removing working directory..."
rm -rf $DIR > /dev/null 2>&1
#unmount drive
echo "Unmounting drive..."
sleep 20
umount $dev* > /dev/null 2>&1
umount /mnt/myusb > /dev/null 2>&1
#remove mountpoint
echo "Removing mountpoint..."
rm -r /mnt/myusb > /dev/null 2>&1
#Complete
echo "Script completed, your USB has been created!"
exit 1
else
echo "You said no, stopping."
exit 1
fi