Skip to content

Commit 603c945

Browse files
committed
cgroups: refactor cgroups
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 c2f7cd7 commit 603c945

File tree

3 files changed

+176
-175
lines changed

3 files changed

+176
-175
lines changed

init.d/cgroups.in

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,42 @@ cgroup1_controllers()
6868
cgroup2_base()
6969
{
7070
grep -qw cgroup2 /proc/filesystems || return 0
71-
local base
72-
base="$(cgroup2_find_path)"
73-
mkdir -p "${base}"
74-
mount -t cgroup2 cgroup2 -o "${cgroup_opts},nsdelegate" "${base}" 2> /dev/null ||
75-
mount -t cgroup2 cgroup2 -o "${cgroup_opts}" "${base}"
71+
mkdir -p "${cgroup_root}"
72+
mount -t cgroup2 cgroup2 -o "${cgroup_opts},nsdelegate" "${cgroup_root}" 2> /dev/null ||
73+
mount -t cgroup2 cgroup2 -o "${cgroup_opts}" "${cgroup_root}"
74+
mkdir -p "${cgroup_root}/rc"
7675
return 0
7776
}
7877

79-
cgroup2_controllers()
78+
setup_controller()
8079
{
81-
grep -qw cgroup2 /proc/filesystems || return 0
82-
local active cgroup_path x y
83-
cgroup_path="$(cgroup2_find_path)"
84-
[ -z "${cgroup_path}" ] && return 0
85-
[ ! -e "${cgroup_path}/cgroup.controllers" ] && return 0
86-
[ ! -e "${cgroup_path}/cgroup.subtree_control" ]&& return 0
87-
read -r active < "${cgroup_path}/cgroup.controllers"
88-
for x in ${active}; do
89-
case "${rc_cgroup_mode:-unified}" in
90-
unified)
91-
echo "+${x}" > "${cgroup_path}/cgroup.subtree_control"
92-
;;
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" ;;
9389
hybrid)
94-
for y in ${rc_cgroup_controllers}; do
95-
if [ "$x" = "$y" ]; then
96-
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"
9793
fi
9894
done
9995
;;
10096
esac
10197
done
102-
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
103107
}
104108

105109
cgroups_hybrid()

sh/openrc-run.sh.in

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,12 @@ status|describe) ;;
280280
[ -n "${rc_ulimit:-$RC_ULIMIT}" ] && apply_ulimits ${rc_ulimit:-$RC_ULIMIT}
281281

282282
# Apply cgroups settings if defined
283-
if [ "$(command -v cgroup_add_service)" = "cgroup_add_service" ]; then
284-
if grep -qs /sys/fs/cgroup /proc/1/mountinfo
285-
then
286-
if [ -d /sys/fs/cgroup -a ! -w /sys/fs/cgroup ]; then
287-
eerror "No permission to apply cgroup settings"
288-
break
289-
fi
290-
fi
291-
cgroup_add_service
292-
fi
283+
[ -n "$cgroup_path" ] || break
284+
285+
cgroup_add_service
286+
cgroup_set_limits
293287

294-
[ "$(command -v cgroup_set_limits)" = "cgroup_set_limits" ] && cgroup_set_limits
295-
[ "$(command -v cgroup2_set_limits)" = "cgroup2_set_limits" ] && cgroup2_set_limits
296-
;;
288+
break
297289
esac
298290

299291
eval "printf '%s\n' $required_dirs" | while read _d; do

0 commit comments

Comments
 (0)