Repository navigation
Expand file tree
/
Copy pathcleanup-bootstrap-root.sh
More file actions
executable file
·1804 lines (1338 loc) · 41.6 KB
/
Copy pathcleanup-bootstrap-root.sh
File metadata and controls
executable file
·1804 lines (1338 loc) · 41.6 KB
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# cleanup-bootstrap-root.sh
#
# Safely removes verified filesystem content from a retired
# Btrfs bootstrap root after transitioning to a snapshot root.
#
# Version: 1.1.0
# License: GPL-3.0-or-later
#
# Copyright (C) 2026 Scott McClain
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Execution stages:
# 1. Environment verification
# 2. Mount bootstrap root
# 3. Discover subvolumes
# 4. Discover active snapshot
# 5. Build snapshot map
# 6. Build bootstrap map
# 7. Compare and build retained map
# 8. Policy classification
# 9. Verification report
# 10. Build execution plan
# 11. Execute cleanup
# 12. Policy verification
set -euo pipefail
###############################################################################
# Helper Functions
###############################################################################
pass() {
printf " [PASS] %s\n" "$1"
}
warn() {
printf " [WARN] %s\n" "$1"
}
fail() {
printf " [FAIL] %s\n" "$1"
exit 1
}
derive_bootstrap_subvolume() {
local current="$1"
local bootstrap
current="${current#/}"
if [[ "$current" =~ ^(.+)/\.snapshots/([0-9]+)/snapshot$ ]]; then
bootstrap="${BASH_REMATCH[1]}"
else
return 1
fi
[[ -n "$bootstrap" ]] || return 1
[[ "$bootstrap" =~ ^[a-zA-Z0-9_@.+/-]+$ ]] || return 1
case "/$bootstrap/" in
*//*|*/./*|*/../*|*/.snapshots/*)
return 1
;;
esac
printf '%s\n' "$bootstrap"
}
device_node_signature() {
local object="$1"
local allow_symlink="${2:-false}"
local kind
local device_number
if [[ -L "$object" && "$allow_symlink" != true ]]; then
return 1
fi
if [[ -c "$object" ]]; then
kind="character"
elif [[ -b "$object" ]]; then
kind="block"
else
return 1
fi
device_number=$(stat -Lc '%t:%T' -- "$object") || return 1
printf '%s:%s\n' "$kind" "$device_number"
}
matching_device_node() {
local bootstrap_object="$1"
local active_object="$2"
local bootstrap_signature
local active_signature
local active_resolved
bootstrap_signature=$(device_node_signature "$bootstrap_object") \
|| return 1
[[ "$active_object" == /dev/* ]] || return 1
if [[ -L "$active_object" ]]; then
active_resolved=$(readlink -f -- "$active_object") || return 1
[[ "$active_resolved" == /dev/* ]] || return 1
fi
active_signature=$(device_node_signature "$active_object" true) \
|| return 1
[[ "$bootstrap_signature" == "$active_signature" ]]
}
###############################################################################
# Cleanup
###############################################################################
cleanup() {
local original_status=$?
local cleanup_failed=false
trap - EXIT
if [[ -n "${BOOTSTRAP_ROOT:-}" ]] &&
mountpoint -q "$BOOTSTRAP_ROOT" 2>/dev/null; then
if ! umount "$BOOTSTRAP_ROOT"; then
warn "Unable to unmount temporary bootstrap root:"
printf " %s\n" "$BOOTSTRAP_ROOT"
cleanup_failed=true
fi
fi
if [[ -n "${BOOTSTRAP_ROOT:-}" ]] &&
[[ -d "$BOOTSTRAP_ROOT" ]] &&
! mountpoint -q "$BOOTSTRAP_ROOT" 2>/dev/null; then
if ! rmdir "$BOOTSTRAP_ROOT"; then
warn "Unable to remove temporary mount directory:"
printf " %s\n" "$BOOTSTRAP_ROOT"
cleanup_failed=true
fi
fi
if (( original_status == 0 )) && [[ "$cleanup_failed" == true ]]; then
exit 1
fi
exit "$original_status"
}
trap cleanup EXIT
###############################################################################
# Execution Helper Functions
###############################################################################
delete_object() {
local OBJECT="$1"
if [[ "$DRY_RUN" == true ]]; then
return 0
fi
rm -- "$OBJECT"
}
###############################################################################
# Runtime Environment
###############################################################################
DEVICE=""
ROOT_FSTYPE=""
CURRENT_SOURCE=""
CURRENT_SUBVOL=""
CURRENT_SUBVOL_ID=""
DEFAULT_SUBVOL_ID=""
BOOTSTRAP_ROOT=""
BOOTSTRAP_SUBVOL=""
BOOTSTRAP_SUBVOL_ID=""
MOUNTED_BOOTSTRAP_SUBVOL=""
SNAPSHOT_STORE_SUBVOL=""
SUBVOLUME_LIST=()
declare -A SNAPSHOT_MAP
declare -A BOOTSTRAP_MAP
declare -A RETAINED_MAP
declare -A SUBVOLUME_PATH_MAP
declare -A SUBVOLUME_PARENT_MAP
MATCH_LIST=()
MISSING_LIST=()
KEEP_BOOT=()
KEEP_SNAPSHOTS=()
KEEP_SUBVOLUME=()
DELETE_CANDIDATE=()
RETAINED_OBJECTS=()
###############################################################################
# Execution Queues
###############################################################################
DELETE_LINKS=()
DELETE_FILES=()
DELETE_SOCKETS=()
DELETE_DEVICES=()
DELETE_DIRS=()
DELETE_OTHER=()
###############################################################################
# Execution Statistics
###############################################################################
REMOVED_LINKS=0
FAILED_LINKS=0
REMOVED_FILES=0
FAILED_FILES=0
REMOVED_SOCKETS=0
FAILED_SOCKETS=0
REMOVED_DEVICES=0
FAILED_DEVICES=0
REMOVED_DIRS=0
###############################################################################
# Execution Settings
###############################################################################
DRY_RUN=""
usage() {
printf '\n'
printf 'Usage:\n'
printf '\n'
printf ' %s --dryrun\n' "$0"
printf ' %s --execute\n' "$0"
printf '\n'
exit 1
}
(( $# == 1 )) || usage
case "$1" in
--dryrun)
DRY_RUN=true
;;
--execute)
DRY_RUN=false
;;
*)
usage
;;
esac
###############################################################################
# Banner
###############################################################################
echo
echo "============================================================"
echo "cleanup-bootstrap-root.sh"
echo "Version 1.1.0"
echo "============================================================"
echo
echo "Module 1 - Environment Verification"
###############################################################################
# Root
###############################################################################
[[ $EUID -eq 0 ]] \
&& pass "Running as root." \
|| fail "This script must be run with sudo."
###############################################################################
# Filesystem
###############################################################################
ROOT_FSTYPE=$(findmnt -no FSTYPE /)
[[ "$ROOT_FSTYPE" == "btrfs" ]] \
&& pass "Root filesystem is BTRFS." \
|| fail "Root filesystem is not BTRFS."
###############################################################################
# Current Root
###############################################################################
CURRENT_SOURCE=$(findmnt -no SOURCE /)
CURRENT_SUBVOL=$(findmnt -no FSROOT /)
CURRENT_SUBVOL="${CURRENT_SUBVOL#/}"
[[ -n "$CURRENT_SUBVOL" ]] \
|| fail "Unable to determine the current root subvolume."
echo
echo "Current Root"
echo "------------"
echo "$CURRENT_SOURCE"
echo
echo "Current Subvolume"
echo "-----------------"
echo "$CURRENT_SUBVOL"
echo
#
# Derive the retired bootstrap root from the exact nested snapshot path.
# This supports @, @rootfs, @btrfs, and other root-subvolume names while
# refusing a snapshot nested beneath another snapshot.
#
BOOTSTRAP_SUBVOL=$(derive_bootstrap_subvolume "$CURRENT_SUBVOL") \
|| fail "System is NOT running from a supported nested snapshot."
SNAPSHOT_STORE_SUBVOL="${BOOTSTRAP_SUBVOL}/.snapshots"
pass "Running from a snapshot beneath '$BOOTSTRAP_SUBVOL'."
###############################################################################
# Default Subvolume
###############################################################################
echo
echo "Default Subvolume"
echo "-----------------"
DEFAULT_INFO=$(btrfs subvolume get-default /)
echo "$DEFAULT_INFO"
DEFAULT_SUBVOL_ID=$(awk '$1 == "ID" { print $2 }' <<< "$DEFAULT_INFO")
CURRENT_SUBVOL_ID=$(btrfs subvolume show / | \
sed -n 's/^[[:space:]]*Subvolume ID:[[:space:]]*//p')
if [[ -n "$CURRENT_SUBVOL_ID" &&
-n "$DEFAULT_SUBVOL_ID" &&
"$CURRENT_SUBVOL_ID" == "$DEFAULT_SUBVOL_ID" ]]; then
pass "Default subvolume matches running system."
else
fail "Running snapshot does not match the default snapshot."
fi
###############################################################################
# Locate Device
###############################################################################
DEVICE=$(findmnt -no SOURCE / | sed 's/\[.*//')
echo
echo "Filesystem Device"
echo "-----------------"
echo "$DEVICE"
[[ -b "$DEVICE" ]] \
&& pass "Filesystem device located." \
|| fail "Unable to determine filesystem device."
###############################################################################
# Verify Derived Bootstrap Root Exists
###############################################################################
echo
echo "Checking for bootstrap root subvolume..."
echo
BOOTSTRAP_SUBVOL_ID=$(btrfs subvolume list / | awk -v expected="$BOOTSTRAP_SUBVOL" '
$1 == "ID" && $2 ~ /^[0-9]+$/ && index($0, " path ") {
path=$0
sub(/^.* path /, "", path)
if (path == expected) {
print $2
found++
}
}
END { if (found != 1) exit 1 }
') || fail "Bootstrap root subvolume '$BOOTSTRAP_SUBVOL' not found or ambiguous."
if [[ "$BOOTSTRAP_SUBVOL_ID" =~ ^[0-9]+$ ]]; then
pass "Bootstrap root '$BOOTSTRAP_SUBVOL' found (ID $BOOTSTRAP_SUBVOL_ID)."
else
fail "Invalid subvolume ID for bootstrap root '$BOOTSTRAP_SUBVOL'."
fi
###############################################################################
# Verify .snapshots
###############################################################################
echo
echo "Checking for .snapshots subvolume..."
echo
if btrfs subvolume list / | awk -v expected="$SNAPSHOT_STORE_SUBVOL" '
$1 == "ID" && index($0, " path ") {
path=$0
sub(/^.* path /, "", path)
if (path == expected) found++
}
END { exit(found == 1 ? 0 : 1) }
'; then
pass "$SNAPSHOT_STORE_SUBVOL found."
else
fail "$SNAPSHOT_STORE_SUBVOL not found."
fi
###############################################################################
# Execution Mode
###############################################################################
echo
echo "Execution Mode"
echo "--------------"
if [[ "$DRY_RUN" == true ]]; then
echo "Mode : Dry Run"
pass "Filesystem modifications are disabled."
echo " Analysis, planning, and validation will be performed."
else
echo "Mode : Execute"
warn "Filesystem modifications are ENABLED."
echo " The retired bootstrap ($BOOTSTRAP_SUBVOL) subvolume will be modified."
fi
echo
###############################################################################
# Summary
###############################################################################
echo
echo "============================================================"
pass "Environment verification complete."
echo "Proceed to Module 2."
echo "============================================================"
echo
###############################################################################
# Module 2 - Mount Bootstrap Root
###############################################################################
echo
echo "============================================================"
echo "Module 2 - Mount Bootstrap Root"
echo "============================================================"
#
# Create a temporary mount point
#
BOOTSTRAP_ROOT=$(mktemp -d /run/bootstrap-root.XXXXXX)
pass "Created temporary mount point:"
echo " $BOOTSTRAP_ROOT"
#
# Mount the retired bootstrap subvolume read-only
#
echo
echo "Mounting retired $BOOTSTRAP_SUBVOL subvolume..."
mount -t btrfs -o "ro,subvolid=$BOOTSTRAP_SUBVOL_ID" \
"$DEVICE" "$BOOTSTRAP_ROOT"
#
# Verify mount
#
MOUNTED_BOOTSTRAP_SUBVOL=$(findmnt -no FSROOT "$BOOTSTRAP_ROOT")
MOUNTED_BOOTSTRAP_SUBVOL="${MOUNTED_BOOTSTRAP_SUBVOL#/}"
echo
echo "Mounted Filesystem"
echo "------------------"
echo "Mount Point : $BOOTSTRAP_ROOT"
echo "Device : $DEVICE"
echo "Subvolume : $MOUNTED_BOOTSTRAP_SUBVOL"
echo "Access : Read-only"
if [[ "$MOUNTED_BOOTSTRAP_SUBVOL" != "$BOOTSTRAP_SUBVOL" ]]; then
fail "Expected subvolume '$BOOTSTRAP_SUBVOL' but mounted '$MOUNTED_BOOTSTRAP_SUBVOL'."
fi
pass "Bootstrap root mounted successfully."
#
# Bootstrap Root Summary
#
echo
echo "Bootstrap Root Summary"
echo "----------------------"
echo "Mount Point : $BOOTSTRAP_ROOT"
echo "Device : $DEVICE"
echo "Subvolume : $BOOTSTRAP_SUBVOL"
echo
echo "Top-Level Objects"
echo "-----------------"
find "$BOOTSTRAP_ROOT" \
-mindepth 1 \
-maxdepth 1 \
-printf "%f\n" | sort
echo
###############################################################################
# Mount Verification
###############################################################################
echo
pass "Bootstrap root mounted and ready for analysis."
echo
pass "Module 2 complete."
###############################################################################
# Module 3 - Discover Btrfs Subvolumes
###############################################################################
echo
echo "============================================================"
echo "Module 3 - Discover Btrfs Subvolumes"
echo "============================================================"
#
# Discover every Btrfs subvolume on the filesystem.
#
mapfile -t SUBVOLUME_LIST < <(
btrfs subvolume list / | awk '
$1 == "ID" && index($0, " path ") {
sub(/^.* path /, "")
print
}
' | sort
)
#
# Verify discovery.
#
if [[ ${#SUBVOLUME_LIST[@]} -eq 0 ]]; then
fail "No Btrfs subvolumes were discovered."
fi
###############################################################################
# Build subvolume and parent-directory lookup tables
###############################################################################
for subvol in "${SUBVOLUME_LIST[@]}"; do
#
# Only subvolumes nested beneath the derived bootstrap root exist inside
# the retired bootstrap-root namespace.
#
# Sibling subvolumes outside that exact namespace require no mapping.
#
[[ "$subvol" == "$BOOTSTRAP_SUBVOL/"* ]] || continue
#
# Convert the Btrfs subvolume path into a path
# relative to the mounted bootstrap root.
#
path="${subvol#"$BOOTSTRAP_SUBVOL/"}"
[[ -n "$path" ]] || continue
#
# Record the actual subvolume path.
#
# This path and everything beneath it must remain.
#
SUBVOLUME_PATH_MAP["$path"]=1
#
# Record only the parent directory entries leading
# to the subvolume.
#
# Their unrelated contents are not protected.
#
parent="$path"
while [[ "$parent" == */* ]]; do
parent="${parent%/*}"
[[ -n "$parent" ]] || break
SUBVOLUME_PARENT_MAP["$parent"]=1
done
done
echo
echo "Protected Subvolume Paths : ${#SUBVOLUME_PATH_MAP[@]}"
echo
echo "Protected Parent Paths : ${#SUBVOLUME_PARENT_MAP[@]}"
echo
echo "Indexed Subvolumes : ${#SUBVOLUME_LIST[@]}"
#
# Display summary.
#
echo
echo "Discovered Subvolumes"
echo "---------------------"
for subvol in "${SUBVOLUME_LIST[@]}"; do
printf " %s\n" "$subvol"
done
echo
echo "Total Subvolumes : ${#SUBVOLUME_LIST[@]}"
#
# Sanity checks.
#
bootstrap_found=false
snapshot_store_found=false
for subvol in "${SUBVOLUME_LIST[@]}"; do
[[ "$subvol" == "$BOOTSTRAP_SUBVOL" ]] && bootstrap_found=true
[[ "$subvol" == "$SNAPSHOT_STORE_SUBVOL" ]] && snapshot_store_found=true
done
if [[ "$bootstrap_found" == true ]]; then
pass "$BOOTSTRAP_SUBVOL subvolume found."
else
fail "$BOOTSTRAP_SUBVOL subvolume missing."
fi
if [[ "$snapshot_store_found" == true ]]; then
pass "$SNAPSHOT_STORE_SUBVOL subvolume found."
else
fail "$SNAPSHOT_STORE_SUBVOL subvolume missing."
fi
echo
pass "Module 3 complete."
###############################################################################
# Module 4 - Discover Active Snapshot Tree
###############################################################################
echo
echo "============================================================"
echo "Module 4 - Discover Active Snapshot Tree"
echo "============================================================"
#
# Discover every object in the currently running snapshot.
#
mapfile -t SNAPSHOT_LIST < <(
cd / &&
find . -xdev \
| sed 's#^\./##' \
| grep -v '^\.$' \
| sort
)
#
# Verify discovery.
#
if [[ ${#SNAPSHOT_LIST[@]} -eq 0 ]]; then
fail "Active snapshot appears to be empty."
fi
#
# Summary
#
echo
echo "Snapshot Statistics"
echo "-------------------"
echo "Objects Found : ${#SNAPSHOT_LIST[@]}"
echo
echo "Sample Objects"
echo "--------------"
for ((i=0; i<20 && i<${#SNAPSHOT_LIST[@]}; i++)); do
printf " %s\n" "${SNAPSHOT_LIST[$i]}"
done
echo
pass "Module 4 complete."
###############################################################################
# Module 5 - Build Snapshot Lookup Table
###############################################################################
echo
echo "============================================================"
echo "Module 5 - Build Snapshot Lookup Table"
echo "============================================================"
#
# Build an associative array for O(1) lookups.
#
for path in "${SNAPSHOT_LIST[@]}"; do
SNAPSHOT_MAP["$path"]=1
done
#
# Verify.
#
if [[ ${#SNAPSHOT_MAP[@]} -eq 0 ]]; then
fail "Snapshot lookup table is empty."
fi
echo
echo "Snapshot Lookup Table"
echo "---------------------"
echo "Indexed Objects : ${#SNAPSHOT_MAP[@]}"
echo
echo "Lookup Tests"
echo "------------"
for path in \
etc \
usr \
var \
boot \
boot/grub \
var/log \
does/not/exist
do
if [[ -n "${SNAPSHOT_MAP[$path]:-}" ]]; then
printf " %-20s YES\n" "$path"
else
printf " %-20s NO\n" "$path"
fi
done
echo
pass "Module 5 complete."
###############################################################################
# Module 6 - Build Bootstrap Lookup Table
###############################################################################
echo
echo "============================================================"
echo "Module 6 - Build Bootstrap Lookup Table"
echo "============================================================"
#
# Build a lookup table from the retired bootstrap-root subvolume.
#
while IFS= read -r path; do
#
# Remove the leading mount point.
#
relative="${path#${BOOTSTRAP_ROOT}/}"
#
# Skip the root itself.
#
[[ "$relative" == "$BOOTSTRAP_ROOT" ]] && continue
[[ -z "$relative" ]] && continue
BOOTSTRAP_MAP["$relative"]=1
done < <(
find "$BOOTSTRAP_ROOT" -xdev
)
#
# Verify.
#
if [[ ${#BOOTSTRAP_MAP[@]} -eq 0 ]]; then
fail "Bootstrap lookup table is empty."
fi
echo
echo "Bootstrap Lookup Table"
echo "----------------------"
echo "Indexed Objects : ${#BOOTSTRAP_MAP[@]}"
echo
echo "Lookup Tests"
echo "------------"
for path in \
etc \
usr \
var \
boot \
boot/grub \
var/log \
does/not/exist
do
if [[ -n "${BOOTSTRAP_MAP[$path]:-}" ]]; then
printf " %-20s YES\n" "$path"
else
printf " %-20s NO\n" "$path"
fi
done
echo
pass "Module 6 complete."
###############################################################################
# Module 7 - Compare Bootstrap to Active Snapshot
###############################################################################
echo
echo "============================================================"
echo "Module 7 - Compare Bootstrap to Active Snapshot"
echo "============================================================"
MATCH_LIST=()
MISSING_LIST=()
#
# Compare every bootstrap object against the active snapshot.
#
for path in "${!BOOTSTRAP_MAP[@]}"; do
if [[ -n "${SNAPSHOT_MAP[$path]:-}" ]]; then
MATCH_LIST+=("$path")
else
#
# Object is missing from the active snapshot.
# If its immediate parent directory still exists
# in the active snapshot, treat it as obsolete.
#
PARENT="${path%/*}"
#
# Top-level objects have no parent directory.
#
if [[ "$PARENT" == "$path" ]]; then
MISSING_LIST+=("$path")
continue
fi
#
# Parent directory still exists in the snapshot.
#
if [[ -n "${SNAPSHOT_MAP[$PARENT]:-}" ]]; then
MATCH_LIST+=("$path")
else
MISSING_LIST+=("$path")
fi
fi
done
###############################################################################
# Build retained object lookup table
###############################################################################
RETAINED_MAP=()
for path in "${MISSING_LIST[@]}"; do
RETAINED_MAP["$path"]=1
done
echo
echo "Comparison Results"
echo "------------------"
echo "Bootstrap Objects : ${#BOOTSTRAP_MAP[@]}"
echo "Snapshot Objects : ${#SNAPSHOT_MAP[@]}"
echo "Matched Objects : ${#MATCH_LIST[@]}"
echo "Retained Objects : ${#MISSING_LIST[@]}"
echo
#
# Display the first few retained objects.
#
if (( ${#MISSING_LIST[@]} > 0 )); then
echo "Retained Objects"
echo "----------------"
LIMIT=20
(( ${#MISSING_LIST[@]} < LIMIT )) && LIMIT=${#MISSING_LIST[@]}
for ((i=0; i<LIMIT; i++)); do
path="${MISSING_LIST[$i]}"
if [[ -L "${BOOTSTRAP_ROOT}/${path}" ]]; then
TYPE="LINK"
elif [[ -d "${BOOTSTRAP_ROOT}/${path}" ]]; then
TYPE="DIR"
elif [[ -f "${BOOTSTRAP_ROOT}/${path}" ]]; then
TYPE="FILE"
else
TYPE="OTHER"
fi
printf " %-5s %s\n" "$TYPE" "$path"
done
else
pass "No retained objects were discovered."
fi
echo
pass "Module 7 complete."
###############################################################################
# Module 8 - Policy Classification
###############################################################################
echo
echo "============================================================"
echo "Module 8 - Policy Classification"
echo "============================================================"
KEEP_BOOT=()
KEEP_SNAPSHOTS=()
KEEP_SUBVOLUME=()
DELETE_CANDIDATE=()
RETAINED_OBJECTS=()
#
# Classify every object in the retired bootstrap root.
#
TOTAL_OBJECTS=${#BOOTSTRAP_MAP[@]}
CLASSIFIED=0
for path in "${!BOOTSTRAP_MAP[@]}"; do
((++CLASSIFIED))
if (( CLASSIFIED % 1000 == 0 || CLASSIFIED == TOTAL_OBJECTS )); then
printf "\rClassifying Objects: %7d / %7d" \
"$CLASSIFIED" \
"$TOTAL_OBJECTS"
fi
#
# Rule 1: Never touch /boot.