From 457f928e793cb1f6ef254935ad07f58b8762c72f Mon Sep 17 00:00:00 2001 From: William Hubbs Date: Thu, 14 Sep 2017 10:38:10 -0500 Subject: add support for control groups version 2 This is for #94. --- sh/openrc-run.sh.in | 15 ++++++++++----- sh/rc-cgroup.sh.in | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) (limited to 'sh') diff --git a/sh/openrc-run.sh.in b/sh/openrc-run.sh.in index a38d46d6..e778bd09 100644 --- a/sh/openrc-run.sh.in +++ b/sh/openrc-run.sh.in @@ -258,8 +258,7 @@ for _cmd; do [ -n "${rc_ulimit:-$RC_ULIMIT}" ] && \ ulimit ${rc_ulimit:-$RC_ULIMIT} # Apply cgroups settings if defined - if [ "$(command -v cgroup_add_service)" = \ - "cgroup_add_service" ] + if [ "$(command -v cgroup_add_service)" = "cgroup_add_service" ] then if [ -d /sys/fs/cgroup -a ! -w /sys/fs/cgroup ]; then eerror "No permission to apply cgroup settings" @@ -268,9 +267,11 @@ for _cmd; do cgroup_add_service /sys/fs/cgroup/openrc cgroup_add_service /sys/fs/cgroup/systemd/system fi - [ "$(command -v cgroup_set_limits)" = \ - "cgroup_set_limits" ] && \ - cgroup_set_limits + [ "$(command -v cgroup_set_limits)" = "cgroup_set_limits" ] && + cgroup_set_limits + [ "$(command -v cgroup2_set_limits)" = "cgroup2_set_limits" ] && + [ "$_cmd" = start ] && + cgroup2_set_limits break fi done @@ -368,6 +369,10 @@ while [ -n "$1" ]; do "$1" = "stop" ] && \ yesno "${rc_cgroup_cleanup}" && \ cgroup_cleanup + if [ "$(command -v cgroup2_remove)" = "cgroup2_remove" ]; then + [ "$1" = stop ] || [ -z "${command}" ] && + cgroup2_remove + fi shift continue 2 else diff --git a/sh/rc-cgroup.sh.in b/sh/rc-cgroup.sh.in index 5987f966..40501f22 100644 --- a/sh/rc-cgroup.sh.in +++ b/sh/rc-cgroup.sh.in @@ -152,3 +152,54 @@ cgroup_cleanup() kill -9 $pids eend $(cgroup_running && echo 1 || echo 0) "fail to stop all processes" } + +cgroup2_find_path() +{ + case "${rc_cgroup_mode:-hybrid}" in + hybrid) printf "/sys/fs/cgroup/unified" ;; + unified) printf "/sys/fs/cgroup" ;; + esac + return 0 +} + +cgroup2_remove() +{ + local cgroup_path="$(cgroup2_find_path)" rc_cgroup_path + [ -z "${cgroup_path}" ] && return 0 + rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}" + [ ! -d "${rc_cgroup_path}" ] || + [ ! -e "${rc_cgroup_path}"/cgroup.events ] && + return 0 + grep -qx "$$" "${rc_cgroup_path}/cgroup.procs" && + echo 0 > "${cgroup_path}/cgroup.procs" + local key populated vvalue + while read key value; do + case "${key}" in + populated) populated=${value} ;; + *) ;; + esac + done < "${rc_cgroup_path}/cgroup.events" + [ "${populated}" = 1 ] && return 0 + rmdir "${rc_cgroup_path}" + return 0 +} + +cgroup2_set_limits() +{ + local cgroup_path="$(cgroup2_find_path)" + [ -z "${cgroup_path}" ] && return 0 + rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}" + local OIFS="$IFS" + IFS=" +" + [ ! -d "${rc_cgroup_path}" ] && mkdir "${rc_cgroup_path}" + echo 0 > "${rc_cgroup_path}/cgroup.procs" + echo "${rc_cgroup_settings}" | while IFS="$OIFS" read key value; do + [ -z "${key}" ] || [ -z "${value}" ] && continue + [ ! -e "${rc_cgroup_path}/${key}" ] && continue + veinfo "${RC_SVCNAME}: cgroups: ${key} ${value}" + echo "${value}" > "${rc_cgroup_path}/${key}" + done + IFS="$OIFS" + return 0 +} -- cgit v1.2.3