-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfactory_reset
378 lines (298 loc) · 10.5 KB
/
factory_reset
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/bin/bash
set -eu -o pipefail
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
echo "This needs to run as root"
exit 99
fi
OPT_RESET=""
OPT_COPY_PI_PASS=""
OPT_COPY_ROOT_PASS=""
OPT_COPY_WIFI=""
OPT_DEBUG=""
OPT_BOOT_RECOVERY=""
OPT_BOOT_ROOT=""
OPT_NO_REBOOT=""
GREENFG=$(tput setaf 2 2>/dev/null )
GREENBG=$(tput setab 2 2>/dev/null )
BLUEFG=$(tput setaf 4 2>/dev/null )
BLUEBG=$(tput setab 4 2>/dev/null )
LIME_YELLOW=$(tput setaf 190 2>/dev/null )
POWDER_BLUE=$(tput setaf 153 2>/dev/null )
ORANGEFG=$(tput setaf 3 2>/dev/null )
ORANGEBG=$(tput setab 3 2>/dev/null )
MAGENTAFG=$(tput setaf 5 2>/dev/null )
MAGENTABG=$(tput setab 5 2>/dev/null )
CYANFG=$(tput setaf 6 2>/dev/null )
CYANBG=$(tput setab 6 2>/dev/null )
RESET=$(tput sgr0 2>/dev/null )
usage()
{
cat << EOF
usage: $0 options
Calling this script causes the rPi to reboot and factory reset. All data is lost
OPTIONS:
--reset set this option to proceed with reset,
otherwise script will exit and do nothing
(mutually exclusive with --boot-to-recovery )
--copy-pi-password during the reset, preserve the pi user password
--copy-root-password during the reset, preserve the root password
--copy-wifi preserve the current wifi settings
--copy-all preserve pi, root and wifi settings
ADVANCED OPTIONS:
--no-reboot do everything, but don't reboot at the end, to allow
for inspection
POSSIBLY BROKEN OPTIONS:
--debug enable debugging verbosity
--boot-to-recovery don't initiate reest, just reboot into the recovery
partition (mutually exclusive with --reset)
--boot-to-root boot back to rootfs (without reset)
this only makes any sense when in recovery
EOF
}
for ((i=1; i<=$#; i++))
do
# echo "arg is \"${!i}\""
ARG="${!i}"
case $ARG in
--reset)
OPT_RESET=1
;;
--copy-pi-password)
OPT_COPY_PI_PASS=1
;;
--copy-root-password)
OPT_COPY_ROOT_PASS=1
;;
--copy-wifi)
OPT_COPY_WIFI=1
;;
--copy-all)
OPT_COPY_PI_PASS=1
OPT_COPY_ROOT_PASS=1
OPT_COPY_WIFI=1
;;
--debug)
OPT_DEBUG=1
;;
--boot-to-recovery)
OPT_BOOT_RECOVERY=1
;;
--boot-to-root)
OPT_BOOT_ROOT=1
;;
--no-reboot)
OPT_NO_REBOOT=1
;;
*)
echo “unrecognised option: $ARG”
echo
usage
exit 1
esac
done
if [ "${OPT_RESET}" -a "${OPT_BOOT_RECOVERY}" ] ; then
echo "${MAGENTAFG} only set one of --boot-to-recovery and --reset ${RESET}"
echo
usage
exit 1
fi
if [ ! "${OPT_RESET}" ] && [ ! "${OPT_BOOT_RECOVERY}" ] &&
[ ! "${OPT_BOOT_ROOT}" ] ; then
echo "${ORANGEFG}set only one of --boot-to-recovery, --boot-to-root or --reset ${RESET}"
echo
usage
exit 1
fi
if [ -b "/dev/mmcblk0" ]; then
DEVICE="/dev/mmcblk0p"
elif [ -b "/dev/sda" ]; then
DEVICE="/dev/sda"
fi
if [ -z "${DEVICE}" ]; then
echo "${MAGENTAFG} expected /dev/mmcblk (SD card) or /dev/sda (USB disk) ${RESET}"
echo
exit 1
fi
ROOT_PART_PARTUUID=$(blkid -s PARTUUID -o value \
/dev/disk/by-label/rootfs)
ROOT_PART_UUID=$(blkid -s UUID -o value \
/dev/disk/by-label/rootfs)
RECOVERY_PART_PARTUUID=$(blkid -s PARTUUID -o value \
/dev/disk/by-label/recoveryfs)
RECOVERY_PART_UUID=$(blkid -s UUID -o value \
/dev/disk/by-label/recoveryfs)
P1_UUID="$(blkid -o value -s UUID ${DEVICE}1)"
P2_UUID="$(blkid -o value -s UUID ${DEVICE}2)"
P3_UUID="$(blkid -o value -s UUID ${DEVICE}3)"
P1_PARTUUID="$(blkid -o value -s PARTUUID ${DEVICE}1)"
P2_PARTUUID="$(blkid -o value -s PARTUUID ${DEVICE}2)"
P3_PARTUUID="$(blkid -o value -s PARTUUID ${DEVICE}3)"
mkdir -p /mnt/recoveryfs
mkdir -p /mnt/rootfs
echo "factory restore script - resetting"
sleep 2
if [ "${OPT_BOOT_ROOT}" ] ; then
echo "booting to rootfs"
mount /dev/disk/by-label/rootfs /mnt/rootfs || \
{ [ $? -eq 32 ] && echo "already mounted" || \
{ echo "some other error" ; exit 99 ;} }
fstab_file=/mnt/rootfs/etc/fstab
# @TODO this is repeated in a bunch of places, put in libs somewhere?
# check that /boot device mount was a partuuid
if egrep '^PARTUUID=' "$fstab_file" | grep '/boot' ; then
echo "${ORANGEFG}/boot was a PARTUUID${RESET}"
sed -i -E "s|^PARTUUID=([^[:space:]]+)[[:space:]]+/boot([[:space:]]+)(.*)|PARTUUID=${P1_PARTUUID} /boot \3|" "$fstab_file"
fixed_boot=1
fi
# check that /boot device mount was a uuid
if egrep '^UUID=' "$fstab_file" | grep '/boot' ; then
echo "${ORANGEFG}/boot was a UUID${RESET}"
sed -i -E "s|^UUID=([^[:space:]]+)[[:space:]]+/boot([[:space:]]+)(.*)|UUID=${P1_UUID} /boot \3|" "$fstab_file"
fixed_boot=1
fi
# check that / device mount was a partuuid
if egrep '^PARTUUID=' "$fstab_file" | egrep '[[:space:]]/[[:space:]]' ; then
echo "/ was a PARTUUID"
sed -i -E "s|^PARTUUID=([^[:space:]]+)[[:space:]]+/([[:space:]]+)(.*)|PARTUUID=${P3_PARTUUID} / \3|" "$fstab_file"
fixed_root=1
fi
# check that / device mount was a uuid
if egrep '^UUID=' "$fstab_file" | egrep '[[:space:]]/[[:space:]]' ; then
echo "/ was a UUID"
sed -i -E "s|^UUID=([^[:space:]]+)[[:space:]]+/([[:space:]]+)(.*)|UUID=${P3_UUID} / \3|" "$fstab_file"
fixed_root=1
fi
if grep 'root=PARTUUID' /boot/cmdline.txt; then
sed -i -E "s|(root=PARTUUID)=([^[:space:]]+)|root=PARTUUID=$ROOT_PART_PARTUUID|" \
/boot/cmdline.txt
elif grep 'root=UUID' /boot/cmdline.txt; then
sed -i -E "s|(root=UUID)=([^[:space:]]+)|root=UUID=$ROOT_PART_UUID|" \
/boot/cmdline.txt
else
echo "unable to find UUID or PARTUUID in cmdline.txt"
echo "current cmdline.txt is"
cat /boot/cmdline.txt
exit 99
fi
# sed -i "s/init=[^[:space:]]*//" /boot/cmdline.txt
if [ ! "${OPT_NO_REBOOT}" ] ; then
umount -f /mnt/rootfs || true
cat /boot/cmdline.txt
echo "rebooting in 10..."
sleep 10
reboot
fi
exit 0
fi
if [ "${OPT_BOOT_RECOVERY}" ] || [ "${OPT_RESET}" ] ; then
echo "booting to recoveryfs"
mount /dev/disk/by-label/recoveryfs /mnt/recoveryfs || \
{ [ $? -eq 32 ] && echo "already mounted" || \
{ echo "some other error" ; exit 99 ;} }
P1_UUID="$(blkid -o value -s UUID ${DEVICE}1)"
P2_UUID="$(blkid -o value -s UUID ${DEVICE}2)"
P1_PARTUUID="$(blkid -o value -s PARTUUID ${DEVICE}1)"
P2_PARTUUID="$(blkid -o value -s PARTUUID ${DEVICE}2)"
fstab_file=/mnt/recoveryfs/etc/fstab
# @TODO this is repeated in a bunch of places, put in libs somewhere?
# check that /boot device mount was a partuuid
if egrep '^PARTUUID=' "$fstab_file" | grep '/boot' ; then
echo "${ORANGEFG}/boot was a PARTUUID${RESET}"
sed -i -E "s|^PARTUUID=([^[:space:]]+)[[:space:]]+/boot([[:space:]]+)(.*)|PARTUUID=${P1_PARTUUID} /boot \3|" "$fstab_file"
fixed_boot=1
fi
# check that /boot device mount was a uuid
if egrep '^UUID=' "$fstab_file" | grep '/boot' ; then
echo "${ORANGEFG}/boot was a UUID${RESET}"
sed -i -E "s|^UUID=([^[:space:]]+)[[:space:]]+/boot([[:space:]]+)(.*)|UUID=${P1_UUID} /boot \3|" "$fstab_file"
fixed_boot=1
fi
# check that / device mount was a partuuid
if egrep '^PARTUUID=' "$fstab_file" | egrep '[[:space:]]/[[:space:]]' ; then
echo "/ was a PARTUUID"
sed -i -E "s|^PARTUUID=([^[:space:]]+)[[:space:]]+/([[:space:]]+)(.*)|PARTUUID=${P2_PARTUUID} / \3|" "$fstab_file"
fixed_root=1
fi
# check that / device mount was a uuid
if egrep '^UUID=' "$fstab_file" | egrep '[[:space:]]/[[:space:]]' ; then
echo "/ was a UUID"
sed -i -E "s|^UUID=([^[:space:]]+)[[:space:]]+/([[:space:]]+)(.*)|UUID=${P2_UUID} / \3|" "$fstab_file"
fixed_root=1
fi
[ ! "${OPT_NO_REBOOT}" ] && umount -f /mnt/recoveryfs || true
fi
if [ "${OPT_BOOT_RECOVERY}" ] || [ "${OPT_RESET}" ] ; then
echo "show current cmdline.txt"
cat /boot/cmdline.txt
echo ""
if grep 'root=PARTUUID' /boot/cmdline.txt; then
sed -i -E "s|(root=PARTUUID)=([^[:space:]]+)|root=PARTUUID=$RECOVERY_PART_PARTUUID|" \
/boot/cmdline.txt
elif grep 'root=UUID' /boot/cmdline.txt; then
sed -i -E "s|(root=UUID)=([^[:space:]]+)|root=UUID=$RECOVERY_PART_UUID|" \
/boot/cmdline.txt
else
echo "unable to find UUID or PARTUUID in cmdline.txt"
echo "current cmdline.txt is"
cat /boot/cmdline.txt
exit 99
fi
fi
if [ "${OPT_BOOT_RECOVERY}" ] ; then
echo "rebooting to recovery - not resetting"
cat /boot/cmdline.txt
touch /boot/ssh
if [ ! "${OPT_NO_REBOOT}" ] ; then
umount -f /mnt/rootfs || true
cat /boot/cmdline.txt
echo "rebooting in 10..."
sleep 10
reboot
fi
exit 0
fi
if [ "${OPT_RESET}" ] ; then
echo "resetting"
# remove init option if we are still pre-resize mode. this would have to
# be done manually from raspi-config after this...
sed -i "s/init=[^[:space:]]*//g" /boot/cmdline.txt
# add resize init script to end of boot option
echo "$(cat /boot/cmdline.txt) init=/usr/lib/raspi-config/init_restore.sh" > /boot/cmdline.txt_tmp
cp -f /boot/cmdline.txt_tmp /boot/cmdline.txt
echo "show current cmdline.txt"
cat /boot/cmdline.txt
echo ""
[[ "${OPT_COPY_PI_PASS}" ]] && {
echo "copy pi pass is set"
cat /etc/shadow | egrep '^pi' | awk -F: '{print $2}' > /boot/restore_pi_pass
echo "value of pi pass"
cat /boot/restore_pi_pass
}
[[ "${OPT_COPY_ROOT_PASS}" ]] && {
echo "copy root pass is set"
cat /etc/shadow | egrep '^root' | awk -F: '{print $2}' > /boot/restore_root_pass
echo "value of root pass"
cat /boot/restore_root_pass
}
[[ "${OPT_COPY_WIFI}" ]] && {
echo "copy wifi settings is set"
if [ -f /etc/wpa_supplicant/wpa_supplicant.conf ] ; then
echo "wpa file exists"
cp /etc/wpa_supplicant/wpa_supplicant.conf \
/boot/wpa_supplicant.conf
echo "current contents of wpa_supplicant in /boot"
cat /boot/wpa_supplicant.conf
fi
}
cat <<EOF
OPT_COPY_PI_PASS=${OPT_COPY_PI_PASS}
OPT_COPY_ROOT_PASS=${OPT_COPY_ROOT_PASS}
OPT_COPY_WIFI=${OPT_COPY_WIFI}
EOF
if [ ! "${OPT_NO_REBOOT}" ] ; then
cat /boot/cmdline.txt
echo "rebooting in 10..."
sleep 10
reboot
fi
fi