#!/bin/bash
# set -x
if [[ $(id -u) -ne 0 ]]; then
echo "You must be root to execute this script."
exit 1
fi
if [[ $# -lt 2 ]]; then
echo "Usage: $0 /path/to/windows.iso /dev/destination/pendrive"
exit 1
fi
ISOFILE=$1
ISODIR=/tmp/iso.$$
DSTDEVICE=$2
DSTDIR=/tmp/mounted.$$
function check_dirs() {
if [[ ! -d "$ISODIR" ]]; then
mkdir -p $ISODIR
fi
if [[ ! -d "$DSTDIR" ]]; then
mkdir -p $DSTDIR
fi
}
function gogogo() {
# $1 - isofile
# $2 - /dst/device
echo "Checking for dependencies... "
apt-get install gparted grub-pc-bin p7zip-full ntfs-3g -y -q > /dev/null \
&& check_dirs \
&& echo "Preparing device $2"; create_partition $2 \
&& mount $1 ${ISODIR} > /dev/null \
&& mount ${2}1 ${DSTDIR} > /dev/null \
&& echo "rsync $ISODIR -> $DSTDIR"; rsync --progress -a $ISODIR/ $DSTDIR/ \
ls -l $DSTDIR
echo
UUID=$(blkid | grep ${DSTDEVICE}1 | cut -d '"' -f 2)
echo "UUID: $UUID"
install_grub $UUID
echo "Syncing...."; sync
}
function install_grub() {
# $1 - device UUID
sudo grub-install --target=i386-pc --boot-directory="$DSTDIR/boot" ${DSTDEVICE}
cat <<- _EOF_ > $DSTDIR/boot/grub/grub.cfg
echo "If you see this, you have successfully booted from USB :)"
insmod ntfs
insmod search_fs_uuid
search --no-floppy --fs-uuid $1 --set root
ntldr /bootmgr
boot
_EOF_
}
function create_partition() {
#
# create new partition table on device with bootable NTFS partition
#
echo "WARNING! Non turning back point ahead."
echo "If you continue, you will lose all data on $1 device."
echo "Press ENTER to proceed."
read
(
echo o
echo n
echo
echo
echo
echo
echo a
echo w
) | fdisk $1 > /dev/null \
&& mkfs.ntfs -f ${1}1 > /dev/null
}
function cleanup() {
umount $ISODIR
umount $DSTDIR
rm -rf $ISODIR $DSTDIR
}
echo "WARNING! WARNING! WARNING! WARNING! WARNING!"
echo "All data on $2 will be lost."
echo -n "Are you sure, you want to continue? [y/n]: "
read choice
case "$choice" in
y|Y)
gogogo $ISOFILE $DSTDEVICE
;;
*)
echo "Bye"
exit 0
esac
cleanup
Comments