Zbigniew Jędrzejewski-Szmek 89715a
From 93651582aef1ee626dc6f8d032195acd73bc9372 Mon Sep 17 00:00:00 2001
Zbigniew Jędrzejewski-Szmek 89715a
From: Jonathan Lebon <jonathan@jlebon.com>
Zbigniew Jędrzejewski-Szmek 89715a
Date: Mon, 23 Mar 2020 12:25:19 -0400
Zbigniew Jędrzejewski-Szmek 89715a
Subject: [PATCH] manager: optionally, do a full preset on first boot
Zbigniew Jędrzejewski-Szmek 89715a
MIME-Version: 1.0
Zbigniew Jędrzejewski-Szmek 89715a
Content-Type: text/plain; charset=UTF-8
Zbigniew Jędrzejewski-Szmek 89715a
Content-Transfer-Encoding: 8bit
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
A compile time option is added to select behaviour: by default
Zbigniew Jędrzejewski-Szmek 89715a
UNIT_FILE_PRESET_ENABLE_ONLY is still used, but the intent is to change to
Zbigniew Jędrzejewski-Szmek 89715a
UNIT_FILE_PRESET_FULL at some point in the future. Distros that want to
Zbigniew Jędrzejewski-Szmek 89715a
opt-in can use the config option to change the behaviour.
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
(The option is just a boolean: it would be possible to make it multi-valued,
Zbigniew Jędrzejewski-Szmek 89715a
and allow full, enable-only, disable-only, none. But so far nobody has asked
Zbigniew Jędrzejewski-Szmek 89715a
for this, and it's better not to complicate things needlessly.)
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
With the configuration option flipped, instead of only doing enablements,
Zbigniew Jędrzejewski-Szmek 89715a
perform a full preset on first boot. The reason is that although
Zbigniew Jędrzejewski-Szmek 89715a
`/etc/machine-id` might be missing, there may be other files provisioned in
Zbigniew Jędrzejewski-Szmek 89715a
`/etc` (in fact, this use case is mentioned in `log_execution_mode`). Some of
Zbigniew Jędrzejewski-Szmek 89715a
those possible files include enablement symlinks even if presets dictate it
Zbigniew Jędrzejewski-Szmek 89715a
should be disabled.
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
Such a seemingly contradictory situation occurs in {RHEL,Fedora} CoreOS,
Zbigniew Jędrzejewski-Szmek 89715a
where we ship `/etc` as if `preset-all` were called. However, we want to
Zbigniew Jędrzejewski-Szmek 89715a
allow users to disable default-enabled services via Ignition, which does
Zbigniew Jędrzejewski-Szmek 89715a
this by creating preset dropins before switchroot. (For why we do
Zbigniew Jędrzejewski-Szmek 89715a
`preset-all` at compose time, see:
Zbigniew Jędrzejewski-Szmek 89715a
https://github.com/coreos/fedora-coreos-config/pull/77).
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
For example, the composed FCOS image has a `enable zincati.service`
Zbigniew Jędrzejewski-Szmek 89715a
preset and an enablement for that in `/etc`, while at boot time when we
Zbigniew Jędrzejewski-Szmek 89715a
switch root, there may be a `disable zincati.service` preset with higher
Zbigniew Jędrzejewski-Szmek 89715a
precedence. In that case, we want systemd to disable the service.
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
This is essentially a revert of 304b3079a203. It seems like systemd
Zbigniew Jędrzejewski-Szmek 89715a
*used* to do this, but it was changed to try to make the container
Zbigniew Jędrzejewski-Szmek 89715a
workflow a bit faster.
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
Resolves: https://github.com/coreos/fedora-coreos-tracker/issues/392
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
Co-authored-by: Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Zbigniew Jędrzejewski-Szmek 89715a
---
Zbigniew Jędrzejewski-Szmek 89715a
 meson.build        | 3 +++
Zbigniew Jędrzejewski-Szmek 89715a
 meson_options.txt  | 2 ++
Zbigniew Jędrzejewski-Szmek 89715a
 src/core/manager.c | 4 +++-
Zbigniew Jędrzejewski-Szmek 89715a
 3 files changed, 8 insertions(+), 1 deletion(-)
Zbigniew Jędrzejewski-Szmek 89715a
Zbigniew Jędrzejewski-Szmek 89715a
diff --git a/meson.build b/meson.build
Zbigniew Jędrzejewski-Szmek 89715a
index 582e33c9a73d..72e586aa97c7 100644
Zbigniew Jędrzejewski-Szmek 89715a
--- a/meson.build
Zbigniew Jędrzejewski-Szmek 89715a
+++ b/meson.build
Zbigniew Jędrzejewski-Szmek 89715a
@@ -285,6 +285,8 @@ conf.set10('MEMORY_ACCOUNTING_DEFAULT',                       memory_accounting_
Zbigniew Jędrzejewski-Szmek 89715a
 conf.set('STATUS_UNIT_FORMAT_DEFAULT',                        'STATUS_UNIT_FORMAT_' + status_unit_format_default.to_upper())
Zbigniew Jędrzejewski-Szmek 89715a
 conf.set_quoted('STATUS_UNIT_FORMAT_DEFAULT_STR',             status_unit_format_default)
Zbigniew Jędrzejewski-Szmek 89715a
 
Zbigniew Jędrzejewski-Szmek 89715a
+conf.set10('FIRST_BOOT_FULL_PRESET',                          get_option('first-boot-full-preset'))
Zbigniew Jędrzejewski-Szmek 89715a
+
Zbigniew Jędrzejewski-Szmek 89715a
 #####################################################################
Zbigniew Jędrzejewski-Szmek 89715a
 
Zbigniew Jędrzejewski-Szmek 89715a
 cc = meson.get_compiler('c')
Zbigniew Jędrzejewski-Szmek 89715a
@@ -4271,6 +4273,7 @@ foreach tuple : [
Zbigniew Jędrzejewski-Szmek 89715a
         ['link-networkd-shared',  get_option('link-networkd-shared')],
Zbigniew Jędrzejewski-Szmek 89715a
         ['link-timesyncd-shared', get_option('link-timesyncd-shared')],
Zbigniew Jędrzejewski-Szmek 89715a
         ['link-boot-shared',      get_option('link-boot-shared')],
Zbigniew Jędrzejewski-Szmek 89715a
+        ['first-boot-full-preset'],
Zbigniew Jędrzejewski-Szmek 89715a
         ['fexecve'],
Zbigniew Jędrzejewski-Szmek 89715a
         ['standalone-binaries',   get_option('standalone-binaries')],
Zbigniew Jędrzejewski-Szmek 89715a
         ['coverage',              get_option('b_coverage')],
Zbigniew Jędrzejewski-Szmek 89715a
diff --git a/meson_options.txt b/meson_options.txt
Zbigniew Jędrzejewski-Szmek 89715a
index 2a030ac28ec0..28765f900e87 100644
Zbigniew Jędrzejewski-Szmek 89715a
--- a/meson_options.txt
Zbigniew Jędrzejewski-Szmek 89715a
+++ b/meson_options.txt
Zbigniew Jędrzejewski-Szmek 89715a
@@ -27,6 +27,8 @@ option('link-timesyncd-shared', type: 'boolean',
Zbigniew Jędrzejewski-Szmek 89715a
        description : 'link systemd-timesyncd and its helpers to libsystemd-shared.so')
Zbigniew Jędrzejewski-Szmek 89715a
 option('link-boot-shared', type: 'boolean',
Zbigniew Jędrzejewski-Szmek 89715a
        description : 'link bootctl and systemd-bless-boot against libsystemd-shared.so')
Zbigniew Jędrzejewski-Szmek 89715a
+option('first-boot-full-preset', type: 'boolean', value: false,
Zbigniew Jędrzejewski-Szmek 89715a
+       description : 'during first boot, do full preset-all (default will be changed to true later)')
Zbigniew Jędrzejewski-Szmek 89715a
 
Zbigniew Jędrzejewski-Szmek 89715a
 option('static-libsystemd', type : 'combo',
Zbigniew Jędrzejewski-Szmek 89715a
        choices : ['false', 'true', 'pic', 'no-pic'],
Zbigniew Jędrzejewski-Szmek 89715a
diff --git a/src/core/manager.c b/src/core/manager.c
Zbigniew Jędrzejewski-Szmek 89715a
index 18daff66c780..f4dacef1005d 100644
Zbigniew Jędrzejewski-Szmek 89715a
--- a/src/core/manager.c
Zbigniew Jędrzejewski-Szmek 89715a
+++ b/src/core/manager.c
Zbigniew Jędrzejewski-Szmek 89715a
@@ -1728,7 +1728,9 @@ static void manager_preset_all(Manager *m) {
Zbigniew Jędrzejewski-Szmek 89715a
                 return;
Zbigniew Jędrzejewski-Szmek 89715a
 
Zbigniew Jędrzejewski-Szmek 89715a
         /* If this is the first boot, and we are in the host system, then preset everything */
Zbigniew Jędrzejewski-Szmek 89715a
-        r = unit_file_preset_all(LOOKUP_SCOPE_SYSTEM, 0, NULL, UNIT_FILE_PRESET_ENABLE_ONLY, NULL, 0);
Zbigniew Jędrzejewski-Szmek 89715a
+        UnitFilePresetMode mode = FIRST_BOOT_FULL_PRESET ? UNIT_FILE_PRESET_FULL : UNIT_FILE_PRESET_ENABLE_ONLY;
Zbigniew Jędrzejewski-Szmek 89715a
+
Zbigniew Jędrzejewski-Szmek 89715a
+        r = unit_file_preset_all(LOOKUP_SCOPE_SYSTEM, 0, NULL, mode, NULL, 0);
Zbigniew Jędrzejewski-Szmek 89715a
         if (r < 0)
Zbigniew Jędrzejewski-Szmek 89715a
                 log_full_errno(r == -EEXIST ? LOG_NOTICE : LOG_WARNING, r,
Zbigniew Jędrzejewski-Szmek 89715a
                                "Failed to populate /etc with preset unit settings, ignoring: %m");