a4b143
From 61ca2800f3a1666478bdc01fc796d36c917a5032 Mon Sep 17 00:00:00 2001
a4b143
From: Tom Gundersen <teg@jklm.no>
a4b143
Date: Sun, 18 Aug 2013 14:59:00 +0800
a4b143
Subject: [PATCH] cryptsetup-generator: allow specifying options in
a4b143
 /proc/cmdline
a4b143
MIME-Version: 1.0
a4b143
Content-Type: text/plain; charset=UTF-8
a4b143
Content-Transfer-Encoding: 8bit
a4b143
a4b143
The main usecase for this is to make it possible to use cryptsetup in
a4b143
the initrd without it having to include a host-specific /etc/crypttab.
a4b143
a4b143
Tested-by: Thomas Bächler <thomas@archlinux.org>
a4b143
---
a4b143
 man/systemd-cryptsetup-generator.xml  | 23 ++++++++++
a4b143
 src/cryptsetup/cryptsetup-generator.c | 79 +++++++++++++++++++++++++++++++++--
a4b143
 2 files changed, 98 insertions(+), 4 deletions(-)
a4b143
a4b143
diff --git a/man/systemd-cryptsetup-generator.xml b/man/systemd-cryptsetup-generator.xml
a4b143
index 215ac2d..d6b7e49 100644
a4b143
--- a/man/systemd-cryptsetup-generator.xml
a4b143
+++ b/man/systemd-cryptsetup-generator.xml
a4b143
@@ -137,6 +137,29 @@
a4b143
                                 will be activated in the initrd or the real root.</para>
a4b143
                                 </listitem>
a4b143
                         </varlistentry>
a4b143
+
a4b143
+                        <varlistentry>
a4b143
+                                <term><varname>luks.options=</varname></term>
a4b143
+                                <term><varname>rd.luks.options=</varname></term>
a4b143
+
a4b143
+                                <listitem><para>Takes a LUKS super
a4b143
+                                block UUID followed by an '=' and a string
a4b143
+                                of options separated by commas as argument.
a4b143
+                                This will override the options for the given
a4b143
+                                UUID.</para>
a4b143
+                                <para>If only a list of options, without an
a4b143
+                                UUID, is specified, they apply to any UUIDs not
a4b143
+                                specified elsewhere, and without an entry in
a4b143
+                                /etc/crypttab.</para><para>
a4b143
+                                <varname>rd.luks.options=</varname>
a4b143
+                                is honored only by initial RAM disk
a4b143
+                                (initrd) while
a4b143
+                                <varname>luks.options=</varname> is
a4b143
+                                honored by both the main system and
a4b143
+                                the initrd.</para>
a4b143
+                                </listitem>
a4b143
+                        </varlistentry>
a4b143
+
a4b143
                         <varlistentry>
a4b143
                                 <term><varname>luks.key=</varname></term>
a4b143
                                 <term><varname>rd.luks.key=</varname></term>
a4b143
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
a4b143
index 967c5e6..ba6efa6 100644
a4b143
--- a/src/cryptsetup/cryptsetup-generator.c
a4b143
+++ b/src/cryptsetup/cryptsetup-generator.c
a4b143
@@ -240,7 +240,7 @@ static int create_disk(
a4b143
         return 0;
a4b143
 }
a4b143
 
a4b143
-static int parse_proc_cmdline(char ***arg_proc_cmdline_disks, char **arg_proc_cmdline_keyfile) {
a4b143
+static int parse_proc_cmdline(char ***arg_proc_cmdline_disks, char ***arg_proc_cmdline_options, char **arg_proc_cmdline_keyfile) {
a4b143
         _cleanup_free_ char *line = NULL;
a4b143
         char *w = NULL, *state = NULL;
a4b143
         int r;
a4b143
@@ -307,7 +307,20 @@ static int parse_proc_cmdline(char ***arg_proc_cmdline_disks, char **arg_proc_cm
a4b143
                                         return log_oom();
a4b143
                         }
a4b143
 
a4b143
+                } else if (startswith(word, "luks.options=")) {
a4b143
+                        if (strv_extend(arg_proc_cmdline_options, word + 13) < 0)
a4b143
+                                return log_oom();
a4b143
+
a4b143
+                } else if (startswith(word, "rd.luks.options=")) {
a4b143
+
a4b143
+                        if (in_initrd()) {
a4b143
+                                if (strv_extend(arg_proc_cmdline_options, word + 16) < 0)
a4b143
+                                        return log_oom();
a4b143
+                        }
a4b143
+
a4b143
                 } else if (startswith(word, "luks.key=")) {
a4b143
+                        if (*arg_proc_cmdline_keyfile)
a4b143
+                                free(*arg_proc_cmdline_keyfile);
a4b143
                         *arg_proc_cmdline_keyfile = strdup(word + 9);
a4b143
                         if (!*arg_proc_cmdline_keyfile)
a4b143
                                 return log_oom();
a4b143
@@ -337,6 +350,7 @@ static int parse_proc_cmdline(char ***arg_proc_cmdline_disks, char **arg_proc_cm
a4b143
 int main(int argc, char *argv[]) {
a4b143
         _cleanup_strv_free_ char **arg_proc_cmdline_disks_done = NULL;
a4b143
         _cleanup_strv_free_ char **arg_proc_cmdline_disks = NULL;
a4b143
+        _cleanup_strv_free_ char **arg_proc_cmdline_options = NULL;
a4b143
         _cleanup_free_ char *arg_proc_cmdline_keyfile = NULL;
a4b143
         _cleanup_fclose_ FILE *f = NULL;
a4b143
         unsigned n = 0;
a4b143
@@ -357,7 +371,7 @@ int main(int argc, char *argv[]) {
a4b143
 
a4b143
         umask(0022);
a4b143
 
a4b143
-        if (parse_proc_cmdline(&arg_proc_cmdline_disks, &arg_proc_cmdline_keyfile) < 0)
a4b143
+        if (parse_proc_cmdline(&arg_proc_cmdline_disks, &arg_proc_cmdline_options, &arg_proc_cmdline_keyfile) < 0)
a4b143
                 return EXIT_FAILURE;
a4b143
 
a4b143
         if (!arg_enabled)
a4b143
@@ -412,6 +426,26 @@ int main(int argc, char *argv[]) {
a4b143
                                 continue;
a4b143
                         }
a4b143
 
a4b143
+                        if (arg_proc_cmdline_options) {
a4b143
+                                /*
a4b143
+                                  If options are specified on the kernel commandline, let them override
a4b143
+                                  the ones from crypttab.
a4b143
+                                */
a4b143
+                                STRV_FOREACH(i, arg_proc_cmdline_options) {
a4b143
+                                        _cleanup_free_ char *proc_uuid = NULL, *proc_options = NULL;
a4b143
+                                        const char *p = *i;
a4b143
+
a4b143
+                                        k = sscanf(p, "%m[0-9a-fA-F-]=%ms", &proc_uuid, &proc_options);
a4b143
+                                        if (k == 2 && streq(proc_uuid, device + 5)) {
a4b143
+                                                if (options)
a4b143
+                                                        free(options);
a4b143
+                                                options = strdup(p);
a4b143
+                                                if (!proc_options)
a4b143
+                                                        return log_oom();
a4b143
+                                        }
a4b143
+                                }
a4b143
+                        }
a4b143
+
a4b143
                         if (arg_proc_cmdline_disks) {
a4b143
                                 /*
a4b143
                                   If luks UUIDs are specified on the kernel command line, use them as a filter
a4b143
@@ -452,7 +486,7 @@ next:
a4b143
                   on the kernel command line and not yet written.
a4b143
                 */
a4b143
 
a4b143
-                _cleanup_free_ char *name = NULL, *device = NULL;
a4b143
+                _cleanup_free_ char *name = NULL, *device = NULL, *options = NULL;
a4b143
                 const char *p = *i;
a4b143
 
a4b143
                 if (startswith(p, "luks-"))
a4b143
@@ -467,7 +501,44 @@ next:
a4b143
                 if (!name || !device)
a4b143
                         return log_oom();
a4b143
 
a4b143
-                if (create_disk(name, device, arg_proc_cmdline_keyfile, "timeout=0") < 0)
a4b143
+                if (arg_proc_cmdline_options) {
a4b143
+                        /*
a4b143
+                          If options are specified on the kernel commandline, use them.
a4b143
+                        */
a4b143
+                        char **j;
a4b143
+
a4b143
+                        STRV_FOREACH(j, arg_proc_cmdline_options) {
a4b143
+                                _cleanup_free_ char *proc_uuid = NULL, *proc_options = NULL;
a4b143
+                                const char *s = *j;
a4b143
+                                int k;
a4b143
+
a4b143
+                                k = sscanf(s, "%m[0-9a-fA-F-]=%ms", &proc_uuid, &proc_options);
a4b143
+                                if (k == 2) {
a4b143
+                                        if (streq(proc_uuid, device + 5)) {
a4b143
+                                                if (options)
a4b143
+                                                        free(options);
a4b143
+                                                options = strdup(proc_options);
a4b143
+                                                if (!options)
a4b143
+                                                        return log_oom();
a4b143
+                                        }
a4b143
+                                } else if (!options) {
a4b143
+                                        /*
a4b143
+                                          Fall back to options without a specified UUID
a4b143
+                                        */
a4b143
+                                        options = strdup(s);
a4b143
+                                        if (!options)
a4b143
+                                                return log_oom();
a4b143
+                                }
a4b143
+                        }
a4b143
+                }
a4b143
+
a4b143
+                if (!options) {
a4b143
+                        options = strdup("timeout=0");
a4b143
+                        if (!options)
a4b143
+                                return log_oom();
a4b143
+                }
a4b143
+
a4b143
+                if (create_disk(name, device, arg_proc_cmdline_keyfile, options) < 0)
a4b143
                         r = EXIT_FAILURE;
a4b143
         }
a4b143