mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-14 15:54:15 +08:00
5a704629f2
The gethostname system call returns the hostname for the current machine. However, the kernel has no mechanism to initially set the current machine's name in such a way as to guarantee that the first userspace process to call gethostname will receive a meaningful result. It relies on some unspecified userspace process to first call sethostname before gethostname can produce a meaningful name. Traditionally the machine's hostname is set from userspace by the init system. The init system, in turn, often relies on a configuration file (say, /etc/hostname) to provide the value that it will supply in the call to sethostname. Consequently, the file system containing /etc/hostname usually must be available before the hostname will be set. There may, however, be earlier userspace processes that could call gethostname before the file system containing /etc/hostname is mounted. Such a process will get some other, likely meaningless, name from gethostname (such as "(none)", "localhost", or "darkstar"). A real-world example where this can happen, and lead to undesirable results, is with mdadm. When assembling arrays, mdadm distinguishes between "local" arrays and "foreign" arrays. A local array is one that properly belongs to the current machine, and a foreign array is one that is (possibly temporarily) attached to the current machine, but properly belongs to some other machine. To determine if an array is local or foreign, mdadm may compare the "homehost" recorded on the array with the current hostname. If mdadm is run before the root file system is mounted, perhaps because the root file system itself resides on an md-raid array, then /etc/hostname isn't yet available and the init system will not yet have called sethostname, causing mdadm to incorrectly conclude that all of the local arrays are foreign. Solving this problem *could* be delegated to the init system. It could be left up to the init system (including any init system that starts within an initramfs, if one is in use) to ensure that sethostname is called before any other userspace process could possibly call gethostname. However, it may not always be obvious which processes could call gethostname (for example, udev itself might not call gethostname, but it could via udev rules invoke processes that do). Additionally, the init system has to ensure that the hostname configuration value is stored in some place where it will be readily accessible during early boot. Unfortunately, every init system will attempt to (or has already attempted to) solve this problem in a different, possibly incorrect, way. This makes getting consistently working configurations harder for users. I believe it is better for the kernel to provide the means by which the hostname may be set early, rather than making this a problem for the init system to solve. The option to set the hostname during early startup, via a kernel parameter, provides a simple, reliable way to solve this problem. It also could make system configuration easier for some embedded systems. [dmoulding@me.com: v2] Link: https://lkml.kernel.org/r/20220506060310.7495-2-dmoulding@me.com Link: https://lkml.kernel.org/r/20220505180651.22849-2-dmoulding@me.com Signed-off-by: Dan Moulding <dmoulding@me.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Jonathan Corbet <corbet@lwn.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
67 lines
1.6 KiB
C
67 lines
1.6 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* linux/init/version.c
|
|
*
|
|
* Copyright (C) 1992 Theodore Ts'o
|
|
*
|
|
* May be freely distributed as part of Linux.
|
|
*/
|
|
|
|
#include <generated/compile.h>
|
|
#include <linux/build-salt.h>
|
|
#include <linux/elfnote-lto.h>
|
|
#include <linux/export.h>
|
|
#include <linux/init.h>
|
|
#include <linux/printk.h>
|
|
#include <linux/uts.h>
|
|
#include <linux/utsname.h>
|
|
#include <generated/utsrelease.h>
|
|
#include <linux/version.h>
|
|
#include <linux/proc_ns.h>
|
|
|
|
struct uts_namespace init_uts_ns = {
|
|
.ns.count = REFCOUNT_INIT(2),
|
|
.name = {
|
|
.sysname = UTS_SYSNAME,
|
|
.nodename = UTS_NODENAME,
|
|
.release = UTS_RELEASE,
|
|
.version = UTS_VERSION,
|
|
.machine = UTS_MACHINE,
|
|
.domainname = UTS_DOMAINNAME,
|
|
},
|
|
.user_ns = &init_user_ns,
|
|
.ns.inum = PROC_UTS_INIT_INO,
|
|
#ifdef CONFIG_UTS_NS
|
|
.ns.ops = &utsns_operations,
|
|
#endif
|
|
};
|
|
EXPORT_SYMBOL_GPL(init_uts_ns);
|
|
|
|
static int __init early_hostname(char *arg)
|
|
{
|
|
size_t bufsize = sizeof(init_uts_ns.name.nodename);
|
|
size_t maxlen = bufsize - 1;
|
|
size_t arglen;
|
|
|
|
arglen = strlcpy(init_uts_ns.name.nodename, arg, bufsize);
|
|
if (arglen > maxlen) {
|
|
pr_warn("hostname parameter exceeds %zd characters and will be truncated",
|
|
maxlen);
|
|
}
|
|
return 0;
|
|
}
|
|
early_param("hostname", early_hostname);
|
|
|
|
/* FIXED STRINGS! Don't touch! */
|
|
const char linux_banner[] =
|
|
"Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@"
|
|
LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\n";
|
|
|
|
const char linux_proc_banner[] =
|
|
"%s version %s"
|
|
" (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ")"
|
|
" (" LINUX_COMPILER ") %s\n";
|
|
|
|
BUILD_SALT;
|
|
BUILD_LTO_INFO;
|