From 9b8a31274ee5331f98d90df959fee283b2fc465e Mon Sep 17 00:00:00 2001 From: Kamalesh Babulal Date: Tue, 10 Jan 2023 14:13:35 +0000 Subject: [PATCH] tools/cgcreate: add support for default systemd delegation slice/scope Enhance the cgcreate tool to support default systemd delegation slice/scope, if set in the cgconfig.conf's systemd setting. Parse the configuration file and read the systemd::delegate setting, if any, and set it as the default systemd slice/scope in systemd_default_cgroup. Setting it appends the slice/scope name to the constructed default cgroup mount path. When the user passes the relative cgroup name, its appends to the sub-tree (delegated slice/scope) by default and if the user wishes to create a cgroup in another subtree or cgroup root hierarchy, they need to use the absolute path. For example: cgroup_root / \ / \ systemd.slice* cgrp1 | systemd.scope | cgrp-d1 * default system delegation slice/scope - read from cgconfig.conf $ sudo cgcreate -g cpu:cgrp-d1 will create the cgroup under the cpu controller hirearchy cgroup_root:systemd_default_cgroup:cgrp-d1 and for creating a cgroup cgrp1, the user can use: $ sudo cgcreate -g cpu:/cgrp1 or use the newly introduced -b switch to ignore the systemd slice/scope: $ sudo cgcreate -b -g cpu:cgrp1 Signed-off-by: Kamalesh Babulal Signed-off-by: Tom Hromatka --- src/tools/cgcreate.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tools/cgcreate.c b/src/tools/cgcreate.c index 2ef8d997..5a3e0f48 100644 --- a/src/tools/cgcreate.c +++ b/src/tools/cgcreate.c @@ -41,6 +41,10 @@ static void usage(int status, const char *program_name) info(" -h, --help Display this help\n"); info(" -s, --tperm=mode Tasks file permissions\n"); info(" -t : Owner of the tasks file\n"); +#ifdef WITH_SYSTEMD + info(" -b Ignore default systemd "); + info("delegate hierarchy\n"); +#endif } int main(int argc, char *argv[]) @@ -59,6 +63,8 @@ int main(int argc, char *argv[]) uid_t tuid = CGRULE_INVALID, auid = CGRULE_INVALID; gid_t tgid = CGRULE_INVALID, agid = CGRULE_INVALID; + int ignore_default_systemd_delegate_slice = 0; + struct cgroup_group_spec **cgroup_list; struct cgroup_controller *cgc; struct cgroup *cgroup; @@ -90,9 +96,17 @@ int main(int argc, char *argv[]) goto err; } +#ifdef WITH_SYSTEMD /* parse arguments */ + while ((c = getopt_long(argc, argv, "a:t:g:hd:f:s:b", long_opts, NULL)) > 0) { + switch (c) { + case 'b': + ignore_default_systemd_delegate_slice = 1; + break; +#else while ((c = getopt_long(argc, argv, "a:t:g:hd:f:s:", long_opts, NULL)) > 0) { switch (c) { +#endif case 'h': usage(0, argv[0]); ret = 0; @@ -155,6 +169,10 @@ int main(int argc, char *argv[]) goto err; } + /* this is false always for disable-systemd */ + if (!ignore_default_systemd_delegate_slice) + cgroup_set_default_systemd_cgroup(); + /* for each new cgroup */ for (i = 0; i < capacity; i++) { if (!cgroup_list[i]) -- 2.47.2