]> git.ipfire.org Git - thirdparty/mkosi.git/commitdiff
Add SplitArtifacts=os-release
authorLuca Boccassi <luca.boccassi@gmail.com>
Sun, 16 Feb 2025 21:09:10 +0000 (21:09 +0000)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 17 Feb 2025 08:37:35 +0000 (09:37 +0100)
Useful to work on metadata in post-processing scripts

mkosi/__init__.py
mkosi/config.py
mkosi/resources/man/mkosi.1.md

index e7d6df3eb96a216f86e0e5db290727658bc63882..59da9a4b054a14c45b83ffc0b156fc0aa842b616 100644 (file)
@@ -345,8 +345,6 @@ def check_root_populated(context: Context) -> None:
 
 def configure_os_release(context: Context) -> None:
     """Write IMAGE_ID and IMAGE_VERSION to /usr/lib/os-release in the image."""
-    if not (context.config.image_id or context.config.image_version or context.config.hostname):
-        return
 
     if context.config.overlay or context.config.output_format.is_extension_image():
         return
@@ -357,34 +355,38 @@ def configure_os_release(context: Context) -> None:
         if not osrelease.is_file() or osrelease.is_symlink():
             continue
 
-        # at this point we know we will either change or add to the file
-        newosrelease = osrelease.with_suffix(".new")
-
-        image_id_written = image_version_written = default_hostname_written = False
-        with osrelease.open("r") as old, newosrelease.open("w") as new:
-            # fix existing values
-            for line in old.readlines():
-                if context.config.image_id and line.startswith("IMAGE_ID="):
+        if context.config.image_id or context.config.image_version or context.config.hostname:
+            # at this point we know we will either change or add to the file
+            newosrelease = osrelease.with_suffix(".new")
+
+            image_id_written = image_version_written = default_hostname_written = False
+            with osrelease.open("r") as old, newosrelease.open("w") as new:
+                # fix existing values
+                for line in old.readlines():
+                    if context.config.image_id and line.startswith("IMAGE_ID="):
+                        new.write(f'IMAGE_ID="{context.config.image_id}"\n')
+                        image_id_written = True
+                    elif context.config.image_version and line.startswith("IMAGE_VERSION="):
+                        new.write(f'IMAGE_VERSION="{context.config.image_version}"\n')
+                        image_version_written = True
+                    elif context.config.hostname and line.startswith("DEFAULT_HOSTNAME="):
+                        new.write(f'DEFAULT_HOSTNAME="{context.config.hostname}"\n')
+                        default_hostname_written = True
+                    else:
+                        new.write(line)
+
+                # append if they were missing
+                if context.config.image_id and not image_id_written:
                     new.write(f'IMAGE_ID="{context.config.image_id}"\n')
-                    image_id_written = True
-                elif context.config.image_version and line.startswith("IMAGE_VERSION="):
+                if context.config.image_version and not image_version_written:
                     new.write(f'IMAGE_VERSION="{context.config.image_version}"\n')
-                    image_version_written = True
-                elif context.config.hostname and line.startswith("DEFAULT_HOSTNAME="):
+                if context.config.hostname and not default_hostname_written:
                     new.write(f'DEFAULT_HOSTNAME="{context.config.hostname}"\n')
-                    default_hostname_written = True
-                else:
-                    new.write(line)
 
-            # append if they were missing
-            if context.config.image_id and not image_id_written:
-                new.write(f'IMAGE_ID="{context.config.image_id}"\n')
-            if context.config.image_version and not image_version_written:
-                new.write(f'IMAGE_VERSION="{context.config.image_version}"\n')
-            if context.config.hostname and not default_hostname_written:
-                new.write(f'DEFAULT_HOSTNAME="{context.config.hostname}"\n')
+            newosrelease.rename(osrelease)
 
-        newosrelease.rename(osrelease)
+        if ArtifactOutput.os_release in context.config.split_artifacts:
+            shutil.copy(osrelease, context.staging / context.config.output_split_os_release)
 
 
 def configure_extension_release(context: Context) -> None:
index c08a4ac94ab99228fd5b9caf5d7123c68c1df45b..15c523b3ccab75d7943b0596164b76ea23c55a92 100644 (file)
@@ -573,6 +573,7 @@ class ArtifactOutput(StrEnum):
     tar = enum.auto()
     pcrs = enum.auto()
     roothash = enum.auto()
+    os_release = enum.auto()
 
     @staticmethod
     def compat_no() -> list["ArtifactOutput"]:
@@ -2119,6 +2120,10 @@ class Config:
     def output_split_roothash(self) -> str:
         return f"{self.output}.roothash"
 
+    @property
+    def output_split_os_release(self) -> str:
+        return f"{self.output}.osrelease"
+
     @property
     def output_nspawn_settings(self) -> str:
         return f"{self.output}.nspawn"
@@ -2159,6 +2164,7 @@ class Config:
             self.output_split_initrd,
             self.output_split_pcrs,
             self.output_split_roothash,
+            self.output_split_os_release,
             self.output_nspawn_settings,
             self.output_checksum,
             self.output_signature,
index 6234090f5eb000379d67ebd0937817228d8bf0d5..35dc2ed80da83581ee14f8257b43e4975917e546 100644 (file)
@@ -615,7 +615,7 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
 
 `SplitArtifacts=`, `--split-artifacts=`
 :   The artifact types to split out of the final image. A comma-delimited
-    list consisting of `uki`, `kernel`, `initrd`, `prcs`, `partitions`,
+    list consisting of `uki`, `kernel`, `initrd`, `os-release`, `prcs`, `partitions`,
     `roothash` and `tar`. When building a bootable image `kernel` and `initrd`
     correspond to their artifact found in the image (or in the UKI),
     while `uki` copies out the entire UKI. If `pcrs` is specified, a JSON