Preserve native desktop hard links during image assembly
@@ -151,14 +151,14 @@ python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" "
151151
152152The 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.
153153
154-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.
154+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.
155155
156156```sh
157157python3 tools/stage-image.py "$arch" "$client" "$repo" "$output" "$work/audit" \
158158 "$trusted_fingerprint" --native-layer "$desktop_layer" --initramfs
159159```
160160
161-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.
161+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.
162162
163163The 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`.
164164
@@ -2,6 +2,7 @@
22import csv
33import hashlib
44import io
5+import os
56from pathlib import Path
67import runpy
78import tempfile
@@ -45,6 +46,21 @@ class NativeLayer(unittest.TestCase):
4546 ["usr/bin/vinix-system-information", "link", "", "vinix-desktop"],
4647 ])
4748
49+ def test_preserves_hard_links_inside_the_staged_root(self):
50+ program = self.source / "usr/bin/vinix-desktop"
51+ program.write_bytes(b"native desktop")
52+ program.chmod(0o755)
53+ fallback = "usr/libexec/vinix-desktop-system"
54+ (self.source / fallback).parent.mkdir()
55+ os.link(program, self.source / fallback)
56+
57+ rows = self.install()
58+
59+ installed = self.root / "usr/bin/vinix-desktop"
60+ self.assertTrue(installed.samefile(self.root / fallback))
61+ self.assertFalse(installed.samefile(program))
62+ self.assertEqual(rows[1], [fallback, *rows[0][1:]])
63+
4864 def test_refuses_to_replace_a_package_owner(self):
4965 (self.source / "usr/bin/pkg").write_bytes(b"shadow")
5066
@@ -50,13 +50,16 @@ def native_link_target(relative, link):
5050
5151
5252def install_native_layer(source, root, table):
53+ hard_links = {}
54+
5355 def walk(directory, destination):
5456 for origin in sorted(directory.iterdir()):
5557 target = destination / origin.name
5658 relative = target.relative_to(root)
5759 if any(relative.is_relative_to(path) for path in NATIVE_RESERVED):
5860 raise ValueError(f"native layer touches a reserved path: /{relative}")
59- mode = origin.lstat().st_mode
61+ info = origin.lstat()
62+ mode = info.st_mode
6063 exists = os.path.lexists(target)
6164 if stat.S_ISDIR(mode):
6265 if exists and (target.is_symlink() or not target.is_dir()):
@@ -75,9 +78,16 @@ def install_native_layer(source, root, table):
7578 target.symlink_to(link)
7679 table.writerow((relative, "link", "", link))
7780 elif stat.S_ISREG(mode):
78- shutil.copy2(origin, target)
79- with target.open("rb") as stream:
80- digest = hashlib.file_digest(stream, "sha256").hexdigest()
81+ inode = (info.st_dev, info.st_ino)
82+ if inode in hard_links:
83+ staged, digest = hard_links[inode]
84+ os.link(staged, target)
85+ else:
86+ shutil.copy2(origin, target)
87+ with target.open("rb") as stream:
88+ digest = hashlib.file_digest(stream, "sha256").hexdigest()
89+ if info.st_nlink > 1:
90+ hard_links[inode] = (target, digest)
8191 table.writerow((relative, "file", f"{stat.S_IMODE(mode):04o}", digest))
8292 else:
8393 raise ValueError(f"unsupported native file type: /{relative}")