From: Zbigniew Jędrzejewski-Szmek Date: Thu, 28 Nov 2019 08:48:26 +0000 (+0100) Subject: pid1: use initrd.target in the initramfs by default X-Git-Tag: v245-rc1~320^2~2 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fsystemd.git;a=commitdiff_plain;h=8755dbad5b2a72f084c48ae27a91b90d0b215eb3 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. --- diff --git a/src/basic/special.h b/src/basic/special.h index add1c1d507e..0eb3f3a3686 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 6e101f02785..ff9058aadb1 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; + + 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, arg_default_unit, NULL, &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;