Skip to content

Commit f01649b

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 cc95210 commit f01649b

3 files changed

Lines changed: 192 additions & 193 deletions

File tree

init.d/cgroups.in

Lines changed: 27 additions & 23 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 none -o "${cgroup_opts},nsdelegate" "${base}" 2> /dev/null ||
75-
mount -t cgroup2 none -o "${cgroup_opts}" "${base}"
76-
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"
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: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -278,29 +278,18 @@ if yesno "${rc_verbose:-$RC_VERBOSE}"; then
278278
fi
279279

280280
for _cmd; do
281-
if [ "$_cmd" != status -a "$_cmd" != describe ]; then
282-
# Apply any ulimit(s) defined
283-
[ -n "${rc_ulimit:-$RC_ULIMIT}" ] && \
284-
apply_ulimits ${rc_ulimit:-$RC_ULIMIT}
285-
# Apply cgroups settings if defined
286-
if [ "$(command -v cgroup_add_service)" = "cgroup_add_service" ]
287-
then
288-
if grep -qs /sys/fs/cgroup /proc/1/mountinfo
289-
then
290-
if [ -d /sys/fs/cgroup -a ! -w /sys/fs/cgroup ]; then
291-
eerror "No permission to apply cgroup settings"
292-
break
293-
fi
294-
fi
295-
cgroup_add_service
296-
fi
297-
[ "$(command -v cgroup_set_limits)" = "cgroup_set_limits" ] &&
298-
cgroup_set_limits
299-
[ "$(command -v cgroup2_set_limits)" = "cgroup2_set_limits" ] &&
300-
[ "$_cmd" = start ] &&
301-
cgroup2_set_limits
302-
break
303-
fi
281+
[ "$_cmd" = status ] || [ "$_cmd" = describe ] && continu
282+
283+
# Apply any ulimit(s) defined
284+
[ -n "${rc_ulimit:-$RC_ULIMIT}" ] && apply_ulimits ${rc_ulimit:-$RC_ULIMIT}
285+
286+
# Apply cgroups settings if defined
287+
[ -n "$cgroup_path" ] || continue
288+
289+
cgroup_add_service
290+
cgroup_set_limits
291+
292+
break
304293
done
305294

306295
eval "printf '%s\n' $required_dirs" | while read _d; do
@@ -392,14 +381,12 @@ while [ -n "$1" ]; do
392381
then
393382
"$1"_post || exit $?
394383
fi
395-
[ "$(command -v cgroup_cleanup)" = "cgroup_cleanup" ] &&
396-
[ "$1" = "stop" ] &&
397-
yesno "${rc_cgroup_cleanup}" && \
398-
cgroup_cleanup
399-
if [ "$(command -v cgroup2_remove)" = "cgroup2_remove" ]; then
400-
[ "$1" = stop ] || [ -z "${command}" ] &&
401-
cgroup2_remove
384+
385+
if [ "$1" = "stop" ] && [ -d "$cgroup_path" ]; then
386+
yesno "${rc_cgroup_cleanup}" && cgroup_cleanup
387+
[ -z "${command}" ] && cgroup2_remove
402388
fi
389+
403390
shift
404391
continue 2
405392
else

0 commit comments

Comments
 (0)