chiark / gitweb /
getty: move automatic serial getty logic into generator
[elogind.git] / src / cryptsetup-generator.c
index 73c3679..00f4800 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-%%i.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"
+                "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
                 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
-                options && has_option(options, "swap") ? "format-and-attach" : "attach",
                 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);
@@ -186,7 +222,8 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
-        arg_dest = argv[1];
+        if (argc > 1)
+                arg_dest = argv[1];
 
         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
         log_parse_environment();