]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-setup: read hostname from system.hostname credential
authorIvan Shapovalov <intelfx@intelfx.name>
Sun, 7 Jan 2024 02:01:28 +0000 (03:01 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 23 Jan 2024 22:03:06 +0000 (22:03 +0000)
`system.hostname` credential is treated similarly to the pre-existing
`system.machine_id` credential. It is considered after /etc/hostname,
but prior to the kernel defaults or os-release defaults.

Fixes #30667.

Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name>
man/systemd.system-credentials.xml
src/shared/hostname-setup.c

index c1c8e97f0c33ca59af06aa0e767e3d95379d2dfa..adc0052456e2511b1378b4d775065e50b7670126 100644 (file)
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><varname>system.hostname</varname></term>
+        <listitem>
+          <para>Accepts a (transient) hostname to configure during early boot. The static hostname specified
+            in <filename>/etc/hostname</filename>, if configured, takes precedence over this setting.
+            Interpreted by the service manager (PID 1). For details see
+            <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>.</para>
+
+          <xi:include href="version-info.xml" xpointer="v254"/>
+        </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><varname>home.create.*</varname></term>
         <listitem>
index 137c29aef5c44e3565dd875632b2caacd5ec6abb..c538506eaf616e626284884bdd58584c77a05e32 100644 (file)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "creds-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "fs-util.h"
@@ -71,6 +72,26 @@ int shorten_overlong(const char *s, char **ret) {
         return 1;
 }
 
+static int acquire_hostname_from_credential(char **ret) {
+        _cleanup_free_ char *cred = NULL;
+        int r;
+
+        assert(ret);
+
+        r = read_credential_with_decryption("system.hostname", (void **) &cred, /* ret_size= */ NULL);
+        if (r < 0)
+                return log_warning_errno(r, "Failed to read system.hostname credential, ignoring: %m");
+        if (r == 0) /* not found */
+                return -ENXIO;
+
+        if (!hostname_is_valid(cred, VALID_HOSTNAME_TRAILING_DOT)) /* check that the hostname we return is valid */
+                return log_warning_errno(SYNTHETIC_ERRNO(EBADMSG), "Hostname specified in system.hostname credential is invalid, ignoring: %s", cred);
+
+        log_info("Initializing hostname from credential.");
+        *ret = TAKE_PTR(cred);
+        return 0;
+}
+
 int read_etc_hostname_stream(FILE *f, char **ret) {
         int r;
 
@@ -164,6 +185,14 @@ int hostname_setup(bool really) {
                 }
         }
 
+        if (!hn) {
+                r = acquire_hostname_from_credential(&b);
+                if (r >= 0) {
+                        hn = b;
+                        source = HOSTNAME_TRANSIENT;
+                }
+        }
+
         if (!hn) {
                 _cleanup_free_ char *buf = NULL;