/
image-to-usb
executable file
·149 lines (109 loc) · 4.44 KB
/
image-to-usb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
####################################################################################################
## Authored and maintained by Anthony Nordquist of the SalineOS project. http://www.salineos.com/ salinelinux@gmail.com
## Copyright Anthony Nordquist 2012
Icon="--window-icon=drive-removable-media-usb"
DisplayName=$(echo "$1" | awk -F '/' '{print $NF}')
Iso="$1"
progressbar () {
tail -f /usr/bin/image-to-usb | yad --text="$@" --title="Image to USB" --progress --pulsate --auto-close --no-buttons $Icon
}
USBDrives=""
## Check to make sure that is at least one USB drive connected to the system.
## Without this code the user will be displayed something REALLY ugly
## Pay no mind to the man behind the curtain :)
USBDrives=""
if [ "$(ls /dev/disk/by-path | grep 'usb')" != "" ]; then
USBDriveTemp=$(find /dev/disk/by-path/ -name "*usb*" | xargs ls -l | grep -v "part" | awk '{print $NF}' | awk -F "/" '{print $NF}')
for i in $USBDriveTemp; do
DriveTest=$(cat /var/log/messages | grep "$i" | grep "removable disk" | tail -1)
if [ "$DriveTest" != "" ]; then
if [ "$USBDrives" = "" ]; then
USBDrives="$i"
else
USBDrives="$USBDrives $i"
fi
fi
done
fi
if [ "$USBDrives" = "" ]; then
yad $Icon --title="Image to USB" --text=" No USB drives found. \n" --width=250 --button=$"gtk-ok:1"
MainMenu
exit 0
fi
GetDrive () {
USBDriveMenu=""
for i in $USBDrives; do
## Get the label of the disk if available
Label=$(find /dev/disk/by-label/* -printf %f" "%l\\n | grep "$i" | awk '{print $1}' | sed -e 's|\\x20| |g')
if [ "$Label" = "" ]; then
Label=' '
else
Label="$Label "
fi
DriveSize=$(grep -m 1 "$i" /proc/partitions | awk '{print $3}')
## Part Drive Size is in blocks, first we need to convert the blocks to bytes
BlockSize=$(df -P /dev/$i | grep "blocks" | awk '{print $2}' | awk -F '-' '{print $1}')
## Multiply blocks by block size to get bytes
SizeInBytes=$(($DriveSize * $BlockSize))
## Divide Bytes by 1024 to get Kilobytes
SizeInKB=$(($SizeInBytes / 1024))
## Divide Kilobytes by 1024 to get Megabytes
SizeInMB=$(($SizeInKB / 1024))
## Divide Megabytes by 1024 to get Gigabytes
SizeInGB=$(($SizeInMB / 1024))
if [ "$USBDriveMenu" = "" ]; then
USBDriveMenu=("$i" "$Label" "$SizeInGB" "$SizeInMB")
else
USBDriveMenu=("$i" "$Label" "$SizeInGB" "$SizeInMB" "${USBDriveMenu[@]}")
fi
done
UseDrive=$(yad --height="350" --list $Icon --title="Image to USB" --column="Name" --column="Associated Label(s)" --column="Approx. Size in GiB" --column="Approx. Size in MiB" --print-column=1 --button=$"gtk-quit:3" --button=$"gtk-ok:2" --text=" Please select the drive to copy the image to. \n" "${USBDriveMenu[@]}")
ret="$?"
if [ "$ret" = "3" ]; then
exit 0
elif [ "$ret" = "252" ]; then
exit 0
elif [ "$UseDrive" = "" ]; then
yad $Icon --title="Image to USB" --text="No USB drive selected, please retry." --button=$"gtk-ok:1"
GetDrive
exit 0
fi
UseDrive=$(echo "$UseDrive" | awk -F '|' '{print $1}')
Continue
}
Continue () {
yad --width=300 --wrap --image=gtk-dialog-warning --title="Image to USB" $Icon --text="This will completely erase the contents of $UseDrive and replace them with the contents of $DisplayName. Are you sure you want to continue?" --button=$"gtk-no:1" --button=$"gtk-yes:0"
if [ "$?" != "0" ]; then
exit
fi
## Unmount all partitions on the drive
Unmount=$(mount | grep "$UseDrive" | awk '{print $1}')
for i in $Unmount; do
umount "$i"
umount -l "$i"
done
progressbar " Copying $DisplayName to $UseDrive. \n" &
dd if="$Iso" of=/dev/$UseDrive
sync
killall -KILL tail
yad $Icon --title="Image to USB" --text=" Copying of image complete. \n" --button=$"gtk-ok:1"
exit 0
}
GetDrive
exit