Repository navigation
feat(linux): Pass the container's host name in ip= - #1235
Conversation
✅ Deploy Preview for urunc canceled.
|
cf98594 to
e823ea4
Compare
cmainas
left a comment
There was a problem hiding this comment.
Thank you @ananos for the PR.
The fix works well for nerdct/docker/ctr, but in K8s containerd has the weird behavior of setting the hostname field of the spec only in the pause container. Therefore, we will need to read /etc/hostname to get it in the Kubernetes path. We can do that in a followup or please integrate it with the other changes. There is also a small spelling issue.
| // container's, or urunc when it is unset or cannot be passed there. ':' and | ||
| // blanks would split the argument, and the kernel holds at most 64 bytes. | ||
| func kernelHostname(name string) string { | ||
| if name == "" || len(name) > maxHostnameLen || strings.ContainsAny(name, ": \t\n") { |
There was a problem hiding this comment.
We should have a stricter filter for the characters in hostname. The Linux kernel also uses the characters "\v, \f, \r" and 0xA0 to split the kernel boot arguments, so we can do the same. Also, since ip= uses . to split parameters, we should also reject .. The easiest thing to do would be to create a regexp with all the accepted characters as we do with annotations. For example, ^[A-Za-z0-9-]{1,64}$
| // blanks would split the argument, and the kernel holds at most 64 bytes. | ||
| func kernelHostname(name string) string { | ||
| if name == "" || len(name) > maxHostnameLen || strings.ContainsAny(name, ": \t\n") { | ||
| return defaultHostname |
There was a problem hiding this comment.
nit: We should log here that the hostname was rejected.
e823ea4 to
86d80c8
Compare
The kernel sets its host name from the fifth field of ip=, which was always "urunc". Fill it with the container's host name instead: - the OCI spec's hostname, or, when it is empty, the first line of the file mounted at /etc/hostname. containerd sets the spec's hostname for a Kubernetes pause container only, so an application container carries its pod's name in that file. - a name of letters, digits and '-', up to the kernel's 64 bytes. ip= ends the host name at a '.', and the kernel splits its command line on blanks, \v, \f, \r and 0xA0. Any other name is logged and the guest keeps "urunc". PR: #1235 Signed-off-by: Anastassios Nanos <ananos@nubificus.co.uk> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
86d80c8 to
79a22c9
Compare
Description
The guest kernel takes its host name from the fifth field of
ip=(net/ipv4/ipconfig.c), and urunc always writesuruncthere. So every Linux guest calls itselfurunc: a shell prompt readsroot@urunc, and so does the console of every pod on k8s.This patch fills that field with the container's host name instead:
hostname: with nerdctl or docker that is--hostname, or the container ID's first 12 characters by default.hostnamefor the pause container only, the first line of the file mounted at/etc/hostname, which holds the pod's name.A name must match
^[A-Za-z0-9-]{1,64}$:ip=ends the host name at a., and the kernel splits its command line on blanks,\v,\f,\rand 0xA0. Any other name is logged as a warning, and the guest keepsurunc.Related issues
hull run --hostname, reported inhull version --jsonfeat/linux-hvi-v050, which hull pinssethostname()from/urunc-hostnameHow was this tested?
TestLinuxCommandStringHostname(the filter, incl..,\v,\r, 0xA0),TestGuestHostname(spec hostname, the/etc/hostnamemount, first line only, a missing file, an invalid name) andTestGuestHostnameLogsARejectedName.go test ./pkg/unikontainers/...,go build ./...andgo vetpass on Linux amd64.make lintimage, run without-it): 0 issues. cspell with.github/linters/.cspell.json: 0 issues.brig run claude@refactorboots withip=10.4.2.2::10.4.2.1:255.255.255.0:claude-code-refactor-20d51f16:eth0:off, anduname -nand/etc/hostnamein the guest both readclaude-code-refactor-20d51f16.make test_ctr,make test_nerdctl, ...) were not run locally; CI ran them on the previous revision. The Kubernetes path is covered by the unit tests above, not by a pod booted on a cluster.LLM usage
Claude Opus 5.5 (Claude Code) wrote the patch and the unit test, and ran the checks above.
Checklist
make lint).make test_ctr,make test_nerdctl,make test_docker,make test_crictl).