cryptsetup: use WantsMountsFor= for key/header when nofail is set

The header and keyfile are necessary only for opening the device, not
for closing, so it is not necessary to deactivate the generated
cryptsetup unit when the header or keyfile backing store are removed.

This is especially useful in the case of softreboot, when the new
mount root is setup under /run/nextroot/ but we don't want to close
the cryptsetup devices for encrypted /var/ or so, and we simply
mount it directly on /run/nextroot/var/ before the soft-reboot.
This commit is contained in:
Luca Boccassi
2023-11-29 11:04:59 +00:00
parent 8284c2cb68
commit c9be8e420e
3 changed files with 39 additions and 11 deletions
+13 -10
View File
@@ -229,7 +229,8 @@ static int generate_device_umount(const char *name,
static int print_dependencies(FILE *f, const char* device_path, const char* timeout_value, bool canfail) {
int r;
assert(!canfail || timeout_value);
assert(f);
assert(device_path);
if (STR_IN_SET(device_path, "-", "none"))
/* None, nothing to do */
@@ -263,11 +264,13 @@ static int print_dependencies(FILE *f, const char* device_path, const char* time
fprintf(f, "After=%1$s\n", unit);
if (canfail) {
fprintf(f, "Wants=%1$s\n", unit);
r = write_drop_in_format(arg_dest, unit, 90, "device-timeout",
"# Automatically generated by systemd-cryptsetup-generator \n\n"
"[Unit]\nJobRunningTimeoutSec=%s", timeout_value);
if (r < 0)
return log_error_errno(r, "Failed to write device drop-in: %m");
if (timeout_value) {
r = write_drop_in_format(arg_dest, unit, 90, "device-timeout",
"# Automatically generated by systemd-cryptsetup-generator \n\n"
"[Unit]\nJobRunningTimeoutSec=%s", timeout_value);
if (r < 0)
return log_error_errno(r, "Failed to write device drop-in: %m");
}
} else
fprintf(f, "Requires=%1$s\n", unit);
} else {
@@ -276,7 +279,7 @@ static int print_dependencies(FILE *f, const char* device_path, const char* time
if (!escaped_path)
return log_oom();
fprintf(f, "RequiresMountsFor=%s\n", escaped_path);
fprintf(f, "%s=%s\n", canfail ? "WantsMountsFor" : "RequiresMountsFor", escaped_path);
}
return 0;
@@ -486,7 +489,7 @@ static int create_disk(
if (key_file && !keydev) {
r = print_dependencies(f, key_file,
keyfile_timeout_value,
/* canfail= */ keyfile_can_timeout > 0);
/* canfail= */ keyfile_can_timeout > 0 || nofail);
if (r < 0)
return r;
}
@@ -494,8 +497,8 @@ static int create_disk(
/* Check if a header option was specified */
if (detached_header > 0 && !headerdev) {
r = print_dependencies(f, header_path,
NULL,
/* canfail= */ false); /* header is always necessary */
/* timeout_value= */ NULL,
/* canfail= */ nofail);
if (r < 0)
return r;
}