From ff16e79b5341e3f29780b8ad7603470b08d80428 Mon Sep 17 00:00:00 2001 From: Tom Hromatka Date: Wed, 9 Feb 2022 12:28:57 -0700 Subject: [PATCH] python: Add support to create a cgroup on-disk Add support to the python bindings to create a cgroup on disk. Signed-off-by: Tom Hromatka Reviewed-by: Kamalesh Babulal --- src/python/cgroup.pxd | 1 + src/python/libcgroup.pyx | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/python/cgroup.pxd b/src/python/cgroup.pxd index ac0f7702..29beca53 100644 --- a/src/python/cgroup.pxd +++ b/src/python/cgroup.pxd @@ -43,6 +43,7 @@ cdef extern from "libcgroup.h": const cgroup_library_version * cgroup_version() cgroup *cgroup_new_cgroup(const char *name) + int cgroup_create_cgroup(cgroup *cg, int ignore_ownership) int cgroup_convert_cgroup(cgroup *out_cg, cg_version_t out_version, cgroup *in_cg, cg_version_t in_version) void cgroup_free(cgroup **cg) diff --git a/src/python/libcgroup.pyx b/src/python/libcgroup.pyx index 0001cd28..558fbf5f 100644 --- a/src/python/libcgroup.pyx +++ b/src/python/libcgroup.pyx @@ -147,6 +147,12 @@ cdef class Cgroup: if cgcp == NULL: self.add_controller(ctrl_name) + cgcp = cgroup.cgroup_get_controller(self._cgp, + c_str(ctrl_name)) + if cgcp == NULL: + raise RuntimeError("Failed to get controller {}".format( + ctrl_name)) + if setting_value == None: ret = cgroup.cgroup_add_value_string(cgcp, c_str(setting_name), NULL) @@ -270,5 +276,19 @@ cdef class Cgroup: if ret != 0: raise RuntimeError("cgxset failed: {}".format(ret)) + def create(self, ignore_ownership=True): + """Write this cgroup to the cgroup sysfs + + Arguments: + ignore_ownership - if true, all errors are ignored when setting ownership + of the group and its tasks file + + Return: + None + """ + ret = cgroup.cgroup_create_cgroup(self._cgp, ignore_ownership) + if ret != 0: + raise RuntimeError("Failed to create cgroup: {}".format(ret)) + def __dealloc__(self): cgroup.cgroup_free(&self._cgp); -- 2.47.2