carlos / platform Public

rootfsimage: keep a hosts symlink's target (Astra R1 P2)

A symlinked /etc/hosts that resolves inside the image to a non-empty
regular file is materialised at etc/hosts (a hard link to its bytes)
instead of being replaced; dangling, looping, empty or directory targets
still get DefaultHosts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 9135ae714bd99b2f7e75d05e7582853a64c56a8b parent 73c872e
2 files changed, +80 −6
  • internal/rootfsimage/mounts.go +18 −4
  • internal/rootfsimage/mounts_test.go +62 −2

Intent

Human

<task-notification> <task-id>b5zf1qb70</task-id> <tool-use-id>toolu_01MxSLFRPXk2YiEBqio7j6sZ</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/07c1c3c2-c3f5-4715-929e-caf930c5532c/tasks/b5zf1qb70.output</output-file> <status>completed</status> <summary>Background command "Run Astra review round 1" completed (exit code 0)</summary> </task-notification>

diff --git a/internal/rootfsimage/mounts.go b/internal/rootfsimage/mounts.go
index 1f803a3c5..34d46cc3d 100644
--- a/internal/rootfsimage/mounts.go
+++ b/internal/rootfsimage/mounts.go
@@ -29,7 +29,7 @@ const hostsFile = "etc/hosts"
// AddMountPoints adds what MountPoints and MountFiles name that the tree
// does not have, and the parents they need, and DefaultHosts at etc/hosts
-// unless the image has its own non-empty regular file there. A new directory is 0:0 mode 0755
+// unless the image has its own non-empty hosts file. A new directory is 0:0 mode 0755
// ("tmp" is 01777), a new file 0:0 0644 and empty, all with the time of the
// tree's newest entry, so that adding them does not make a rebuild of the
// same source differ.
@@ -41,7 +41,8 @@ const hostsFile = "etc/hosts"
// else.) A mount file that is not a regular file, such as the symlink most
// images make of resolv.conf, becomes the empty file, since the box binds
// over it. An etc/hosts that is missing, empty (the placeholder a container
-// export leaves) or not a regular file becomes DefaultHosts, 0:0 0644.
+// export leaves), a directory, or a symlink that leads nowhere useful
+// becomes DefaultHosts, 0:0 0644 (addDefaultHosts).
func AddMountPoints(t *Tree) error {
when := t.Newest()
for _, p := range MountPoints {
@@ -68,14 +69,27 @@ func AddMountPoints(t *Tree) error {
}
// addDefaultHosts writes DefaultHosts at etc/hosts unless the tree has a
-// non-empty regular file there.
+// non-empty regular file there. A symlink there that resolves inside the
+// tree to a non-empty regular file is replaced by that file (a hard link
+// to its bytes, with its mode, owner and time), since its target may sit
+// under a directory a box mounts over.
func addDefaultHosts(t *Tree, when time.Time) error {
if err := t.mountDir(parentOf(hostsFile), when); err != nil {
return err
}
- if e, ok := t.Lookup(hostsFile); ok && e.Type == TypeFile && e.Size > 0 {
+ e, ok := t.Lookup(hostsFile)
+ if ok && e.Type == TypeFile && e.Size > 0 {
return nil
}
+ if ok && e.Type == TypeSymlink {
+ if p, found := t.Resolve(hostsFile); found {
+ if target, ok := t.Lookup(p); ok && target.Type == TypeFile && target.Size > 0 {
+ c := *target
+ c.Path = hostsFile
+ return t.add(&c)
+ }
+ }
+ }
src, err := t.writeBlob(strings.NewReader(DefaultHosts), int64(len(DefaultHosts)))
if err != nil {
return err
diff --git a/internal/rootfsimage/mounts_test.go b/internal/rootfsimage/mounts_test.go
index d26e321f2..5d176bc2f 100644
--- a/internal/rootfsimage/mounts_test.go
+++ b/internal/rootfsimage/mounts_test.go
@@ -177,10 +177,23 @@ func TestAddMountPointsAddsADefaultHostsFile(t *testing.T) {
{"no etc at all", []tEntry{{name: "app/run", typ: tar.TypeReg, mode: 0o755, body: "x", mtime: 1_700_000_500}}},
{"no hosts", []tEntry{{name: "etc/", typ: tar.TypeDir, mode: 0o755, mtime: 1_700_000_500}}},
{"an empty hosts", []tEntry{{name: "etc/hosts", typ: tar.TypeReg, mode: 0o600, mtime: 1_700_000_500}}},
- {"a hosts symlink", []tEntry{
- {name: "etc/hosts.real", typ: tar.TypeReg, mode: 0o644, body: "10.0.0.9 db\n", mtime: 1_700_000_500},
+ {"a dangling hosts symlink", []tEntry{
+ {name: "etc/", typ: tar.TypeDir, mode: 0o755, mtime: 1_700_000_500},
+ {name: "etc/hosts", typ: tar.TypeSymlink, link: "../run/hosts"},
+ }},
+ {"a looping hosts symlink", []tEntry{
+ {name: "etc/", typ: tar.TypeDir, mode: 0o755, mtime: 1_700_000_500},
+ {name: "etc/hosts", typ: tar.TypeSymlink, link: "hosts.b"},
+ {name: "etc/hosts.b", typ: tar.TypeSymlink, link: "hosts"},
+ }},
+ {"a hosts symlink to an empty file", []tEntry{
+ {name: "etc/hosts.real", typ: tar.TypeReg, mode: 0o644, mtime: 1_700_000_500},
{name: "etc/hosts", typ: tar.TypeSymlink, link: "hosts.real"},
}},
+ {"a hosts symlink to a directory", []tEntry{
+ {name: "etc/hosts.d/", typ: tar.TypeDir, mode: 0o755, mtime: 1_700_000_500},
+ {name: "etc/hosts", typ: tar.TypeSymlink, link: "hosts.d"},
+ }},
{"a hosts directory", []tEntry{
{name: "etc/hosts/", typ: tar.TypeDir, mode: 0o755, mtime: 1_700_000_500},
{name: "etc/hosts/x", typ: tar.TypeReg, mode: 0o644, body: "x"},
@@ -233,6 +246,53 @@ func TestAddMountPointsKeepsTheImagesOwnHostsFile(t *testing.T) {
}
}
+// Astra R1 P2: a hosts symlink that leads, inside the image, to a non-empty
+// regular file keeps that file's bytes, materialised at etc/hosts (so a
+// target under a directory a box mounts over is not lost).
+func TestAddMountPointsKeepsAHostsSymlinksTarget(t *testing.T) {
+ const own = "127.0.0.1 localhost\n10.0.0.9 db\n"
+ for _, c := range []struct {
+ name string
+ es []tEntry
+ }{
+ {"relative", []tEntry{
+ {name: "etc/hosts.real", typ: tar.TypeReg, mode: 0o640, uid: 5, gid: 6, body: own, mtime: 1_600_000_000},
+ {name: "etc/hosts", typ: tar.TypeSymlink, link: "hosts.real"},
+ }},
+ {"absolute and chained", []tEntry{
+ {name: "nix/store/abc-hosts", typ: tar.TypeReg, mode: 0o640, uid: 5, gid: 6, body: own, mtime: 1_600_000_000},
+ {name: "etc/static/hosts", typ: tar.TypeSymlink, link: "/nix/store/abc-hosts"},
+ {name: "etc/hosts", typ: tar.TypeSymlink, link: "/etc/static/hosts"},
+ }},
+ {"into a directory boxes mount over", []tEntry{
+ {name: "run/hosts", typ: tar.TypeReg, mode: 0o640, uid: 5, gid: 6, body: own, mtime: 1_600_000_000},
+ {name: "etc/hosts", typ: tar.TypeSymlink, link: "../run/hosts"},
+ }},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ tr := NewTree(t.TempDir())
+ apply(t, tr, layerTar(t, c.es...))
+ if err := AddMountPoints(tr); err != nil {
+ t.Fatal(err)
+ }
+ h := mustLookup(t, tr, "etc/hosts")
+ if h.Type != TypeFile || h.LinkTarget != "" || h.Size != int64(len(own)) || h.Mode != 0o640 || h.UID != 5 || h.GID != 6 {
+ t.Errorf("etc/hosts = %+v, want a copy of the link's target", h)
+ }
+ if got := readAll(t, tr, h); got != own {
+ t.Errorf("etc/hosts holds %q, want the target's bytes", got)
+ }
+ n := len(tr.Entries())
+ if err := AddMountPoints(tr); err != nil {
+ t.Fatal(err)
+ }
+ if len(tr.Entries()) != n || readAll(t, tr, mustLookup(t, tr, "etc/hosts")) != own {
+ t.Error("a second call changed the tree")
+ }
+ })
+ }
+}
+
func TestAddMountPointsRefusesASymlinkAtEtcForTheHostsFile(t *testing.T) {
// etc is a parent of a mount file already; the hosts file never follows
// a link out of it.