3d02e2
From dc1e2fe7783748528cac2f7aa516c89d1959b052 Mon Sep 17 00:00:00 2001
3d02e2
From: Vojtech Trefny <vtrefny@redhat.com>
3d02e2
Date: Thu, 29 Jul 2021 14:44:22 +0200
3d02e2
Subject: [PATCH] Do not set chunk size for RAID 1
3d02e2
3d02e2
Setting chunk size for RAID 1 doesn't make sense and latest
3d02e2
mdadm started returning error instead of ignoring the --chunk
3d02e2
option when creating an array.
3d02e2
3d02e2
Resolves: rhbz#1987170
3d02e2
---
3d02e2
 blivet/devicelibs/raid.py     | 12 ++++++++++
3d02e2
 blivet/devices/md.py          | 15 ++++++++++---
3d02e2
 tests/devices_test/md_test.py | 41 +++++++++++++++++++++++++++++++++--
3d02e2
 3 files changed, 63 insertions(+), 5 deletions(-)
3d02e2
3d02e2
diff --git a/blivet/devicelibs/raid.py b/blivet/devicelibs/raid.py
3d02e2
index 19c3fae98..a9e241c7a 100644
3d02e2
--- a/blivet/devicelibs/raid.py
3d02e2
+++ b/blivet/devicelibs/raid.py
3d02e2
@@ -462,6 +462,18 @@ def _pad(self, size, chunk_size):
3d02e2
     def _get_recommended_stride(self, member_count):
3d02e2
         return None
3d02e2
 
3d02e2
+    def get_size(self, member_sizes, num_members=None, chunk_size=None, superblock_size_func=None):
3d02e2
+        if not member_sizes:
3d02e2
+            return Size(0)
3d02e2
+
3d02e2
+        if num_members is None:
3d02e2
+            num_members = len(member_sizes)
3d02e2
+
3d02e2
+        min_size = min(member_sizes)
3d02e2
+        superblock_size = superblock_size_func(min_size)
3d02e2
+        min_data_size = self._trim(min_size - superblock_size, chunk_size)
3d02e2
+        return self.get_net_array_size(num_members, min_data_size)
3d02e2
+
3d02e2
 
3d02e2
 RAID1 = RAID1()
3d02e2
 ALL_LEVELS.add_raid_level(RAID1)
3d02e2
diff --git a/blivet/devices/md.py b/blivet/devices/md.py
3d02e2
index 69eee93a5..d1a2faf1f 100644
3d02e2
--- a/blivet/devices/md.py
3d02e2
+++ b/blivet/devices/md.py
3d02e2
@@ -138,7 +138,7 @@ def __init__(self, name, level=None, major=None, minor=None, size=None,
3d02e2
         if self.exists:
3d02e2
             self._chunk_size = self.read_chunk_size()
3d02e2
         else:
3d02e2
-            self._chunk_size = chunk_size or mdraid.MD_CHUNK_SIZE
3d02e2
+            self.chunk_size = chunk_size or Size(0)
3d02e2
 
3d02e2
         if not self.exists and not isinstance(metadata_version, str):
3d02e2
             self.metadata_version = "default"
3d02e2
@@ -208,8 +208,14 @@ def sector_size(self):
3d02e2
 
3d02e2
     @property
3d02e2
     def chunk_size(self):
3d02e2
-        if self.exists and self._chunk_size == Size(0):
3d02e2
-            self._chunk_size = self.read_chunk_size()
3d02e2
+        if self._chunk_size == Size(0):
3d02e2
+            if self.exists:
3d02e2
+                return self.read_chunk_size()
3d02e2
+            else:
3d02e2
+                if self.level == raid.RAID1:
3d02e2
+                    return self._chunk_size
3d02e2
+                else:
3d02e2
+                    return mdraid.MD_CHUNK_SIZE
3d02e2
         return self._chunk_size
3d02e2
 
3d02e2
     @chunk_size.setter
3d02e2
@@ -223,6 +229,9 @@ def chunk_size(self, newsize):
3d02e2
         if self.exists:
3d02e2
             raise ValueError("cannot set chunk size for an existing device")
3d02e2
 
3d02e2
+        if self.level == raid.RAID1 and newsize != Size(0):
3d02e2
+            raise ValueError("specifying chunk size is not allowed for raid1")
3d02e2
+
3d02e2
         self._chunk_size = newsize
3d02e2
 
3d02e2
     def read_chunk_size(self):
3d02e2
diff --git a/tests/devices_test/md_test.py b/tests/devices_test/md_test.py
3d02e2
index 46df76d3d..47a0fa0cc 100644
3d02e2
--- a/tests/devices_test/md_test.py
3d02e2
+++ b/tests/devices_test/md_test.py
3d02e2
@@ -1,6 +1,11 @@
3d02e2
 import six
3d02e2
 import unittest
3d02e2
 
3d02e2
+try:
3d02e2
+    from unittest.mock import patch
3d02e2
+except ImportError:
3d02e2
+    from mock import patch
3d02e2
+
3d02e2
 import blivet
3d02e2
 
3d02e2
 from blivet.devices import StorageDevice
3d02e2
@@ -27,9 +32,27 @@ def test_chunk_size1(self):
3d02e2
         raid_array = MDRaidArrayDevice(name="raid", level="raid0", member_devices=2,
3d02e2
                                        total_devices=2, parents=[member1, member2])
3d02e2
 
3d02e2
-        # no chunk_size specified -- default value
3d02e2
+        # no chunk_size specified and RAID0 -- default value
3d02e2
         self.assertEqual(raid_array.chunk_size, mdraid.MD_CHUNK_SIZE)
3d02e2
 
3d02e2
+        with patch("blivet.devices.md.blockdev.md.create") as md_create:
3d02e2
+            raid_array._create()
3d02e2
+            md_create.assert_called_with("/dev/md/raid", "raid0", ["/dev/member1", "/dev/member2"],
3d02e2
+                                         0, version="default", bitmap=False,
3d02e2
+                                         chunk_size=mdraid.MD_CHUNK_SIZE)
3d02e2
+
3d02e2
+        raid_array = MDRaidArrayDevice(name="raid", level="raid1", member_devices=2,
3d02e2
+                                       total_devices=2, parents=[member1, member2])
3d02e2
+
3d02e2
+        # no chunk_size specified and RAID1 -- no chunk size set (0)
3d02e2
+        self.assertEqual(raid_array.chunk_size, Size(0))
3d02e2
+
3d02e2
+        with patch("blivet.devices.md.blockdev.md.create") as md_create:
3d02e2
+            raid_array._create()
3d02e2
+            md_create.assert_called_with("/dev/md/raid", "raid1", ["/dev/member1", "/dev/member2"],
3d02e2
+                                         0, version="default", bitmap=True,
3d02e2
+                                         chunk_size=0)
3d02e2
+
3d02e2
     def test_chunk_size2(self):
3d02e2
 
3d02e2
         member1 = StorageDevice("member1", fmt=blivet.formats.get_format("mdmember"),
3d02e2
@@ -40,11 +63,25 @@ def test_chunk_size2(self):
3d02e2
         raid_array = MDRaidArrayDevice(name="raid", level="raid0", member_devices=2,
3d02e2
                                        total_devices=2, parents=[member1, member2],
3d02e2
                                        chunk_size=Size("1024 KiB"))
3d02e2
-
3d02e2
         self.assertEqual(raid_array.chunk_size, Size("1024 KiB"))
3d02e2
 
3d02e2
+        # for raid0 setting chunk_size = 0 means "default"
3d02e2
+        raid_array.chunk_size = Size(0)
3d02e2
+        self.assertEqual(raid_array.chunk_size, mdraid.MD_CHUNK_SIZE)
3d02e2
+
3d02e2
         with six.assertRaisesRegex(self, ValueError, "new chunk size must be of type Size"):
3d02e2
             raid_array.chunk_size = 1
3d02e2
 
3d02e2
         with six.assertRaisesRegex(self, ValueError, "new chunk size must be multiple of 4 KiB"):
3d02e2
             raid_array.chunk_size = Size("5 KiB")
3d02e2
+
3d02e2
+        with six.assertRaisesRegex(self, ValueError, "specifying chunk size is not allowed for raid1"):
3d02e2
+            MDRaidArrayDevice(name="raid", level="raid1", member_devices=2,
3d02e2
+                              total_devices=2, parents=[member1, member2],
3d02e2
+                              chunk_size=Size("1024 KiB"))
3d02e2
+
3d02e2
+        raid_array = MDRaidArrayDevice(name="raid", level="raid1", member_devices=2,
3d02e2
+                                       total_devices=2, parents=[member1, member2])
3d02e2
+
3d02e2
+        with six.assertRaisesRegex(self, ValueError, "specifying chunk size is not allowed for raid1"):
3d02e2
+            raid_array.chunk_size = Size("512 KiB")