]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Make sure we always delete workspace / "root" as a subvolume
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 15 Feb 2025 21:58:04 +0000 (22:58 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Sat, 15 Feb 2025 21:59:03 +0000 (22:59 +0100)
Currently, we delete it as a subvolume if the build succeeds but not
if the build fails. Let's handle the deletion in setup_workspace() so
we delete it as a subvolume both on success and on failure.

mkosi/__init__.py

index 0828c87cde33513efab2c9f6e366c5cf65fa6c7e..9a6f221eabcff98c8c88eef6a60487732f8b931b 100644 (file)
@@ -3770,7 +3770,9 @@ def setup_workspace(args: Args, config: Config) -> Iterator[Path]:
         workspace = Path(tempfile.mkdtemp(dir=config.workspace_dir_or_default(), prefix="mkosi-workspace-"))
         # Discard setuid/setgid bits as these are inherited and can leak into the image.
         workspace.chmod(stat.S_IMODE(workspace.stat().st_mode) & ~(stat.S_ISGID | stat.S_ISUID))
-        stack.callback(lambda: rmtree(workspace, sandbox=config.sandbox))
+        # Explicitly pass the "root" subdirectory first because on btrfs it's likely a subvolume and this
+        # allows us to delete it with btrfs subvolume delete instead of a costly rm -rf.
+        stack.callback(lambda: rmtree(workspace / "root", workspace, sandbox=config.sandbox))
         (workspace / "tmp").mkdir(mode=0o1777)
 
         with scopedenv({"TMPDIR": os.fspath(workspace / "tmp")}):
@@ -4016,9 +4018,6 @@ def build_image(context: Context) -> None:
     run_postoutput_scripts(context)
     finalize_staging(context)
 
-    if not context.args.debug_workspace:
-        rmtree(context.root, sandbox=context.sandbox)
-
     print_output_size(context.config.output_dir_or_cwd() / context.config.output_with_compression)