Skip to content

Commit 4dfe37a

Browse files
committed
Add cgroups v2 support for Jammy stemcells
This enables warden stemcells to function on hosts that are using cgroups v2, which is increasingly common. This should not have any effects on other infrastructures as the stemcell kernel will continue to be booted with cgroups v1.
1 parent 8fd3fff commit 4dfe37a

3 files changed

Lines changed: 50 additions & 12 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ acceptance-tests/os-conf-release
1313
**/*.log
1414

1515
ci/docker/VMware-ovftool-*.bundle
16+
tmp/

stemcell_builder/stages/bosh_monit/assets/monit-access-helper.sh

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,32 @@
1313
monit_isolation_classid=2958295041
1414

1515
permit_monit_access() {
16-
net_cls_location="$(cat /proc/self/mounts | grep ^cgroup | grep net_cls | awk '{ print $2 }' )"
17-
net_cls_subproc="$(grep net_cls /proc/self/cgroup | awk -F ":" '{ print $3 }' )"
18-
monit_access_cgroup="${net_cls_location}/${net_cls_subproc}/monit-api-access"
16+
if grep -q '^0::' /proc/self/cgroup 2>/dev/null; then
17+
# cgroupv2 (unified hierarchy)
18+
# Create a sub-cgroup under the current process's cgroup and move into it.
19+
# The iptables rules match on this cgroup path.
20+
cgroup_mount="$(awk '$3 == "cgroup2" { print $2 }' /proc/self/mounts)"
21+
current_cgroup="$(grep '^0::' /proc/self/cgroup | cut -d: -f3)"
22+
if [ -z "${cgroup_mount}" ] || [ -z "${current_cgroup}" ]; then
23+
echo "permit_monit_access: unable to resolve cgroup v2 mount or path" >&2
24+
return 1
25+
fi
26+
monit_access_cgroup="${cgroup_mount}${current_cgroup}/monit-api-access"
1927

20-
mkdir -p "${monit_access_cgroup}"
21-
echo "${monit_isolation_classid}" > "${monit_access_cgroup}/net_cls.classid"
28+
mkdir -p "${monit_access_cgroup}"
29+
echo $$ > "${monit_access_cgroup}/cgroup.procs"
30+
else
31+
# cgroupv1 - use net_cls classid
32+
net_cls_location="$(cat /proc/self/mounts | grep ^cgroup | grep net_cls | awk '{ print $2 }')"
33+
net_cls_subproc="$(grep net_cls /proc/self/cgroup | awk -F ":" '{ print $3 }')"
34+
if [ -z "${net_cls_location}" ] || [ -z "${net_cls_subproc}" ]; then
35+
echo "permit_monit_access: unable to resolve cgroup v1 net_cls location or path" >&2
36+
return 1
37+
fi
38+
monit_access_cgroup="${net_cls_location}/${net_cls_subproc}/monit-api-access"
2239

23-
echo $$ > "${monit_access_cgroup}/tasks"
40+
mkdir -p "${monit_access_cgroup}"
41+
echo "${monit_isolation_classid}" > "${monit_access_cgroup}/net_cls.classid"
42+
echo $$ > "${monit_access_cgroup}/tasks"
43+
fi
2444
}

stemcell_builder/stages/bosh_monit/assets/restrict-monit-api-access

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@
22

33
source /var/vcap/bosh/etc/monit-access-helper.sh
44

5-
if iptables -t mangle -C POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
6-
-m cgroup \! --cgroup "${monit_isolation_classid}" -j DROP
7-
then
8-
/bin/true
5+
if grep -q '^0::' /proc/self/cgroup 2>/dev/null; then
6+
# cgroupv2: dynamically determine the cgroup path for this process.
7+
# The agent calls permit_monit_access() to join the monit-api-access sub-cgroup.
8+
current_cgroup="$(grep '^0::' /proc/self/cgroup | cut -d: -f3)"
9+
if [ -z "${current_cgroup}" ]; then
10+
echo "restrict-monit-api-access: unable to resolve cgroup v2 path" >&2
11+
exit 1
12+
fi
13+
cgroup_match="--path ${current_cgroup}/monit-api-access"
914
else
15+
# cgroupv1: use the classid from monit-access-helper.sh
16+
cgroup_match="--cgroup ${monit_isolation_classid}"
17+
fi
18+
19+
# Idempotently ensure both iptables rules exist.
20+
# The ACCEPT rule must be checked/inserted first so it appears above the DROP rule in the chain.
21+
if ! iptables -t mangle -C POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
22+
-m state --state ESTABLISHED,RELATED -j ACCEPT 2>/dev/null; then
1023
iptables -t mangle -I POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
11-
-m cgroup \! --cgroup "${monit_isolation_classid}" -j DROP
24+
-m state --state ESTABLISHED,RELATED -j ACCEPT
25+
fi
26+
27+
if ! iptables -t mangle -C POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
28+
-m cgroup ! ${cgroup_match} -j DROP 2>/dev/null; then
1229
iptables -t mangle -I POSTROUTING -d 127.0.0.1 -p tcp --dport 2822 \
13-
-m state --state ESTABLISHED,RELATED -j ACCEPT
30+
-m cgroup ! ${cgroup_match} -j DROP
1431
fi

0 commit comments

Comments
 (0)