Nathaniel McCallum ac49c7
From 3acb27df403c9e5772eb1d81aba1c65b6c7acc08 Mon Sep 17 00:00:00 2001
Nathaniel McCallum ac49c7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Nathaniel McCallum ac49c7
Date: Tue, 5 Sep 2017 09:14:51 +0200
Nathaniel McCallum ac49c7
Subject: [PATCH 1/3] units: order cryptsetup-pre.target before
Nathaniel McCallum ac49c7
 cryptsetup.target
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
Normally this happens automatically, but if it happened that both targets were
Nathaniel McCallum ac49c7
pulled in, even though there were no cryptsetup units, they could be started
Nathaniel McCallum ac49c7
in reverse order, which would be somewhat confusing. Add an explicit ordering
Nathaniel McCallum ac49c7
to avoid this potential issue.
Nathaniel McCallum ac49c7
---
Nathaniel McCallum ac49c7
 units/cryptsetup-pre.target | 1 +
Nathaniel McCallum ac49c7
 1 file changed, 1 insertion(+)
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
diff --git a/units/cryptsetup-pre.target b/units/cryptsetup-pre.target
Nathaniel McCallum ac49c7
index 65353419f..42e35dd4e 100644
Nathaniel McCallum ac49c7
--- a/units/cryptsetup-pre.target
Nathaniel McCallum ac49c7
+++ b/units/cryptsetup-pre.target
Nathaniel McCallum ac49c7
@@ -9,3 +9,4 @@
Nathaniel McCallum ac49c7
 Description=Encrypted Volumes (Pre)
Nathaniel McCallum ac49c7
 Documentation=man:systemd.special(7)
Nathaniel McCallum ac49c7
 RefuseManualStart=yes
Nathaniel McCallum ac49c7
+Before=cryptsetup.target
Nathaniel McCallum ac49c7
-- 
Nathaniel McCallum ac49c7
2.14.1
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
From 51a012da40e8d0d4d8df931b3bc56ea913c3856a Mon Sep 17 00:00:00 2001
Nathaniel McCallum ac49c7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Nathaniel McCallum ac49c7
Date: Tue, 5 Sep 2017 10:15:13 +0200
Nathaniel McCallum ac49c7
Subject: [PATCH 2/3] units: add remote-cryptsetup.target and
Nathaniel McCallum ac49c7
 remote-cryptsetup-pre.target
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
The pair is similar to remote-fs.target and remote-fs-pre.target. Any
Nathaniel McCallum ac49c7
cryptsetup devices which require network shall be ordered after
Nathaniel McCallum ac49c7
remote-cryptsetup-pre.target and before remote-cryptsetup.target.
Nathaniel McCallum ac49c7
---
Nathaniel McCallum ac49c7
 man/systemd.special.xml            | 23 +++++++++++++++++++++++
Nathaniel McCallum ac49c7
 units/cryptsetup-pre.target        |  2 +-
Nathaniel McCallum ac49c7
 units/cryptsetup.target            |  2 +-
Nathaniel McCallum ac49c7
 units/meson.build                  |  3 +++
Nathaniel McCallum ac49c7
 units/remote-cryptsetup-pre.target | 15 +++++++++++++++
Nathaniel McCallum ac49c7
 units/remote-cryptsetup.target     | 10 ++++++++++
Nathaniel McCallum ac49c7
 6 files changed, 53 insertions(+), 2 deletions(-)
Nathaniel McCallum ac49c7
 create mode 100644 units/remote-cryptsetup-pre.target
Nathaniel McCallum ac49c7
 create mode 100644 units/remote-cryptsetup.target
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
diff --git a/man/systemd.special.xml b/man/systemd.special.xml
Nathaniel McCallum ac49c7
index 66c45e39a..7107b8a92 100644
Nathaniel McCallum ac49c7
--- a/man/systemd.special.xml
Nathaniel McCallum ac49c7
+++ b/man/systemd.special.xml
Nathaniel McCallum ac49c7
@@ -81,6 +81,8 @@
Nathaniel McCallum ac49c7
     <filename>poweroff.target</filename>,
Nathaniel McCallum ac49c7
     <filename>printer.target</filename>,
Nathaniel McCallum ac49c7
     <filename>reboot.target</filename>,
Nathaniel McCallum ac49c7
+    <filename>remote-cryptsetup-pre.target</filename>,
Nathaniel McCallum ac49c7
+    <filename>remote-cryptsetup.target</filename>,
Nathaniel McCallum ac49c7
     <filename>remote-fs-pre.target</filename>,
Nathaniel McCallum ac49c7
     <filename>remote-fs.target</filename>,
Nathaniel McCallum ac49c7
     <filename>rescue.target</filename>,
Nathaniel McCallum ac49c7
@@ -450,6 +452,27 @@
Nathaniel McCallum ac49c7
           this target unit, for compatibility with SysV.</para>
Nathaniel McCallum ac49c7
         </listitem>
Nathaniel McCallum ac49c7
       </varlistentry>
Nathaniel McCallum ac49c7
+      <varlistentry>
Nathaniel McCallum ac49c7
+        <term><filename>remote-cryptsetup-pre.target</filename></term>
Nathaniel McCallum ac49c7
+        <listitem>
Nathaniel McCallum ac49c7
+          <para>This target unit is automatically ordered before all cryptsetup devices
Nathaniel McCallum ac49c7
+          marked with the <option>_netdev</option>. It can be used to execute additional
Nathaniel McCallum ac49c7
+          units before such devices are set up.</para>
Nathaniel McCallum ac49c7
+
Nathaniel McCallum ac49c7
+          <para>It is ordered after <filename>network.target</filename> and
Nathaniel McCallum ac49c7
+          <filename>network-online.target</filename>, and also pulls the latter in as a
Nathaniel McCallum ac49c7
+          <varname>Wants=</varname> dependency.</para>
Nathaniel McCallum ac49c7
+        </listitem>
Nathaniel McCallum ac49c7
+      </varlistentry>
Nathaniel McCallum ac49c7
+      <varlistentry>
Nathaniel McCallum ac49c7
+        <term><filename>remote-cryptsetup.target</filename></term>
Nathaniel McCallum ac49c7
+        <listitem>
Nathaniel McCallum ac49c7
+          <para>Similar to <filename>cryptsetup.target</filename>, but for encrypted
Nathaniel McCallum ac49c7
+          devices which are accessed over the network. It is used for
Nathaniel McCallum ac49c7
+          <citerefentry><refentrytitle>crypttab</refentrytitle><manvolnum>8</manvolnum></citerefentry>
Nathaniel McCallum ac49c7
+          entries marked with <option>_netdev</option>.</para>
Nathaniel McCallum ac49c7
+        </listitem>
Nathaniel McCallum ac49c7
+      </varlistentry>
Nathaniel McCallum ac49c7
       <varlistentry>
Nathaniel McCallum ac49c7
         <term><filename>remote-fs.target</filename></term>
Nathaniel McCallum ac49c7
         <listitem>
Nathaniel McCallum ac49c7
diff --git a/units/cryptsetup-pre.target b/units/cryptsetup-pre.target
Nathaniel McCallum ac49c7
index 42e35dd4e..6cb28a61a 100644
Nathaniel McCallum ac49c7
--- a/units/cryptsetup-pre.target
Nathaniel McCallum ac49c7
+++ b/units/cryptsetup-pre.target
Nathaniel McCallum ac49c7
@@ -6,7 +6,7 @@
Nathaniel McCallum ac49c7
 #  (at your option) any later version.
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
 [Unit]
Nathaniel McCallum ac49c7
-Description=Encrypted Volumes (Pre)
Nathaniel McCallum ac49c7
+Description=Local Encrypted Volumes (Pre)
Nathaniel McCallum ac49c7
 Documentation=man:systemd.special(7)
Nathaniel McCallum ac49c7
 RefuseManualStart=yes
Nathaniel McCallum ac49c7
 Before=cryptsetup.target
Nathaniel McCallum ac49c7
diff --git a/units/cryptsetup.target b/units/cryptsetup.target
Nathaniel McCallum ac49c7
index 25d3e33f6..10b17fd38 100644
Nathaniel McCallum ac49c7
--- a/units/cryptsetup.target
Nathaniel McCallum ac49c7
+++ b/units/cryptsetup.target
Nathaniel McCallum ac49c7
@@ -6,5 +6,5 @@
Nathaniel McCallum ac49c7
 #  (at your option) any later version.
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
 [Unit]
Nathaniel McCallum ac49c7
-Description=Encrypted Volumes
Nathaniel McCallum ac49c7
+Description=Local Encrypted Volumes
Nathaniel McCallum ac49c7
 Documentation=man:systemd.special(7)
Nathaniel McCallum ac49c7
diff --git a/units/meson.build b/units/meson.build
Nathaniel McCallum ac49c7
index e94add6a6..e6351c7a2 100644
Nathaniel McCallum ac49c7
--- a/units/meson.build
Nathaniel McCallum ac49c7
+++ b/units/meson.build
Nathaniel McCallum ac49c7
@@ -47,6 +47,9 @@ units = [
Nathaniel McCallum ac49c7
         ['proc-sys-fs-binfmt_misc.mount',       'ENABLE_BINFMT'],
Nathaniel McCallum ac49c7
         ['reboot.target',                       '',
Nathaniel McCallum ac49c7
          'runlevel6.target ctrl-alt-del.target'],
Nathaniel McCallum ac49c7
+        ['remote-cryptsetup-pre.target',        'HAVE_LIBCRYPTSETUP'],
Nathaniel McCallum ac49c7
+        ['remote-cryptsetup.target',            'HAVE_LIBCRYPTSETUP',
Nathaniel McCallum ac49c7
+         join_paths(pkgsysconfdir, 'system/multi-user.target.wants/')],
Nathaniel McCallum ac49c7
         ['remote-fs-pre.target',                ''],
Nathaniel McCallum ac49c7
         ['remote-fs.target',                    '',
Nathaniel McCallum ac49c7
          join_paths(pkgsysconfdir, 'system/multi-user.target.wants/')],
Nathaniel McCallum ac49c7
diff --git a/units/remote-cryptsetup-pre.target b/units/remote-cryptsetup-pre.target
Nathaniel McCallum ac49c7
new file mode 100644
Nathaniel McCallum ac49c7
index 000000000..a375e6188
Nathaniel McCallum ac49c7
--- /dev/null
Nathaniel McCallum ac49c7
+++ b/units/remote-cryptsetup-pre.target
Nathaniel McCallum ac49c7
@@ -0,0 +1,15 @@
Nathaniel McCallum ac49c7
+#  This file is part of systemd.
Nathaniel McCallum ac49c7
+#
Nathaniel McCallum ac49c7
+#  systemd is free software; you can redistribute it and/or modify it
Nathaniel McCallum ac49c7
+#  under the terms of the GNU Lesser General Public License as published by
Nathaniel McCallum ac49c7
+#  the Free Software Foundation; either version 2.1 of the License, or
Nathaniel McCallum ac49c7
+#  (at your option) any later version.
Nathaniel McCallum ac49c7
+
Nathaniel McCallum ac49c7
+[Unit]
Nathaniel McCallum ac49c7
+Description=Remote Encrypted Volumes (Pre)
Nathaniel McCallum ac49c7
+Documentation=man:systemd.special(7)
Nathaniel McCallum ac49c7
+RefuseManualStart=yes
Nathaniel McCallum ac49c7
+Before=remote-cryptsetup.target
Nathaniel McCallum ac49c7
+
Nathaniel McCallum ac49c7
+After=network.target network-online.target
Nathaniel McCallum ac49c7
+Wants=network-online.target
Nathaniel McCallum ac49c7
diff --git a/units/remote-cryptsetup.target b/units/remote-cryptsetup.target
Nathaniel McCallum ac49c7
new file mode 100644
Nathaniel McCallum ac49c7
index 000000000..60943bd1c
Nathaniel McCallum ac49c7
--- /dev/null
Nathaniel McCallum ac49c7
+++ b/units/remote-cryptsetup.target
Nathaniel McCallum ac49c7
@@ -0,0 +1,10 @@
Nathaniel McCallum ac49c7
+#  This file is part of systemd.
Nathaniel McCallum ac49c7
+#
Nathaniel McCallum ac49c7
+#  systemd is free software; you can redistribute it and/or modify it
Nathaniel McCallum ac49c7
+#  under the terms of the GNU Lesser General Public License as published by
Nathaniel McCallum ac49c7
+#  the Free Software Foundation; either version 2.1 of the License, or
Nathaniel McCallum ac49c7
+#  (at your option) any later version.
Nathaniel McCallum ac49c7
+
Nathaniel McCallum ac49c7
+[Unit]
Nathaniel McCallum ac49c7
+Description=Remote Encrypted Volumes
Nathaniel McCallum ac49c7
+Documentation=man:systemd.special(7)
Nathaniel McCallum ac49c7
-- 
Nathaniel McCallum ac49c7
2.14.1
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
From 543a62336565c840bbda22df0eb2a1c19180a8d5 Mon Sep 17 00:00:00 2001
Nathaniel McCallum ac49c7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Nathaniel McCallum ac49c7
Date: Tue, 5 Sep 2017 11:30:33 +0200
Nathaniel McCallum ac49c7
Subject: [PATCH 3/3] cryptsetup-generator: use remote-cryptsetup.target when
Nathaniel McCallum ac49c7
 _netdev is present
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
This allows such devices to depend on the network. Their startup will
Nathaniel McCallum ac49c7
be delayed similarly to network mount units.
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
Fixes #4642.
Nathaniel McCallum ac49c7
---
Nathaniel McCallum ac49c7
 man/crypttab.xml                      | 13 +++++++++++++
Nathaniel McCallum ac49c7
 src/cryptsetup/cryptsetup-generator.c | 36 ++++++++++++++++++-----------------
Nathaniel McCallum ac49c7
 2 files changed, 32 insertions(+), 17 deletions(-)
Nathaniel McCallum ac49c7
Nathaniel McCallum ac49c7
diff --git a/man/crypttab.xml b/man/crypttab.xml
Nathaniel McCallum ac49c7
index 17976f370..162377ebc 100644
Nathaniel McCallum ac49c7
--- a/man/crypttab.xml
Nathaniel McCallum ac49c7
+++ b/man/crypttab.xml
Nathaniel McCallum ac49c7
@@ -213,6 +213,19 @@
Nathaniel McCallum ac49c7
         <option>size=</option>.</para></listitem>
Nathaniel McCallum ac49c7
       </varlistentry>
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
+      <varlistentry>
Nathaniel McCallum ac49c7
+        <term><option>_netdev</option></term>
Nathaniel McCallum ac49c7
+
Nathaniel McCallum ac49c7
+        <listitem><para>Marks this cryptsetup device as requiring network. It will be
Nathaniel McCallum ac49c7
+        started after the network is available, similarly to
Nathaniel McCallum ac49c7
+        <citerefentry><refentrytitle>systemd.mount</refentrytitle><manvolnum>5</manvolnum></citerefentry>
Nathaniel McCallum ac49c7
+        units marked with <option>_netdev</option>. The service unit to set up this device
Nathaniel McCallum ac49c7
+        will be ordered between <filename>remote-cryptsetup-pre.target</filename> and
Nathaniel McCallum ac49c7
+        <filename>remote-cryptsetup.target</filename>, instead of
Nathaniel McCallum ac49c7
+        <filename>cryptsetup-pre.target</filename> and
Nathaniel McCallum ac49c7
+        <filename>cryptsetup.target</filename>.</para></listitem>
Nathaniel McCallum ac49c7
+      </varlistentry>
Nathaniel McCallum ac49c7
+
Nathaniel McCallum ac49c7
       <varlistentry>
Nathaniel McCallum ac49c7
         <term><option>noauto</option></term>
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
Nathaniel McCallum ac49c7
index b58b6db7c..8571ab06e 100644
Nathaniel McCallum ac49c7
--- a/src/cryptsetup/cryptsetup-generator.c
Nathaniel McCallum ac49c7
+++ b/src/cryptsetup/cryptsetup-generator.c
Nathaniel McCallum ac49c7
@@ -61,7 +61,7 @@ static int create_disk(
Nathaniel McCallum ac49c7
         _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *to = NULL, *e = NULL,
Nathaniel McCallum ac49c7
                 *filtered = NULL;
Nathaniel McCallum ac49c7
         _cleanup_fclose_ FILE *f = NULL;
Nathaniel McCallum ac49c7
-        bool noauto, nofail, tmp, swap;
Nathaniel McCallum ac49c7
+        bool noauto, nofail, tmp, swap, netdev;
Nathaniel McCallum ac49c7
         char *from;
Nathaniel McCallum ac49c7
         int r;
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
@@ -72,6 +72,7 @@ static int create_disk(
Nathaniel McCallum ac49c7
         nofail = fstab_test_yes_no_option(options, "nofail\0" "fail\0");
Nathaniel McCallum ac49c7
         tmp = fstab_test_option(options, "tmp\0");
Nathaniel McCallum ac49c7
         swap = fstab_test_option(options, "swap\0");
Nathaniel McCallum ac49c7
+        netdev = fstab_test_option(options, "_netdev\0");
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
         if (tmp && swap) {
Nathaniel McCallum ac49c7
                 log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
Nathaniel McCallum ac49c7
@@ -102,21 +103,22 @@ static int create_disk(
Nathaniel McCallum ac49c7
         if (!f)
Nathaniel McCallum ac49c7
                 return log_error_errno(errno, "Failed to create unit file %s: %m", p);
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
-        fputs("# Automatically generated by systemd-cryptsetup-generator\n\n"
Nathaniel McCallum ac49c7
-              "[Unit]\n"
Nathaniel McCallum ac49c7
-              "Description=Cryptography Setup for %I\n"
Nathaniel McCallum ac49c7
-              "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
Nathaniel McCallum ac49c7
-              "SourcePath=/etc/crypttab\n"
Nathaniel McCallum ac49c7
-              "DefaultDependencies=no\n"
Nathaniel McCallum ac49c7
-              "Conflicts=umount.target\n"
Nathaniel McCallum ac49c7
-              "BindsTo=dev-mapper-%i.device\n"
Nathaniel McCallum ac49c7
-              "IgnoreOnIsolate=true\n"
Nathaniel McCallum ac49c7
-              "After=cryptsetup-pre.target\n",
Nathaniel McCallum ac49c7
-              f);
Nathaniel McCallum ac49c7
+        fprintf(f,
Nathaniel McCallum ac49c7
+                "# Automatically generated by systemd-cryptsetup-generator\n\n"
Nathaniel McCallum ac49c7
+                "[Unit]\n"
Nathaniel McCallum ac49c7
+                "Description=Cryptography Setup for %%I\n"
Nathaniel McCallum ac49c7
+                "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
Nathaniel McCallum ac49c7
+                "SourcePath=/etc/crypttab\n"
Nathaniel McCallum ac49c7
+                "DefaultDependencies=no\n"
Nathaniel McCallum ac49c7
+                "Conflicts=umount.target\n"
Nathaniel McCallum ac49c7
+                "IgnoreOnIsolate=true\n"
Nathaniel McCallum ac49c7
+                "After=%s\n",
Nathaniel McCallum ac49c7
+                netdev ? "remote-cryptsetup-pre.target" : "cryptsetup-pre.target");
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
         if (!nofail)
Nathaniel McCallum ac49c7
                 fprintf(f,
Nathaniel McCallum ac49c7
-                        "Before=cryptsetup.target\n");
Nathaniel McCallum ac49c7
+                        "Before=%s\n",
Nathaniel McCallum ac49c7
+                        netdev ? "remote-cryptsetup.target" : "cryptsetup.target");
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
         if (password) {
Nathaniel McCallum ac49c7
                 if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
Nathaniel McCallum ac49c7
@@ -200,10 +202,10 @@ static int create_disk(
Nathaniel McCallum ac49c7
                         return log_error_errno(errno, "Failed to create symlink %s: %m", to);
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
                 free(to);
Nathaniel McCallum ac49c7
-                if (!nofail)
Nathaniel McCallum ac49c7
-                        to = strjoin(arg_dest, "/cryptsetup.target.requires/", n);
Nathaniel McCallum ac49c7
-                else
Nathaniel McCallum ac49c7
-                        to = strjoin(arg_dest, "/cryptsetup.target.wants/", n);
Nathaniel McCallum ac49c7
+                to = strjoin(arg_dest,
Nathaniel McCallum ac49c7
+                             netdev ? "/remote-cryptsetup" : "/cryptsetup",
Nathaniel McCallum ac49c7
+                             ".target.",
Nathaniel McCallum ac49c7
+                             nofail ? "wants/" : "requires/", n);
Nathaniel McCallum ac49c7
                 if (!to)
Nathaniel McCallum ac49c7
                         return log_oom();
Nathaniel McCallum ac49c7
 
Nathaniel McCallum ac49c7
-- 
Nathaniel McCallum ac49c7
2.14.1
Nathaniel McCallum ac49c7