File cryptsetup-Add-a-better-warning-if-luksFormat-no-space-for-data.patch of Package cryptsetup.36961
From 819902a33a4098628f47d8d6a765095a1935fde8 Mon Sep 17 00:00:00 2001
From: Milan Broz <gmazyland@gmail.com>
Date: Thu, 17 Nov 2022 21:41:36 +0100
Subject: [PATCH] Add a better warning if luksFormat ends with image without
any space for data.
Header write can call falloc() to increase image size, so we should
check data offset after header is written.
Also change wording to be less cryptic and describe what is the real problem.
Note that the code can be used this way to crate detached header (without
space for data), so it is not an error.
---
lib/setup.c | 29 +++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)
Index: cryptsetup-2.4.3/lib/setup.c
===================================================================
--- cryptsetup-2.4.3.orig/lib/setup.c
+++ cryptsetup-2.4.3/lib/setup.c
@@ -1681,9 +1681,6 @@ static int _crypt_format_luks1(struct cr
if (r < 0)
return r;
- if (!device_size(crypt_data_device(cd), &dev_size) &&
- dev_size < (crypt_get_data_offset(cd) * SECTOR_SIZE))
- log_std(cd, _("WARNING: Data offset is outside of currently available data device.\n"));
if (asprintf(&cd->u.luks1.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
cd->u.luks1.cipher_spec = NULL;
@@ -1699,10 +1696,17 @@ static int _crypt_format_luks1(struct cr
}
r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
- if (r)
+ if (r) {
free(cd->u.luks1.cipher_spec);
+ return r;
+ }
+
+ if (!device_size(crypt_data_device(cd), &dev_size) &&
+ dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
+ log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
+ device_path(crypt_data_device(cd)));
- return r;
+ return 0;
}
static int _crypt_format_luks2(struct crypt_device *cd,
@@ -1878,9 +1882,6 @@ static int _crypt_format_luks2(struct cr
if (r < 0)
goto out;
- if (dev_size < (crypt_get_data_offset(cd) * SECTOR_SIZE))
- log_std(cd, _("WARNING: Data offset is outside of currently available data device.\n"));
-
if (cd->metadata_size && (cd->metadata_size != LUKS2_metadata_size(&cd->u.luks2.hdr)))
log_std(cd, _("WARNING: LUKS2 metadata size changed to %" PRIu64 " bytes.\n"),
LUKS2_metadata_size(&cd->u.luks2.hdr));
@@ -1961,10 +1962,18 @@ static int _crypt_format_luks2(struct cr
}
out:
- if (r)
+ if (r) {
LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
+ return r;
+ }
+
+ /* Device size can be larger now if it is a file container */
+ if (!device_size(crypt_data_device(cd), &dev_size) &&
+ dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
+ log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
+ device_path(crypt_data_device(cd)));
- return r;
+ return 0;
}
static int _crypt_format_loopaes(struct crypt_device *cd,