chiark / gitweb /
path: after installing inotify watches, recheck file again to fix race
[elogind.git] / src / cryptsetup-generator.c
index 792c1f52d7aa0af466083be9dc3af33903971c63..00120f67a0a63fa6739a9217f222ca8e49116c06 100644 (file)
@@ -59,7 +59,7 @@ static int create_disk(
                 const char *password,
                 const char *options) {
 
-        char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *from = NULL, *to = NULL;
+        char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *from = NULL, *to = NULL, *e = NULL;
         int r;
         FILE *f = NULL;
 
@@ -98,11 +98,11 @@ static int create_disk(
 
         fprintf(f,
                 "[Unit]\n"
-                "Description=Cryptography Setup for %%f\n"
+                "Description=Cryptography Setup for %%I\n"
                 "DefaultDependencies=no\n"
-                "BindTo=%s\n"
+                "BindTo=%s dev-mapper-%%i.device\n"
                 "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n"
-                "Before=dev-mapper-%%f.device shutdown.target\n",
+                "Before=dev-mapper-%%i.device shutdown.target cryptsetup.target\n",
                 d, d);
 
         if (password && (streq(password, "/dev/urandom") ||
@@ -115,21 +115,20 @@ static int create_disk(
                 "\n[Service]\n"
                 "Type=oneshot\n"
                 "RemainAfterExit=yes\n"
-                "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " %s '%s' '%s' '%s' '%s'\n"
-                "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " remove '%s'\n",
-                options && has_option(options, "swap") ? "format" : "create",
+                "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
+                "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
                 name, u, strempty(password), strempty(options),
                 name);
 
         if (options && has_option(options, "tmp"))
                 fprintf(f,
-                        "ExecStartPost=/sbin/mke2fs '%s'",
-                        u);
+                        "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'",
+                        name);
 
         if (options && has_option(options, "swap"))
                 fprintf(f,
-                        "ExecStartPost=/sbin/mkswap '%s'",
-                        u);
+                        "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'",
+                        name);
 
         fflush(f);
 
@@ -139,6 +138,11 @@ static int create_disk(
                 goto fail;
         }
 
+        if (asprintf(&from, "../%s", n) < 0) {
+                r = -ENOMEM;
+                goto fail;
+        }
+
         if (!options || !has_option(options, "noauto")) {
 
                 if (asprintf(&to, "%s/%s.wants/%s", arg_dest, d, n) < 0) {
@@ -146,11 +150,6 @@ static int create_disk(
                         goto fail;
                 }
 
-                if (asprintf(&from, "../%s", n) < 0) {
-                        r = -ENOMEM;
-                        goto fail;
-                }
-
                 mkdir_parents(to, 0755);
 
                 if (symlink(from, to) < 0) {
@@ -158,6 +157,42 @@ static int create_disk(
                         r = -errno;
                         goto fail;
                 }
+
+                free(to);
+                to = NULL;
+
+                if (!options || !has_option(options, "nofail")) {
+
+                        if (asprintf(&to, "%s/cryptsetup.target.wants/%s", arg_dest, n) < 0) {
+                                r = -ENOMEM;
+                                goto fail;
+                        }
+
+                        mkdir_parents(to, 0755);
+
+                        if (symlink(from, to) < 0) {
+                                log_error("Failed to create symlink '%s' to '%s': %m", from, to);
+                                r = -errno;
+                                goto fail;
+                        }
+                }
+        }
+
+        free(to);
+        to = NULL;
+
+        e = unit_name_escape(name);
+        if (asprintf(&to, "%s/dev-mapper-%s.device.requires/%s", arg_dest, e, n) < 0) {
+                r = -ENOMEM;
+                goto fail;
+        }
+
+        mkdir_parents(to, 0755);
+
+        if (symlink(from, to) < 0) {
+                log_error("Failed to create symlink '%s' to '%s': %m", from, to);
+                r = -errno;
+                goto fail;
         }
 
         r = 0;
@@ -166,6 +201,7 @@ fail:
         free(p);
         free(n);
         free(d);
+        free(e);
 
         free(from);
         free(to);