]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Fix install_tree()
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 14 Dec 2023 15:30:02 +0000 (16:30 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Thu, 14 Dec 2023 21:47:28 +0000 (22:47 +0100)
Let's make sure that all the skeleton, extra and package manager
trees we get have absolute targets. That allows us to stop using
with_prefix() when installing these trees, which means we pass
target=None instead of target="/" which makes install_tree do the
right thing.

mkosi/__init__.py
mkosi/config.py
mkosi/tree.py

index aefff3eabc2ef53de0a939126278451fcf1a331b..6e714681ea6f6e913ea47cfbf454d3d425335c48 100644 (file)
@@ -1227,8 +1227,7 @@ def install_skeleton_trees(state: MkosiState) -> None:
 
     with complete_step("Copying in skeleton file trees…"):
         for tree in state.config.skeleton_trees:
-            source, target = tree.with_prefix()
-            install_tree(source, state.root, target, use_subvolumes=state.config.use_subvolumes)
+            install_tree(tree.source, state.root, tree.target, use_subvolumes=state.config.use_subvolumes)
 
 
 def install_package_manager_trees(state: MkosiState) -> None:
@@ -1237,8 +1236,12 @@ def install_package_manager_trees(state: MkosiState) -> None:
 
     with complete_step("Copying in package manager file trees…"):
         for tree in state.config.package_manager_trees:
-            source, target = tree.with_prefix()
-            install_tree(source, state.workspace / "pkgmngr", target, use_subvolumes=state.config.use_subvolumes)
+            install_tree(
+                tree.source,
+                state.workspace / "pkgmngr",
+                tree.target,
+                use_subvolumes=state.config.use_subvolumes
+            )
 
 
 def install_extra_trees(state: MkosiState) -> None:
@@ -1247,8 +1250,7 @@ def install_extra_trees(state: MkosiState) -> None:
 
     with complete_step("Copying in extra file trees…"):
         for tree in state.config.extra_trees:
-            source, target = tree.with_prefix()
-            install_tree(source, state.root, target, use_subvolumes=state.config.use_subvolumes)
+            install_tree(tree.source, state.root, tree.target, use_subvolumes=state.config.use_subvolumes)
 
 
 def install_build_dest(state: MkosiState) -> None:
index 631eb6b72e5659a061cabf962b301d34739fa109..c733ef879ad4c1cbc352e19e86e1987cda4b3146 100644 (file)
@@ -241,7 +241,8 @@ def parse_path(value: str,
                executable: bool = False,
                expanduser: bool = True,
                expandvars: bool = True,
-               secret: bool = False) -> Path:
+               secret: bool = False,
+               absolute: bool = False) -> Path:
     if expandvars:
         value = os.path.expandvars(value)
 
@@ -255,6 +256,9 @@ def parse_path(value: str,
     if required and not path.exists():
         die(f"{value} does not exist")
 
+    if absolute and not path.is_absolute():
+        die(f"{value} must be an absolute path")
+
     if resolve:
         path = path.resolve()
 
@@ -272,13 +276,22 @@ def parse_path(value: str,
     return path
 
 
-def parse_tree(value: str) -> ConfigTree:
-    src, sep, tgt = value.partition(':')
+def make_tree_parser(absolute: bool = True) -> Callable[[str], ConfigTree]:
+    def parse_tree(value: str) -> ConfigTree:
+        src, sep, tgt = value.partition(':')
 
-    return ConfigTree(
-        source=parse_path(src, required=False),
-        target=parse_path(tgt, required=False, resolve=False, expanduser=False) if sep else None,
-    )
+        return ConfigTree(
+            source=parse_path(src, required=False),
+            target=parse_path(
+                tgt,
+                required=False,
+                resolve=False,
+                expanduser=False,
+                absolute=absolute,
+            ) if sep else None,
+        )
+
+    return parse_tree
 
 
 def config_match_build_sources(match: str, value: list[ConfigTree]) -> bool:
@@ -1349,7 +1362,7 @@ SETTINGS = (
         long="--package-manager-tree",
         metavar="PATH",
         section="Distribution",
-        parse=config_make_list_parser(delimiter=",", parse=parse_tree),
+        parse=config_make_list_parser(delimiter=",", parse=make_tree_parser()),
         default_factory=lambda ns: ns.skeleton_trees,
         default_factory_depends=("skeleton_trees",),
         help="Use a package manager tree to configure the package manager",
@@ -1550,7 +1563,7 @@ SETTINGS = (
         long="--skeleton-tree",
         metavar="PATH",
         section="Content",
-        parse=config_make_list_parser(delimiter=",", parse=parse_tree),
+        parse=config_make_list_parser(delimiter=",", parse=make_tree_parser()),
         paths=("mkosi.skeleton", "mkosi.skeleton.tar"),
         path_default=False,
         help="Use a skeleton tree to bootstrap the image before installing anything",
@@ -1560,7 +1573,7 @@ SETTINGS = (
         long="--extra-tree",
         metavar="PATH",
         section="Content",
-        parse=config_make_list_parser(delimiter=",", parse=parse_tree),
+        parse=config_make_list_parser(delimiter=",", parse=make_tree_parser()),
         paths=("mkosi.extra", "mkosi.extra.tar"),
         path_default=False,
         help="Copy an extra tree on top of image",
@@ -1645,7 +1658,7 @@ SETTINGS = (
         dest="build_sources",
         metavar="PATH",
         section="Content",
-        parse=config_make_list_parser(delimiter=",", parse=parse_tree),
+        parse=config_make_list_parser(delimiter=",", parse=make_tree_parser(absolute=False)),
         match=config_match_build_sources,
         help="Path for sources to build",
     ),
@@ -2170,7 +2183,7 @@ SETTINGS = (
         long="--runtime-tree",
         metavar="SOURCE:[TARGET]",
         section="Host",
-        parse=config_make_list_parser(delimiter=",", parse=parse_tree),
+        parse=config_make_list_parser(delimiter=",", parse=make_tree_parser(absolute=False)),
         help="Additional mounts to add when booting the image",
     ),
     MkosiConfigSetting(
index a35c6abdcd1386bede755d26cf50e1e3070522df..3d8363df819551bfc2174e5efd9ce150c8ad0e1e 100644 (file)
@@ -145,4 +145,5 @@ def install_tree(
     elif src.suffix == ".raw":
         run(["systemd-dissect", "--copy-from", src, "/", t])
     else:
-        die(f"Source tree {src} has unsupported source tree type \"{src.suffix}\"")
+        # If we get an unknown file without a target, we just copy it into /.
+        copy_tree(src, t, preserve_owner=False, use_subvolumes=use_subvolumes)