Add systemd.default_debug_tty=

Let's allow configuring the debug tty independently of enabling/disabling
the debug shell. This allows mkosi to configure the correct tty while
leaving enabling/disabling the debug tty to the user.
This commit is contained in:
Daan De Meyer 2024-02-08 10:54:54 +01:00
parent c078f4af6b
commit 7bf52f5d1c
4 changed files with 36 additions and 24 deletions

View file

@ -86,6 +86,7 @@
<term><varname>systemd.mask=</varname></term>
<term><varname>systemd.wants=</varname></term>
<term><varname>systemd.debug_shell</varname></term>
<term><varname>systemd.default_debug_tty=</varname></term>
<listitem>
<para>Additional parameters understood by
<citerefentry><refentrytitle>systemd-debug-generator</refentrytitle><manvolnum>8</manvolnum></citerefentry>,

View file

@ -55,19 +55,16 @@
RAM disk (initrd) while <option>systemd.wants=</option> is
honored only in the main system.</para>
<para>If the <option>systemd.debug_shell</option> or
<option>rd.systemd.debug_shell</option> option is
specified, the debug shell service
<literal>debug-shell.service</literal> is pulled into the boot
transaction and a debug shell will be spawned during early boot.
By default, <filename>&DEBUGTTY;</filename> is used, but a specific tty can also be set,
either with or without the <filename>/dev/</filename> prefix.
Note that the shell may also be turned on persistently by enabling it with
<citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<command>enable</command> command.
<option>rd.systemd.debug_shell=</option> is honored only by initial
RAM disk (initrd) while <option>systemd.debug_shell</option> is
honored only in the main system.</para>
<para>If the <option>systemd.debug_shell</option> or <option>rd.systemd.debug_shell</option> option is
specified, the debug shell service <literal>debug-shell.service</literal> is pulled into the boot
transaction and a debug shell will be spawned during early boot. By default,
<filename>&DEBUGTTY;</filename> is used, but a specific tty can also be specified, either with or without
the <filename>/dev/</filename> prefix. To set the tty to use without enabling the debug shell, the
<option>systemd.default_debug_tty=</option> option can be used which also takes a tty with or without the
<filename>/dev/</filename> prefix. Note that the shell may also be turned on persistently by enabling it
with <citerefentry><refentrytitle>systemctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<command>enable</command> command. <option>rd.systemd.debug_shell=</option> is honored only by initial
RAM disk (initrd) while <option>systemd.debug_shell</option> is honored only in the main system.</para>
<para><filename>systemd-debug-generator</filename> implements
<citerefentry><refentrytitle>systemd.generator</refentrytitle><manvolnum>7</manvolnum></citerefentry>.</para>

View file

@ -20,12 +20,15 @@ static const char *arg_dest = NULL;
static char *arg_default_unit = NULL;
static char **arg_mask = NULL;
static char **arg_wants = NULL;
static char *arg_debug_shell = NULL;
static bool arg_debug_shell = false;
static char *arg_debug_tty = NULL;
static char *arg_default_debug_tty = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_default_unit, freep);
STATIC_DESTRUCTOR_REGISTER(arg_mask, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_wants, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_debug_shell, freep);
STATIC_DESTRUCTOR_REGISTER(arg_debug_tty, freep);
STATIC_DESTRUCTOR_REGISTER(arg_default_debug_tty, freep);
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
int r;
@ -61,15 +64,18 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
return log_oom();
} else if (proc_cmdline_key_streq(key, "systemd.debug_shell")) {
const char *t = NULL;
r = value ? parse_boolean(value) : 1;
if (r < 0)
t = skip_dev_prefix(value);
else if (r > 0)
t = skip_dev_prefix(DEBUGTTY);
arg_debug_shell = r != 0;
if (r >= 0)
return 0;
return free_and_strdup_warn(&arg_debug_shell, t);
return free_and_strdup_warn(&arg_debug_tty, skip_dev_prefix(value));
} else if (proc_cmdline_key_streq(key, "systemd.default_debug_tty")) {
if (proc_cmdline_value_missing(key, value))
return 0;
return free_and_strdup_warn(&arg_default_debug_tty, skip_dev_prefix(value));
} else if (streq(key, "systemd.unit")) {
@ -136,9 +142,10 @@ static int generate_wants_symlinks(void) {
}
static void install_debug_shell_dropin(const char *dir) {
const char *tty = arg_debug_tty ?: arg_default_debug_tty;
int r;
if (streq(arg_debug_shell, skip_dev_prefix(DEBUGTTY)))
if (!tty || path_equal(tty, skip_dev_prefix(DEBUGTTY)))
return;
r = write_drop_in_format(dir, "debug-shell.service", 50, "tty",
@ -147,7 +154,7 @@ static void install_debug_shell_dropin(const char *dir) {
"ConditionPathExists=\n"
"[Service]\n"
"TTYPath=/dev/%s",
arg_debug_shell, arg_debug_shell);
tty, tty);
if (r < 0)
log_warning_errno(r, "Failed to write drop-in for debug-shell.service, ignoring: %m");
}

View file

@ -62,6 +62,13 @@ SYSTEMD_PROC_CMDLINE="$CMDLINE" run_and_list "$GENERATOR_BIN" "$OUT_DIR"
link_endswith "$OUT_DIR/early/default.target.wants/debug-shell.service" /lib/systemd/system/debug-shell.service
grep -F "/dev/tty666" "$OUT_DIR/early/debug-shell.service.d/50-tty.conf"
# Same thing, but with custom tty using systemd.default_debug_tty
: "debug-shell: regular + systemd.default_debug_tty=/dev/tty666 systemd.debug_shell=yes"
CMDLINE="$CMDLINE systemd.default_debug_tty=/dev/tty666 systemd.debug_shell=yes"
SYSTEMD_PROC_CMDLINE="$CMDLINE" run_and_list "$GENERATOR_BIN" "$OUT_DIR"
link_endswith "$OUT_DIR/early/default.target.wants/debug-shell.service" /lib/systemd/system/debug-shell.service
grep -F "/dev/tty666" "$OUT_DIR/early/debug-shell.service.d/50-tty.conf"
# Now override the default target via systemd.unit=
: "debug-shell: regular + systemd.unit="
CMDLINE="$CMDLINE systemd.unit=my-fancy.target"