Use paths specified from environment variables for /etc configuration files

Some configuration files that need updates are directly under in /etc. To
update them atomically, we need write access to /etc. For Ubuntu Core this is
an issue as /etc is not writable. Only a selection of subdirectories can be
writable. The general solution is symlinks or bind mounts to writable places.
But for atomic writes in /etc, that does not work. So Ubuntu has had a patch
for that that did not age well.

Instead we would like to introduce some environment variables for alternate
paths.

 * SYSTEMD_ETC_HOSTNAME: /etc/hostname
 * SYSTEMD_ETC_MACHINE_INFO: /etc/machine-info
 * SYSTEMD_ETC_LOCALTIME: /etc/localtime
 * SYSTEMD_ETC_LOCALE_CONF: /etc/locale.conf
 * SYSTEMD_ETC_VCONSOLE_CONF: /etc/vconsole.conf
 * SYSTEMD_ETC_ADJTIME: /etc/adjtime

While it is for now expected that there is a symlink from the standard, we
still try to read them from that alternate path. This is important for
`/etc/localtime`, which is a symlink, so we cannot have an indirect symlink or
bind mount for it.

Since machine-id is typically written only once and not updated. This commit
does not cover it. An initrd can properly create it and bind mount it.
This commit is contained in:
Valentin David
2025-06-23 15:32:11 +02:00
committed by Lennart Poettering
parent fa34fda2eb
commit 0dc39dffbd
19 changed files with 376 additions and 48 deletions
+10 -10
View File
@@ -127,7 +127,7 @@ static void context_read_etc_hostname(Context *c) {
assert(c);
if (stat("/etc/hostname", &current_stat) >= 0 &&
if (stat(etc_hostname(), &current_stat) >= 0 &&
stat_inode_unmodified(&c->etc_hostname_stat, &current_stat))
return;
@@ -160,7 +160,7 @@ static void context_read_machine_info(Context *c) {
assert(c);
if (stat("/etc/machine-info", &current_stat) >= 0 &&
if (stat(etc_machine_info(), &current_stat) >= 0 &&
stat_inode_unmodified(&c->etc_machine_info_stat, &current_stat))
return;
@@ -175,7 +175,7 @@ static void context_read_machine_info(Context *c) {
(UINT64_C(1) << PROP_HARDWARE_SKU) |
(UINT64_C(1) << PROP_HARDWARE_VERSION));
r = parse_env_file(NULL, "/etc/machine-info",
r = parse_env_file(NULL, etc_machine_info(),
"PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
"ICON_NAME", &c->data[PROP_ICON_NAME],
"CHASSIS", &c->data[PROP_CHASSIS],
@@ -806,14 +806,14 @@ static int context_write_data_static_hostname(Context *c) {
s = &c->etc_hostname_stat;
if (isempty(c->data[PROP_STATIC_HOSTNAME])) {
if (unlink("/etc/hostname") < 0 && errno != ENOENT)
if (unlink(etc_hostname()) < 0 && errno != ENOENT)
return -errno;
TAKE_PTR(s);
return 0;
}
r = write_string_file("/etc/hostname", c->data[PROP_STATIC_HOSTNAME], WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
r = write_string_file(etc_hostname(), c->data[PROP_STATIC_HOSTNAME], WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
if (r < 0)
return r;
@@ -839,7 +839,7 @@ static int context_write_data_machine_info(Context *c) {
* already, even if we can't make it hit the disk. */
s = &c->etc_machine_info_stat;
r = load_env_file(NULL, "/etc/machine-info", &l);
r = load_env_file(NULL, etc_machine_info(), &l);
if (r < 0 && r != -ENOENT)
return r;
@@ -852,7 +852,7 @@ static int context_write_data_machine_info(Context *c) {
}
if (strv_isempty(l)) {
if (unlink("/etc/machine-info") < 0 && errno != ENOENT)
if (unlink(etc_machine_info()) < 0 && errno != ENOENT)
return -errno;
TAKE_PTR(s);
@@ -861,8 +861,8 @@ static int context_write_data_machine_info(Context *c) {
r = write_env_file(
AT_FDCWD,
"/etc/machine-info",
/* headers= */ NULL,
etc_machine_info(),
/* headers= */NULL,
l,
WRITE_ENV_FILE_LABEL);
if (r < 0)
@@ -1687,7 +1687,7 @@ static int build_describe_response(Context *c, bool privileged, sd_json_variant
(void) vsock_get_local_cid(&local_cid);
(void) load_os_release_pairs(/* root= */ NULL, &os_release_pairs);
(void) load_env_file_pairs(/* f=*/ NULL, "/etc/machine-info", &machine_info_pairs);
(void) load_env_file_pairs(/* f=*/ NULL, etc_machine_info(), &machine_info_pairs);
r = sd_json_buildo(
&v,