test/cgroup_yard: always clean up a test cgroup yard

Submitted by Andrei Vagin on Sept. 15, 2019, 6:49 p.m.

Details

Message ID 20190915184927.29026-1-avagin@gmail.com
State Accepted
Series "test/cgroup_yard: always clean up a test cgroup yard"
Commit 45d49a6cfbf012dce1ff84a4d49fa74691336fe5
Headers show

Commit Message

Andrei Vagin Sept. 15, 2019, 6:49 p.m.
Right now it is cleaned up from a post-restore hook,
but zdtm.py can be executed with the norst option:
$ zdtm.py run -t zdtm/static/cgroup_yard --norst
...
OSError: [Errno 17] File exists: 'external_yard'

Cc: Michał Cłapiński <mclapinski@google.com>
Signed-off-by: Andrei Vagin <avagin@gmail.com>
---
 test/zdtm/static/cgroup_yard.hook | 39 ++++++++-----------------------
 1 file changed, 10 insertions(+), 29 deletions(-)

Patch hide | download patch | download mbox

diff --git a/test/zdtm/static/cgroup_yard.hook b/test/zdtm/static/cgroup_yard.hook
index 7ae53342c..cc3971707 100755
--- a/test/zdtm/static/cgroup_yard.hook
+++ b/test/zdtm/static/cgroup_yard.hook
@@ -12,6 +12,7 @@  if sys.argv[1] == "--pre-dump":
 	Create external cgroup yard to be passed to CRIU via --cgroup-yard
 	'''
 	os.mkdir(yard)
+	subprocess.check_call(["mount", "-t", "tmpfs", "zdtm_yard", yard])
 	with open("/proc/self/cgroup") as f:
 		for line in f:
 			cgr = line.split(":")[1]
@@ -29,26 +30,6 @@  if sys.argv[1] == "--pre-dump":
 			os.mkdir(yard + "/" + ctrl)
 			subprocess.check_call(["mount", "-t", "cgroup", "none", yard + "/" + ctrl, "-o", opts])
 
-if sys.argv[1] == "--post-restore":
-	'''
-	Clean up the cgroup yard created during `--pre-dump`
-	'''
-	with open("/proc/self/cgroup") as f:
-		for line in f:
-			cgr = line.split(":")[1]
-
-			if cgr == "":
-				continue
-
-			if cgr.startswith("name="):
-				ctrl = cgr[len("name="):]
-			else:
-				ctrl = cgr
-
-			subprocess.check_call(["umount", yard + "/" + ctrl])
-			os.rmdir(yard + "/" + ctrl)
-	os.rmdir(yard)
-
 if sys.argv[1] in ["--pre-restore", "--clean"]:
 	'''
 	Clean up the leftover cgroups created by the test
@@ -56,15 +37,15 @@  if sys.argv[1] in ["--pre-restore", "--clean"]:
 	tname = tempfile.mkdtemp()
 	subprocess.call(["mount", "-t", "cgroup", "none", tname, "-o", "none,name=zdtmtst"])
 
-	try:
-		os.rmdir(os.path.join(tname, "subcg00", "subsubcg"))
-	except:
-		pass
-
-	try:
-		os.rmdir(os.path.join(tname, "subcg00"))
-	except:
-		pass
+	for cg in [os.path.join(tname, "subcg00", "subsubcg"),
+			os.path.join(tname, "subcg00")]:
+		if os.access(cg, os.F_OK):
+			os.rmdir(cg)
 
 	subprocess.call(["umount", tname])
 	os.rmdir(tname)
+
+if sys.argv[1] == "--clean":
+	if os.access(yard, os.F_OK):
+		subprocess.call(["umount", "-l", yard])
+		os.rmdir(yard)