-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflash
executable file
·257 lines (221 loc) · 7.09 KB
/
flash
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
#!/bin/bash
# https://github.com/hypriot/flash/commit/47d4780eea353569dc3a81e715310efb2c81616b
# Flash Raspberry Pi SD card images on your Mac
# Stefan Scherer - [email protected]
# MIT License
usage()
{
cat << EOF
usage: $0 [options] name-of-rpi.img
Flash a local or remote Raspberry Pi SD card image.
OPTIONS:
--help|-h Show this message
--bootconf|-C Copy this config file to /boot/config.txt
--config|-c Copy this config file to /boot/device-init.yaml (or occidentalis.txt)
--hostname|-n Set hostname for this SD image
--ssid|-s Set WiFi SSID for this SD image
--password|-p Set WiFI password for this SD image
For HypriotOS devices:
The config file device-init.yaml should look like
hostname: black-pearl
For Raspberry Pi until Hector release:
The config file occidentalis.txt should look like
# hostname for your Hypriot Raspberry Pi:
hostname=hypriot-pi
# basic wireless networking options:
wifi_ssid=SSID
wifi_password=12345
The boot config file config.txt has name/value pairs such as:
max_usb_current=1
EOF
exit 1
}
# translate long options to short
for arg
do
delim=""
case "${arg}" in
--help) args="${args}-h ";;
--verbose) args="${args}-v ";;
--config) args="${args}-c ";;
--hostname) args="${args}-n ";;
--ssid) args="${args}-s ";;
--password) args="${args}-p ";;
--bootconf) args="${args}-C ";;
# pass through anything else
*) [[ "${arg:0:1}" == "-" ]] || delim="\""
args="${args}${delim}${arg}${delim} ";;
esac
done
# reset the translated args
eval set -- $args
# now we can process with getopt
while getopts ":hc:n:s:p:C:" opt; do
case $opt in
h) usage ;;
c) CONFIG_FILE=$OPTARG ;;
C) BOOT_CONF=$OPTARG ;;
n) SD_HOSTNAME=$OPTARG ;;
s) WIFI_SSID=$OPTARG ;;
p) WIFI_PASSWORD=$OPTARG ;;
\?) usage ;;
:)
echo "option -$OPTARG requires an argument"
usage
;;
esac
done
shift $((OPTIND -1))
beginswith() { case $2 in $1*) true;; *) false;; esac; }
endswith() { case $2 in *$1) true;; *) false;; esac; }
image=$1
occi=$2
if [ "$1" == "--help" ]; then
usage
fi
filename=$(basename "${image}")
extension="${filename##*.}"
filename="${filename%.*}"
if [ -f "/tmp/${filename}.${extension}" ]; then
image=/tmp/${filename}.${extension}
echo "Using cached image ${image}"
else
if beginswith http:// "${image}" || beginswith https:// "${image}"; then
which curl >/dev/null || (echo "Error: curl not found. Aborting" && exit 1)
echo "Downloading ${image} ..."
curl -L -o /tmp/image.img.${extension} "${image}"
image=/tmp/image.img.${extension}
fi
if beginswith s3:// "${image}"; then
which aws >/dev/null || (echo "Error: aws not found. Aborting" && exit 1)
echo "Downloading ${image} ..."
aws s3 cp "${image}" /tmp/image.img.${extension}
image=/tmp/image.img.${extension}
fi
if [ ! -f "${image}" ]; then
echo "File not found."
exit 10
fi
if [[ "$(file "${image}")" == *"Zip archive"* ]]; then
echo "Uncompressing ${image} ..."
unzip -o "${image}" -d /tmp
image=$(unzip -l "${image}" | grep --color=never -v Archive: | grep --color=never img | awk 'NF>1{print $NF}')
image="/tmp/${image}"
echo "Use ${image}"
fi
if [[ "$(file "${image}")" == *"xz compressed data"* ]]; then
echo "Uncompressing ${image} ..."
xz -d "${image}" -c >/tmp/image.img
image=/tmp/image.img
echo "Use ${image}"
fi
fi
if [[ "${OSTYPE}" != "darwin"* ]]; then
echo "This version does only support Mac."
echo "Please download correct version from https://github.com/hypriot/flash"
exit 11
fi
while true; do
# try to find the correct disk of the inserted SD card
disk=$(diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}')
if [ "${disk}" == "" ]; then
echo "No SD card found. Please insert SD card, I'll wait for it..."
while [ "${disk}" == "" ]; do
sleep 1
disk=$(diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}')
done
fi
df -h
while true; do
echo ""
read -p "Is /dev/${disk} correct? " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
echo "Unmounting ${disk} ..."
diskutil unmountDisk /dev/${disk}s1
diskutil unmountDisk /dev/${disk}
readonlymedia=$(diskutil info "/dev/${disk}" | grep "Read-Only Media" | awk 'NF>1{print $NF}')
if [ "$readonlymedia" == "No" ]; then
break
else
afplay /System/Library/Sounds/Basso.aiff
echo "👎 The SD card is write protected. Please eject, remove protection and insert again."
fi
done
echo "Flashing ${image} to ${disk} ..."
pv=`which pv 2>/dev/null`
if [ $? -eq 0 ]; then
# this sudo here is used for a login without pv's progress bar
# hiding the password prompt
size=`sudo stat -f %z ${image}`
cat ${image} | pv -s ${size} | sudo dd bs=1m of=/dev/r${disk}
else
echo "No `pv` command found, so no progress available."
echo "Press CTRL+T if you want to see the current info of dd command."
sudo dd bs=1m if=${image} of=/dev/r${disk}
fi
boot=$(df | grep --color=never "/dev/${disk}s1" | sed 's,.*/Volumes,/Volumes,')
if [ "${boot}" == "" ]; then
COUNTER=0
while [ $COUNTER -lt 5 ]; do
sleep 1
boot=$(df | grep --color=never "/dev/${disk}s1" | sed 's,.*/Volumes,/Volumes,')
if [ "${boot}" != "" ]; then
break
fi
let COUNTER=COUNTER+1
done
fi
# customize for first boot
if [ "${boot}" ]; then
if [ -f "${CONFIG_FILE}" ]; then
if [ "${CONFIG_FILE}" == *"occi"* ]; then
echo "Copying ${CONFIG_FILE} to ${boot}/occidentalis.txt ..."
cp "${CONFIG_FILE}" "${boot}/occidentalis.txt"
else
echo "Copying ${CONFIG_FILE} to ${boot}/device-init.yaml ..."
cp "${CONFIG_FILE}" "${boot}/device-init.yaml"
fi
fi
if [[ -f "${BOOT_CONF}" ]]; then
echo "Copying ${BOOT_CONF} to ${boot}/config.txt ..."
sudo cp "${BOOT_CONF}" "${boot}/config.txt"
fi
if [ -f "${boot}/device-init.yaml" ]; then
if [ ! -z "${SD_HOSTNAME}" ]; then
echo "Set hostname=${SD_HOSTNAME}"
sed -i "" -e "s/.*hostname:.*\$/hostname: ${SD_HOSTNAME}/" "${boot}/device-init.yaml"
fi
fi
# legacy: /boot/occidentalis.txt of old Hector release
if [ -f "${boot}/occidentalis.txt" ]; then
if [ ! -z "${SD_HOSTNAME}" ]; then
echo "Set hostname=${SD_HOSTNAME}"
sed -i "" -e "s/.*hostname.*=.*\$/hostname=${SD_HOSTNAME}/" "${boot}/occidentalis.txt"
fi
if [ ! -z "${WIFI_SSID}" ]; then
echo "Set wifi_ssid=${WIFI_SSID}"
sed -i "" -e "s/.*wifi_ssid.*=.*\$/wifi_ssid=${WIFI_SSID}/" "${boot}/occidentalis.txt"
fi
if [ ! -z "${WIFI_PASSWORD}" ]; then
echo "Set wifi_password=${WIFI_PASSWORD}"
sed -i "" -e "s/.*wifi_password.*=.*\$/wifi_password=${WIFI_PASSWORD}/" "${boot}/occidentalis.txt"
fi
fi
fi
echo "Unmounting and ejecting ${disk} ..."
sleep 1
diskutil unmountDisk /dev/${disk}s1
diskutil unmountDisk /dev/${disk}s2
diskutil eject /dev/${disk}
if [ $? -eq 0 ]; then
afplay /System/Library/Sounds/Bottle.aiff
echo "🍺 Finished."
else
afplay /System/Library/Sounds/Basso.aiff
echo "👎 Someting went wrong."
fi