-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquiz-cleanup
executable file
·138 lines (120 loc) · 3.21 KB
/
quiz-cleanup
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
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2023-2025, Rob Norris <[email protected]>
set -uo pipefail
usage() {
cat <<EOF
cleanup quiz build, work and other outdated artifacts
usage: quiz-cleanup [opts]
options:
-k remove build and installed files for kernel
-K remove build and installed files for all but newest
kernel within series
-D actually delete things; without just lists things
that would be deleted
-h this help
EOF
exit 1
}
trace() {
local STAMP=$(date +%Y%m%d-%H:%M:%S)
echo "[quiz-cleanup] $STAMP $@" >&2
}
fail() {
local TEXT=${@:-}
if [[ -z $TEXT ]] ; then
TEXT="[${BASH_SOURCE[0]}:${BASH_LINENO[0]}: $BASH_COMMAND]"
fi
trace "FATAL $TEXT"
exit 1
}
trap fail ERR
RUNDIR=$(realpath $(dirname $0))
source $RUNDIR/quiz-config
source $RUNDIR/quiz-lib
opt_kernel=""
opt_old_kernels=""
opt_delete=""
OPTIND=1
while getopts "k:KDh" opt
do
case "$opt" in
'k') opt_kernel=$OPTARG ;;
'K') opt_old_kernels=1 ;;
'D') opt_delete=1 ;;
'h') usage ;;
*) exit 1 ;;
esac
done
shift $(expr $OPTIND - 1)
cd $RUNDIR
if [[ -n $opt_kernel && -n $opt_old_kernels ]] ; then
fail "invalid kernel version options; can't use -k and -K together"
fi
function find_all_kernels () {
(
ls system/bzImage-* | grep -E -- '-[0-9]+\.[0-9]+\.[0-9]+$' | cut -f2 -d-
ls system/work/lib/modules | grep -E -- '^[0-9]+\.[0-9]+\.[0-9]+$'
ls -d system/work/usr/local/src/*/* | grep -oE -- '/[0-9]+\.[0-9]+\.[0-9]+$' | cut -f2 -d/
ls -d build/kernel/linux-* | grep -oE -- '[0-9]+\.[0-9]+\.[0-9]+'
) | sort -V | uniq | tac
}
all_kernels=$(find_all_kernels)
cleanup_kernels=""
keep_kernels=""
if [[ -n $opt_kernel ]] ; then
for k in $all_kernels ; do
if [[ $k == $opt_kernel ]] ; then
cleanup_kernels=$opt_kernel
fi
done
fi
if [[ -z $cleanup_kernels ]] ; then
min_kernels=""
if [[ -n $opt_kernel ]] ; then
min_kernels=$opt_kernel
else
min_kernels=$(echo $all_kernels | xargs -n1 echo | cut -f1-2 -d. | sort -V | uniq )
fi
for kmin in $min_kernels ; do
first=0
for k in $all_kernels ; do
if [[ $(echo $k | cut -f1-2 -d.) == $kmin ]] ; then
if [[ $first -eq 0 ]] ; then
keep_kernels="$keep_kernels $k"
first=1
else
cleanup_kernels="$cleanup_kernels $k"
fi
fi
done
done
fi
if [[ -z $cleanup_kernels ]] ; then
fail "no matching kernels found, nothing to do!"
fi
trace "will cleanup kernels:$cleanup_kernels"
if [[ -n $keep_kernels ]] ; then
trace "keeping kernels:$keep_kernels"
fi
for k in $cleanup_kernels ; do
(
ls system/bzImage-$k || true
ls -d system/work/usr/lib/modules/$k || true
ls -d system/work/usr/local/src/*/$k || true
ls -d build/kernel/linux-$k* || true
) 2>/dev/null | sort | while read d ; do
if [[ -n $opt_delete ]] ; then
trace "deleting: $d"
rm -rf $d
else
trace "would delete: $d"
fi
done
done
# vim: ft=bash