From 3cd5418ca258531f5215d10a02db78bccbd42233 Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 19:36:00 +0100 Subject: [PATCH] Preserve native desktop hard links during image assembly --- tests/FEED.md | 4 ++-- tests/native-layer.py | 16 ++++++++++++++++ tools/stage-image.py | 18 ++++++++++++++---- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/tests/FEED.md b/tests/FEED.md index 6f72570..51d5b42 100644 --- a/tests/FEED.md +++ b/tests/FEED.md @@ -151,14 +151,14 @@ python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" " The tool audits the package-owned root, makes a separate copy, and seeds the verification keyring using the distributor's fingerprint. It copies the local package records into `/var/lib/vpac/local`, checks their versions against the signed database, and installs the repository under `/var/lib/vinix/repository/$arch`. It verifies the database, file index and each included package signature, and compares the payload hashes with the signed database. The sync cache receives independent copies of the database and signature. Assembly exposes the final root only after these checks pass. -Pass the native desktop programs and assets in a staging directory with `--native-layer`. Assembly rejects a native file at a package-owned path. `native-layer.tsv` records the installed file hashes and the symbolic link targets. `--initramfs` writes the root-owned USTAR archive for Vinix's ISO builder. +Pass the native desktop programs and assets in a staging directory with `--native-layer`. Assembly rejects a native file at a package-owned path and preserves hard links within the layer. `native-layer.tsv` records the installed file hashes and the symbolic link targets. `--initramfs` writes the root-owned USTAR archive for Vinix's ISO builder. ```sh python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" \ "$trusted_fingerprint" --native-layer "$desktop_layer" --initramfs ``` -Use `$output/initramfs.tar` as `VINIX_AMD64_INITRAMFS` or `VINIX_AARCH64_INITRAMFS` in Vinix's architecture-specific ISO assembly command. Run `python3 tests/native-layer.py` for the five layer regression checks. +Use `$output/initramfs.tar` as `VINIX_AMD64_INITRAMFS` or `VINIX_AARCH64_INITRAMFS` in Vinix's architecture-specific ISO assembly command. Run `python3 tests/native-layer.py` for the layer regression checks. The x86_64 ISO proof in `foundation/native-layer-image-01` cold-installed 45 signed packages and audited 7,761 owned files and links and 162 target ELFs. The native layer used five checksum-pinned programs from the November 28 Vinix release. Assembly preserved the signed database, program modes and relative app link. An attempted replacement of the package-owned `usr/bin/pkg` failed before exposing an output root or archive. The worker compared the ISO's embedded initramfs with the staged archive, then booted the ISO in QEMU. The fixture reached registration at 1024×768 and sampled at least 24 framebuffer colors. The receipt SHA-256 is `6cbcbaa46d5642228b8ec8512270916de131968e47dc4776f2e6843d00dbd143`. diff --git a/tests/native-layer.py b/tests/native-layer.py index d56de47..15ae61f 100644 --- a/tests/native-layer.py +++ b/tests/native-layer.py @@ -2,6 +2,7 @@ import csv import hashlib import io +import os from pathlib import Path import runpy import tempfile @@ -45,6 +46,21 @@ class NativeLayer(unittest.TestCase): ["usr/bin/vinix-system-information", "link", "", "vinix-desktop"], ]) + def test_preserves_hard_links_inside_the_staged_root(self): + program = self.source / "usr/bin/vinix-desktop" + program.write_bytes(b"native desktop") + program.chmod(0o755) + fallback = "usr/libexec/vinix-desktop-system" + (self.source / fallback).parent.mkdir() + os.link(program, self.source / fallback) + + rows = self.install() + + installed = self.root / "usr/bin/vinix-desktop" + self.assertTrue(installed.samefile(self.root / fallback)) + self.assertFalse(installed.samefile(program)) + self.assertEqual(rows[1], [fallback, *rows[0][1:]]) + def test_refuses_to_replace_a_package_owner(self): (self.source / "usr/bin/pkg").write_bytes(b"shadow") diff --git a/tools/stage-image.py b/tools/stage-image.py index bb288ba..fc65d76 100644 --- a/tools/stage-image.py +++ b/tools/stage-image.py @@ -50,13 +50,16 @@ def native_link_target(relative, link): def install_native_layer(source, root, table): + hard_links = {} + def walk(directory, destination): for origin in sorted(directory.iterdir()): target = destination / origin.name relative = target.relative_to(root) if any(relative.is_relative_to(path) for path in NATIVE_RESERVED): raise ValueError(f"native layer touches a reserved path: /{relative}") - mode = origin.lstat().st_mode + info = origin.lstat() + mode = info.st_mode exists = os.path.lexists(target) if stat.S_ISDIR(mode): if exists and (target.is_symlink() or not target.is_dir()): @@ -75,9 +78,16 @@ def install_native_layer(source, root, table): target.symlink_to(link) table.writerow((relative, "link", "", link)) elif stat.S_ISREG(mode): - shutil.copy2(origin, target) - with target.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + inode = (info.st_dev, info.st_ino) + if inode in hard_links: + staged, digest = hard_links[inode] + os.link(staged, target) + else: + shutil.copy2(origin, target) + with target.open("rb") as stream: + digest = hashlib.file_digest(stream, "sha256").hexdigest() + if info.st_nlink > 1: + hard_links[inode] = (target, digest) table.writerow((relative, "file", f"{stat.S_IMODE(mode):04o}", digest)) else: raise ValueError(f"unsupported native file type: /{relative}") -- 2.56.1