[PATCH v2 0/1] Allow setting hostname before userspace starts

Dan Moulding posted 1 patch 4 years ago
Documentation/admin-guide/kernel-parameters.txt | 13 +++++++++++++
init/version.c                                  | 17 +++++++++++++++++
2 files changed, 30 insertions(+)
[PATCH v2 0/1] Allow setting hostname before userspace starts
Posted by Dan Moulding 4 years ago
Some userspace processes may rely on gethostname to always return the
correct machine name. However, the only way that the hostname may be
set is by some other userspace process calling sethostname
first. During boot, if a process that depends on gethostname runs
before sethostname has been called, then the process that called
gethostname is going to get an incorrect result.

A real-world case where this comes up is with mdadm, which if
gethostname returns the wrong name, can cause local md-raid arrays to
appear to be foreign arrays. This can alter how mdadm assembles the
array, or can even cause array assembly to fail. I imagine there are
probbaly other real-world cases where undesirable behavior results when
the hostname is not set early enough.

I'm proposing adding the option to set the hostname from a kernel
parameter, so that the correct host name can be guaranteed to be set
before any userspace process can call gethostname.

I can imagine an even better way to do this would be to have the
hostname written to some non-volatile storage (like a firmware NVRAM
variable or such), which the kernel could read out during early
boot. But, alas, such designs require hardware support, standards, and
cooperation. This proposal is an alternative that can provide a simple
and immediate solution.

v2:
* Use strlcpy instead of strncpy to eliminate W=1 compiler warning
  (assuaging it from its string truncation fears). Thanks to kernel
  test robot for finding this.
* Move "hostname" after "hlt" in kernel-parameters.txt (I promise I
  know my ABCs).

Dan Moulding (1):
  init: Add "hostname" kernel parameter

 Documentation/admin-guide/kernel-parameters.txt | 13 +++++++++++++
 init/version.c                                  | 17 +++++++++++++++++
 2 files changed, 30 insertions(+)

-- 
2.35.1
Re: [PATCH v2 0/1] Allow setting hostname before userspace starts
Posted by Pavel Machek 4 years ago
Hi1

> Some userspace processes may rely on gethostname to always return the
> correct machine name. However, the only way that the hostname may be
> set is by some other userspace process calling sethostname
> first. During boot, if a process that depends on gethostname runs
> before sethostname has been called, then the process that called
> gethostname is going to get an incorrect result.

Don't do that, then? :-).

init gets passed command line parameters kernel did not use. So init
can do that easily. Plus, there's initrd.

Best regards,
								Pavel
-- 
People of Russia, stop Putin before his war on Ukraine escalates.
Re: [PATCH v2 0/1] Allow setting hostname before userspace starts
Posted by Dan Moulding 4 years ago
On May 6, 2022, at 1:23 AM, Pavel Machek <pavel@ucw.cz> wrote:

> init gets passed command line parameters kernel did not use. So init
> can do that easily.

I agree this is a problem that *can* be delegated to init to solve.
But is it *better* to do that? Why have the kernel pass the the hostname
to the init system, just so that the init system has to pass it back to the
kernel later?

> Plus, there's initrd.

This to me is a reason why it's better for the kernel to do this. If left up
to the init system, it must be sure it gets done correctly both when an
initramfs is used, and when one is not.

P.S. Apologies to anyone who received this twice, my first reply was
bounced by the list servers.

Cheers,

  -- Dan