From 059afcadfd89c6c302f20c9ac1a1c44592b716df Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 13 Jul 2025 04:25:26 +0900 Subject: [PATCH] fsck: add fsck.mode and fsck.repair credentials support Maybe useful when kernel command line is hard to change, e.g. when UKI is used. --- man/systemd-fsck@.service.xml | 28 +++++++++++++++++++ man/systemd.system-credentials.xml | 14 ++++++++++ src/fsck/fsck.c | 28 +++++++++++++++++++ src/shared/generator.c | 3 +- .../systemd-fsck-root.service | 1 + .../systemd-fsck-root.service | 1 + .../systemd-fsck-root.service | 1 + .../systemd-fsck-root.service | 1 + .../systemd-fsck-root.service | 1 + .../systemd-fsck-root.service | 1 + .../systemd-fsck-root.service | 1 + .../systemd-fsck-usr.service | 1 + units/systemd-fsck-root.service.in | 1 + units/systemd-fsck@.service.in | 1 + 14 files changed, 82 insertions(+), 1 deletion(-) diff --git a/man/systemd-fsck@.service.xml b/man/systemd-fsck@.service.xml index b0e2ebafbd2..3df215ba1fe 100644 --- a/man/systemd-fsck@.service.xml +++ b/man/systemd-fsck@.service.xml @@ -110,6 +110,34 @@ + + Credentials + + systemd-fsck supports the service credentials logic as implemented by + ImportCredential=/LoadCredential=/SetCredential= + (see systemd.exec5 for + details). The following credentials are used when passed in: + + + + fsck.mode + fsck.repair + + + The contents of the credentials are parsed as same as the kernel command line options with + the same name. See above for more details. + + + + + + + Note that by default the systemd-fsck@.service, + systemd-fsck-root.service, and systemd-fsck-usr.service unit + files are set up to inherit both fsck.mode and fsck.repair + credentials from the service manager. + + See Also diff --git a/man/systemd.system-credentials.xml b/man/systemd.system-credentials.xml index d436cff5d48..0411061ca39 100644 --- a/man/systemd.system-credentials.xml +++ b/man/systemd.system-credentials.xml @@ -510,6 +510,20 @@ + + + fsck.* + + + Read by systemd-fsck@.service, + systemd-fsck-root.service, and systemd-fsck-usr.service. + See + systemd-fsck@.service8 + for more details. + + + + diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index 7f2b0f5d36e..1d7f4ea3afa 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -16,6 +16,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "bus-util.h" +#include "creds-util.h" #include "device-util.h" #include "fd-util.h" #include "fs-util.h" @@ -129,6 +130,31 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat return 0; } +static void parse_credentials(void) { + _cleanup_free_ char *value = NULL; + int r; + + r = read_credential("fsck.mode", (void**) &value, /* ret_size = */ NULL); + if (r < 0) + log_debug_errno(r, "Failed to read credential 'fsck.mode', ignoring: %m"); + else { + arg_mode = fsck_mode_from_string(value); + if (arg_mode < 0) + log_warning_errno(arg_mode, "Invalid 'fsck.mode' credential, ignoring: %s", value); + } + + value = mfree(value); + + r = read_credential("fsck.repair", (void**) &value, /* ret_size = */ NULL); + if (r < 0) + log_debug_errno(r, "Failed to read credential 'fsck.repair', ignoring: %m"); + else { + arg_repair = fsck_repair_from_string(value); + if (arg_repair < 0) + log_warning_errno(arg_repair, "Invalid 'fsck.repair' credential, ignoring: %s", value); + } +} + static double percent(int pass, unsigned long cur, unsigned long max) { /* Values stolen from e2fsck */ @@ -256,6 +282,8 @@ static int run(int argc, char *argv[]) { if (r < 0) log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); + parse_credentials(); + bool show_progress = access("/run/systemd/show-status", F_OK) >= 0; if (arg_mode == FSCK_SKIP) diff --git a/src/shared/generator.c b/src/shared/generator.c index ea665a6c337..f978d43fe89 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -250,7 +250,8 @@ static int write_fsck_sysroot_service( "Type=oneshot\n" "RemainAfterExit=yes\n" "ExecStart=" SYSTEMD_FSCK_PATH " %6$s\n" - "TimeoutSec=infinity\n", + "TimeoutSec=infinity\n" + "ImportCredential=fsck.*\n", escaped, unit, device, diff --git a/test/test-fstab-generator/test-12-dev-sdx.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-12-dev-sdx.expected/systemd-fsck-root.service index 147348899d1..797ffa08db1 100644 --- a/test/test-fstab-generator/test-12-dev-sdx.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-12-dev-sdx.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/sdx1 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-13-label.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-13-label.expected/systemd-fsck-root.service index 85c1936bce1..ee146aa3b2a 100644 --- a/test/test-fstab-generator/test-13-label.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-13-label.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/disk/by-label/Root TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-14-uuid.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-14-uuid.expected/systemd-fsck-root.service index 1c7eaea1035..73e9134fb17 100644 --- a/test/test-fstab-generator/test-14-uuid.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-14-uuid.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/disk/by-uuid/3f5ad593-4546-4a94-a374-bcfb68aa11f7 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-15-partuuid.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-15-partuuid.expected/systemd-fsck-root.service index ab27bfd79cc..2d5c9bfbfd1 100644 --- a/test/test-fstab-generator/test-15-partuuid.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-15-partuuid.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/disk/by-partuuid/3f5ad593-4546-4a94-a374-bcfb68aa11f7 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-17-initrd-sysroot.fstab.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-17-initrd-sysroot.fstab.expected/systemd-fsck-root.service index 147348899d1..797ffa08db1 100644 --- a/test/test-fstab-generator/test-17-initrd-sysroot.fstab.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-17-initrd-sysroot.fstab.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/sdx1 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-18-options.fstab.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-18-options.fstab.expected/systemd-fsck-root.service index 147348899d1..797ffa08db1 100644 --- a/test/test-fstab-generator/test-18-options.fstab.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-18-options.fstab.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/sdx1 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-root.service b/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-root.service index 147348899d1..797ffa08db1 100644 --- a/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-root.service +++ b/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-root.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/sdx1 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-usr.service b/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-usr.service index 512e7b1636c..61c0af0490a 100644 --- a/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-usr.service +++ b/test/test-fstab-generator/test-19-mounts-from-cmdline.expected/systemd-fsck-usr.service @@ -15,3 +15,4 @@ Type=oneshot RemainAfterExit=yes ExecStart=/usr/lib/systemd/systemd-fsck /dev/sdx5 TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/units/systemd-fsck-root.service.in b/units/systemd-fsck-root.service.in index ebe8262a49e..22e86acadda 100644 --- a/units/systemd-fsck-root.service.in +++ b/units/systemd-fsck-root.service.in @@ -22,3 +22,4 @@ Type=oneshot RemainAfterExit=yes ExecStart={{LIBEXECDIR}}/systemd-fsck TimeoutSec=infinity +ImportCredential=fsck.* diff --git a/units/systemd-fsck@.service.in b/units/systemd-fsck@.service.in index 8eb4821d41f..d5caed6ae0c 100644 --- a/units/systemd-fsck@.service.in +++ b/units/systemd-fsck@.service.in @@ -22,3 +22,4 @@ Type=oneshot RemainAfterExit=yes ExecStart={{LIBEXECDIR}}/systemd-fsck %f TimeoutSec=infinity +ImportCredential=fsck.* -- 2.47.3