-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsyncUUIDs.sh
executable file
·496 lines (399 loc) · 13.7 KB
/
syncUUIDs.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/bin/bash
#
#######################################################################################################################
#
# Check and optionally update /boot/cmdline.txt and /etc/fstab on a device with installed RaspbianOS
# with the actual UUIDs/PARTUUIDs/LABELs of the partitions. Useful if a cloned RaspbianOS fails to boot because
# of UUID/PARTUUID/LABEL mismatch.
#
# Either download this script
# curl -sO https://raw.githubusercontent.com/framps/raspberryTools/master/invokeTool.sh syncUUIDs.sh
# and invoke
# sudo ./syncUUIDs.sh <options>
#
# or use following command to directly download and invoke syncUUIDs:
# curl -s https://raw.githubusercontent.com/framps/raspberryTools/master/invokeTool.sh | sudo bash -s -- synUUIDs.sh <options>
# for example
# curl -s https://raw.githubusercontent.com/framps/raspberryTools/master/invokeTool.sh | sudo bash -s -- synUUIDs.sh -u /dev/mmcblk0`
#
# Copyright (C) 2022-2025 framp at linux-tips-and-tricks dot de
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#######################################################################################################################
set -euo pipefail
readonly VERSION="0.3.3.1"
readonly GITREPO="https://github.com/framps/raspberryTools"
#shellcheck disable=SC2155
#(warning): Declare and assign separately to avoid masking return values.
readonly MYSELF="$(basename $0)"
readonly MYNAME=${MYSELF##*/}
readonly CMDLINE="cmdline.txt"
readonly FSTAB="etc/fstab"
readonly MOUNTPOINT_BOOT="/mnt/boot"
readonly MOUNTPOINT_ROOT="/mnt/root"
readonly BOOT="/boot"
readonly BOOT_FIRMWARE="/boot/firmware"
readonly ROOT="/"
readonly ROOT_TARGET="root="
readonly LOG_FILE="$MYNAME.log"
declare -r PS4='|${LINENO}> \011${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
dryrun=1 # default, enable update with option -u
verbose=0
randomize=0
mismatchDetected=0
fstabSaved=0
function cleanup() {
local rc="$1"
umount "$MOUNTPOINT_BOOT" &>/dev/null || true
umount "$MOUNTPOINT_ROOT" &>/dev/null || true
rmdir "$MOUNTPOINT_BOOT" &>/dev/null || true
rmdir "$MOUNTPOINT_ROOT" &>/dev/null || true
if (( $rc != 0 )); then
if [[ -e "$LOG_FILE" ]]; then
error "Error log"
cat "$LOG_FILE"
rm "$LOG_FILE" &>/dev/null || true
fi
else
rm "$LOG_FILE" &>/dev/null || true
fi
}
function error() {
echo "??? $*" > /dev/tty
exit 1
}
function info() {
echo "--- $*"
}
function note() {
echo "!!! $*"
}
function err() {
local rc="$1"
echo "??? Unexpected error occured with RC $rc"
local i=0
local FRAMES=${#BASH_LINENO[@]}
for ((i=FRAMES-2; i>=0; i--)); do
echo ' File' \"${BASH_SOURCE[i+1]}\", line ${BASH_LINENO[i]}, in ${FUNCNAME[i+1]}
sed -n "${BASH_LINENO[i]}{s/^/ /;p}" "${BASH_SOURCE[i+1]}"
done
}
function isSupportedSystem() {
local RPI_ISSUE=$MOUNTPOINT_ROOT/etc/rpi-issue
if [[ ! -e "$RPI_ISSUE" ]]; then
return 1
fi
if [[ ! -e $MOUNTPOINT_ROOT/etc/os-release ]]; then
return 1
fi
local version
version=$(grep -E "^VERSION_ID" /etc/os-release | cut -f 2 -d "=" | sed 's/"//g')
if (( version < 12 )); then
return 1
fi
return 0
}
trap 'cleanup $?' SIGINT SIGTERM SIGHUP EXIT
trap 'err $?' ERR
function isMounted() {
grep -qs "$1" /proc/mounts
}
function parseCmdline {
if [[ ! -e ${MOUNTPOINT_BOOT}/${CMDLINE} ]]; then
error "Unable to find ${MOUNTPOINT_BOOT}/${CMDLINE}"
fi
local rootTarget
if ! rootTarget=$(grep -Eo "${ROOT_TARGET}\S+=\S+" ${MOUNTPOINT_BOOT}/${CMDLINE} | sed -E "s/${ROOT_TARGET}//"); then
error "Parsing of ${CMDLINE} for '${ROOT_TARGET}' failed"
fi
if [[ -z $rootTarget ]]; then
error "Parsing of ${CMDLINE} for '${ROOT_TARGET}' failed"
fi
echo "$rootTarget"
}
function parseFstab {
local tgt mnt
local bootTgt="" rootTgt=""
if [[ ! -e ${MOUNTPOINT_ROOT}/${FSTAB} ]]; then
error "Unable to find ${MOUNTPOINT_ROOT}/${CMDLINE}"
fi
#shellcheck disable=SC2034
#(warning): r appears unused. Verify use (or export if used externally).
while read tgt mnt r; do
case $mnt in
"$BOOT" | "$BOOT_FIRMWARE")
bootTgt=$tgt
;;
"$ROOT")
rootTgt=$tgt
;;
esac
done <"${MOUNTPOINT_ROOT}/${FSTAB}"
if [[ -z $bootTgt ]]; then
error "Parsing of /${FSTAB} for '$BOOT' or '$BOOT_FIRMWARE' failed"
fi
if [[ -z $rootTgt ]]; then
error "Parsing of /${FSTAB} for '$ROOT' failed"
fi
echo "$bootTgt $rootTgt"
}
function parseBLKID { # device uuid/poartuuid/label
#shellcheck disable=SC2155
#(warning): Declare and assign separately to avoid masking return values.
local blkid="$(blkid $1 -o udev | grep "ID_FS_${2}=")"
if [[ -z $blkid ]]; then
error "Parsing of blkid $1 for filesystem type $2 failed"
fi
echo $blkid
}
function updateFstab() { # bootType uuid newUUID
if (( $dryrun )); then
note "$1 $2 should be updated to $3 in $rootPartition/$FSTAB "
return
fi
if (( ! fstabSaved )); then
info "Creating fstab backup /${FSTAB}.bak on $rootPartition"
cp ${MOUNTPOINT_ROOT}/${FSTAB} ${MOUNTPOINT_ROOT}/${FSTAB}.bak
fstabSaved=1
fi
info "Updating $1 $2 to $3 in $rootPartition/$FSTAB"
if ! sed -i "s/^$1=$2/$1=$3/" ${MOUNTPOINT_ROOT}/${FSTAB}; then
error "Unable to update $rootPartition/$FSTAB"
fi
}
function updateCmdline() { # bootType uuid newUUID
if (( $dryrun )); then
note "$1 $2 should be updated to $3 in $bootPartition/${CMDLINE}"
return
fi
info "Creating cmdline backup /${CMDLINE}.bak on $bootPartition"
cp ${MOUNTPOINT_BOOT}/${CMDLINE} ${MOUNTPOINT_BOOT}/${CMDLINE}.bak
info "Updating $1 $2 to $3 in $bootPartition/${CMDLINE}"
if ! sed -i --follow-symlinks "s/$1=$2/$1=$3/" ${MOUNTPOINT_BOOT}/${CMDLINE}; then
error "Unable to update $bootPartition/$CMDLINE"
fi
}
function randomizePartitions() {
local answer
echo -n "!!! Creating new UUID and PARTUUID on $device. Are you sure? (y|N) "
read answer
answer=${answer:0:1} # first char only
answer=${answer:-"n"} # set default no
if [[ $answer != "y" ]]; then
exit 1
fi
#shellcheck disable=SC2155
#(warning): Declare and assign separately to avoid masking return values.
local newPARTUUID=$(od -A n -t x -N 4 /dev/urandom | tr -d " ")
info "Creating new PARTUUID $newPARTUUID on $device"
echo -ne "x\ni\n0x$newPARTUUID\nr\nw\nq\n" | fdisk "$device" &>>$LOG_FILE
#shellcheck disable=SC2155
#(warning): Declare and assign separately to avoid masking return values.
local newUUID="$(od -A n -t x -N 4 /dev/urandom | tr -d " " | sed -r 's/(.{4})/\1-/')"
newUUID="${newUUID^^*}"
info "Creating new UUID $newUUID on $bootPartition"
printf "\x${newUUID:7:2}\x${newUUID:5:2}\x${newUUID:2:2}\x${newUUID:0:2}" | dd bs=1 seek=67 count=4 conv=notrunc of=$bootPartition &>>$LOG_FILE
newUUID="$(</proc/sys/kernel/random/uuid)"
info "Creating new UUID $newUUID on $rootPartition"
if ! umount $MOUNTPOINT_ROOT; then
error "Unable to umount $MOUNTPOINT_ROOT"
fi
e2fsck -y -f $rootPartition &>>$LOG_FILE
tune2fs -U "$newUUID" $rootPartition &>>$LOG_FILE
if ! mount $rootPartition $MOUNTPOINT_ROOT; then
error "Unable to mount $rootPartition"
fi
sync
sleep 3
partprobe $device
sleep 3
udevadm settle
}
function usage() {
cat << EOF
$MYSELF - $VERSION ($GITREPO)
Synchronize UUIDs, PARTUUIDs or LABELs in /etc/fstab and /boot/cmdline.txt
with existing UUIDs, PARTUUIDs or LABELs of device partitions.
If no option is passed the used UUIDs, PARTUUIDs or LABELs are retrieved
and displayed only. No files are updated.
Create new UUIDs and PARTUUIDs on device partitions and update
/etc/fstab and /boot/cmdline.txt.
Usage: $0 [-n | -u]? [-v]? device
-n: Create new random UUIDs and PARTUUIDs and sync
/boot/cmdline.txt and /etc/fstab afterwards
-u: Create backup of files and update the UUIDs, PARTUUIDs or LABELs in
/boot/cmdline.txt and /etc/fstab
-v: Be verbose
Device examples: /dev/sda, /dev/mmcblk0, /dev/nvme0n1
EOF
}
#
# main
#
(( $# <= 0 )) && { usage; exit; }
echo "$MYSELF $VERSION ($GITREPO)"
rm $LOG_FILE &>/dev/null || true
while getopts ":h :n :u :v" opt; do
case "$opt" in
h) usage
exit 0
;;
u) dryrun=0
;;
n) randomize=1
dryrun=0
;;
v) verbose=1
;;
\? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
: ) echo "Missing option argument for -$OPTARG" >&2; exit 1;;
* ) echo "Unimplemented option: -$OPTARG" >&2; exit 1;;
esac
done
shift $(( $OPTIND - 1 ))
if [[ -z $1 ]]; then
error "Missing device to update UUIDs, PARTUUIDs or LABELs"
exit 1
fi
if (( $UID != 0 )); then
info "Please invoke $MYSELF as root or with sudo"
exit 1
fi
device=$1
case $device in
/dev/sd*)
bootPartition="${device}1"
rootPartition="${device}2"
;;
/dev/mmcblk*|/dev/nvme*)
bootPartition="${device}p1"
rootPartition="${device}p2"
;;
*)
echo "Invalid device $device"
echo "Device examples: /dev/sda, /dev/mmcblk0, /dev/nvme0n1"
exit 1
esac
if [[ ! -e $device ]]; then
error "$device does not exist"
exit 1
fi
if [[ ! -b $device ]]; then
error "$device is no blockdevice"
exit 1
fi
if [[ ! -e $bootPartition ]]; then
error "$bootPartition not found"
exit 1
fi
if [[ ! -e $rootPartition ]]; then
error "$rootPartition not found"
exit 1
fi
if (( randomize )); then
if isMounted $bootPartition; then
error "$bootPartition mounted. No update possible."
exit 1
fi
if isMounted $rootPartition; then
error "$rootPartition mounted. No update possible."
exit 1
fi
fi
if (( $verbose )); then
info "bootPartition: $bootPartition"
info "rootPartition: $rootPartition"
echo
fi
if ! mkdir $MOUNTPOINT_BOOT; then
error "Unable to mkdir $MOUNTPOINT_BOOT"
fi
if ! mount $bootPartition $MOUNTPOINT_BOOT; then
error "Unable to mount $bootPartition"
fi
if ! mkdir $MOUNTPOINT_ROOT; then
error "Unable to mkdir $MOUNTPOINT_ROOT"
fi
if ! mount $rootPartition $MOUNTPOINT_ROOT; then
error "Unable to mount $rootPartition"
fi
if ! isSupportedSystem; then
error "$MYSELF supports Raspberries running RasbianOS Bookworm only"
fi
cmdline=$(parseCmdline)
#shellcheck disable=SC2207
#(warning): Prefer mapfile or read -a to split command output (or quote to avoid splitting).
fstab=( $(parseFstab) )
cmdlineRootType="$(cut -d= -f1 <<< $cmdline)"
cmdlineRootUUID="$(cut -d= -f2 <<< $cmdline)"
fstabBootType="$(cut -d= -f1 <<< "${fstab[0]}")"
fstabBootUUID="$(cut -d= -f2 <<< "${fstab[0]}")"
fstabRootType="$(cut -d= -f1 <<< "${fstab[1]}")"
fstabRootUUID="$(cut -d= -f2 <<< "${fstab[1]}")"
actualCmdlineRootUUID="$(parseBLKID ${rootPartition} $cmdlineRootType | cut -d= -f2)"
actualFstabBootUUID="$(parseBLKID ${bootPartition} $fstabBootType | cut -d= -f2)"
actualFstabRootUUID="$(parseBLKID ${rootPartition} $fstabRootType | cut -d= -f2)"
if [[ -z $actualCmdlineRootUUID || \
-z $actualFstabBootUUID || \
-z $actualFstabRootUUID || \
-z $cmdlineRootType || \
-z $cmdlineRootUUID || \
-z $fstabBootType || \
-z $fstabBootUUID || \
-z $fstabRootType || \
-z $fstabRootUUID \
]]; then
echo "??? ASSERTION FAILED: Unable to collect required data"
exit 1
fi
if (( $verbose )); then
echo "... $fstabBootType on $bootPartition: $actualFstabBootUUID"
echo "... $fstabRootType on $rootPartition: $actualFstabRootUUID"
echo
echo "... Boot $fstabBootType used in fstab: $fstabBootUUID"
echo "... Root $fstabRootType used in fstab: $fstabRootUUID"
echo "... Root $cmdlineRootType used in cmdline: $cmdlineRootUUID"
echo
fi
if (( randomize )); then
randomizePartitions
fi
actualCmdlineRootUUID="$(parseBLKID ${rootPartition} $cmdlineRootType | cut -d= -f2)"
actualFstabBootUUID="$(parseBLKID ${bootPartition} $fstabBootType | cut -d= -f2)"
actualFstabRootUUID="$(parseBLKID ${rootPartition} $fstabRootType | cut -d= -f2)"
if [[ $cmdlineRootUUID == "$actualCmdlineRootUUID" ]]; then
info "Root $fstabRootType $actualFstabRootUUID already used in $bootPartition/$CMDLINE"
else
updateCmdline $cmdlineRootType $cmdlineRootUUID $actualCmdlineRootUUID
mismatchDetected=1
fi
if [[ $fstabBootUUID == "$actualFstabBootUUID" ]]; then
info "Boot $fstabBootType $actualFstabBootUUID already used in $rootPartition/$FSTAB"
else
updateFstab $fstabBootType $fstabBootUUID $actualFstabBootUUID
mismatchDetected=1
fi
if [[ $fstabRootUUID == "$actualFstabRootUUID" ]]; then
info "Root $fstabRootType $actualFstabRootUUID already used in $rootPartition/$FSTAB"
else
updateFstab $fstabRootType $fstabRootUUID $actualFstabRootUUID
mismatchDetected=1
fi
if (( $mismatchDetected && $dryrun)); then
note "Use option -u to update the incorrect UUIDs, PARTUUIDs or LABELs"
fi
if (( ! $mismatchDetected && ! $dryrun)); then
info "No incorrect UUIDs, PARTUUIDs or LABELs detected"
fi