-
Notifications
You must be signed in to change notification settings - Fork 60
/
image-config
289 lines (269 loc) · 6.26 KB
/
image-config
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
#!/bin/bash
trap '{ stty sane; echo ""; errexit "Aborted"; }' SIGINT SIGTERM
MNTPATH="/tmp/image-config-mnt"
errexit()
{
echo ""
echo -e "$1"
echo ""
if [ "${MNTED}" = "TRUE" ]; then
umount "${BOOTMNT2}/" &> /dev/null
umount "${MNTPATH}/" &> /dev/null
fi
rm -rf "${MNTPATH}/" &> /dev/null
rmloop
exit 1
}
mkloop()
{
LOOP="$(losetup -f --show -P "${IMGFILE}")"
if [ $? -ne 0 ]; then
errexit "Unable to create loop device"
fi
}
rmloop()
{
if [ "${LOOP}" != "" ]; then
losetup -d "${LOOP}"
LOOP=""
fi
}
mntimg()
{
mkloop
if [ ! -d "${MNTPATH}/" ]; then
mkdir "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to make ROOT partition mount point"
fi
fi
mount "${LOOP}p2" "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image ROOT partition"
fi
MNTED=TRUE
BOOTMNT1="$(sed -n '/^[[:space:]]*#/!s|^\S\+\s\+\(/boot\S*\)\s\+.*$|\1|p' "${MNTPATH}/etc/fstab")"
BOOTMNT2="${MNTPATH}${BOOTMNT1}"
if [ ! -d "${BOOTMNT2}/" ]; then
mkdir -p "${BOOTMNT2}/"
if [ $? -ne 0 ]; then
errexit "Unable to make BOOT partition mount point"
fi
fi
mount "${LOOP}p1" "${BOOTMNT2}/"
if [ $? -ne 0 ]; then
errexit "Unable to mount image BOOT partition"
fi
}
umntimg()
{
umount "${BOOTMNT2}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image BOOT partition"
fi
umount "${MNTPATH}/"
if [ $? -ne 0 ]; then
errexit "Unable to unmount image ROOT partition"
fi
MNTED=FALSE
rmloop
rm -r "${MNTPATH}/"
}
usage()
{
errexit "Usage: $0 [options] pathto/imagefile\n\
-h,--help This usage description"
}
LOOP=""
MNTED=FALSE
if [ $(id -u) -ne 0 ]; then
errexit "Must be run as root user: sudo $0"
fi
PGMNAME="$(basename $0)"
for PID in $(pidof -x -o %PPID "${PGMNAME}"); do
if [ ${PID} -ne $$ ]; then
errexit "${PGMNAME} is already running"
fi
done
IMGFILE=""
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage
;;
-*|--*)
usage
;;
*)
IMGFILE="$1"
shift
;;
esac
done
if [ "${IMGFILE}" = "" ]; then
errexit "No image file specified"
fi
if [ ! -f "${IMGFILE}" ]; then
errexit "${IMGFILE} not found"
fi
while :
do
echo ""
echo -n "User account to create (optional): "
read -r USERACCT
if [ "${USERACCT}" != "" ]; then
if [[ "${USERACCT}" =~ ^[[:lower:]][[:lower:][:digit:]\-]*$ && ${#USERACCT} -le 32 ]]; then
break
else
echo "Invalid user account"
fi
else
break
fi
done
if [ "${USERACCT}" != "" ]; then
while :
do
echo ""
echo -n "Enter password for user account '${USERACCT}': "
read -r -s USERPASS
echo ""
echo -n "Retype password: "
read -r -s USERPASS2
echo ""
if [ "${USERPASS2}" = "${USERPASS}" ]; then
if [[ "${USERPASS}" =~ ^[[:graph:]]+$ ]]; then
break
else
echo "Invalid password"
fi
else
echo "Passwords don't match"
fi
done
fi
while :
do
echo ""
echo -n "WiFi (optional) \"SSID\" \"Password\" \"Country\": "
read -r WPA_CONF
if [ "${WPA_CONF}" != "" ]; then
WPA_CONF="$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' <<< "${WPA_CONF}")"
WPA_SSID="$(sed -n 's|^"\(.*\)"\s\+".*"\s\+".*"$|\1|p' <<< ${WPA_CONF})"
WPA_PASS="$(sed -n 's|^".*"\s\+"\(.*\)"\s\+".*"$|\1|p' <<< ${WPA_CONF})"
WPA_CC="$(sed -n 's|^".*"\s\+".*"\s\+"\(.*\)"$|\1|p' <<< ${WPA_CONF})"
WPA_CC="$(tr [a-z] [A-Z] <<< ${WPA_CC})"
if [[ "${WPA_SSID}" =~ ^[^[:blank:]][[:print:]]+[^[:blank:]]$ && "${WPA_PASS}" =~ ^[^[:blank:]][[:print:]]+[^[:blank:]]$ && "${WPA_CC}" =~ ^[[:alpha:]][[:alpha:]]$ ]]; then
break
else
echo "Invalid WiFi parameter(s): '${WPA_CONF}'"
fi
else
break
fi
done
echo ""
echo -n "Enable SSH (y/n)? "
while read -r -n 1 -s answer; do
if [[ ${answer} = [yYnN] ]]; then
echo "${answer}"
if [[ ${answer} = [yY] ]]; then
SSH=TRUE
else
SSH=FALSE
fi
break
fi
done
echo ""
if [ "${USERACCT}" != "" ]; then
echo "User account: ${USERACCT}"
else
echo "User account: (None)"
fi
if [ "${WPA_CONF}" != "" ]; then
echo "WiFi: SSID=${WPA_SSID} Password=${WPA_PASS} Country=${WPA_CC}"
else
echo "WiFi: (None)"
fi
echo -n "SSH: "
if [ "${SSH}" = "TRUE" ]; then
echo "Enabled"
else
echo "Disabled"
fi
echo ""
echo -n "Ok to configure '${IMGFILE}' (y/n)? "
while read -r -n 1 -s answer; do
if [[ ${answer} = [yYnN] ]]; then
echo "${answer}"
if [[ ${answer} = [yY] ]]; then
break
else
errexit "Aborted"
fi
fi
done
mntimg
rm "${BOOTMNT2}/userconf" "${BOOTMNT2}/userconf.txt" &> /dev/null
if [ "${USERACCT}" != "" ]; then
ENCPWD="$(echo ${USERPASS} | openssl passwd -6 -stdin)"
echo "${USERACCT}:${ENCPWD}" > "${BOOTMNT2}/userconf"
fi
rm "${BOOTMNT2}/firstrun.sh" &> /dev/null
sed -i 's| systemd.run.*||g' "${BOOTMNT2}/cmdline.txt"
rm -r "${MNTPATH}/etc/NetworkManager/system-connections/"
mkdir "${MNTPATH}/etc/NetworkManager/system-connections/"
rm "${BOOTMNT2}/wpa_supplicant.conf" &> /dev/null
if [ "${WPA_CONF}" != "" ]; then
OSVER="$(sed -n 's|^VERSION=".*(\(.*\))"|\1|p' ${MNTPATH}/etc/os-release)"
if [ "${OSVER}" = "bookworm" ]; then
cat <<EOF > "${MNTPATH}/etc/NetworkManager/system-connections/${WPA_SSID}.nmconnection"
[connection]
id=${WPA_SSID}
uuid=$(cat /proc/sys/kernel/random/uuid)
type=wifi
interface-name=wlan0
[wifi]
mode=infrastructure
ssid=${WPA_SSID}
[wifi-security]
auth-alg=open
key-mgmt=wpa-psk
psk=${WPA_PASS}
[ipv4]
method=auto
[ipv6]
method=auto
EOF
chmod 600 "${MNTPATH}/etc/NetworkManager/system-connections/${WPA_SSID}.nmconnection"
cat <<EOF > "${BOOTMNT2}/firstrun.sh"
#!/bin/bash
set +e
raspi-config nonint do_wifi_country ${WPA_CC}
rm -f ${BOOTMNT1}/firstrun.sh
sed -i 's| systemd.run.*||g' ${BOOTMNT1}/cmdline.txt
exit 0
EOF
sed -i "/^[[:space:]]*#/!s|^\(.*\)$|\1 systemd.run=${BOOTMNT1}/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target|" "${BOOTMNT2}/cmdline.txt"
else
cat <<EOF > "${BOOTMNT2}/wpa_supplicant.conf"
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
country=${WPA_CC}
update_config=1
network={
ssid="${WPA_SSID}"
psk="${WPA_PASS}"
}
EOF
fi
fi
rm "${BOOTMNT2}/ssh" "${BOOTMNT2}/ssh.txt" &> /dev/null
if [ "${SSH}" = "TRUE" ]; then
touch "${BOOTMNT2}/ssh"
fi
umntimg
sync
echo ""
echo "${IMGFILE} configured"
echo ""