Skip to content

Commit f213f8d

Browse files
committed
cgroups: refactor cgroups
add a deprecation warning for cgroups v1, and rework rc-cgroups.sh into cgroups.sh with an api focused on cgroups v2 move into a new layout with a supervision root cgroup as /rc, services will then go in /rc/$RC_SVCNAME, and compatibility with the old /openrc.$RC_SVCNAME is kept though restarting a service will use the new layout this also fixes a bug where cgroup_cleanup would not ever trigger due to a bug in cgroup_running
1 parent e095ddb commit f213f8d

4 files changed

Lines changed: 213 additions & 215 deletions

File tree

init.d/cgroups.in

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ depend()
2121

2222
cgroup1_base()
2323
{
24+
eerror "cgroups v1 is deprecated. Please unset rc_cgroup_mode."
2425
grep -qw cgroup /proc/filesystems || return 0
2526
if ! mountinfo -q /sys/fs/cgroup; then
2627
ebegin "Mounting cgroup filesystem"
@@ -44,63 +45,65 @@ cgroup1_controllers()
4445
{
4546
yesno "${rc_controller_cgroups:-YES}" && [ -e /proc/cgroups ] &&
4647
grep -qw cgroup /proc/filesystems || return 0
48+
4749
while read -r name _ _ enabled _; do
48-
case "${enabled}" in
49-
1) mountinfo -q "/sys/fs/cgroup/${name}" && continue
50-
local x
51-
for x in $rc_cgroup_controllers; do
52-
[ "${name}" = "blkio" ] && [ "${x}" = "io" ] &&
53-
continue 2
54-
[ "${name}" = "${x}" ] &&
55-
continue 2
56-
done
57-
mkdir "/sys/fs/cgroup/${name}"
58-
mount -n -t cgroup -o "${cgroup_opts},${name}" \
59-
"${name}" "/sys/fs/cgroup/${name}"
60-
yesno "${rc_cgroup_memory_use_hierarchy:-no}" &&
61-
[ "${name}" = memory ] &&
62-
echo 1 > /sys/fs/cgroup/memory/memory.use_hierarchy
63-
;;
64-
esac
50+
[ "$enabled" = 1 ] || continue;
51+
mountinfo -q "/sys/fs/cgroup/${name}" && continue
52+
53+
local x
54+
for x in $rc_cgroup_controllers; do
55+
[ "${name}" = "blkio" ] && [ "${x}" = "io" ] && continue 2
56+
[ "${name}" = "${x}" ] && continue 2
57+
done
58+
59+
mkdir "/sys/fs/cgroup/${name}"
60+
mount -n -t cgroup -o "${cgroup_opts},${name}" "${name}" "/sys/fs/cgroup/${name}"
61+
if [ yesno "${rc_cgroup_memory_use_hierarchy:-no}" && [ "${name}" = memory ]; then
62+
echo 1 > /sys/fs/cgroup/memory/memory.use_hierarchy
63+
fi
6564
done < /proc/cgroups
6665
return 0
6766
}
6867

6968
cgroup2_base()
7069
{
7170
grep -qw cgroup2 /proc/filesystems || return 0
72-
local base
73-
base="$(cgroup2_find_path)"
74-
mkdir -p "${base}"
75-
mount -t cgroup2 none -o "${cgroup_opts},nsdelegate" "${base}" 2> /dev/null ||
76-
mount -t cgroup2 none -o "${cgroup_opts}" "${base}"
77-
return 0
71+
72+
mkdir -p "${cgroup_root}"
73+
mount -t cgroup2 none -o "${cgroup_opts},nsdelegate" "${cgroup_root}" 2> /dev/null ||
74+
mount -t cgroup2 none -o "${cgroup_opts}" "${cgroup_root}"
75+
mkdir -p "${cgroup_root}/rc"
7876
}
7977

80-
cgroup2_controllers()
78+
setup_controller()
8179
{
82-
grep -qw cgroup2 /proc/filesystems || return 0
83-
local active cgroup_path x y
84-
cgroup_path="$(cgroup2_find_path)"
85-
[ -z "${cgroup_path}" ] && return 0
86-
[ ! -e "${cgroup_path}/cgroup.controllers" ] && return 0
87-
[ ! -e "${cgroup_path}/cgroup.subtree_control" ]&& return 0
88-
read -r active < "${cgroup_path}/cgroup.controllers"
89-
for x in ${active}; do
90-
case "${rc_cgroup_mode:-unified}" in
91-
unified)
92-
echo "+${x}" > "${cgroup_path}/cgroup.subtree_control"
93-
;;
80+
local root="$1" controller allowed
81+
shift
82+
83+
[ ! -e "${root}/cgroup.controllers" ] && return 0
84+
[ ! -e "${root}/cgroup.subtree_control" ] && return 0
85+
86+
for controller; do
87+
case "${rc_cgroup_mode:-unified}" in
88+
unified) echo "+${controller}" > "${root}/cgroup.subtree_control" ;;
9489
hybrid)
95-
for y in ${rc_cgroup_controllers}; do
96-
if [ "$x" = "$y" ]; then
97-
echo "+${x}" > "${cgroup_path}/cgroup.subtree_control"
90+
for allowed in ${rc_cgroup_controllers}; do
91+
if [ "$controller" = "$allowed" ]; then
92+
echo "+${controller}" > "${root}/cgroup.subtree_control"
9893
fi
9994
done
10095
;;
10196
esac
10297
done
103-
return 0
98+
}
99+
100+
cgroup2_controllers()
101+
{
102+
grep -qw cgroup2 /proc/filesystems || return 0
103+
local active
104+
read -r active < "${cgroup_root}/cgroup.controllers"
105+
setup_controller "${cgroup_root}" $active
106+
setup_controller "${cgroup_root}/rc" $active
104107
}
105108

106109
cgroups_hybrid()
@@ -109,21 +112,18 @@ cgroups_hybrid()
109112
cgroup2_base
110113
cgroup2_controllers
111114
cgroup1_controllers
112-
return 0
113115
}
114116

115117
cgroups_legacy()
116118
{
117119
cgroup1_base
118120
cgroup1_controllers
119-
return 0
120121
}
121122

122123
cgroups_unified()
123124
{
124125
cgroup2_base
125126
cgroup2_controllers
126-
return 0
127127
}
128128

129129
mount_cgroups()
@@ -133,7 +133,6 @@ mount_cgroups()
133133
legacy) cgroups_legacy ;;
134134
unified) cgroups_unified ;;
135135
esac
136-
return 0
137136
}
138137

139138
restorecon_cgroups()
@@ -143,7 +142,6 @@ restorecon_cgroups()
143142
restorecon -rF /sys/fs/cgroup >/dev/null 2>&1
144143
eend $?
145144
fi
146-
return 0
147145
}
148146

149147
start()
@@ -153,5 +151,6 @@ start()
153151
mount_cgroups
154152
restorecon_cgroups
155153
fi
154+
156155
return 0
157156
}

0 commit comments

Comments
 (0)