]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/cryptsetup/cryptsetup-generator.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-generator.c
index 3fdf65be6552bcaaf0a05c5615a18569f9d26326..7918c89564fafafb2f40985eb214ce85e52f4235 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -62,7 +63,7 @@ static int create_disk(
                 *filtered = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         const char *dmname;
-        bool noauto, nofail, tmp, swap;
+        bool noauto, nofail, tmp, swap, netdev;
         int r;
 
         assert(name);
@@ -72,6 +73,7 @@ static int create_disk(
         nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
         tmp = fstab_test_option(options, "tmp\0");
         swap = fstab_test_option(options, "swap\0");
+        netdev = fstab_test_option(options, "_netdev\0");
 
         if (tmp && swap) {
                 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
@@ -102,25 +104,26 @@ static int create_disk(
         if (!f)
                 return log_error_errno(errno, "Failed to create unit file %s: %m", p);
 
-        fputs("# Automatically generated by systemd-cryptsetup-generator\n\n"
-              "[Unit]\n"
-              "Description=Cryptography Setup for %I\n"
-              "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
-              "SourcePath=/etc/crypttab\n"
-              "DefaultDependencies=no\n"
-              "Conflicts=umount.target\n"
-              "BindsTo=dev-mapper-%i.device\n"
-              "IgnoreOnIsolate=true\n"
-              "After=cryptsetup-pre.target\n",
-              f);
+        fprintf(f,
+                "# Automatically generated by systemd-cryptsetup-generator\n\n"
+                "[Unit]\n"
+                "Description=Cryptography Setup for %%I\n"
+                "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
+                "SourcePath=/etc/crypttab\n"
+                "DefaultDependencies=no\n"
+                "Conflicts=umount.target\n"
+                "IgnoreOnIsolate=true\n"
+                "After=%s\n",
+                netdev ? "remote-fs-pre.target" : "cryptsetup-pre.target");
 
         if (!nofail)
                 fprintf(f,
-                        "Before=cryptsetup.target\n");
+                        "Before=%s\n",
+                        netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
 
         if (password) {
                 if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
-                        fputs("After=systemd-random-seed.service\n", f);
+                        fputs_unlocked("After=systemd-random-seed.service\n", f);
                 else if (!STR_IN_SET(password, "-", "none")) {
                         _cleanup_free_ char *uu;
 
@@ -130,7 +133,7 @@ static int create_disk(
 
                         if (!path_equal(uu, "/dev/null")) {
 
-                                if (is_device_path(uu)) {
+                                if (path_startswith(uu, "/dev/")) {
                                         _cleanup_free_ char *dd = NULL;
 
                                         r = unit_name_from_path(uu, ".device", &dd);
@@ -144,7 +147,7 @@ static int create_disk(
                 }
         }
 
-        if (is_device_path(u)) {
+        if (path_startswith(u, "/dev/")) {
                 fprintf(f,
                         "BindsTo=%s\n"
                         "After=%s\n"
@@ -152,8 +155,8 @@ static int create_disk(
                         d, d);
 
                 if (swap)
-                        fputs("Before=dev-mapper-%i.swap\n",
-                              f);
+                        fputs_unlocked("Before=dev-mapper-%i.swap\n",
+                                       f);
         } else
                 fprintf(f,
                         "RequiresMountsFor=%s\n",
@@ -168,6 +171,7 @@ static int create_disk(
                 "Type=oneshot\n"
                 "RemainAfterExit=yes\n"
                 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
+                "KeyringMode=shared\n" /* make sure we can share cached keys among instances */
                 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
                 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
                 name, u, strempty(password), strempty(filtered),
@@ -192,7 +196,8 @@ static int create_disk(
                 if (r < 0)
                         return r;
 
-                r = generator_add_symlink(arg_dest, "cryptsetup.target",
+                r = generator_add_symlink(arg_dest,
+                                          netdev ? "remote-cryptsetup.target" : "cryptsetup.target",
                                           nofail ? "wants" : "requires", n);
                 if (r < 0)
                         return r;
@@ -374,7 +379,7 @@ static int add_crypttab_devices(void) {
                 crypttab_line++;
 
                 l = strstrip(line);
-                if (*l == '#' || *l == 0)
+                if (IN_SET(*l, 0, '#'))
                         continue;
 
                 k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &keyfile, &options);