rootfs: carlos ship adds /etc/hosts to images that have none (#1349)
Docker and Kubernetes write /etc/hosts into a container at start, so images rarely carry it, and rootfs apps had none: Ruby's Resolv (Excon, fog's S3) failed with ENOENT. carlos ship now writes localhost entries (IPv4 and IPv6) at etc/hosts when the image has no file there, an empty one, a directory, or a symlink that leads nowhere useful. A symlink to a non-empty in-image file is kept as that file. Built at ship time, so images already shipped keep starting; no box or unit change. Rootfs page line RFS-4-C155 (Luna pass under Paul's delegation). Astra R1 READY AFTER FIXES (one P2, fixed), R2 READY. Census census-ruling-rootfs-etc-hosts-1349: rootfsimage envelope, Astra APPROVE (#280). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Prompts: <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> Squashed from rootfs-etc-hosts-1349 into main.
9 files changed,
+217
−7
approved_rootfs_copy_test.go+4 −2docs/site/rootfs.md+2 −0docs/superpowers/notes/census-proposal-rootfs-etc-hosts-1349.md+11 −0docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md+7 −2internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-rootfs-etc-hosts-1349.tsv+3 −0internal/rootfsimage/mounts.go+46 −3internal/rootfsimage/mounts_test.go+139 −0ship_rootfs_test.go+4 −0
CI cancelled — superseded by a newer push — run details
CI log
=== gate === go: downloading go1.26.10 (linux/arm64) go version go1.26.10 linux/arm64 opentofu: arm64, tofu 1.13.9, aws 6.73.0, archive 2.8.1 tofu: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/tofu_1.13.9_linux_arm64.tar.gz tofu: downloaded, digest verified aws: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-aws_6.73.0_linux_arm64.zip aws: downloaded, digest verified archive: downloading https://carlos-ci-mirror-640012953448.s3.eu-west-1.amazonaws.com/opentofu/terraform-provider-archive_2.8.1_linux_arm64.zip archive: downloaded, digest verified installed in /tmp/carlos-opentofu-FLYEl9 .amadan/test-gate.sh go: downloading github.com/aws/aws-sdk-go-v2 v1.44.0 go: downloading github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.68.0 go: downloading github.com/aws/aws-sdk-go-v2/service/s3 v1.105.0 go: downloading github.com/benbjohnson/litestream v0.5.14 go: downloading github.com/aws/aws-sdk-go-v2/service/costexplorer v1.67.5 go: downloading github.com/superfly/ltx v0.5.1 go: downloading golang.org/x/net v0.62.0 go: downloading modernc.org/sqlite v1.55.0 go: downloading github.com/aws/aws-sdk-go-v2/service/sns v1.43.0 go: downloading golang.org/x/sys v0.49.0 go: downloading golang.org/x/crypto v0.58.0 go: downloading github.com/aws/aws-sdk-go-v2/service/route53 v1.64.1 go: downloading github.com/aws/aws-sdk-go-v2/service/iam v1.58.2 go: downloading github.com/aws/aws-sdk-go-v2/service/sesv2 v1.66.6 go: downloading github.com/pion/logging v0.2.4 go: downloading github.com/pion/stun/v3 v3.1.5 go: downloading github.com/pion/turn/v4 v4.1.4 go: downloading github.com/aws/aws-sdk-go-v2/service/acm v1.45.0 go: downloading github.com/aws/aws-sdk-go-v2/service/ec2 v1.324.0 go: downloading github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi v1.37.0 go: downloading github.com/pierrec/lz4/v4 v4.1.22 go: downloading github.com/prometheus/client_golang v1.17.0 go: downloading github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.40 go: downloading github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.38 go: downloading github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.40 go: downloading github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.20.18 go: downloading github.com/pion/dtls/v3 v3.1.4 go: downloading github.com/pion/transport/v4 v4.0.2 go: downloading golang.org/x/text v0.43.0 go: downloading github.com/klauspost/compress v1.20.2 go: downloading github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.40 go: downloading github.com/wlynxg/anet v0.0.5 go: downloading github.com/prometheus/procfs v0.12.0 go: downloading google.golang.org/protobuf v1.36.10 go: downloading github.com/prometheus/common v0.45.0 go: downloading github.com/prometheus/client_model v0.5.0 go: downloading github.com/cespare/xxhash/v2 v2.3.0 go: downloading github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.31 go: downloading github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.23 go: downloading github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 go: downloading github.com/pion/randutil v0.1.0 go: downloading golang.org/x/crypto/x509roots/fallback v0.0.0-20260209214922-2f26647a795e go: downloading github.com/klauspost/compress v1.20.2 go: downloading modernc.org/sqlite v1.55.0 go: downloading github.com/aws/aws-sdk-go-v2/service/ecs v1.91.0 go: downloading github.com/pierrec/lz4/v4 v4.1.22 go: downloading modernc.org/libc v1.74.1 go: downloading modernc.org/libc v1.74.1 go: downloading modernc.org/mathutil v1.7.1 go: downloading github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec amadan: job cancelled — a newer push superseded it
landed from rootfs-etc-hosts-1349
diff --git a/approved_rootfs_copy_test.go b/approved_rootfs_copy_test.go| index c6d3843a3..de55a3986 100644 |
| --- a/approved_rootfs_copy_test.go |
| +++ b/approved_rootfs_copy_test.go |
| @@ -21,7 +21,7 @@ import ( |
| const rootfsCopyRecordPath = "docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md" |
| // Canonical ids only: two digits from C01 to C99, three digits from C100 |
| -// (Plan 4 runs to RFS-4-C146; RFS-4-C147 is the rootfs-default-home follow-up). No leading zero on a three-digit id, no C00. |
| +// (Plan 4 runs to RFS-4-C146; RFS-4-C147 is the rootfs-default-home follow-up, RFS-4-C155 the /etc/hosts one). No leading zero on a three-digit id, no C00. |
| var rootfsCopyIDPattern = regexp.MustCompile(`^RFS-[1-6]-C(0[1-9]|[1-9][0-9]{1,2})$`) |
| // rootfsCopyIDs is the deck's inventory, pinned as a literal list in the |
| @@ -82,7 +82,7 @@ var rootfsCopyIDs = []string{ |
| "RFS-4-C33", "RFS-4-C34", "RFS-4-C35", "RFS-4-C36", |
| "RFS-4-C37", "RFS-4-C38", "RFS-4-C39", "RFS-4-C40", |
| "RFS-4-C41", "RFS-4-C42", "RFS-4-C43", "RFS-4-C44", |
| - "RFS-4-C45", "RFS-4-C46", "RFS-4-C47", "RFS-4-C147", "RFS-4-C48", |
| + "RFS-4-C45", "RFS-4-C46", "RFS-4-C47", "RFS-4-C147", "RFS-4-C155", "RFS-4-C48", |
| "RFS-4-C49", "RFS-4-C50", "RFS-4-C51", "RFS-4-C52", |
| "RFS-4-C53", "RFS-4-C54", "RFS-4-C55", "RFS-4-C56", |
| "RFS-4-C57", "RFS-4-C58", "RFS-4-C59", "RFS-4-C60", |
| @@ -254,6 +254,8 @@ var rootfsCopyBuilt = []string{ |
| "RFS-4-C141", "RFS-4-C142", "RFS-4-C143", "RFS-4-C144", "RFS-4-C146", |
| // rootfs-default-home (2026-11-01): HOME is /tmp unless set (docs/site/rootfs.md) |
| "RFS-4-C147", |
| + // rootfs-etc-hosts-1349 (2026-11-22): carlos ship adds /etc/hosts (docs/site/rootfs.md) |
| + "RFS-4-C155", |
| // rails-guide-run-console (2026-11-06): Commands and consoles on the |
| // Rootfs page and the Rails guide's pointer; RFS-4-C100 struck |
| "RFS-4-C148", "RFS-4-C149", "RFS-4-C150", "RFS-4-C151", "RFS-4-C152", "RFS-4-C153", "RFS-4-C154", |
diff --git a/docs/site/rootfs.md b/docs/site/rootfs.md| index c01f2f9d9..d77ea92a8 100644 |
| --- a/docs/site/rootfs.md |
| +++ b/docs/site/rootfs.md |
| @@ -49,6 +49,8 @@ Every process of the instance gets the same environment: web, the workers and re |
| - HOME is /tmp unless the image or the app sets it. |
| +- /etc/hosts contains 127.0.0.1 localhost and ::1 localhost ip6-localhost ip6-loopback unless the image has a non-empty regular file there, or a link to one. carlos ship adds it when needed because Docker and Kubernetes write /etc/hosts when a container starts, and the read-only image prevents the app from creating it. |
| + |
| ## Health |
| web must answer GET / on CARLOS_SOCKET with a status below 500 within 120 seconds of starting. Until it does, the instance does not serve and its workers do not start. |
diff --git a/docs/superpowers/notes/census-proposal-rootfs-etc-hosts-1349.md b/docs/superpowers/notes/census-proposal-rootfs-etc-hosts-1349.md| new file mode 100644 |
| index 000000000..f647875cb |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-rootfs-etc-hosts-1349.md |
| @@ -0,0 +1,11 @@ |
| +# Census approval: rootfs-etc-hosts-1349 |
| + |
| +Ruling `census-ruling-rootfs-etc-hosts-1349`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +Envelopes re-pinned: `internal/rootfsimage`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Seal: `892cf87db700600490e3ec9e944f9dbe7151e43bc5471090634f1735bcb9a7bd` → `892cf87db700600490e3ec9e944f9dbe7151e43bc5471090634f1735bcb9a7bd`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-rootfs-etc-hosts-1349.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md b/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md| index a70db34f1..8e3461e13 100644 |
| --- a/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md |
| +++ b/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md |
| @@ -290,6 +290,7 @@ From the plan: Placeholders: `<app>` an app; `<fleet>` a fleet name; `<region>` |
| | RFS-4-C46 | docs/site/rootfs.md | CARLOS_DB is the instance's SQLite database, the one file the platform replicates. An app that keeps its data somewhere else can ignore it. | approved | |
| | RFS-4-C47 | docs/site/rootfs.md (fact) | CARLOS_HOST, CARLOS_REGION, CARLOS_CONTROL_SOCKET and CARLOS_PURGE mean what they mean for a binary instance, as [What an instance is started with](/docs/cli#what-an-instance-is-started-with) says. | approved | |
| | RFS-4-C147 | docs/site/rootfs.md (fact, rootfs-default-home) | HOME is /tmp unless the image or the app sets it. | approved | |
| +| RFS-4-C155 | docs/site/rootfs.md (fact, rootfs-etc-hosts-1349) | /etc/hosts contains 127.0.0.1 localhost and ::1 localhost ip6-localhost ip6-loopback unless the image has a non-empty regular file there, or a link to one. carlos ship adds it when needed because Docker and Kubernetes write /etc/hosts when a container starts, and the read-only image prevents the app from creating it. | approved | |
| | RFS-4-C48 | docs/site/rootfs.md | Health | approved | |
| | RFS-4-C49 | docs/site/rootfs.md | web must answer GET / on CARLOS_SOCKET with a status below 500 within 120 seconds of starting. Until it does, the instance does not serve and its workers do not start. | approved | |
| | RFS-4-C50 | docs/site/rootfs.md | What it can write | approved | |
| @@ -605,6 +606,10 @@ brought carlos run and terminal sessions to rootfs instances, so the line |
| was wrong. RFS-4-C148 to C154 replace it with a Commands and consoles |
| section on the Rootfs page and a pointer in the Rails guide; Paul approved |
| all seven unchanged in his copy review on 2026-11-06. |
| +RFS-4-C155 (2026-11-22, #1349: carlos ship adds /etc/hosts) was approved |
| +by a Luna copy pass under Paul's delegation; its revision was applied, |
| +then ", or a link to one" added when Astra R1 kept a symlinked hosts |
| +file's target. |
| Recounted from the rows on 2026-10-30 (Plan 6 amendment, Astra R1 P2-7); |
| the earlier counts had not followed Plans 1 and 4's later rows. |
| @@ -612,7 +617,7 @@ the earlier counts had not followed Plans 1 and 4's later rows. |
| - Plan 1: 99 live, 0 struck, 99 rows |
| - Plan 2: 12 live, 0 struck, 12 rows |
| - Plan 3: 32 live, 3 struck, 35 rows |
| -- Plan 4: 150 live, 3 struck, 153 rows (RFS-4-C100 struck and RFS-4-C148 to C154 added on 2026-11-06) |
| +- Plan 4: 151 live, 3 struck, 154 rows (RFS-4-C100 struck and RFS-4-C148 to C154 added on 2026-11-06; RFS-4-C155 added on 2026-11-22) |
| - Plan 5: 44 live, 0 struck, 44 rows |
| - Plan 6: 75 live, 0 struck, 75 rows (RFS-6-C47 revised and RFS-6-C65 added and approved on 2026-10-30; RFS-6-C66 to C75 added and approved on 2026-10-31) |
| -- Total: 412 live, 6 struck, 418 rows |
| +- Total: 413 live, 6 struck, 419 rows |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 78c7ff501..77c114797 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -291,3 +291,4 @@ plan14-root-reads-console-addr-from-install.tsv f273b5cb7bbf37b6dca4bb964a9293cb |
| plan14-root-reads-instance-ready-before-wake.tsv aa953b1a43009c8208e44c67f5b8127ae1914b15d8b8822bcf16b19a6abdf121 |
| plan14-root-reads-media-results-cleanup.tsv 6921575b59564ed947e30c02e314dd429016c6c11a85db9b8af50b2f31d5a5db |
| plan14-root-reads-release-copy-across-consoles.tsv 892cf87db700600490e3ec9e944f9dbe7151e43bc5471090634f1735bcb9a7bd |
| +plan14-root-reads-rootfs-etc-hosts-1349.tsv 892cf87db700600490e3ec9e944f9dbe7151e43bc5471090634f1735bcb9a7bd |
diff --git a/internal/provision/testdata/plan14-root-reads-rootfs-etc-hosts-1349.tsv b/internal/provision/testdata/plan14-root-reads-rootfs-etc-hosts-1349.tsv| new file mode 100644 |
| index 000000000..1a9148827 |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-rootfs-etc-hosts-1349.tsv |
| @@ -0,0 +1,3 @@ |
| +# Census overlay rootfs-etc-hosts-1349, written by census-propose (#369). census-ruling-rootfs-etc-hosts-1349, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-rootfs-etc-hosts-1349.md. |
| +# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@coverage internal/rootfsimage 2ba2c948bcf471414042cd8c0a8ac110bb06291ec9d0d46f388d83a5fd781ac5 |
diff --git a/internal/rootfsimage/mounts.go b/internal/rootfsimage/mounts.go| index d5f1fcfd2..34d46cc3d 100644 |
| --- a/internal/rootfsimage/mounts.go |
| +++ b/internal/rootfsimage/mounts.go |
| @@ -16,8 +16,20 @@ var MountPoints = []string{"data", "run/carlos", "opt/carlos", "tmp", "dev", "pr |
| // onto each: root's resolver copy onto etc/resolv.conf. |
| var MountFiles = []string{"etc/resolv.conf"} |
| +// DefaultHosts is the /etc/hosts an image gets when it brings none (#1349). |
| +// Docker and Kubernetes write /etc/hosts into a container when it starts, |
| +// so an image's layers almost never carry one, and libraries that read it |
| +// (Ruby's Resolv, which Excon uses) fail without it. Only localhost: the |
| +// instance's hostname is the box's and is not known when the image is |
| +// built. |
| +const DefaultHosts = "127.0.0.1\tlocalhost\n::1\tlocalhost ip6-localhost ip6-loopback\n" |
| + |
| +// hostsFile is where DefaultHosts goes. |
| +const hostsFile = "etc/hosts" |
| + |
| // AddMountPoints adds what MountPoints and MountFiles name that the tree |
| -// does not have, and the parents they need. A new directory is 0:0 mode 0755 |
| +// does not have, and the parents they need, and DefaultHosts at etc/hosts |
| +// 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. |
| @@ -28,7 +40,9 @@ var MountFiles = []string{"etc/resolv.conf"} |
| // Lookup, not Resolve: a symlink at "tmp" would send the mount somewhere |
| // 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. |
| +// over it. An etc/hosts that is missing, empty (the placeholder a container |
| +// 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 { |
| @@ -51,7 +65,36 @@ func AddMountPoints(t *Tree) error { |
| return err |
| } |
| } |
| - return nil |
| + return addDefaultHosts(t, when) |
| +} |
| + |
| +// addDefaultHosts writes DefaultHosts at etc/hosts unless the tree has a |
| +// 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 |
| + } |
| + 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 |
| + } |
| + return t.add(&Entry{Path: hostsFile, Type: TypeFile, Mode: 0o644, ModTime: when, Size: int64(len(DefaultHosts)), src: src}) |
| } |
| // mountDir makes sure p and each of its parents is a directory of the tree. |
diff --git a/internal/rootfsimage/mounts_test.go b/internal/rootfsimage/mounts_test.go| index 53ed2ff08..5d176bc2f 100644 |
| --- a/internal/rootfsimage/mounts_test.go |
| +++ b/internal/rootfsimage/mounts_test.go |
| @@ -164,3 +164,142 @@ func TestAddMountPointsOnAnEmptyTreeUsesTheRootsTime(t *testing.T) { |
| t.Errorf("data mtime = %v, want the root's %v", e.ModTime, tr.Root.ModTime) |
| } |
| } |
| + |
| +// #1349: an image has no /etc/hosts of its own (Docker and Kubernetes write |
| +// one at start), so ship adds one naming localhost. |
| +const wantHosts = "127.0.0.1\tlocalhost\n::1\tlocalhost ip6-localhost ip6-loopback\n" |
| + |
| +func TestAddMountPointsAddsADefaultHostsFile(t *testing.T) { |
| + for _, c := range []struct { |
| + name string |
| + es []tEntry |
| + }{ |
| + {"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 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"}, |
| + }}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + tr := NewTree(t.TempDir()) |
| + apply(t, tr, layerTar(t, c.es...)) |
| + newest := tr.Newest() |
| + if err := AddMountPoints(tr); err != nil { |
| + t.Fatal(err) |
| + } |
| + h := mustLookup(t, tr, "etc/hosts") |
| + if h.Type != TypeFile || h.Mode != 0o644 || h.UID != 0 || h.GID != 0 || h.LinkTarget != "" || h.Size != int64(len(wantHosts)) || !h.ModTime.Equal(newest) { |
| + t.Errorf("etc/hosts = %+v, want a 0:0 0644 regular file of %d bytes dated %v", h, len(wantHosts), newest) |
| + } |
| + if got := readAll(t, tr, h); got != wantHosts { |
| + t.Errorf("etc/hosts holds %q, want %q", got, wantHosts) |
| + } |
| + if _, ok := tr.Lookup("etc/hosts/x"); ok { |
| + t.Error("the directory's contents stayed") |
| + } |
| + // A second call changes nothing. |
| + 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")) != wantHosts { |
| + t.Error("a second call changed the tree") |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestAddMountPointsKeepsTheImagesOwnHostsFile(t *testing.T) { |
| + tr := NewTree(t.TempDir()) |
| + apply(t, tr, layerTar(t, |
| + tEntry{name: "etc/", typ: tar.TypeDir, mode: 0o755}, |
| + tEntry{name: "etc/hosts", typ: tar.TypeReg, mode: 0o600, uid: 5, body: "127.0.0.1 localhost\n10.0.0.9 db\n"}, |
| + )) |
| + if err := AddMountPoints(tr); err != nil { |
| + t.Fatal(err) |
| + } |
| + h := mustLookup(t, tr, "etc/hosts") |
| + if got := readAll(t, tr, h); got != "127.0.0.1 localhost\n10.0.0.9 db\n" { |
| + t.Errorf("etc/hosts = %q, want the image's bytes", got) |
| + } |
| + if h.Mode != 0o600 || h.UID != 5 { |
| + t.Errorf("etc/hosts = %+v, want the image's mode and owner", h) |
| + } |
| +} |
| + |
| +// 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. |
| + tr := NewTree(t.TempDir()) |
| + apply(t, tr, layerTar(t, tEntry{name: "etc", typ: tar.TypeSymlink, link: "usr/etc"})) |
| + var me MountPointError |
| + if err := AddMountPoints(tr); !errors.As(err, &me) || me != (MountPointError{"etc", "symlink"}) { |
| + t.Errorf("err = %v, want the etc symlink refused", err) |
| + } |
| +} |
diff --git a/ship_rootfs_test.go b/ship_rootfs_test.go| index 1d1458d04..822ebc5bd 100644 |
| --- a/ship_rootfs_test.go |
| +++ b/ship_rootfs_test.go |
| @@ -256,11 +256,15 @@ func TestShipRootfsDirectFromADirectory(t *testing.T) { |
| } |
| want := append([]string{"rails/bin/rails", "rails/config/application.rb", "usr/bin/ruby"}, rootfsimage.MountPoints...) |
| want = append(want, rootfsimage.MountFiles...) |
| + want = append(want, "etc/hosts") |
| for _, p := range want { |
| if _, err := fs.Stat(sb, p); err != nil { |
| t.Errorf("image lacks %s: %v", p, err) |
| } |
| } |
| + if hosts, err := fs.ReadFile(sb, "etc/hosts"); err != nil || string(hosts) != rootfsimage.DefaultHosts { |
| + t.Errorf("etc/hosts in the image: %v, %q", err, hosts) |
| + } |
| body, err := fs.ReadFile(sb, "rails/bin/rails") |
| if err != nil || !bytes.Equal(body, rootfsTestELF(elf.EM_AARCH64)) { |
| t.Errorf("rails/bin/rails in the image: %v, %q", err, body) |