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