From fe7a6da8c5a95064c7e7117ca9362c84be0cb2d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 28 Nov 2019 09:13:45 +0100 Subject: [PATCH 1/5] core: use SPECIAL_DEFAULT_TARGET more --- src/run-generator/run-generator.c | 3 ++- src/sulogin-shell/sulogin-shell.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/run-generator/run-generator.c b/src/run-generator/run-generator.c index 7d6c110912..a149b9c9c2 100644 --- a/src/run-generator/run-generator.c +++ b/src/run-generator/run-generator.c @@ -9,6 +9,7 @@ #include "generator.h" #include "mkdir.h" #include "proc-cmdline.h" +#include "special.h" #include "specifier.h" #include "strv.h" @@ -116,7 +117,7 @@ static int generate(void) { return log_error_errno(r, "Failed to create unit file %s: %m", p); /* And now redirect default.target to our new target */ - p = strjoina(arg_dest, "/default.target"); + p = strjoina(arg_dest, "/" SPECIAL_DEFAULT_TARGET); if (symlink("kernel-command-line.target", p) < 0) return log_error_errno(errno, "Failed to link unit file kernel-command-line.target → %s: %m", p); diff --git a/src/sulogin-shell/sulogin-shell.c b/src/sulogin-shell/sulogin-shell.c index 6d65efbb9e..9f90981e71 100644 --- a/src/sulogin-shell/sulogin-shell.c +++ b/src/sulogin-shell/sulogin-shell.c @@ -14,6 +14,7 @@ #include "process-util.h" #include "sd-bus.h" #include "signal-util.h" +#include "special.h" static int reload_manager(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -57,7 +58,7 @@ static int start_default_target(sd_bus *bus) { "StartUnit", &error, NULL, - "ss", "default.target", "isolate"); + "ss", SPECIAL_DEFAULT_TARGET, "isolate"); if (r < 0) return log_error_errno(r, "Failed to start default target: %s", bus_error_message(&error, r)); From 6e86b24db3c5490b5c05b1ca5e440a2deec25481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 28 Nov 2019 09:14:22 +0100 Subject: [PATCH 2/5] tree-wide: normalize includes of public headers They are supposed to go into a sectinon of their own. --- src/journal-remote/journal-upload.h | 1 + src/libsystemd-network/sd-dhcp-server.c | 2 +- src/shared/bus-wait-for-jobs.h | 1 + src/shared/journal-importer.h | 3 ++- src/sulogin-shell/sulogin-shell.c | 3 ++- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/journal-remote/journal-upload.h b/src/journal-remote/journal-upload.h index 5711905f86..4994cd8391 100644 --- a/src/journal-remote/journal-upload.h +++ b/src/journal-remote/journal-upload.h @@ -4,6 +4,7 @@ #include "sd-event.h" #include "sd-journal.h" + #include "time-util.h" typedef enum { diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index 9e51bf74f5..546b5d02c4 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -6,6 +6,7 @@ #include #include "sd-dhcp-server.h" +#include "sd-id128.h" #include "alloc-util.h" #include "dhcp-internal.h" @@ -13,7 +14,6 @@ #include "fd-util.h" #include "in-addr-util.h" #include "io-util.h" -#include "sd-id128.h" #include "siphash24.h" #include "string-util.h" #include "unaligned.h" diff --git a/src/shared/bus-wait-for-jobs.h b/src/shared/bus-wait-for-jobs.h index 8f21eb9cd6..614a2c9de9 100644 --- a/src/shared/bus-wait-for-jobs.h +++ b/src/shared/bus-wait-for-jobs.h @@ -2,6 +2,7 @@ #pragma once #include "sd-bus.h" + #include "macro.h" typedef struct BusWaitForJobs BusWaitForJobs; diff --git a/src/shared/journal-importer.h b/src/shared/journal-importer.h index b2e3c817f5..783edff5d3 100644 --- a/src/shared/journal-importer.h +++ b/src/shared/journal-importer.h @@ -6,8 +6,9 @@ #include #include -#include "io-util.h" #include "sd-id128.h" + +#include "io-util.h" #include "time-util.h" /* Make sure not to make this smaller than the maximum coredump size. diff --git a/src/sulogin-shell/sulogin-shell.c b/src/sulogin-shell/sulogin-shell.c index 9f90981e71..4cf9d22c87 100644 --- a/src/sulogin-shell/sulogin-shell.c +++ b/src/sulogin-shell/sulogin-shell.c @@ -6,13 +6,14 @@ #include #include +#include "sd-bus.h" + #include "bus-util.h" #include "bus-error.h" #include "def.h" #include "env-util.h" #include "log.h" #include "process-util.h" -#include "sd-bus.h" #include "signal-util.h" #include "special.h" From 8755dbad5b2a72f084c48ae27a91b90d0b215eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 28 Nov 2019 09:48:26 +0100 Subject: [PATCH 3/5] pid1: use initrd.target in the initramfs by default This makes the code do what the documentation says. The code had no inkling about initrd.target, so I think this change is fairly risky. As a fallback, default.target will be loaded, so initramfses which relied on current behaviour will still work, as along as they don't have a different initrd.target. In an initramfs created with recent dracut: $ ls -l usr/lib/systemd/system/{default.target,initrd.target} lrwxrwxrwx. usr/lib/systemd/system/default.target -> initrd.target -rw-r--r--. usr/lib/systemd/system/initrd.target So at least for dracut, there should be no difference. Also avoid a pointless allocation. --- src/basic/special.h | 1 + src/core/main.c | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/basic/special.h b/src/basic/special.h index add1c1d507..0eb3f3a368 100644 --- a/src/basic/special.h +++ b/src/basic/special.h @@ -2,6 +2,7 @@ #pragma once #define SPECIAL_DEFAULT_TARGET "default.target" +#define SPECIAL_INITRD_TARGET "initrd.target" /* Shutdown targets */ #define SPECIAL_UMOUNT_TARGET "umount.target" diff --git a/src/core/main.c b/src/core/main.c index 6e101f0278..ff9058aadb 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1967,20 +1967,36 @@ static int do_queue_default_job( const char **ret_error_message) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + const char* default_unit; Job *default_unit_job; Unit *target = NULL; int r; - log_debug("Activating default unit: %s", arg_default_unit); + if (arg_default_unit) + default_unit = arg_default_unit; + else if (in_initrd()) + default_unit = SPECIAL_INITRD_TARGET; + else + default_unit = SPECIAL_DEFAULT_TARGET; - r = manager_load_startable_unit_or_warn(m, arg_default_unit, NULL, &target); + log_debug("Activating default unit: %s", default_unit); + + r = manager_load_startable_unit_or_warn(m, default_unit, NULL, &target); + if (r < 0 && in_initrd() && !arg_default_unit) { + /* Fall back to default.target, which we used to always use by default. Only do this if no + * explicit configuration was given. */ + + log_info("Falling back to " SPECIAL_DEFAULT_TARGET "."); + + r = manager_load_startable_unit_or_warn(m, SPECIAL_DEFAULT_TARGET, NULL, &target); + } if (r < 0) { - log_info("Falling back to rescue target: " SPECIAL_RESCUE_TARGET); + log_info("Falling back to " SPECIAL_RESCUE_TARGET "."); r = manager_load_startable_unit_or_warn(m, SPECIAL_RESCUE_TARGET, NULL, &target); if (r < 0) { - *ret_error_message = r == -ERFKILL ? "Rescue target masked" - : "Failed to load rescue target"; + *ret_error_message = r == -ERFKILL ? SPECIAL_RESCUE_TARGET " masked" + : "Failed to load " SPECIAL_RESCUE_TARGET; return r; } } @@ -2192,15 +2208,6 @@ static int load_configuration( return r; } - /* Initialize default unit */ - if (!arg_default_unit) { - arg_default_unit = strdup(SPECIAL_DEFAULT_TARGET); - if (!arg_default_unit) { - *ret_error_message = "Failed to set default unit"; - return log_oom(); - } - } - /* Initialize the show status setting if it hasn't been set explicitly yet */ if (arg_show_status == _SHOW_STATUS_INVALID) arg_show_status = SHOW_STATUS_YES; From 2b1daf24dc82dcadf584b2a7ea2801bd9710f894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 28 Nov 2019 10:01:27 +0100 Subject: [PATCH 4/5] man: document initrd.target --- man/systemd.special.xml | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/man/systemd.special.xml b/man/systemd.special.xml index 248fb924db..c839af1842 100644 --- a/man/systemd.special.xml +++ b/man/systemd.special.xml @@ -38,6 +38,7 @@ hibernate.target, hybrid-sleep.target, suspend-then-hibernate.target, + initrd.target, initrd-fs.target, initrd-root-device.target, initrd-root-fs.target, @@ -202,14 +203,16 @@ default.target - The default unit systemd starts at bootup. Usually, - this should be aliased (symlinked) to - multi-user.target or - graphical.target. + The default unit systemd starts at bootup. Usually, this should be aliased (symlinked) to + multi-user.target or graphical.target. See + bootup7 for + more discussion. - The default unit systemd starts at bootup can be - overridden with the systemd.unit= kernel - command line option. + The default unit systemd starts at bootup can be overridden with the + systemd.unit= kernel command line option, or more conveniently, with the short + names like single, rescue, 1, + 3, 5, …; see + systemd1. @@ -341,6 +344,15 @@ is active as long as the system is running. + + initrd.target + + This is the default target in the initramfs, similar to default.target + in the main system. It is used to mount the real root and transition to it. See + bootup7 for + more discussion. + + initrd-fs.target From 23e5e79a51f92c1b0ab3812e0efe65055d5d3a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 3 Dec 2019 14:48:27 +0100 Subject: [PATCH 5/5] initrd: fix systemd.debug-shell & friends They would get assigned to an inactive target in the initramfs. --- src/debug-generator/debug-generator.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/debug-generator/debug-generator.c b/src/debug-generator/debug-generator.c index bc8714c4c7..4e41eca352 100644 --- a/src/debug-generator/debug-generator.c +++ b/src/debug-generator/debug-generator.c @@ -126,7 +126,15 @@ static int generate_wants_symlinks(void) { STRV_FOREACH(u, arg_wants) { _cleanup_free_ char *p = NULL, *f = NULL; - const char *target = arg_default_unit ?: SPECIAL_DEFAULT_TARGET; + const char *target; + + /* This should match what do_queue_default_job() in core/main.c does. */ + if (arg_default_unit) + target = arg_default_unit; + else if (in_initrd()) + target = SPECIAL_INITRD_TARGET; + else + target = SPECIAL_DEFAULT_TARGET; p = strjoin(arg_dest, "/", target, ".wants/", *u); if (!p)