forked from LanderU/ScriptsRPi2-BBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashMAC.sh
executable file
·346 lines (326 loc) · 9.73 KB
/
flashMAC.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/bin/bash
: '
Maintainer Lander Usategui San Juan, e-mail: [email protected]
'
#set -x
#####################
#### CONSTANTS ###
#####################
# CONSTANTS
IMAGE_PATH="/Users/lander/ImagesFlash" #Change for your path
DEVICE=""
FLASH_DEVICE=""
# USER
currentUser=`whoami`
# Array
cont=1
cont2=1
indexArray=0
indexArrayDevices=0
imageArray=( )
deviceArray=( )
#####################
#### FUNCTIONS ###
#####################
function listDevices()
{
for i in `df -h | cut -d " " -f 1 | grep "^/dev/"`; do
if [ "$i" != "/dev/disk1" ]; then # Avoid show OS hdd
echo $cont2"- "$i
deviceArray[indexArrayDevices]=$i
indexArrayDevices=$(($indexArrayDevices+1))
cont2=$(($cont2+1))
fi
done
if [ $cont2 -eq 1 ]; then # Check if we've available some resource
redColor
echo "No devices found, insert your SD Card..."
resetColor
exit 1
fi
read -p "Choose the device: " device
if [ $device -gt ${#deviceArray[@]} ] || [ $device -lt 0 ]; then
#Bad device number
clear
redColor
echo "Invalid number, launch the script again and choose a number of the list."
resetColor
exit 1
else
device=$(($device-1))
DEVICE="${deviceArray[$device]}"
NUMBER_DISK=(`echo $DEVICE | grep -o -E '[0-9]+' | head -1 | sed -e 's/^0\+//'`)
UNMOUNT_DEVICE=(`echo "/dev/disk"$NUMBER_DISK`)
FLASH_DEVICE="/dev/rdisk$NUMBER_DISK"
clear
checkImage
fi
}
function checkPV()
{
EXISTSPV=`command -v pv`
if [ -z $EXISTSPV ]; then
EXISTSPV="false"
else
EXISTSPV="true"
fi
echo $EXISTSPV
}
function checkUser()
{
# Clear screen
clear
if [ $currentUser == "root" ]; then
listDevices
else
# Bad user
redColor
echo "\"sudo\" is needed..."
resetColor
exit 1
fi
}
function checkImage()
{
# Set vector with images
for i in `ls $IMAGE_PATH`; do
echo $cont"- "$i
imageArray[$indexArray]=$i
indexArray=$(($indexArray+1))
cont=$(($cont+1))
done
# Read selection
read -p "Choose the image: " flash
if [ $flash -gt ${#imageArray[@]} ] || [ $flash -lt 0 ]; then
# Bad number
clear
redColor
echo "Invalid number, launch the script again and choose a number of the list."
resetColor
exit 1
else
flashSd
fi
}
function checkSD()
{
READ_ONLY=`diskutil info $UNMOUNT_DEVICE | grep "Read-Only Media"| awk '{print $3}'`
if [ $READ_ONLY == "Yes" ]; then
clear
redColor
echo "Unable to flash your SD card, the SD is protected..."
resetColor
exit 1
fi
}
function flashSd()
{
# Clear screen
clear
flash=$(($flash-1))
# Check file extension
EXTENSION=`echo $IMAGE_PATH/${imageArray[$flash]} | cut -d "." -f3`
# Empty?
if [ -z $EXTENSION ]; then
# Check again
EXTENSION=`echo $IMAGE_PATH/${imageArray[$flash]} | cut -d "." -f2`
case $EXTENSION in
"img") # Check PV
if [ $( checkPV ) == "true" ]; then
# Flash with progress bar
checkSD
if [ "`diskutil unmountDisk $UNMOUNT_DEVICE 2>/dev/null`" ]; then
echo "Unmounted correctly"
echo "Flashing, please wait..."
SIZE=`du -h $IMAGE_PATH/${imageArray[$flash]} | cut -d "," -f1`G
dd if=$IMAGE_PATH/${imageArray[$flash]} | pv -s $SIZE | dd of=$FLASH_DEVICE bs=8m 2>/dev/null
if [ "`diskutil list | grep $UNMOUNT_DEVICE`" ]; then
`diskutil eject $UNMOUNT_DEVICE 2>/dev/null`
unmountCheck="`df -h | grep $UNMOUNT_DEVICE`"
if [ -z "${unmountCheck}" ]; then
clear
echo -e "Done\nRemove your SD Card"
exit 0
else
clear
redColor
echo "Unable to unmount the SD Card"
resetColor
exit 1
fi
fi
fi
else
checkSD
if [ "`diskutil unmountDisk $UNMOUNT_DEVICE 2>/dev/null`" ]; then
# Flash without progress bar
echo "Unmounted correctly"
echo "Flashing, please wait..."
dd if=$IMAGE_PATH/${imageArray[$flash]} | dd of=$FLASH_DEVICE bs=8m 2>/dev/null
if [ "`diskutil list | grep $UNMOUNT_DEVICE`" ]; then
`diskutil eject $UNMOUNT_DEVICE 2>/dev/null`
unmountCheck="`df -h | grep $UNMOUNT_DEVICE`"
if [ -z "${unmountCheck}" ]; then
clear
echo -e "Done\nRemove your SD Card"
exit 0
else
clear
redColor
echo "Unable to unmount the SD Card"
resetColor
exit 1
fi
fi
fi
fi
;;
* ) clear
redColor
echo "Unknown file extension..."
resetColor
exit 1
;;
esac
else
case $EXTENSION in
"gz") # Check PV
if [ $( checkPV ) == "true" ]; then
# Flash with progress bar
checkSD
if [ "`diskutil unmountDisk $UNMOUNT_DEVICE 2>/dev/null`" ]; then
echo "Unmounted correctly"
echo "Flashing, please wait..."
SIZE=`du -h $IMAGE_PATH/${imageArray[$flash]} | cut -d "," -f1`G
gunzip -c $IMAGE_PATH/${imageArray[$flash]} | pv -s 7g | dd of=$FLASH_DEVICE bs=8m 2>/dev/null
if [ "`diskutil list | grep $UNMOUNT_DEVICE`" ]; then
`diskutil eject $UNMOUNT_DEVICE 2>/dev/null`
unmountCheck="`df -h | grep $UNMOUNT_DEVICE`"
if [ -z "${unmountCheck}" ]; then
clear
echo -e "Done\nRemove your SD Card"
exit 0
else
clear
redColor
echo "Unable to unmount the SD Card"
resetColor
exit 1
fi
fi
fi
else
checkSD
if [ "`diskutil unmountDisk $UNMOUNT_DEVICE 2>/dev/null`" ]; then
# Flash without progress bar
echo "Unmounted correctly"
echo "Flashing, please wait..."
gunzip -c $IMAGE_PATH/${imageArray[$flash]} | dd of=$FLASH_DEVICE bs=8m 2>/dev/null
if [ "`diskutil list | grep $UNMOUNT_DEVICE`" ]; then
`diskutil eject $UNMOUNT_DEVICE 2>/dev/null`
unmountCheck="`df -h | grep $UNMOUNT_DEVICE`"
if [ -z "${unmountCheck}" ]; then
clear
echo -e "Done\nRemove your SD Card"
exit 0
else
clear
redColor
echo "Unable to unmount the SD Card"
resetColor
exit 1
fi
fi
else
clear
redColor
echo "Please, change the device number"
resetColor
exit 1
fi
fi
;;
"img")
EXTENSION=`echo $IMAGE_PATH/${imageArray[$flash]} | cut -d "." -f4`
if [ -z $EXTENSION ]; then
# Check PV
checkSD
if [ $( checkPV ) == "true" ]; then
# Flash with progress bar
if [ "`diskutil unmountDisk $UNMOUNT_DEVICE 2>/dev/null`" ]; then
echo "Unmounted correctly"
echo "Flashing, please wait..."
SIZE=`du -h $IMAGE_PATH/${imageArray[$flash]} | cut -d "," -f1`G
dd if=$IMAGE_PATH/${imageArray[$flash]} | pv -s $SIZE | dd of=$FLASH_DEVICE bs=8m 2>/dev/null
if [ "`diskutil list | grep $UNMOUNT_DEVICE`" ]; then
`diskutil eject $UNMOUNT_DEVICE 2>/dev/null`
unmountCheck="`df -h | grep $UNMOUNT_DEVICE`"
if [ -z "${unmountCheck}" ]; then
clear
echo -e "Done\nRemove your SD Card"
exit 0
else
clear
redColor
echo "Unable to unmount the SD Card"
resetColor
exit 1
fi
fi
fi
else
#check SD
checkSD
if [ "`diskutil unmountDisk $UNMOUNT_DEVICE 2>/dev/null`" ]; then
# Flash without progress bar
echo "Unmounted correctly"
echo "Flashing, please wait..."
dd if=$IMAGE_PATH/${imageArray[$flash]} | dd of=$FLASH_DEVICE bs=8m
if [ "`diskutil list | grep $UNMOUNT_DEVICE`" ]; then
`diskutil eject $UNMOUNT_DEVICE 2>/dev/null`
unmountCheck="`df -h | grep $UNMOUNT_DEVICE`"
if [ -z "${unmountCheck}" ]; then
clear
echo -e "Done\nRemove your SD Card"
exit 0
else
clear
redColor
echo "Unable to unmount the SD Card"
resetColor
exit 1
fi
fi
fi
fi
else
redColor
echo "zip version?? extract image first..."
resetColor
exit 1
fi
;;
* ) clear
redColor
echo "Unknown file extension..."
resetColor
exit 1
;;
esac
fi
}
# COLORS
function redColor()
{
tput setaf 1
} # End red color
function resetColor()
{
tput sgr 0
} # End Reset color
function main()
{
checkUser
}
# Start script
main