@@ -16,6 +16,7 @@ unset IMAGES
1616
1717RUNC=" ${INTEGRATION_ROOT} /../../runc"
1818RECVTTY=" ${INTEGRATION_ROOT} /../../contrib/cmd/recvtty/recvtty"
19+ SD_HELPER=" ${INTEGRATION_ROOT} /../../contrib/cmd/sd-helper/sd-helper"
1920
2021# Test data path.
2122# shellcheck disable=SC2034
@@ -131,24 +132,85 @@ function init_cgroup_paths() {
131132 fi
132133}
133134
135+ function create_parent() {
136+ if [ -n " $RUNC_USE_SYSTEMD " ]; then
137+ [ -z " $SD_PARENT_NAME " ] && return
138+ " $SD_HELPER " --parent machine.slice start " $SD_PARENT_NAME "
139+ else
140+ [ -z " $REL_PARENT_PATH " ] && return
141+ if [ " $CGROUP_UNIFIED " == " yes" ]; then
142+ mkdir " /sys/fs/cgroup$REL_PARENT_PATH "
143+ else
144+ local subsys
145+ for subsys in ${CGROUP_SUBSYSTEMS} ; do
146+ # Have to ignore EEXIST (-p) as some subsystems
147+ # are mounted together (e.g. cpu,cpuacct), so
148+ # the path is created more than once.
149+ mkdir -p " /sys/fs/cgroup/$subsys$REL_PARENT_PATH "
150+ done
151+ fi
152+ fi
153+ }
154+
155+ function remove_parent() {
156+ if [ -n " $RUNC_USE_SYSTEMD " ]; then
157+ [ -z " $SD_PARENT_NAME " ] && return
158+ " $SD_HELPER " --parent machine.slice stop " $SD_PARENT_NAME "
159+ else
160+ [ -z " $REL_PARENT_PATH " ] && return
161+ if [ " $CGROUP_UNIFIED " == " yes" ]; then
162+ rmdir " /sys/fs/cgroup/$REL_PARENT_PATH "
163+ else
164+ local subsys
165+ for subsys in ${CGROUP_SUBSYSTEMS} systemd; do
166+ rmdir " /sys/fs/cgroup/$subsys /$REL_PARENT_PATH "
167+ done
168+ fi
169+ fi
170+ unset SD_PARENT_NAME
171+ unset REL_PARENT_PATH
172+ }
173+
174+ function set_parent_systemd_properties() {
175+ [ -z " $SD_PARENT_NAME " ] && return
176+ local user
177+ [ " $( id -u) " != " 0" ] && user=" --user"
178+ systemctl set-property $user " $SD_PARENT_NAME " " $@ "
179+ }
180+
134181# Randomize cgroup path(s), and update cgroupsPath in config.json.
135182# This function sets a few cgroup-related variables.
183+ #
184+ # Optional parameter $1 is a pod/parent name. If set, a parent/pod cgroup is
185+ # created, and variables $REL_PARENT_PATH and $SD_PARENT_NAME can be used to
186+ # refer to it.
136187function set_cgroups_path() {
137188 init_cgroup_paths
189+ local pod dash_pod slash_pod pod_slice
190+ if [ " $# " -ne 0 ] && [ " $1 " != " " ]; then
191+ # Set up a parent/pod cgroup.
192+ pod=" $1 "
193+ dash_pod=" -$pod "
194+ slash_pod=" /$pod "
195+ SD_PARENT_NAME=" machine-${pod} .slice"
196+ pod_slice=" /$SD_PARENT_NAME "
197+ fi
138198
139199 local rnd=" $RANDOM "
140200 if [ -n " ${RUNC_USE_SYSTEMD} " ]; then
141201 SD_UNIT_NAME=" runc-cgroups-integration-test-${rnd} .scope"
142202 if [ " $( id -u) " = " 0" ]; then
143- REL_CGROUPS_PATH =" /machine.slice/ $SD_UNIT_NAME "
144- OCI_CGROUPS_PATH=" machine.slice:runc-cgroups:integration-test-${rnd} "
203+ REL_PARENT_PATH =" /machine.slice${pod_slice} "
204+ OCI_CGROUPS_PATH=" machine${dash_pod} .slice:runc-cgroups:integration-test-${rnd} "
145205 else
146- REL_CGROUPS_PATH =" /user.slice/user-$( id -u) .slice/user@$( id -u) .service/machine.slice/ $SD_UNIT_NAME "
206+ REL_PARENT_PATH =" /user.slice/user-$( id -u) .slice/user@$( id -u) .service/machine.slice${pod_slice} "
147207 # OCI path doesn't contain "/user.slice/user-$(id -u).slice/user@$(id -u).service/" prefix
148- OCI_CGROUPS_PATH=" machine.slice:runc-cgroups:integration-test-${rnd} "
208+ OCI_CGROUPS_PATH=" machine${dash_pod} .slice:runc-cgroups:integration-test-${rnd} "
149209 fi
210+ REL_CGROUPS_PATH=" $REL_PARENT_PATH /$SD_UNIT_NAME "
150211 else
151- REL_CGROUPS_PATH=" /runc-cgroups-integration-test/test-cgroup-${rnd} "
212+ REL_PARENT_PATH=" /runc-cgroups-integration-test${slash_pod} "
213+ REL_CGROUPS_PATH=" $REL_PARENT_PATH /test-cgroup-${rnd} "
152214 OCI_CGROUPS_PATH=$REL_CGROUPS_PATH
153215 fi
154216
@@ -157,6 +219,8 @@ function set_cgroups_path() {
157219 CGROUP_PATH=${CGROUP_BASE_PATH}${REL_CGROUPS_PATH}
158220 fi
159221
222+ [ -n " $pod " ] && create_parent
223+
160224 update_config ' .linux.cgroupsPath |= "' " ${OCI_CGROUPS_PATH} " ' "'
161225}
162226
@@ -479,4 +543,5 @@ function teardown_bundle() {
479543 __runc delete -f " $ct "
480544 done
481545 rm -rf " $ROOT "
546+ remove_parent
482547}
0 commit comments