-
Notifications
You must be signed in to change notification settings - Fork 1
/
0014-Set-a-default-unique-hostname-when-it-is-either-clr-.patch
80 lines (75 loc) · 2.73 KB
/
0014-Set-a-default-unique-hostname-when-it-is-either-clr-.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
From 8280f2efacd6ad9d43a5aa9adcc3e513be972944 Mon Sep 17 00:00:00 2001
From: Ikey Doherty <[email protected]>
Date: Thu, 14 Apr 2016 19:15:46 +0100
Subject: [PATCH 14/38] Set a default, unique, hostname, when it is either
'clr' or 'localhost'
Signed-off-by: Ikey Doherty <[email protected]>
---
src/core/main.c | 1 +
src/shared/hostname-setup.c | 37 +++++++++++++++++++++++++++++++++++++
src/shared/hostname-setup.h | 1 +
3 files changed, 39 insertions(+)
diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c
index 0fac0ecab7..d9b5529c2f 100644
--- a/src/shared/hostname-setup.c
+++ b/src/shared/hostname-setup.c
@@ -220,3 +220,40 @@ static const char* const hostname_source_table[] = {
};
DEFINE_STRING_TABLE_LOOKUP(hostname_source, HostnameSource);
+
+int set_first_hostname(void)
+{
+ char h[HOST_NAME_MAX+1];
+ h[HOST_NAME_MAX] = '\0';
+ _cleanup_free_ char *n_host = NULL;
+ _cleanup_free_ char *mid = NULL;
+
+ if (access("/etc/hostname", F_OK) == 0) {
+ return 1;
+ }
+
+ if (gethostname(h, HOST_NAME_MAX) != 0) {
+ return 0;
+ }
+
+ if (!streq(h, "clr") && !streq(h, "localhost")) {
+ return 0;
+ }
+
+ if (read_etc_hostname("/etc/machine-id", &mid) != 0) {
+ /* First boot, use transient machine-id until synced */
+ if (read_etc_hostname("/run/machine-id", &mid) != 0) {
+ return 0;
+ }
+ }
+
+ if (!asprintf(&n_host, "clr-%s", mid)) {
+ return 0;
+ }
+
+ if (sethostname_idempotent(n_host) != 0) {
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/src/shared/hostname-setup.h b/src/shared/hostname-setup.h
index 6def36c350..c9d880166e 100644
--- a/src/shared/hostname-setup.h
+++ b/src/shared/hostname-setup.h
@@ -23,3 +23,4 @@ int read_etc_hostname(const char *path, char **ret);
void hostname_update_source_hint(const char *hostname, HostnameSource source);
int hostname_setup(bool really);
+int set_first_hostname(void);
--
2.36.1
--- systemd-stable-255.2/src/core/main.c~ 2023-12-24 09:00:51.000000000 +0000
+++ systemd-stable-255.2/src/core/main.c 2024-01-02 15:32:38.681980582 +0000
@@ -2236,6 +2236,7 @@
(void) hostname_setup(true);
/* Force transient machine-id on first boot. */
machine_id_setup(/* root= */ NULL, /* force_transient= */ first_boot, arg_machine_id, /* ret_machine_id */ NULL);
+ set_first_hostname();
(void) loopback_setup();
bump_unix_max_dgram_qlen();
bump_file_max_and_nr_open();