]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools/cgdelete: add support for default systemd delegation slice/scope
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Fri, 10 Feb 2023 21:48:48 +0000 (14:48 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 10 Feb 2023 21:48:55 +0000 (14:48 -0700)
Enhance the cgdelete 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
delete the cgroups 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 cgdelete -g cpu:cgrp-d1

will delete the cgroup cgrp-d1 under the
cgroup_root:systemd_default_cgroup:cgrp-d1

and for deleting the cgroup cgrp1, the user can use:
$ sudo cgdelete -g cpu:/cgrp1

or use the newly introduced -b switch to ignore the systemd slice/scope:
$ sudo cgdelete -b -g cpu:cgrp1

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit 529f0e77f4e252187298a4e91c891efc1223cd56)

src/tools/cgdelete.c

index 8bedc5de2ffad63e2930215311558a69cd7351d4..5e85a9316c669634ba822307727ac063232bcdcc 100644 (file)
@@ -44,6 +44,9 @@ static void usage(int status, const char *program_name)
        info("  -g <controllers>:<path> Control group to be removed (-g is optional)\n");
        info("  -h, --help                      Display this help\n");
        info("  -r, --recursive         Recursively remove all subgroups\n");
+#ifdef WITH_SYSTEMD
+       info("  -b                              Ignore default systemd delegate hierarchy\n");
+#endif
 }
 
 /*
@@ -110,6 +113,7 @@ static int skip_add_controller(int counter, int *skip, struct ext_cgroup_record
 
 int main(int argc, char *argv[])
 {
+       int ignore_default_systemd_delegate_slice = 0;
        struct cgroup_group_spec **cgroup_list = NULL;
        struct ext_cgroup_record *ecg_list = NULL;
        struct cgroup_controller *cgc;
@@ -150,9 +154,16 @@ int main(int argc, char *argv[])
        }
 
        /* Parse arguments */
-       while ((c = getopt_long(argc, argv, "rhg:",
-               long_options, NULL)) > 0) {
+#ifdef WITH_SYSTEMD
+       while ((c = getopt_long(argc, argv, "rhg:b", long_options, NULL)) > 0) {
                switch (c) {
+               case 'b':
+                       ignore_default_systemd_delegate_slice = 1;
+                       break;
+#else
+       while ((c = getopt_long(argc, argv, "rhg:", long_options, NULL)) > 0) {
+               switch (c) {
+#endif
                case 'r':
                        flags |= CGFLAG_DELETE_RECURSIVE;
                        break;
@@ -175,6 +186,10 @@ int main(int argc, char *argv[])
                }
        }
 
+       /* this is false always for disable-systemd */
+       if (!ignore_default_systemd_delegate_slice)
+               cgroup_set_default_systemd_cgroup();
+
        /* parse groups on command line */
        for (i = optind; i < argc; i++) {
                ret = parse_cgroup_spec(cgroup_list, argv[i], argc);