-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·116 lines (97 loc) · 3.51 KB
/
test.sh
File metadata and controls
executable file
·116 lines (97 loc) · 3.51 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
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
#######################################
# utilities
#######################################
die() { echo "❌ $*" >&2; exit 1; }
pass() { echo "✅ $*"; }
need() { command -v "$1" &>/dev/null || die "missing tool: $1"; }
need make; need modprobe; need dmesg; need docker
MOD_NAME=energy_proc
#######################################
# cleanup helper (runs on EXIT)
#######################################
mounted_debugfs=0
cleanup() {
modprobe -r "$MOD_NAME" 2>/dev/null || true
(( mounted_debugfs )) && umount /sys/kernel/debug
}
trap cleanup EXIT
#######################################
# ensure module is NOT loaded
#######################################
modprobe -r "$MOD_NAME" 2>/dev/null || true
#######################################
# 0. build the module
#######################################
SRC_DIR=${1:-src}
[[ -d "$SRC_DIR" ]] || die "source directory “$SRC_DIR” not found"
echo "• Building module in $SRC_DIR …"
cd "$SRC_DIR" || die "failed to change to source directory $SRC_DIR"
make clean >/dev/null 2>&1 || true
make >/dev/null
cd ..
MODULE=$(find "$SRC_DIR" -maxdepth 1 -name 'energy_proc*.ko' -print -quit)
[[ -f "$MODULE" ]] || die "build failed - .ko not found in $SRC_DIR"
pass "module built → $(basename "$MODULE")"
#######################################
# 1. make sure debugfs is mounted
#######################################
if ! mountpoint -q /sys/kernel/debug; then
mount -t debugfs none /sys/kernel/debug
mounted_debugfs=1
fi
#######################################
# 2. load the module
#######################################
echo "• Loading $MODULE …"
dmesg -C
cd "$SRC_DIR" || die "failed to change to source directory $SRC_DIR"
make install >/dev/null || die "module install failed"
cd ..
sleep 1 # allow first sampling tick
dmesg | grep -q "$MOD_NAME" \
&& pass "module inserted and logged to dmesg" \
|| die "module did not announce itself in dmesg"
#######################################
# 3. debugfs sanity
#######################################
DBG=/sys/kernel/debug/energy/all
[[ -r $DBG ]] || die "$DBG missing or unreadable"
dbg_out=$(head -n 5 "$DBG") || die "reading $DBG failed"
grep -q '^timestamp=' <<<"$dbg_out" \
&& pass "$DBG returns metrics" \
|| die "$DBG lacks expected keys"
#######################################
# 4. proc files on the host
#######################################
for f in /proc/energy/all /proc/energy/cgroup; do
[[ -r $f ]] || die "$f is missing"
done
pass "/proc/energy/{all,cgroup} present on host"
#######################################
# 5. container checks
#######################################
img=ubuntu:24.04
docker pull -q "$img" >/dev/null
# (a) /proc/energy/all must fail
set +e
docker run --rm --security-opt=no-new-privileges --pull=never "$img" \
bash -c 'cat /proc/energy/all 2>/dev/null'
all_rc=$?
set -e
[[ $all_rc -ne 0 ]] \
&& pass "cat /proc/energy/all fails inside container (expected)" \
|| die "/proc/energy/all readable inside container – should be blocked"
# (b) /proc/energy/cgroup must succeed
cg_out=$(docker run --rm --security-opt=no-new-privileges --pull=never "$img" \
bash -c 'cat /proc/energy/cgroup | head -n 20')
grep -q '^pid=' <<<"$cg_out" \
&& pass "/proc/energy/cgroup works inside container" \
|| die "/proc/energy/cgroup did not return expected data"
#######################################
# all good!
#######################################
pass "all checks passed - module looks good 🎉"