Skip to content

Commit 4dda4e6

Browse files
committed
Generate systemd-fsck-root.service in the initramfs
In the initrafms, generate a systemd-fsck-root.service to replace systemd-fsck@<sysroot-device>.service. This way, after we transition to the real root, systemd-fsck-root.service is marked as already done. This introduces an unnecessary synchronization point, because systemd-fsck@* is ordered after systemd-fsck-root also in the initramfs. In practice this shouldn't be a problem. https://bugzilla.redhat.com/show_bug.cgi?id=1201979 C.f. 956eaf2.
1 parent 7703bd4 commit 4dda4e6

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

TODO

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ Features:
111111
* Maybe add support for the equivalent of "ethtool advertise" to .link files?
112112
http://lists.freedesktop.org/archives/systemd-devel/2015-April/030112.html
113113

114-
* fstab-generator should generate systemd-fsck-root.service when
115-
running in the initrd, and operate on the right device.
116-
117114
* .timer units should optionally support CLOCK_BOOTTIME in addition to CLOCK_MONOTONIC
118115

119116
* create a btrfs qgroup for /var/lib/machines, and add all container
@@ -151,8 +148,6 @@ Features:
151148
* Introduce $LISTEN_NAMES to complement $LISTEN_FDS, containing a
152149
colon separated list of identifiers for the fds passed.
153150

154-
* when the fstab-generator runs in the initrd, it should create a /dev/null mask for systemd-fsck-root.service, to avoid that the the root fs is fsck'ed twice.
155-
156151
* maybe introduce WantsMountsFor=? Usecase:
157152
http://lists.freedesktop.org/archives/systemd-devel/2015-January/027729.html
158153

src/shared/generator.c

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,52 @@
2828
#include "generator.h"
2929
#include "path-util.h"
3030
#include "fstab-util.h"
31+
#include "fileio.h"
3132
#include "dropin.h"
3233

34+
static int write_fsck_sysroot_service(const char *dir, const char *what) {
35+
const char *unit;
36+
_cleanup_free_ char *device = NULL;
37+
_cleanup_fclose_ FILE *f = NULL;
38+
int r;
39+
40+
unit = strjoina(dir, "/systemd-fsck-root.service");
41+
log_debug("Creating %s", unit);
42+
43+
r = unit_name_from_path(what, ".device", &device);
44+
if (r < 0)
45+
return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what);
46+
47+
f = fopen(unit, "wxe");
48+
if (!f)
49+
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
50+
51+
fprintf(f,
52+
"# Automatically generated by %1$s\n\n"
53+
"[Unit]\n"
54+
"Documentation=man:systemd-fsck-root.service(8)\n"
55+
"Description=File System Check on %2$s\n"
56+
"DefaultDependencies=no\n"
57+
"BindsTo=%3$s\n"
58+
"After=%3$s\n"
59+
"Before=shutdown.target\n"
60+
"\n"
61+
"[Service]\n"
62+
"Type=oneshot\n"
63+
"RemainAfterExit=yes\n"
64+
"ExecStart=/usr/lib/systemd/systemd-fsck %2$s\n"
65+
"TimeoutSec=0\n",
66+
program_invocation_short_name,
67+
what,
68+
device);
69+
70+
fflush(f);
71+
if (ferror(f))
72+
return log_error_errno(errno, "Failed to write unit file %s: %m", unit);
73+
74+
return 0;
75+
}
76+
3377
int generator_write_fsck_deps(
3478
FILE *f,
3579
const char *dir,
@@ -69,11 +113,22 @@ int generator_write_fsck_deps(
69113
return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
70114

71115
} else {
72-
_cleanup_free_ char *fsck = NULL;
73-
74-
r = unit_name_from_path_instance("systemd-fsck", what, ".service", &fsck);
75-
if (r < 0)
76-
return log_error_errno(r, "Failed to create fsck service name: %m");
116+
_cleanup_free_ char *_fsck = NULL;
117+
const char *fsck;
118+
119+
if (in_initrd() && path_equal(where, "/sysroot")) {
120+
r = write_fsck_sysroot_service(dir, what);
121+
if (r < 0)
122+
return r;
123+
124+
fsck = "systemd-fsck-root.service";
125+
} else {
126+
r = unit_name_from_path_instance("systemd-fsck", what, ".service", &_fsck);
127+
if (r < 0)
128+
return log_error_errno(r, "Failed to create fsck service name: %m");
129+
130+
fsck = _fsck;
131+
}
77132

78133
fprintf(f,
79134
"RequiresOverridable=%1$s\n"

0 commit comments

Comments
 (0)