barjac

usboot.sh v 1.00

Jun 7th, 2010
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.20 KB | None | 0 0
  1. #!/bin/bash
  2. v="usboot.sh v 1.00"
  3. # by Barry Jackson
  4. #
  5. # Changelog ####################################################
  6. # v 1.00 06/06/2010
  7. #
  8. ################################################################
  9. # This script creates a USB grub boot stick.
  10. # Run it as root:-  ~]# sh usboot.sh
  11. # Simply follow the prompts.
  12. #
  13. # This is free software with absolutely no warranty whatsoever
  14. # and may be freely hacked and modified for any purpose.
  15. # Tested in Mandriva 2010 (i586)
  16. # Enjoy!
  17. #
  18. # To return a stick to normal use, after use as a boot stick, it
  19. # will need re-formatting using gparted. Diskdrake fails to run
  20. # with a boot stick (made this way) plugged in.
  21. #
  22. ################################################################
  23.  
  24. chksu()
  25. {
  26. if [ $UID -ne 0 ] || [ $USER = "root" ] ;then
  27.    echo "Sorry, you must run this as root. (Use su NOT su -)"
  28.    exit 1
  29. fi
  30. }
  31. confirm()
  32. {
  33. params=( "$1" "$2" "$3" "$4" "$5" )
  34. let rval=3
  35. while [ $rval -gt 2 ];do
  36.     echo -ne "${params[0]}"" ""${params[1]}"" "
  37.     ans=
  38.     rval=
  39.     read ans
  40.     if [ -z "$ans" ] || [ ${#ans} -gt 1 ];then
  41.         rval=3
  42.     else
  43.         case "$ans" in
  44.             [${params[2]}]*)
  45.                 rval=0
  46.                 ;;
  47.             [${params[3]}]*)
  48.                 rval=1
  49.                 ;;
  50.         [${params[4]}]*)
  51.                 rval=2
  52.                 ;;
  53.             *)
  54.                 rval=3
  55.                 ;;
  56.         esac
  57.     fi
  58. done
  59.     return $rval
  60. }
  61.  
  62. detect()
  63. {
  64.  blkid -g
  65.   let l=0
  66.   a=($(blkid|cut -d: -f1|cut -d/ -f3))
  67.   echo -n "Plug in empty USB stick now. Detecting."
  68.   while [ ${#d[@]} -eq 0 ]; do
  69.     if [ $l -gt 30 ];then
  70. echo
  71. cat << NOPART
  72. ###########################################################
  73. No device has been detected and the script has timed out.
  74. If a device was plugged in then it may have no partitions,
  75. or an unsupportd format for example iso9660.
  76. Whatever the reason, usboot.sh cannot detect this device.
  77. NOPART
  78.        echo -en "\nHit ENTER to continue..."
  79.       read s ; unset s
  80.           return 1
  81.     fi
  82.   b=($(blkid|cut -d: -f1|cut -d/ -f3))
  83.   d=($( echo -n "${a[@]} ${b[@]}"|tr ' ' '\n'|sort|uniq -u ))
  84.   if echo $d | grep -q "tmp" ;then
  85.      unset d
  86.      continue
  87.   fi
  88.   sleep 1
  89.   let l=$l+1
  90.   echo -n "."
  91.   done
  92.   blkid -g
  93.   return 0
  94. }
  95.  
  96. # Let's get started ----
  97. chksu
  98. if chksession -F | grep -q "GNOME";then
  99. /etc/init.d/haldaemon stop  > /dev/null 2>&1
  100. halstopped=true
  101. fi
  102. clear
  103. echo "USB Boot Stick Creator - "$v
  104. echo "Please close any file managers now."
  105. unset d
  106. if confirm "Unplug the device now. Continue?" "[y/n]" "Yy" "Nn";then
  107.   if detect;then
  108.     d=$(echo $d | cut -b -3)
  109.     echo
  110.     echo "Device $d found"
  111.     if confirm "To make a grub boot stick answer (Y)es - This will delete any data on $d" "[Y/N]" "Yy" "Nn";then
  112.       echo "Creating boot stick"
  113.       if confirm "Are you sure you want to lose all data on device $d" "[Y/N]" "Yy" "Nn";then
  114.       cd /boot/grub
  115.       dd if=stage1 of=/dev/$d bs=512 count=1
  116.       dd if=stage2 of=/dev/$d bs=512 seek=1
  117.       echo "Unplug the device now, before using it."
  118.       fi
  119.     fi  
  120.   fi
  121. fi
  122. if $halstopped ;then
  123. /etc/init.d/haldaemon start > /dev/null 2>&1
  124. fi
  125. echo "Bye! :-)"
  126. # End of usboot.sh
Add Comment
Please, Sign In to add comment