]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: allow overriding the system hostname with systemd.hostname= on the kernel comma...
authorLennart Poettering <lennart@poettering.net>
Thu, 14 May 2020 09:01:31 +0000 (11:01 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 18 May 2020 18:20:50 +0000 (20:20 +0200)
man/kernel-command-line.xml
src/core/hostname-setup.c

index fd49b65a8d43ef050d89390052e75a5e78985707..0f2972a0b6840346228a56198f3e8d6b8d85eddc 100644 (file)
         system clock to. The system time is set to the specified timestamp early during
         boot. It is not propagated to the hardware clock (RTC).</para></listitem>
       </varlistentry>
+
+      <varlistentry>
+        <term><varname>systemd.hostname=</varname></term>
+
+        <listitem><para>Accepts a hostname to set during early boot. If specified takes precedence over what
+        is set in <filename>/etc/hostname</filename>. Note that this does not bar later runtime changes to
+        the hostname, it simply controls the initial hostname set during early boot.</para></listitem>
+      </varlistentry>
     </variablelist>
 
   </refsect1>
index 83cce88131eb656396c2ef86216060ce600b68db..6d047db8388c8e94b3947f75e8c1a4fac35e6ba1 100644 (file)
 #include "hostname-util.h"
 #include "log.h"
 #include "macro.h"
+#include "proc-cmdline.h"
 #include "string-util.h"
 #include "util.h"
 
 int hostname_setup(void) {
         _cleanup_free_ char *b = NULL;
+        const char *hn = NULL;
         bool enoent = false;
-        const char *hn;
         int r;
 
-        r = read_etc_hostname(NULL, &b);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        enoent = true;
-                else
-                        log_warning_errno(r, "Failed to read configured hostname: %m");
+        r = proc_cmdline_get_key("systemd.hostname", 0, &b);
+        if (r < 0)
+                log_warning_errno(r, "Failed to retrieve system hostname from kernel command line, ignoring: %m");
+        else if (r > 0) {
+                if (hostname_is_valid(b, true))
+                        hn = b;
+                else  {
+                        log_warning("Hostname specified on kernel command line is invalid, ignoring: %s", b);
+                        b = mfree(b);
+                }
+        }
 
-                hn = NULL;
-        } else
-                hn = b;
+        if (!hn) {
+                r = read_etc_hostname(NULL, &b);
+                if (r < 0) {
+                        if (r == -ENOENT)
+                                enoent = true;
+                        else
+                                log_warning_errno(r, "Failed to read configured hostname: %m");
+                } else
+                        hn = b;
+        }
 
         if (isempty(hn)) {
-                /* Don't override the hostname if it is already set
-                 * and not explicitly configured */
+                /* Don't override the hostname if it is already set and not explicitly configured */
                 if (hostname_is_set())
                         return 0;