f37345
From 2aba050e74dc5df483da022dcf436b101c7a4301 Mon Sep 17 00:00:00 2001
f37345
From: Vojtech Trefny <vtrefny@redhat.com>
f37345
Date: Wed, 11 Jan 2023 14:59:24 +0100
f37345
Subject: [PATCH] Default to encryption sector size 512 for LUKS devices
f37345
f37345
We are currently letting cryptsetup decide the optimal encryption
f37345
sector size for LUKS. The problem is that for disks with physical
f37345
sector size 4096 cryptsetup will default to 4096 encryption sector
f37345
size even if the drive logical sector size is 512 which means
f37345
these disks cannot be combined with other 512 logical sector size
f37345
disks in LVM. This requires a more sophisticated solution in the
f37345
future, but for now just default to 512 if not specified by the
f37345
user otherwise.
f37345
f37345
Resolves: rhbz#2103800
f37345
---
f37345
 blivet/formats/luks.py                      | 10 +++++++---
f37345
 tests/unit_tests/formats_tests/luks_test.py |  2 +-
f37345
 2 files changed, 8 insertions(+), 4 deletions(-)
f37345
f37345
diff --git a/blivet/formats/luks.py b/blivet/formats/luks.py
f37345
index 8de4911f..2637e0c5 100644
f37345
--- a/blivet/formats/luks.py
f37345
+++ b/blivet/formats/luks.py
f37345
@@ -166,9 +166,13 @@ class LUKS(DeviceFormat):
f37345
             if self.pbkdf_args.type == "pbkdf2" and self.pbkdf_args.max_memory_kb:
f37345
                 log.warning("Memory limit is not used for pbkdf2 and it will be ignored.")
f37345
 
f37345
-        self.luks_sector_size = kwargs.get("luks_sector_size") or 0
f37345
-        if self.luks_sector_size and self.luks_version != "luks2":
f37345
-            raise ValueError("Sector size argument is valid only for LUKS version 2.")
f37345
+        self.luks_sector_size = kwargs.get("luks_sector_size")
f37345
+        if self.luks_version == "luks2":
f37345
+            if self.luks_sector_size is None:
f37345
+                self.luks_sector_size = 512  # XXX we don't want cryptsetup choose automatically here so fallback to 512
f37345
+        else:
f37345
+            if self.luks_sector_size:
f37345
+                raise ValueError("Sector size argument is valid only for LUKS version 2.")
f37345
 
f37345
     def __repr__(self):
f37345
         s = DeviceFormat.__repr__(self)
f37345
diff --git a/tests/unit_tests/formats_tests/luks_test.py b/tests/unit_tests/formats_tests/luks_test.py
f37345
index 5ae6acfe..ec7b7592 100644
f37345
--- a/tests/unit_tests/formats_tests/luks_test.py
f37345
+++ b/tests/unit_tests/formats_tests/luks_test.py
f37345
@@ -53,7 +53,7 @@ class LUKSNodevTestCase(unittest.TestCase):
f37345
 
f37345
     def test_sector_size(self):
f37345
         fmt = LUKS()
f37345
-        self.assertEqual(fmt.luks_sector_size, 0)
f37345
+        self.assertEqual(fmt.luks_sector_size, 512)
f37345
 
f37345
         with self.assertRaises(ValueError):
f37345
             fmt = LUKS(luks_version="luks1", luks_sector_size=4096)
f37345
-- 
f37345
2.39.0
f37345