Skip to content

Guest hostname handling improvements (with override)#48

Merged
jingkaihe merged 10 commits intojingkaihe:mainfrom
comunidadio:feat/id-hostname
Feb 17, 2026
Merged

Guest hostname handling improvements (with override)#48
jingkaihe merged 10 commits intojingkaihe:mainfrom
comunidadio:feat/id-hostname

Conversation

@comunidadio
Copy link
Copy Markdown
Contributor

@comunidadio comunidadio commented Feb 17, 2026

This PR improves guest hostname handling by:

  • defaulting guest hostname to vm's id instead of "matchlock" for all
  • setting hostname via kernel arg so it's set before userspace and guest-init
  • having guest-init also write /etc/hostname to improve compatibility with tools that expect /etc/hostname to match kernel's hostname
  • allowing hostname override via Config or MATCHLOCK_HOSTNAME

Without override:

➜  matchlock git:(feat/id-hostname) ✗ ./bin/matchlock run --image alpine:latest --rm -it sh
~ # hostname
vm-95c743d6
~ # cat /etc/hostname
vm-95c743d6
~ # exit

With override:

➜  matchlock git:(feat/id-hostname) ✗ MATCHLOCK_HOSTNAME=something.internal ./bin/matchlock run --image alpine:latest --rm -it sh
~ # hostname
something.internal
~ # cat /etc/hostname
something.internal
~ # exit

Set hostname before user-space via kernel arg and write to /etc/hostname for wider compatibility.
Allow override via VMConfig or MATCHLOCK_HOSTNAME.
@jingkaihe
Copy link
Copy Markdown
Owner

Thanks for contiributing. Can we do --name or --hostname in run command, as the env var sometimes is not the most obvious thing?
Also /etc/hosts injection will also be nice (it was on my list but didn't get the chance to do it)

@comunidadio
Copy link
Copy Markdown
Contributor Author

comunidadio commented Feb 17, 2026

Makes sense, removed MATCHLOCK_HOSTNAME override, exposing --hostname instead.

Good idea re: /etc/hosts!

For /etc/hosts injection are you comfortable adding a dependency such as https://github.com/txn2/txeh ?
Seems to me it would be the simplest and safest way to precisely inject our guest hostname on localhost lines regardless of how an OCI image organizes its /etc/hosts, without having to handle all the intricacies of the format ourselves.
And that may be better done in another PR probably.

@jingkaihe
Copy link
Copy Markdown
Owner

I'd prefer avoiding dependencies and just write lines into the /etc/hosts, at least to begin with.

Typically OCI images do not have /etc/hosts as they are typically mounted, therefore I believe it is fairly safe to override it manually.

@comunidadio
Copy link
Copy Markdown
Contributor Author

Fair enough, if we're not adding a dependency I'll just roll it into this PR shortly.

@comunidadio
Copy link
Copy Markdown
Contributor Author

Done!

➜  matchlock git:(feat/id-hostname) ✗ ./bin/matchlock run --image alpine:latest --hostname override.internal --rm -it sh
~ # cat /etc/hosts
127.0.0.1       localhost localhost.localdomain override.internal
::1             localhost localhost.localdomain override.internal
~ # ping override.internal
PING override.internal (::1): 56 data bytes
64 bytes from ::1: seq=0 ttl=64 time=0.081 ms

@comunidadio
Copy link
Copy Markdown
Contributor Author

I'll add support for adding the file from scratch as well on images that do not have /etc/hosts at all (eg. ubuntu:latest)

…iguration

Signed-off-by: Jingkai He <jingkai@hey.com>
@comunidadio
Copy link
Copy Markdown
Contributor Author

comunidadio commented Feb 17, 2026

Done!
Interestingly in the case of ubuntu:latest, there is a /etc/hosts file but it's empty.
We handle now all cases I can think of (no file, empty file, standard file, custom-built file)

➜  matchlock git:(feat/id-hostname) ✗ ./bin/matchlock run --image ubuntu:latest --hostname override.internal --rm -it sh
# cat /etc/hosts
127.0.0.1       localhost localhost.localdomain override.internal
::1             localhost localhost.localdomain override.internal
#

…me feature

Signed-off-by: Jingkai He <jingkai@hey.com>
@jingkaihe jingkaihe merged commit ccce106 into jingkaihe:main Feb 17, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants