ecbff1
From d09c35c48005669c4c4663e3ba8a6f979432cead Mon Sep 17 00:00:00 2001
ecbff1
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ecbff1
Date: Tue, 5 Sep 2017 11:30:33 +0200
ecbff1
Subject: [PATCH] cryptsetup-generator: use remote-cryptsetup.target when
ecbff1
 _netdev is present
ecbff1
ecbff1
This allows such devices to depend on the network. Their startup will
ecbff1
be delayed similarly to network mount units.
ecbff1
ecbff1
Fixes #4642.
ecbff1
ecbff1
Cherry-picked from: b001ad61e91b6499897f0c977045c7608c233bfa
ecbff1
Resolves: #1384014
ecbff1
---
de8967
 man/crypttab.xml                      | 13 ++++++++++
de8967
 src/cryptsetup/cryptsetup-generator.c | 36 ++++++++++++++++++---------
ecbff1
 2 files changed, 37 insertions(+), 12 deletions(-)
ecbff1
ecbff1
diff --git a/man/crypttab.xml b/man/crypttab.xml
ecbff1
index 3e249ad23..7085a1623 100644
ecbff1
--- a/man/crypttab.xml
ecbff1
+++ b/man/crypttab.xml
ecbff1
@@ -189,6 +189,19 @@
ecbff1
         <option>size=</option>.</para></listitem>
ecbff1
       </varlistentry>
ecbff1
 
ecbff1
+      <varlistentry>
ecbff1
+        <term><option>_netdev</option></term>
ecbff1
+
ecbff1
+        <listitem><para>Marks this cryptsetup device as requiring network. It will be
ecbff1
+        started after the network is available, similarly to
ecbff1
+        <citerefentry><refentrytitle>systemd.mount</refentrytitle><manvolnum>5</manvolnum></citerefentry>
ecbff1
+        units marked with <option>_netdev</option>. The service unit to set up this device
ecbff1
+        will be ordered between <filename>remote-cryptsetup-pre.target</filename> and
ecbff1
+        <filename>remote-cryptsetup.target</filename>, instead of
ecbff1
+        <filename>cryptsetup-pre.target</filename> and
ecbff1
+        <filename>cryptsetup.target</filename>.</para></listitem>
ecbff1
+      </varlistentry>
ecbff1
+
ecbff1
       <varlistentry>
ecbff1
         <term><option>noauto</option></term>
ecbff1
 
ecbff1
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
ecbff1
index d191def5f..49dc8f14b 100644
ecbff1
--- a/src/cryptsetup/cryptsetup-generator.c
ecbff1
+++ b/src/cryptsetup/cryptsetup-generator.c
ecbff1
@@ -60,7 +60,7 @@ static int create_disk(
ecbff1
         _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *to = NULL, *e = NULL,
ecbff1
                 *filtered = NULL;
ecbff1
         _cleanup_fclose_ FILE *f = NULL;
ecbff1
-        bool noauto, nofail, tmp, swap;
ecbff1
+        bool noauto, nofail, tmp, swap, netdev;
ecbff1
         char *from;
ecbff1
         int r;
ecbff1
 
ecbff1
@@ -71,6 +71,7 @@ static int create_disk(
ecbff1
         nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
ecbff1
         tmp = fstab_test_option(options, "tmp\0");
ecbff1
         swap = fstab_test_option(options, "swap\0");
ecbff1
+        netdev = fstab_test_option(options, "_netdev\0");
ecbff1
 
ecbff1
         if (tmp && swap) {
ecbff1
                 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
ecbff1
@@ -101,22 +102,24 @@ static int create_disk(
ecbff1
         if (!f)
ecbff1
                 return log_error_errno(errno, "Failed to create unit file %s: %m", p);
ecbff1
 
ecbff1
-        fputs(
ecbff1
+        fprintf(f,
ecbff1
                 "# Automatically generated by systemd-cryptsetup-generator\n\n"
ecbff1
                 "[Unit]\n"
ecbff1
-                "Description=Cryptography Setup for %I\n"
ecbff1
+                "Description=Cryptography Setup for %%I\n"
ecbff1
                 "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
ecbff1
                 "SourcePath=/etc/crypttab\n"
ecbff1
                 "DefaultDependencies=no\n"
ecbff1
                 "Conflicts=umount.target\n"
ecbff1
-                "BindsTo=dev-mapper-%i.device\n"
ecbff1
+                "BindsTo=dev-mapper-%%i.device\n"
ecbff1
                 "IgnoreOnIsolate=true\n"
ecbff1
-                "After=systemd-readahead-collect.service systemd-readahead-replay.service cryptsetup-pre.target\n",
ecbff1
-                f);
ecbff1
+                "After=systemd-readahead-collect.service systemd-readahead-replay.service\n"
ecbff1
+                "After=%s\n",
ecbff1
+                netdev ? "remote-cryptsetup-pre.target" : "cryptsetup-pre.target");
ecbff1
 
ecbff1
         if (!nofail)
ecbff1
                 fprintf(f,
ecbff1
-                        "Before=cryptsetup.target\n");
ecbff1
+                        "Before=%s\n",
ecbff1
+                        netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
ecbff1
 
ecbff1
         if (password) {
ecbff1
                 if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
ecbff1
@@ -196,16 +199,25 @@ static int create_disk(
ecbff1
                         return log_error_errno(errno, "Failed to create symlink %s: %m", to);
ecbff1
 
ecbff1
                 free(to);
ecbff1
-                if (!nofail)
ecbff1
-                        to = strjoin(arg_dest, "/cryptsetup.target.requires/", n, NULL);
ecbff1
-                else
ecbff1
-                        to = strjoin(arg_dest, "/cryptsetup.target.wants/", n, NULL);
ecbff1
+                if (!netdev) {
ecbff1
+                        if (!nofail)
ecbff1
+                                to = strjoin(arg_dest, "/cryptsetup.target.requires/", n, NULL);
ecbff1
+                        else
ecbff1
+                                to = strjoin(arg_dest, "/cryptsetup.target.wants/", n, NULL);
ecbff1
+                } else {
ecbff1
+                        if (!nofail)
ecbff1
+                                to = strjoin(arg_dest, "/remote-cryptsetup.target.requires/", n, NULL);
ecbff1
+                        else
ecbff1
+                                to = strjoin(arg_dest, "/remote-cryptsetup.target.wants/", n, NULL);
ecbff1
+                }
ecbff1
                 if (!to)
ecbff1
                         return log_oom();
ecbff1
 
ecbff1
                 mkdir_parents_label(to, 0755);
ecbff1
-                if (symlink(from, to) < 0)
ecbff1
+                if (symlink(from, to) < 0) {
ecbff1
+                        free(to);
ecbff1
                         return log_error_errno(errno, "Failed to create symlink %s: %m", to);
ecbff1
+                }
ecbff1
         }
ecbff1
 
ecbff1
         free(to);