forked from motioneye-project/motioneyeos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeimage.sh
executable file
·176 lines (152 loc) · 4.3 KB
/
writeimage.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
#!/bin/bash -e
function usage() {
echo "Usage: $0 [options...]"
echo ""
echo "Available options:"
echo " <-i image_file> - indicates the path to the image file (e.g. -i /home/user/Download/file.img.gz)"
echo " <-d sdcard_dev> - indicates the path to the sdcard block device (e.g. -d /dev/mmcblk0)"
echo " [-m modem:apn:user:pwd:pin] - configures the mobile network modem (e.g. -m ttyUSB0:internet)"
echo " [-n ssid:psk] - sets the wireless network name and key (e.g. -n mynet:mykey1234)"
echo " [-s ip/cidr:gw:dns] - sets a static IP configuration instead of DHCP (e.g. -s 192.168.1.101/24:192.168.1.1:8.8.8.8)"
exit 1
}
if [ -z "$1" ]; then
usage 1>&2
fi
if [[ $(id -u) -ne 0 ]]; then echo "please run as root"; exit 1; fi
function msg() {
echo " * $1"
}
while getopts "a:d:f:h:i:lm:n:o:p:s:w" o; do
case "$o" in
d)
SDCARD_DEV=$OPTARG
;;
i)
DISK_IMG=$OPTARG
;;
m)
IFS=":" SETTINGS=($OPTARG)
MODEM=${SETTINGS[0]}
APN=${SETTINGS[1]}
MUSER=${SETTINGS[2]}
MPWD=${SETTINGS[3]}
PIN=${SETTINGS[4]}
;;
n)
IFS=":" NETWORK=($OPTARG)
SSID=${NETWORK[0]}
PSK=${NETWORK[1]}
;;
s)
IFS=":" S_IP=($OPTARG)
IP=${S_IP[0]}
GW=${S_IP[1]}
DNS=${S_IP[2]}
;;
*)
usage 1>&2
;;
esac
done
if [ -z "$SDCARD_DEV" ] || [ -z "$DISK_IMG" ]; then
usage 1>&2
fi
function cleanup {
set +e
# unmount sdcard
umount ${SDCARD_DEV}* >/dev/null 2>&1
}
trap cleanup EXIT
BOOT=$(dirname $0)/.boot
if ! [ -f $DISK_IMG ]; then
echo "could not find image file $DISK_IMG"
exit 1
fi
gunzip=$(which unpigz 2> /dev/null || which gunzip 2> /dev/null || true)
unxz=$(which unxz 2> /dev/null || true)
if [[ $DISK_IMG == *.gz ]]; then
if [ -z "$gunzip" ]; then
msg "make sure you have the gzip package installed"
exit 1
fi
msg "decompressing the .gz compressed image"
$gunzip -c $DISK_IMG > ${DISK_IMG%???}
DISK_IMG=${DISK_IMG%???}
elif [[ $DISK_IMG == *.xz ]]; then
if [ -z "$unxz" ]; then
msg "make sure you have the xz package installed"
exit 1
fi
msg "decompressing the .xz compressed image"
$unxz -T 0 -c $DISK_IMG > ${DISK_IMG%???}
DISK_IMG=${DISK_IMG%???}
fi
umount ${SDCARD_DEV}* 2>/dev/null || true
msg "writing disk image to sdcard"
dd if=$DISK_IMG of=$SDCARD_DEV bs=1048576
sync
if which partprobe > /dev/null 2>&1; then
msg "re-reading sdcard partition table"
partprobe ${SDCARD_DEV}
sleep 1
fi
msg "mounting sdcard"
mkdir -p $BOOT
if [ `uname` == "Darwin" ]; then
BOOT_DEV=${SDCARD_DEV}s1 # e.g. /dev/disk4s1
diskutil unmountDisk ${SDCARD_DEV}
mount -ft msdos $BOOT_DEV $BOOT
else # assuming Linux
BOOT_DEV=${SDCARD_DEV}p1 # e.g. /dev/mmcblk0p1
if ! [ -e ${SDCARD_DEV}p1 ]; then
BOOT_DEV=${SDCARD_DEV}1 # e.g. /dev/sdc1
fi
mount $BOOT_DEV $BOOT
fi
# wifi
if [ -n "$SSID" ]; then
msg "creating wireless configuration"
conf=$BOOT/wpa_supplicant.conf
echo "update_config=1" > $conf
echo "ctrl_interface=/var/run/wpa_supplicant" >> $conf
echo "network={" >> $conf
echo " scan_ssid=1" >> $conf
echo " ssid=\"$SSID\"" >> $conf
if [ -n "$PSK" ]; then
echo " psk=\"$PSK\"" >> $conf
fi
echo "}" >> $conf
echo "" >> $conf
fi
# modem
if [ -n "$MODEM" ]; then
msg "creating mobile network configuration"
conf=$BOOT/ppp
mkdir -p $conf
echo $MODEM > $conf/modem
echo "AT+CGDCONT=1,\"IP\",\"$APN\"" > $conf/apn
echo > $conf/extra
echo > $conf/auth
echo > $conf/pin
if [ -n "$MUSER" ]; then
echo "user \"$MUSER\"" > $conf/auth
echo "password \"$MPWD\"" >> $conf/auth
fi
if [ -n "$PIN" ]; then
echo "AT+CPIN=$PIN" > $conf/pin
fi
fi
# static ip
if [ -n "$IP" ] && [ -n "$GW" ] && [ -n "$DNS" ]; then
msg "setting static IP configuration"
conf=$BOOT/static_ip.conf
echo "STATIC_IP=\"$IP\"" > $conf
echo "STATIC_GW=\"$GW\"" >> $conf
echo "STATIC_DNS=\"$DNS\"" >> $conf
fi
msg "unmounting sdcard"
sync
umount $BOOT
rmdir $BOOT
msg "you can now remove the sdcard"