openSUSE Paste > S.C.L.U. - SuSE Create Live USB
Login

Info:

By James D. McDaniel, 1 Year ago, written in Bash. This post will never expire.

URL: http://paste.opensuse.org/800119

  1. #!/bin/bash
  2.  
  3. #: Title       : S.C.L.U. - SuSE Create Live USB
  4. #: Date Created: Mon Dec 12 20:11:40 CST 2011
  5. #: Last Edit   : Thu Dec 22 19:59:40 CST 2011
  6. #: Author      : James D. McDaniel
  7. #: Version     : 1.10
  8. #: Description : Create LiveUSB Thumb Drive from LiveCD/DVD ISO File
  9. #: Options     : None
  10.  
  11. TITLE="S.C.L.U. - SuSE Create Live USB - Version 1.10"
  12.  
  13. #
  14. # Written for the openSUSE forums on Thursday December 22, 2011
  15. #
  16.  
  17. #
  18. # Copy and Paste the text of this script into a text editor and save
  19. # it as the file sclu in the folder ~/bin (/home/username/bin).
  20. # This script must be marked executable to be used.  Please run
  21. # the following Terminal command: chmod +x ~/bin/sclu
  22. #
  23.  
  24. declare -a TDD
  25. declare -a TDS
  26. declare -a TDU
  27. declare -a TDN
  28. declare -a ISO
  29. declare -a ISOFiles
  30. declare -a ISODir
  31.  
  32. #
  33. # Maximum Thumb Drive size in Gigabytes or GB *********************************
  34. #
  35. MaxTDSize=34
  36.  
  37. #
  38. # Where to Download ISO Images ************************************************
  39. #
  40. Downloads="$HOME/Downloads"
  41.  
  42. #
  43. # ISO Download URL & Names ****************************************************
  44. #
  45. NumISOs=12
  46.  
  47. ISODir[1]="http://download.opensuse.org/pub/opensuse/distribution/12.1/iso/"
  48.  
  49. ISO[1]="openSUSE-12.1-KDE-LiveCD-i686.iso,1"
  50. ISO[2]="openSUSE-12.1-KDE-LiveCD-x86_64.iso,1"
  51. ISO[3]="openSUSE-12.1-GNOME-LiveCD-i686.iso,1"
  52. ISO[4]="openSUSE-12.1-GNOME-LiveCD-x86_64.iso,1"
  53. ISO[5]="openSUSE-12.1-DVD-i586.iso,1"
  54. ISO[6]="openSUSE-12.1-DVD-x86_64.iso,1"
  55.  
  56. ISODir[2]="http://download.opensuse.org/pub/opensuse/distribution/11.4/iso/"
  57.  
  58. ISO[7]="openSUSE-11.4-KDE-LiveCD-i686.iso,2"
  59. ISO[8]="openSUSE-11.4-KDE-LiveCD-x86_64.iso,2"
  60. ISO[9]="openSUSE-11.4-GNOME-LiveCD-i686.iso,2"
  61. ISO[10]="openSUSE-11.4-GNOME-LiveCD-x86_64.iso,2"
  62. ISO[11]="openSUSE-11.4-DVD-LiveCD-i686.iso,2"
  63. ISO[12]="openSUSE-11.4-DVD-LiveCD-x86_64.iso,2"
  64.  
  65. #
  66. # Do you want to see S.C.L.U. in color?  **************************************
  67. # The default is true, but can be set to false ********************************
  68. #
  69. use_color=true
  70.  
  71. #
  72. # Color Display Request - color forground background [b/n] ********************
  73. #
  74. # 0:Black 1:Blue 2:Green 3:Cyan 4:Red  5:Magenta 6:Yellow 7:White
  75. function color {
  76.   if [[ $3 == [Bb] ]] ; then
  77.     tput bold
  78.   fi
  79.   if [[ $3 == [Nn] ]] ; then
  80.     tput sgr0
  81.   fi
  82.   if $use_color ; then
  83.     tput setf $(( $1 ))
  84.     tput setb $(( $2 ))
  85.   else
  86.     tput setf 7
  87.     tput setb 0
  88.   fi
  89.   return 0
  90. }
  91.  
  92. #
  93. # This is the standard GPL Statement, leave at the top of the script.
  94. # Just use the command show_gpl after this function for it to be shown.
  95. #
  96. function show_gpl {
  97. echo ""
  98. echo "S.C.L.U. is a bash script file written to be used with openSUSE."
  99. echo "Copyright (C) 2011 by James D. McDaniel, jmcdaniel3@austin.rr.com"
  100. echo ""
  101. echo "This program is free software; you can redistribute it and/or modify"
  102. echo "it under the terms of the GNU General Public License as published by"
  103. echo "the Free Software Foundation; either version 2 of the License, or"
  104. echo "(at your option) any later version."
  105. echo ""
  106. echo "This program is distributed in the hope that it will be useful,"
  107. echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
  108. echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
  109. echo "GNU General Public License for more details."
  110. echo ""
  111. echo "You should have received a copy of the GNU General Public License"
  112. echo "along with this program; if not, write to the Free Software"
  113. echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
  114. echo ""
  115. }
  116.  
  117. #
  118. # Common S.C.L.U. Header display **************************************************
  119. #
  120. function sclu_header {
  121.   dash1="------------------------------------------------------"
  122.   clear
  123.   color 2 0 B
  124.   echo $dash1
  125.   color 7 2 B
  126.   echo "    $TITLE    "
  127.   color 2 0 B
  128.   echo $dash1
  129.   color 7 0 B
  130.   echo
  131.   return 0
  132. }
  133.  
  134. #
  135. # Create a String of spaces ***************************************************
  136. #
  137. function spad {
  138.   spaces=""
  139.   len=$1
  140.   total=1
  141.   while [ $total -le $len ] ; do
  142.     spaces=$spaces" "
  143.     let total=total+1
  144.   done
  145.   return 0
  146. }
  147.  
  148. #
  149. # Pad string with spaces on right *********************************************
  150. #
  151. function rpad {
  152. rword="$1"
  153. len=$2
  154. wlen=${#rword}
  155. if [ ${#rword} -lt $len ] ; then
  156.   let len2=len-wlen
  157.   space=""
  158.   while [ $len2 -gt 0 ] ; do
  159.     space="$space "
  160.     let len2=len2-1
  161.   done
  162.   rword="$rword$space"
  163. fi
  164. return 0
  165. }
  166.  
  167. #
  168. # Pad string with spaces on left **********************************************
  169. #
  170. function lpad {
  171. lword="$1"
  172. len=$2
  173. wlen=${#lword}
  174. if [ ${#lword} -lt $len ] ; then
  175.   let len2=len-wlen
  176.   space=""
  177.   while [ $len2 -gt 0 ] ; do
  178.     space=" "$space
  179.     let len2=len2-1
  180.   done
  181.   lword="$space$lword"
  182. fi
  183. return 0
  184. }
  185.  
  186. #
  187. # Check for the existance of the selected Downloads Folder ********************
  188. #
  189. function check_downloads {
  190. if [[ ! -d "$Downloads" ]] ; then
  191.   color 7 4 B
  192.   echo "The ISO Download Folder: $Downloads, does not exist!"
  193.   color 7 0 B
  194.   echo
  195.   echo -n "Do You wish for this folder to be created for you? (y/N): "
  196.   read CHOICE
  197.   if [[ $CHOICE == [Yy] ]] ; then
  198.     mkdir -p $Downloads
  199.   else
  200.     echo
  201.     echo "Please create or change this folder name in script & restart $(basename $0) again."
  202.     echo
  203.     exit 1
  204.   fi
  205. fi
  206. }
  207.  
  208. #
  209. # Check to see if we are root ************************************************
  210. #
  211. if [[ $UID -ne 0 ]]; then
  212.   check_downloads
  213.   color 7 4 B
  214.   id -Gn | grep -q wheel || echo "Root User Permissions are required, Please Enter the ..."
  215.   color 7 0 B
  216.   echo
  217.   sudo $0 $Downloads
  218.   Exit_Code=$?
  219.   show_gpl
  220.   echo
  221.   read -p "Press <enter> to Exit S.C.L.U. ..."
  222.   exit $Exit_Code
  223. else
  224.   if [ "$1" != "" ] ; then
  225.     Downloads="$1"
  226.   fi
  227. fi
  228.  
  229. #
  230. # Determine if the package dd_rescue is installed ***********************************
  231. #
  232. which dd_rescue > /dev/null
  233. Exit_Code=$?
  234. if [ $(( Exit_Code )) -ge 1 ] ; then
  235.   color 7 4 B
  236.   echo "The dd_rescue Utility Package is not installed!"
  237.   color 7 0 B
  238.   echo
  239.   echo -n "Would you like to install the dd_rescue package(y/N)? "
  240.   read CHOICE
  241.   if [[ $CHOICE == [Yy] ]] ; then
  242.     sudo zypper in dd_rescue
  243.   else
  244.     color 7 4 B
  245.     echo
  246.     echo "The dd_rescue Utility Package was not installed!"
  247.     color 7 0 B
  248.     exit 1
  249.   fi
  250. fi
  251.  
  252. #
  253. # Determine if the package aria2c is installed ***********************************
  254. #
  255. which aria2c > /dev/null
  256. Exit_Code=$?
  257. if [ $(( Exit_Code )) -ge 1 ] ; then
  258.   color 7 4 B
  259.   echo "The aria2 Utility Package is not installed!"
  260.   color 7 0 B
  261.   echo
  262.   echo -n "Would you like to install the aria2 package(y/N)? "
  263.   read CHOICE
  264.   if [[ $CHOICE == [Yy] ]] ; then
  265.     sudo zypper in aria2
  266.   else
  267.     color 7 4 B
  268.     echo
  269.     echo "The aria2 Utility Package was not installed!"
  270.     color 7 0 B
  271.     exit 1
  272.   fi
  273. fi
  274.  
  275. #
  276. # Determine if the package wget is installed ***********************************
  277. #
  278. which wget > /dev/null
  279. Exit_Code=$?
  280. if [ $(( Exit_Code )) -ge 1 ] ; then
  281.   color 7 4 B
  282.   echo "The wget Utility Package is not installed!"
  283.   color 7 0 B
  284.   echo
  285.   echo -n "Would you like to install the wget package(y/N)? "
  286.   read CHOICE
  287.   if [[ $CHOICE == [Yy] ]] ; then
  288.     sudo zypper in wget
  289.   else
  290.     color 7 4 B
  291.     echo
  292.     echo "The wget Utility Package was not installed!"
  293.     color 7 0 B
  294.     exit 1
  295.   fi
  296. fi
  297.  
  298. #
  299. # Determine file size *********************************************************
  300. # $2 is KB (1000), K (1024), MB (1000*1000), M (1024*1024), and so on for G, T, P, E, Z, Y.
  301. #
  302. function file_size {
  303.   if [ -e "$1" ] ; then
  304.     filesize=$(ls -l --block-size=$2 $1  | awk -F " " '{ print $5 }')
  305.   else
  306.     let filesize=0
  307.   fi
  308. }
  309.  
  310. #
  311. # Error message if no Thumb Drive is found ************************************
  312. #
  313. function no_drive_found {
  314.   echo
  315.   echo
  316.   color 7 4 B
  317.   echo -n "No USB connected Thumb Drive was found! Press Any Key to Exit!"
  318.   color 7 0 B
  319.   read something
  320.   exit 1
  321. }
  322.  
  323. #
  324. # Determine if we have an online connection ***********************************
  325. #
  326. function are_we_online {
  327.   wget -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
  328.   if [ ! -s /tmp/index.google ];then
  329.     online=false
  330.   else
  331.     online=true
  332.   fi
  333. }
  334.  
  335.  
  336. #
  337. # Determine Thumb Drive designation *****************************************
  338. #
  339. sclu_header
  340. echo -n "Please Wait while your Drives are being Scaned .... "
  341.  
  342. size=$(grep -Ff <(hwinfo --disk --short) <(hwinfo --usb --short) | grep -c "/dev/")
  343. if [ $size -ge 1 ] ; then
  344.   inc=1
  345.   offset=0
  346.   tot1=0
  347.   tot2=0
  348.   while [ $inc -le $size ] ; do
  349.     TDD[$inc-$offset]=$(grep -Ff <(hwinfo --disk --short) <(hwinfo --usb --short) | grep "/dev/" | awk '{print $1}'| sed -n $(( inc ))p)
  350.     TDN[$inc-$offset]=$(grep -Ff <(hwinfo --disk --short) <(hwinfo --usb --short) | grep "/dev/" | sed -n $(( inc ))p)
  351.     TDNL=${#TDN[$inc-$offset]}
  352.     TDN[$inc-$offset]=${TDN[$inc-$offset]:22:$TDNL-23}
  353.     TDS[$inc-$offset]=$(fdisk -l ${TDD[$inc-$offset]} | grep "Disk /" | awk '{print $3}')
  354.     TDU[$inc-$offset]=$(fdisk -l ${TDD[$inc-$offset]} | grep "Disk /" | awk '{print $4}')
  355.     TDU[$inc-$offset]=${TDU[$inc-$offset]:0:2}
  356.     if [ "${TDS[$inc-$offset]}" == "" ] ; then
  357.       let offset=offset+1
  358.     else
  359.       TDNL=${#TDN[$inc-$offset]}
  360.       if [ $TDNL -gt $tot1 ] ; then
  361.         let tot1=$TDNL
  362.       fi
  363.       TDSL=${#TDS[$inc-$offset]}
  364.       if [ $TDSL -gt $tot2 ] ; then
  365.         let tot2=$TDSL
  366.       fi
  367.       TDSIZE=${TDS[$inc-$offset]}
  368.       if [ "${TDU[$inc-$offset]}" == "MB" ] ; then
  369.         let TDSIZE=$(( TDSIZE / 1000 ))
  370.       fi
  371.       TDSIZE=$(echo "$TDSIZE" | awk '{printf("%d\n",$1 + 0.5)}')
  372.       if [ $TDSIZE -gt $MaxTDSize ] ; then
  373.         let offset=offset+1
  374.       fi
  375.     fi
  376.     let inc=inc+1
  377.   done
  378.   let size=size-offset
  379.   if [ $size -le 0 ] ; then
  380.     no_drive_found
  381.   fi
  382.   inc=1
  383.   let tot1=tot1+3
  384.   while [ $inc -le $size ] ; do
  385.     rpad  "${TDN[$inc]}" $tot1
  386.     TDN[$inc]="$rword"
  387.     lpad  "${TDS[$inc]}" $tot2
  388.     TDS[$inc]="$lword"
  389.     let inc=inc+1
  390.   done
  391. else
  392.   no_drive_found
  393. fi
  394.  
  395. #
  396. # Drive Selection Menu ********************************************************
  397. #
  398. tmenu=true
  399. let tot1=tot1-6
  400. spad $tot1
  401. while $tmenu ; do
  402.   sclu_header
  403.   color 7 0 B
  404.   echo -n "    "
  405.   color 7 1 B
  406.   echo "USB Drive Listing - Select Only a Thumb Drive"
  407.   color 7 0 B
  408.   echo
  409.   if [ $size -gt 1 ] ; then
  410.     echo "         Device    Name $spaces Size"
  411.     echo
  412.     inc=1
  413.     while [ $inc -le $size ] ; do
  414.       color 7 0 B
  415.       echo -n "      "
  416.       color 6 1 B
  417.       echo "$inc) ${TDD[$inc]} ${TDN[$inc]} ${TDS[$inc]} ${TDU[$inc]}"
  418.       let inc=inc+1
  419.     done
  420.     color 7 0 B
  421.     echo
  422.     echo -n "        Enter Drive # to use [1-$size] (Q=Quit):"
  423.     read CHOICE
  424.   else
  425.     CHOICE=$size
  426.   fi
  427.   if [[ $CHOICE =~ ^[0-9]+$ ]] ; then
  428.     if [[ $CHOICE -le $size ]] && [[ $CHOICE -gt 0 ]]; then
  429.       sclu_header
  430.       ThumbDrive=${TDD[$CHOICE]}
  431.       TSize="${TDS[$CHOICE]} ${TDU[$CHOICE]}"
  432.       TName=${TDN[$CHOICE]}
  433.       echo -n "   You Selected Thumb Drive: "
  434.       color 6 1 B
  435.       TDSize=$(echo ${TDS[$CHOICE]} | awk '{print $1}')
  436.       echo "$ThumbDrive - $TDSize ${TDU[$CHOICE]}"
  437.       echo
  438.       color 7 0 B
  439.       echo -n "   Is this the correct Thumb Drive to use? (y/N)"
  440.       read CHOICE
  441.       if [[ $CHOICE == [Yy] ]] ; then
  442.         tmenu=false
  443.       fi
  444.       if [[ $CHOICE == [Nn] ]] ; then
  445.         if [ $size -eq 1 ] ; then
  446.           exit 0
  447.         fi
  448.       fi
  449.     fi
  450.   fi
  451.   if [[ $CHOICE == [Qq] ]] ; then
  452.     tmenu=false
  453.     exit 0
  454.   fi
  455. done
  456.  
  457. #
  458. # Determine if the Thumb Drive is mounted ************************************
  459. #
  460. if mount|grep $ThumbDrive > /dev/null ; then
  461.  
  462.   echo
  463.   color 7 4 B
  464.   echo "Your Thumb Drive $ThumbDrive is mounted and must be un-mounted!"
  465.   color 7 0 B
  466.   echo
  467.   echo -n "Would you like to un-mount your Thumb Drive $ThumbDrive (y/N)?: "
  468.   read CHOICE
  469.  
  470.   if [[ $CHOICE == [Yy] ]] ; then
  471.     umount $ThumbDrive"1"
  472.   else
  473.     echo
  474.     color 7 4 B
  475.     echo "Your Thumb Drive $ThumbDrive can't be used while mounted!"
  476.     color 7 0 B
  477.     exit 1
  478.   fi
  479. fi
  480.  
  481. #
  482. # ISO Slection Menu Starts Here ********************************************
  483. #
  484. imenu=true
  485. while $imenu ; do
  486.   sclu_header
  487.   color 7 0 B
  488.   echo -n "      "
  489.   color 6 1 B
  490.   echo " openSUSE CD/DVD ISO Image Selection menu "
  491.   color 7 0 B
  492.   echo
  493.   inc=1
  494.   while [ $inc -le $NumISOs ] ; do
  495.     echo -n "       $inc) "
  496.     echo ${ISO[$inc]} | awk -F "\"*,\"*" '{print $1}'
  497.     let inc=inc+1
  498.   done
  499.   color 7 0 B
  500.   echo
  501.   echo -n "    "
  502.   color 7 1 B
  503.   echo -n "Enter ISO # to use [1-$NumISOs] (L=Local & Q=Quit):"
  504.   color 7 0 B
  505.   read CHOICE
  506.   if [[ $CHOICE =~ ^[0-9]+$ ]] ; then
  507.     if [[ $CHOICE -le $NumISOs ]] && [[ $CHOICE -gt 0 ]]; then
  508.        ISOName=$(echo ${ISO[$CHOICE]} | awk -F "\"*,\"*" '{print $1}')
  509.        imenu=false
  510.     fi
  511.   fi
  512.   if [[ $CHOICE == [Qq] ]] ; then
  513.     imenu=false
  514.     exit 0
  515.   fi
  516.  
  517. #
  518. # Select ISO from File listing ************************************************
  519. #
  520.   if [[ $CHOICE == [Ll] ]] ; then
  521.     counter=0
  522.     cd $Downloads
  523.     sclu_header
  524.     echo -n "    Using ISO Folder: "
  525.     color 3 0 B
  526.     echo $Downloads
  527.     echo
  528.     color 7 0 B
  529.     for i in *.iso ; do
  530.       if [ -f $i ] ; then
  531.         let counter=counter+1
  532.         ISOFiles[$counter]=$i
  533.         echo -n "    $counter) "
  534.         color 3 0 B
  535.         echo ${ISOFiles[$counter]}
  536.         color 7 0 B
  537.       fi
  538.     done
  539.     if [[ $counter -eq 0 ]]; then
  540.       echo
  541.       color 7 4 B
  542.       echo "No ISO Files were found!"
  543.       color 7 0 B
  544.       echo
  545.       read -p "Press any Key to Continue" something
  546.       CHOICE=0
  547.     else
  548.       echo
  549.       color 7 1 B
  550.       echo -n "Please Select the ISO File You wish to use (q=Quit): "
  551.       color 7 0 B
  552.       read CHOICE
  553.     fi
  554.     if [[ $CHOICE =~ ^[0-9]+$ ]] ; then
  555.       if [[ $CHOICE -le $counter ]] && [[ $CHOICE -gt 0 ]]; then
  556.         ISOName=${ISOFiles[$CHOICE]}
  557.         if [ ! -e "$Downloads/$ISOName" ] ; then
  558.           echo
  559.           color 7 4 B
  560.           read -p "$ISOName was Not Found.  Press Any Key to Continue" something
  561.           color 7 0 B
  562.         else
  563.           imenu=false
  564.         fi
  565.       fi
  566.     else
  567.       if [[ $CHOICE == [Qq] ]] ; then
  568.         exit 0
  569.       fi
  570.     fi
  571.   fi
  572. done
  573.  
  574. #
  575. # Download ISO File from here *************************************************
  576. #
  577. sclu_header
  578.  
  579. if [ ! -e $Downloads/$ISOName ] ; then
  580.   color 7 1 B
  581.   echo "Downloading: $ISOName,"
  582.   echo "Please Wait for the download to complete..."
  583.   color 7 0 B
  584.   echo
  585.   cd $Downloads
  586.   ISOSet=$(echo ${ISO[$CHOICE]} | awk -F "\"*,\"*" '{print $2}')
  587.   are_we_online
  588.   if $online ; then
  589.     aria2c --allow-overwrite=true --summary-interval=0 -x5 "${ISODir[$ISOSet]}$ISOName" 2>/dev/null
  590.   else
  591.     echo
  592.     color 7 4 B
  593.     read -p "We Do Not Appear to have an online Connection.  Press any key to exit!" something
  594.     color 7 0 B
  595.     exit 1
  596.   fi
  597. fi
  598.  
  599. #
  600. # Determine if ISO File Downloaded properly ***********************************
  601. #
  602. if [ -e $Downloads/$ISOName ] ; then
  603.   file_size "$Downloads/$ISOName" "MB"
  604.   color 7 1 B
  605.   echo "   Download of: $ISOName,"
  606.   echo "   was successful (or was previously completed) ...   "
  607.   color 7 0 B
  608.   echo
  609.   echo -n "            The ISO filesize is:"
  610.   color 3 0 B
  611.   echo " $filesize"
  612. else
  613.   color 7 4 B
  614.   echo "Download of $ISOName was Not successful... "
  615.   color 7 0 B
  616.   echo
  617.   read -p "          Press Any Key to Exit S.C.L.U."
  618.   exit 1
  619. fi
  620.  
  621. color 7 0 B
  622. echo
  623. read -p "            Press Any Key to Continue ..."
  624.  
  625. #
  626. # Create USB Bootable Thumb Drive *******************************************
  627. #
  628. sclu_header
  629. color 7 4 B
  630. echo "Warning, you are about to write an ISO image to Your, "
  631. echo
  632. color 7 0 B
  633. echo -n "Thumb Drive:"
  634. color 3 0 B
  635. echo " $ThumbDrive $TSize $TName"
  636. echo
  637. color 7 4 B
  638. echo "All Data on your Thumb Drive will be over written!!!!!"
  639. echo
  640. color 7 1 B
  641. echo "You have selected the CD or DVD image which is called:"
  642. echo
  643. color 7 0 B
  644. echo -n "ISO File Named:"
  645. color 3 0 B
  646. echo " $ISOName "
  647. color 7 0 B
  648. echo
  649. echo -n "The ISO FileSize is: "
  650. color 3 0 B
  651. echo $filesize
  652. echo
  653. color 7 0 B
  654. read -p "       Do you wish to Continue? (y/N) Q=Quit: " CHOICE
  655. echo
  656.  
  657. if [[ $CHOICE == [Qq] ]] ; then
  658.   exit 0
  659. fi
  660. if [[ $CHOICE == [Yy] ]] ; then
  661.   dd_rescue -A "$Downloads/$ISOName" "$ThumbDrive"
  662. fi
  663.  
  664. echo
  665. color 7 1 B
  666. read -p "Writing to your Thumb Drive is Complete. Press Any Key to Exit." CHOICE
  667. color 7 0 B
  668. tput clear
  669. exit 0
  670.  
  671. # End Of Script
  672.