]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cryptsetup: call crypt_load() for LUKS only once
authorMilan Broz <gmazyland@gmail.com>
Mon, 27 May 2019 07:43:03 +0000 (09:43 +0200)
committerMilan Broz <gmazyland@gmail.com>
Tue, 28 May 2019 13:31:25 +0000 (15:31 +0200)
The crypt_load() for LUKS2 can read a quite big area of disk
(metadata area size is configurable and can increase up to megabytes).

This initialization is not needed to be repeated, just use the existing context.

(This patch is also required for the following change.)

src/cryptsetup/cryptsetup.c

index bf523fe50d3d2b2b86f664bcea3f2489abe0462e..1f891764b6a6738251eec4371166caf5d1a07e63 100644 (file)
@@ -487,7 +487,6 @@ static int attach_tcrypt(
 static int attach_luks_or_plain(struct crypt_device *cd,
                                 const char *name,
                                 const char *key_file,
-                                const char *data_device,
                                 char **passwords,
                                 uint32_t flags) {
         int r = 0;
@@ -497,18 +496,6 @@ static int attach_luks_or_plain(struct crypt_device *cd,
         assert(name);
         assert(key_file || passwords);
 
-        if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1)) {
-                r = crypt_load(cd, CRYPT_LUKS, NULL);
-                if (r < 0)
-                        return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
-
-                if (data_device) {
-                        r = crypt_set_data_device(cd, data_device);
-                        if (r < 0)
-                                return log_error_errno(r, "Failed to set LUKS data device %s: %m", data_device);
-                }
-        }
-
         if ((!arg_type && !crypt_get_type(cd)) || streq_ptr(arg_type, CRYPT_PLAIN)) {
                 struct crypt_params_plain params = {
                         .offset = arg_offset,
@@ -718,6 +705,18 @@ static int run(int argc, char *argv[]) {
                                 log_warning("Key file %s is world-readable. This is not a good idea!", key_file);
                 }
 
+                if (!arg_type || STR_IN_SET(arg_type, ANY_LUKS, CRYPT_LUKS1)) {
+                        r = crypt_load(cd, CRYPT_LUKS, NULL);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to load LUKS superblock on device %s: %m", crypt_get_device_name(cd));
+
+                        if (arg_header) {
+                                r = crypt_set_data_device(cd, argv[3]);
+                                if (r < 0)
+                                        return log_error_errno(r, "Failed to set LUKS data device %s: %m", argv[3]);
+                        }
+                }
+
                 for (tries = 0; arg_tries == 0 || tries < arg_tries; tries++) {
                         _cleanup_strv_free_erase_ char **passwords = NULL;
 
@@ -735,7 +734,6 @@ static int run(int argc, char *argv[]) {
                                 r = attach_luks_or_plain(cd,
                                                          argv[2],
                                                          key_file,
-                                                         arg_header ? argv[3] : NULL,
                                                          passwords,
                                                          flags);
                         if (r >= 0)