Advertisement
Guest User

Untitled

a guest
Aug 28th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.96 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Copyright (c) 2012, AVG Technologies CZ, s.r.o., www.avg.com
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  19.  
  20.  
  21. DIALOG=dialog
  22. DIALOG_OK=0
  23. DIALOG_CANCEL=1
  24. DIALOG_HELP=2
  25. DIALOG_EXTRA=3
  26. DIALOG_PSCR=131
  27. DIALOG_ESC=255
  28. ARL_ERROR_LOG_FILE="/tmp/arl_error.log"
  29. VALUE=""
  30. ARL_USBDIR="${0%/*}"
  31. USB_DEVICES_FILE="$ARL_USBDIR/usb_devs_file"
  32.  
  33. error()
  34. {
  35.     # $1 - error message
  36.  
  37.     $DIALOG --colors --backtitle "AVG Rescue CD" --msgbox "Error:\n\n\Z1${1}\Zn" 10 70
  38. }
  39.  
  40. success()
  41. {
  42.     # $1 - success message
  43.  
  44.     $DIALOG --colors --backtitle "AVG Rescue CD" --msgbox "\Z2$1\Zn" 8 70
  45. }
  46.  
  47. infobox()
  48. {
  49.     # $1 - title
  50.     # $2 - message
  51.  
  52.     $DIALOG --backtitle "AVG Rescue CD" --title "$1" --infobox "\n$2" 5 70
  53. }
  54.  
  55. cmd()
  56. {
  57.     # 1 - command
  58.     # 2 - infobox message (optional)
  59.     #   - cmd_INFOBOX_TITLE (optional)
  60.  
  61.     local cmd="$1"
  62.     local info_msg="$2"
  63.     local rv
  64.  
  65.     [ -n "$info_msg" ] && infobox "$cmd_INFOBOX_TITLE" "$info_msg"
  66.  
  67.     VALUE="`eval $cmd 2>&1`"
  68.     rv=$?
  69.     if [ $rv -ne 0 ]
  70.     then
  71.         echo -e "$cmd  returned $rv:\n\t$VALUE" >> $ARL_ERROR_LOG_FILE
  72.         error "$cmd:\n$VALUE"
  73.     fi  
  74.  
  75.     return $rv
  76. }
  77.  
  78. yesno()
  79. {
  80.     # $1 - question
  81.     # $2 - height
  82.     # $3 - width
  83.  
  84.     $DIALOG --backtitle "AVG Rescue CD" --colors --yesno "$1" $2 $3
  85. }
  86.  
  87. usb_devices_radiolist()
  88. {
  89.     local path
  90.     local dev
  91.     local model
  92.     local vendor
  93.     local removable
  94.     local devices
  95.     local size
  96.     local enable="on"
  97.  
  98.     for path in `find /sys/block/ -maxdepth 1 -iname sd* 2>/dev/null`
  99.     do
  100.         removable=`cat $path/removable`
  101.         [ $removable -eq 0 ] && continue
  102.        
  103.         devices=`find $path/sd* -maxdepth 1 -iname sd* 2>/dev/null`
  104.  
  105.         # no partition
  106.         [ -z "$devices" ] && devices=$path
  107.  
  108.         for device in $devices
  109.         do
  110.             dev=`basename $device`
  111.  
  112.             VALUE="`awk '/^.* '$dev'$/{print $3}' /proc/partitions`"
  113.             size="$(($VALUE/1024))"
  114.  
  115.             if [ $size -lt 510 ]
  116.             then
  117.                 continue
  118.             elif [ $size -lt 1024 ]
  119.             then
  120.                 size="${size}MB"
  121.             else
  122.                 size="$(($size/1024))GB"
  123.             fi
  124.  
  125.             model=`cat $path/device/model`
  126.             vendor=`cat $path/device/vendor`
  127.  
  128.             echo "$dev \"$vendor $model $size\" $enable" >> $USB_DEVICES_FILE
  129.  
  130.             enable="off"
  131.         done
  132.     done
  133. }
  134.  
  135. usb_show_devices()
  136. {
  137.     VALUE=`$DIALOG --backtitle "AVG Rescue CD" \
  138.         --title "USB Flash Drive" \
  139.         --cancel-label "Return" \
  140.         --radiolist "Please select drive" 15 70 10 \
  141.         --file $USB_DEVICES_FILE \
  142.         2>&1 1>&3`
  143.  
  144.     return $?
  145. }
  146.  
  147. usb_conf_devices()
  148. {
  149.     local rv
  150.  
  151.     rm -f $USB_DEVICES_FILE
  152.     usb_devices_radiolist
  153.     if [ ! -f "$USB_DEVICES_FILE" ]
  154.     then
  155.         error "No USB Flash Drive found!"
  156.         return 1
  157.     fi
  158.  
  159.     usb_show_devices
  160.     rv=$?
  161.     rm -f $USB_DEVICES_FILE
  162.  
  163.     if [ $rv -eq $DIALOG_CANCEL -o $rv -eq $DIALOG_ESC -o $rv -eq $DIALOG_PSCR ]
  164.     then
  165.         return 1
  166.     fi
  167.  
  168.     if [ $rv -ne $DIALOG_OK ]
  169.     then
  170.  
  171.         echo "error: usb devices dialog: $rv"
  172.         exit
  173.     fi
  174.  
  175.     return 0
  176. }
  177.  
  178. usb_umount()
  179. {
  180.     # $1 - dev name, e.g. sdb
  181.  
  182.     local mntpoints
  183.     local mntpoint
  184.     local dev="$1"
  185.  
  186.     mntpoints=`cat /proc/mounts | grep $dev | cut -d' ' -f2`
  187.     for mntpoint in $mntpoints
  188.     do
  189.         cmd "umount $mntpoint" "unmounting $dev" || return 1
  190.     done
  191. }
  192.  
  193. usb_copy()
  194. {
  195.     # $1 - arl pck files path e.g. /mnt/usb, /mnt/sr0, ...
  196.     # $2 - usb mountpoint
  197.  
  198.     local arl_files="f1.msg syslinux.cfg memtest menu.c32 libutil.c32 vmlinuz CHANGELOG arl-version initrd.lzm arl.conf"
  199.     local f
  200.  
  201.     for f in $arl_files
  202.     do
  203.         cmd "cp $1/$f $2" "Copying file: $f" || return 1
  204.     done
  205.  
  206.     return 0
  207. }
  208.  
  209. usb_ext2_show_size()
  210. {
  211.     # $1 - available free space
  212.  
  213.     VALUE=`$DIALOG --backtitle "AVG Rescue CD" \
  214.         --title "Select" \
  215.         --cancel-label "Return" \
  216.         --inputbox \
  217.         "Please enter ext2 file system size. Minimum size is 500MB.
  218. Maximum free space size available on USB drive is $1MB" \
  219.         10 65 \
  220.         "500" \
  221.         2>&1 1>&3`
  222.  
  223.     return $?
  224. }
  225.  
  226. usb_ext2_size()
  227. {
  228.     # $1 - dev full path
  229.  
  230.     local dev_escp="`echo $1 | sed 's%\/%\\\/%g'`"
  231.     local avail=`df $1 | grep -v Filesystem |  awk '/'$dev_escp'/{print $4}'`
  232.  
  233.     usb_ext2_show_size $avail
  234.     if [ $? -eq $DIALOG_CANCEL -o $? -eq $DIALOG_ESC -o $? -eq $DIALOG_PSCR ]
  235.     then
  236.         return 1
  237.     fi
  238.  
  239.     if [ $? -ne $DIALOG_OK ]
  240.     then
  241.         quit "error: ext2 file system size dialog: $?"
  242.     fi
  243.  
  244.     if [ $VALUE -gt $(($avail/1024)) ]
  245.     then
  246.         error "There is not enough free space on the USB drive"
  247.         return 1
  248.     fi
  249.  
  250.     if [ 500 -gt $VALUE ]
  251.     then
  252.         error "The minimum ext2 file system size is 500MB"
  253.         return 1
  254.     fi
  255.  
  256.     return 0
  257. }
  258.  
  259. usb_ext2()
  260. {
  261.     # $1 - dst. usb size
  262.     # $2 - dst. usb mount point
  263.     # $3 - src. arl_rootfs.ext2 mount point
  264.  
  265.     local rv=0
  266.     local loopdev="`losetup -f`"
  267.  
  268.     cmd "dd if=/dev/zero of=$2/arl_rootfs.ext2 bs=1M count=$1" "Creating arl_rootfs.ext2 loopback device" &&
  269.     cmd "losetup $loopdev $2/arl_rootfs.ext2" "Setting up loopback device" &&
  270.     cmd "mkfs.ext2 $loopdev" "Creating ext2 file system" &&
  271.     cmd "mkdir -p /mnt/ext2" "Creating /mnt/ext2 directory" &&
  272.     cmd "mount $loopdev /mnt/ext2" "Mounting ext2 file system" || return 1
  273.  
  274.     cmd "cp -a $3/* /mnt/ext2" "Creating root file system in ext2 fs" || rv=$?
  275.  
  276.     cmd "umount /mnt/ext2" "Umounting ext2 file system" &&
  277.     cmd "rmdir /mnt/ext2" &&
  278.     cmd "losetup -d $loopdev" "Removing loopback device" || rv=$?
  279.  
  280.     return $rv
  281. }
  282.  
  283. usb_create()
  284. {
  285.     # $1 - dev name, e.g. sdb or sdb1,...
  286.     # $2 - arl src files path e.g. /mnt/usb, /mnt/sr0, ...
  287.  
  288.     local rv=0
  289.     local partition=${1:3:1}
  290.     [ -z "$partition" ] && partition="1"
  291.     local device=${1:0:3}
  292.  
  293.     cmd "mkdir -p /mnt/usb_origfs" &&
  294.     cmd "mount $ARL_USBDIR/arl_rootfs.ext2 /mnt/usb_origfs" || rv=$?
  295.  
  296.     if [ $rv -eq 0 ]
  297.     then
  298.         cmd "mkdir -p /mnt/usb_new" "Mounting USB..." &&
  299.         mount -t vfat /dev/$1 /mnt/usb_new 2>/dev/null ||
  300.         mount -t ntfs /dev/$1 /mnt/usb_new 2>/dev/null || rv=$?
  301.  
  302.         if [ $rv -ne 0 ]
  303.         then
  304.             error "Different file system, FAT16/FAT32/NTFS, required!"
  305.         else
  306.             usb_ext2_size "/dev/$1" &&
  307.             usb_ext2 "$VALUE" "/mnt/usb_new" "/mnt/usb_origfs" &&
  308.             usb_copy "$2" "/mnt/usb_new" || rv=$?
  309.  
  310.             cmd "umount /mnt/usb_new" "Unmounting USB..." &&
  311.             cmd "rmdir /mnt/usb_new"
  312.  
  313.             cmd "/mnt/usb_origfs/opt/bin/syslinux -i /dev/$1" "Installing syslinux..." &&
  314.  
  315.             # create mbr if partition specified
  316.             if [ ${#1} -gt 3 ]
  317.             then
  318.                 cmd "dd if=/dev/zero of=/dev/$device bs=446 count=1" "Creating MBR..." &&
  319.                 cmd "cat /mnt/usb_origfs/usr/lib/syslinux/mbr.bin > /dev/$device" || rv=$?
  320.             fi &&
  321.  
  322.             sleep 1
  323.         fi
  324.  
  325.         cmd "umount /mnt/usb_origfs" &&
  326.         cmd "rmdir /mnt/usb_origfs"
  327.     fi
  328.  
  329.     return $rv
  330. }
  331.  
  332. usb()
  333. {
  334.     cmd_INFOBOX_TITLE="USB Flash Drive"
  335.  
  336.     if [ -z "`which $DIALOG`" ]
  337.     then
  338.         echo "Please install the dialog utility at first."
  339.         echo "For example: \$ sudo apt-get install dialog"
  340.         return
  341.     fi
  342.  
  343.     if [ ! -f "$ARL_USBDIR/arl-version" ]
  344.     then
  345.         error "arl-version file was not found!"
  346.         return
  347.     fi
  348.  
  349.     usb_conf_devices
  350.     if [ $? -eq 1 ]
  351.     then
  352.         return
  353.     fi
  354.     dev=$VALUE
  355.  
  356.     yesno "All the AVG Rescue CD data on the USB Flash Drive will be rewritten?\n\n\
  357. Do you really want to continue?" 8 70
  358.     if [ $? -eq $DIALOG_CANCEL -o $? -eq $DIALOG_ESC -o $? -eq $DIALOG_PSCR ]
  359.     then
  360.         return
  361.     fi
  362.  
  363.     usb_umount $dev
  364.     usb_create $dev "$ARL_USBDIR"
  365.     if [ $? -ne 0 ]
  366.     then
  367.         error "Bootable USB Flash Drive was NOT created!"
  368.         return
  369.     fi
  370.  
  371.     success "Bootable USB Flash Drive successfully created."
  372. }
  373.  
  374. exec 3>&1
  375.  
  376. if [ $(id -u) -ne 0 ]
  377. then
  378.     echo "ERROR: Try to run this as root."
  379.     # LSB specification errorcode
  380.     exit 4
  381. fi
  382.  
  383. usb
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement