Skip to content

Commit 73c679e

Browse files
committed
fix cgroupsv1 support in warden
The new error checking was finding errors. Going back to the old code which did not check for errors but did work for teams. Jammy-on-Jammy -on-Garden is a pretty niche use case these days, so this seems ok.
1 parent feb6c14 commit 73c679e

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@
1212

1313
monit_isolation_classid=2958295041
1414

15+
# True when /sys/fs/cgroup is the root of a cgroup2 mount (unified hierarchy).
16+
# Do not use /proc/self/cgroup's "0::" entry alone: under systemd hybrid mode a
17+
# 0:: line can refer to the small cgroup2 tracking hierarchy while resource
18+
# controllers (including net_cls) remain on cgroup v1.
19+
#
20+
# Prefer cgroup.controllers; also accept stat(2) filesystem type for hosts where
21+
# the file is missing from the mount view but the root is still cgroup2fs.
22+
monit_using_unified_cgroup_v2() {
23+
[ -f /sys/fs/cgroup/cgroup.controllers ] && return 0
24+
[ "$(stat -fc %T /sys/fs/cgroup 2>/dev/null)" = "cgroup2fs" ]
25+
}
26+
1527
permit_monit_access() {
16-
if grep -q '^0::' /proc/self/cgroup 2>/dev/null; then
28+
if monit_using_unified_cgroup_v2; then
1729
# cgroupv2 (unified hierarchy)
1830
# Create a sub-cgroup under the current process's cgroup and move into it.
1931
# The iptables rules match on this cgroup path.
@@ -28,15 +40,10 @@ permit_monit_access() {
2840
mkdir -p "${monit_access_cgroup}"
2941
echo $$ > "${monit_access_cgroup}/cgroup.procs"
3042
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
43+
# this seems to work in docker but net_cls_location is empty in garden
44+
net_cls_location="$(cat /proc/self/mounts | grep ^cgroup | grep net_cls | awk '{ print $2 }' )"
45+
net_cls_subproc="$(grep net_cls /proc/self/cgroup | awk -F ":" '{ print $3 }' )"
3846
monit_access_cgroup="${net_cls_location}/${net_cls_subproc}/monit-api-access"
39-
4047
mkdir -p "${monit_access_cgroup}"
4148
echo "${monit_isolation_classid}" > "${monit_access_cgroup}/net_cls.classid"
4249
echo $$ > "${monit_access_cgroup}/tasks"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
if grep -q '^0::' /proc/self/cgroup 2>/dev/null; then
5+
if monit_using_unified_cgroup_v2; then
66
# cgroupv2: dynamically determine the cgroup path for this process.
77
# The agent calls permit_monit_access() to join the monit-api-access sub-cgroup.
88
current_cgroup="$(grep '^0::' /proc/self/cgroup | cut -d: -f3)"

0 commit comments

Comments
 (0)