Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- v="usboot.sh v 1.00"
- # by Barry Jackson
- #
- # Changelog ####################################################
- # v 1.00 06/06/2010
- #
- ################################################################
- # This script creates a USB grub boot stick.
- # Run it as root:- ~]# sh usboot.sh
- # Simply follow the prompts.
- #
- # This is free software with absolutely no warranty whatsoever
- # and may be freely hacked and modified for any purpose.
- # Tested in Mandriva 2010 (i586)
- # Enjoy!
- #
- # To return a stick to normal use, after use as a boot stick, it
- # will need re-formatting using gparted. Diskdrake fails to run
- # with a boot stick (made this way) plugged in.
- #
- ################################################################
- chksu()
- {
- if [ $UID -ne 0 ] || [ $USER = "root" ] ;then
- echo "Sorry, you must run this as root. (Use su NOT su -)"
- exit 1
- fi
- }
- confirm()
- {
- params=( "$1" "$2" "$3" "$4" "$5" )
- let rval=3
- while [ $rval -gt 2 ];do
- echo -ne "${params[0]}"" ""${params[1]}"" "
- ans=
- rval=
- read ans
- if [ -z "$ans" ] || [ ${#ans} -gt 1 ];then
- rval=3
- else
- case "$ans" in
- [${params[2]}]*)
- rval=0
- ;;
- [${params[3]}]*)
- rval=1
- ;;
- [${params[4]}]*)
- rval=2
- ;;
- *)
- rval=3
- ;;
- esac
- fi
- done
- return $rval
- }
- detect()
- {
- blkid -g
- let l=0
- a=($(blkid|cut -d: -f1|cut -d/ -f3))
- echo -n "Plug in empty USB stick now. Detecting."
- while [ ${#d[@]} -eq 0 ]; do
- if [ $l -gt 30 ];then
- echo
- cat << NOPART
- ###########################################################
- No device has been detected and the script has timed out.
- If a device was plugged in then it may have no partitions,
- or an unsupportd format for example iso9660.
- Whatever the reason, usboot.sh cannot detect this device.
- NOPART
- echo -en "\nHit ENTER to continue..."
- read s ; unset s
- return 1
- fi
- b=($(blkid|cut -d: -f1|cut -d/ -f3))
- d=($( echo -n "${a[@]} ${b[@]}"|tr ' ' '\n'|sort|uniq -u ))
- if echo $d | grep -q "tmp" ;then
- unset d
- continue
- fi
- sleep 1
- let l=$l+1
- echo -n "."
- done
- blkid -g
- return 0
- }
- # Let's get started ----
- chksu
- if chksession -F | grep -q "GNOME";then
- /etc/init.d/haldaemon stop > /dev/null 2>&1
- halstopped=true
- fi
- clear
- echo "USB Boot Stick Creator - "$v
- echo "Please close any file managers now."
- unset d
- if confirm "Unplug the device now. Continue?" "[y/n]" "Yy" "Nn";then
- if detect;then
- d=$(echo $d | cut -b -3)
- echo
- echo "Device $d found"
- if confirm "To make a grub boot stick answer (Y)es - This will delete any data on $d" "[Y/N]" "Yy" "Nn";then
- echo "Creating boot stick"
- if confirm "Are you sure you want to lose all data on device $d" "[Y/N]" "Yy" "Nn";then
- cd /boot/grub
- dd if=stage1 of=/dev/$d bs=512 count=1
- dd if=stage2 of=/dev/$d bs=512 seek=1
- echo "Unplug the device now, before using it."
- fi
- fi
- fi
- fi
- if $halstopped ;then
- /etc/init.d/haldaemon start > /dev/null 2>&1
- fi
- echo "Bye! :-)"
- # End of usboot.sh
Add Comment
Please, Sign In to add comment