-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimg2card
executable file
·175 lines (164 loc) · 4.23 KB
/
img2card
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
#!/usr/bin/env bash
#########################################################################
# Made by : Ewoud Dronkert
# Licence : GNU GPL v3
# Platform : macOS
# Requires : bash, diskutil, dd
# Location : ~/bin/
# Name : img2card
# Version : 2.0.0
# Date : 2018-02-09
# Purpose : Write disk image to SD card
# Parameters : Optional [disk image file name]
# Settings : SEARCHDIR = where to look for image files (~/Downloads)
# : SEARCHPAT = file name pattern of image files (*.img)
# : WPACONF = Wi-Fi settings file (~/.ssh/wpa_supplicant.conf)
# Exit 0 : No script errors, image written, card ejected
# 1 : No argument and no image found
# 2 : Argument or image not a file
# 3 : Argument or image not readable
# 4 : Argument or image not named *.img
# 5 : SD card not found
# 6 : SD card not unmounted
# 7 : Image not written
# 8 : Wi-Fi settings not copied
# 9 : SD card not ejected
#########################################################################
# Settings
SEARCHDIR=~/Downloads
SEARCHPAT='*.img'
WPACONF=~/.ssh/wpa_supplicant.conf
# Find image file
if [ -z "$1" ]; then
N=$(ls -d "$SEARCHDIR"/$SEARCHPAT 2> /dev/null | wc -l | xargs)
if [ "$N" -eq 0 ]; then
SCRIPT=$(basename "$0")
echo "No disk image found in '$SEARCHDIR'. Usage: ${SCRIPT} [.img file]" 1>&2
exit 1
fi
if [ "$N" -eq 1 ]; then
IMG=$(ls -d "$SEARCHDIR"/$SEARCHPAT)
F=$(basename "$IMG")
echo "Disk image found: $F"
else
echo "Multiple disk image files found in $SEARCHDIR"
echo
a=
i=0
for f in "$SEARCHDIR"/$SEARCHPAT; do
(( ++i ))
a[$i]="$f"
g=$(basename "$f")
echo " $i. $g"
(( i == 9 )) && break
done
echo
echo -n "Which one? (1-$i, q to quit) "
j=0
while (( j < 1 || j > i )); do
read -n 1 j
j=$(echo $j | tr '[:lower:]' '[:upper:]')
[ "$j" = "Q" ] && echo && exit 0
[[ "$j" < "1" || "$j" > "9" ]] && j=0
done
echo
IMG="${a[$j]}"
fi
else
IMG="$1"
fi
# Check argument
if [ ! -f "$IMG" ]; then
echo "Not a regular file: $IMG" 1>&2
exit 2
fi
if [ ! -r "$IMG" ]; then
echo "Not readable: $IMG" 1>&2
exit 3
fi
EXT=$(echo ${IMG:(-4)} | tr '[:upper:]' '[:lower:]')
if [ "$EXT" != ".img" ]; then
echo "File name extension '$EXT' should be: .img" 1>&2
exit 4
fi
# Get SD card device name
echo -n "Searching for SD card ... "
for SDCARD in /dev/disk?; do
FOUND=$(diskutil info $SDCARD | grep -cE 'SD/MMC|SDXC')
[ "$FOUND" -gt 0 ] && break
done
if [ "$FOUND" -eq 0 ]; then
echo 'Not found.' 1>&2
exit 5
fi
# Let the user check!
echo "Found as device '$SDCARD'. Please double check! This memory card will be deleted:"
echo
diskutil list $SDCARD
echo
# Ask confirmation
CH=
read -n 1 -p "Write disk image to '$SDCARD', destroying all its data? (y/N) " CH
[ -z "$CH" ] && exit 0
echo
CH=$(echo $CH | tr '[:lower:]' '[:upper:]')
if [ "$CH" != "Y" ]; then
echo "Aborted."
exit 0
fi
RAW=/dev/rdisk${SDCARD:9}
# Unmount mounted SD card volumes
echo -n "Unmounting $SDCARD ... "
diskutil unmountdisk $SDCARD &> /dev/null
if [ $? -ne 0 ]; then
echo "Failed."
exit 6
fi
echo "Done."
# Write disk image file to SD card raw device
echo -n "This will require your password. Writing disk image to SD card ... "
sudo dd bs=1m conv=sync if="$IMG" of=$RAW &> /dev/null
if [ $? -ne 0 ]; then
echo "Failed."
exit 7
fi
echo "Done."
# Prepare image for Wi-Fi connection and SSH access
echo -n "Preparing image for Wi-Fi connection and SSH access ... "
VOL='/Volumes/boot/'
TRY=3
while (( TRY > 0 )); do
sleep 1
(( --TRY ))
[ -e "$VOL" ] && TRY=0
done
if [ -e "$VOL" ]; then
if [ -d "$VOL" -a -r "$VOL" -a -w "$VOL" ]; then
touch "${VOL}ssh"
[ ! -f "${VOL}ssh" ] && echo "SSH access not configured."
# WPACONF value set at top
if [ -f "$WPACONF" -a -r "$WPACONF" ]; then
cp "$WPACONF" "$VOL"
if [ $? -eq 0 ]; then
echo "Done."
else
echo "Wi-Fi configuration not copied."
exit 8
fi
else
echo "Wi-Fi configuration not found at: $WPACONF"
exit 8
fi
else
echo "Volume not mounted correctly."
exit 8
fi
else
echo "Volume not found."
exit 8
fi
# Eject SD card raw device
echo -n "Ejecting SD card ... "
diskutil eject $RAW &> /dev/null
[ $? -ne 0 ] && echo "Failed." && exit 9
echo "Done."