Debian Codegraph @

bin/snapshot-cgroup

raw
1 #!/usr/bin/env bash
2 #
3 # snapshot-cgroup - Copy cgroup v2 files from the live system into a
4 # directory tree suitable for use with TestIntegrationCgroupLimits.
5 #
6 # Usage: snapshot-cgroup <output-dir>
7 #
8 # The script reads /proc/self/cgroup to find the v2 group path and copies
9 # exactly the files that the cgroup package needs:
10 #
11 # proc/self/cgroup
12 # sys/fs/cgroup/<group>/cpu.max
13 # sys/fs/cgroup/<group>/memory.max
14
15 set -euo pipefail
16
17 if [[ $# -ne 1 ]]; then
18 echo "Usage: $0 <output-dir>" >&2
19 exit 1
20 fi
21
22 out="$1"
23 mkdir -p "$out"
24
25 copy_file() {
26 local src="$1" dst="$out/$2"
27 mkdir -p "$(dirname "$dst")"
28 cp "$src" "$dst"
29 }
30
31 copy_file /proc/self/cgroup proc/self/cgroup
32
33 group=$(awk -F: '/^0::/ { print $3 }' /proc/self/cgroup)
34 cgdir="/sys/fs/cgroup${group}"
35
36 for f in cpu.max memory.max; do
37 if [[ -f "$cgdir/$f" ]]; then
38 copy_file "$cgdir/$f" "sys/fs/cgroup${group}/$f"
39 fi
40 done
41
42 echo "Snapshot written to $out"