Skip to content

Commit 280b7e5

Browse files
committed
Optionally save json manifest to /usr/lib/os-manifest
If json manifest is requested and metadata would not be cleaned due to CleanPackageMetadata=, write the manifest to the OS image under the same condition as when os-release would be written (i.e. not sysext, confext, addon, or overlay).
1 parent 0210c4e commit 280b7e5

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

mkosi/__init__.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
from mkosi.context import Context
9090
from mkosi.distribution import Distribution, detect_distribution
9191
from mkosi.documentation import show_docs
92-
from mkosi.installer import clean_package_manager_metadata
92+
from mkosi.installer import clean_package_manager_metadata, package_manager_metadata_to_clean
9393
from mkosi.kmod import (
9494
filter_devicetrees,
9595
gen_required_kernel_modules,
@@ -2542,6 +2542,23 @@ def save_manifest(context: Context, manifest: Optional[Manifest]) -> None:
25422542
manifest.write_package_report(f)
25432543

25442544

2545+
def save_manifest_to_image(context: Context, manifest: Optional[Manifest]) -> None:
2546+
"""Write manifest to /usr/lib/os-manifest in the image"""
2547+
if package_manager_metadata_to_clean(context):
2548+
return
2549+
2550+
if not manifest:
2551+
return
2552+
2553+
if context.config.overlay or context.config.output_format.is_extension_image():
2554+
return
2555+
2556+
if ManifestFormat.json in context.config.manifest_format and manifest.has_data():
2557+
osmanifest = context.root / "usr/lib/os-manifest"
2558+
with osmanifest.open("w") as f:
2559+
manifest.write_json(f)
2560+
2561+
25452562
def print_output_size(path: Path) -> None:
25462563
if path.is_dir():
25472564
log_step(f"{path} size is " + format_bytes(dir_size(path)) + ".")
@@ -3947,6 +3964,7 @@ def build_image(context: Context) -> None:
39473964

39483965
if manifest:
39493966
manifest.record_packages()
3967+
save_manifest_to_image(context, manifest)
39503968
save_manifest(context, manifest)
39513969

39523970
run_selinux_relabel(context)

mkosi/installer/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,16 +201,24 @@ def clean_package_manager_metadata(context: Context) -> None:
201201
Try them all regardless of the distro: metadata is only removed if
202202
the package manager is not present in the image.
203203
"""
204+
pathsToClean = package_manager_metadata_to_clean(context)
205+
rmtree(*pathsToClean, sandbox=context.sandbox)
206+
207+
208+
def package_manager_metadata_to_clean(context: Context) -> list[Path]:
209+
"""
210+
Identifies any metadata paths that should be removed
211+
"""
204212
subdir = context.config.distribution.installer.package_manager(context.config).subdir(context.config)
205213

206214
if context.config.clean_package_metadata == ConfigFeature.disabled:
207-
return
215+
return []
208216

209217
if context.config.clean_package_metadata == ConfigFeature.auto and context.config.output_format in (
210218
OutputFormat.directory,
211219
OutputFormat.tar,
212220
):
213-
return
221+
return []
214222

215223
# If cleaning is not explicitly requested, keep the repository metadata if we're building a directory or
216224
# tar image (which are often used as a base tree for extension images and thus should retain package
@@ -219,7 +227,7 @@ def clean_package_manager_metadata(context: Context) -> None:
219227
executable = context.config.distribution.installer.package_manager(context.config).executable(
220228
context.config
221229
)
222-
remove = []
230+
pathsToClean = []
223231

224232
for tool, paths in (
225233
("rpm", ["var/lib/rpm", "usr/lib/sysimage/rpm"]),
@@ -230,6 +238,6 @@ def clean_package_manager_metadata(context: Context) -> None:
230238
if context.config.clean_package_metadata == ConfigFeature.enabled or not find_binary(
231239
tool, root=context.root
232240
):
233-
remove += [context.root / p for p in paths if (context.root / p).exists()]
241+
pathsToClean += [context.root / p for p in paths if (context.root / p).exists()]
234242

235-
rmtree(*remove, sandbox=context.sandbox)
243+
return pathsToClean

mkosi/resources/man/mkosi.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,13 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`,
623623
text format designed for diffing). By default no manifest is
624624
generated.
625625

626+
If a `json` manifest is generated, it is also written to
627+
`/usr/lib/os-manifest` inside the image when all of these are true:
628+
629+
1. Metadata is not removed by `CleanPackageMetadata=`
630+
2. `Format=` is not `sysext`, `confext`, or `addon`
631+
3. `Overlay=` is not set
632+
626633
`Output=`, `--output=`, `-o`
627634
: Name to use for the generated output image file or directory. Defaults
628635
to `image` or, if `ImageId=` is specified, it is used as the default

0 commit comments

Comments
 (0)