neil / rpms / python-blivet

Forked from rpms/python-blivet a year ago
Clone
Blob Blame History Raw
From dc1e2fe7783748528cac2f7aa516c89d1959b052 Mon Sep 17 00:00:00 2001
From: Vojtech Trefny <vtrefny@redhat.com>
Date: Thu, 29 Jul 2021 14:44:22 +0200
Subject: [PATCH] Do not set chunk size for RAID 1

Setting chunk size for RAID 1 doesn't make sense and latest
mdadm started returning error instead of ignoring the --chunk
option when creating an array.

Resolves: rhbz#1987170
---
 blivet/devicelibs/raid.py     | 12 ++++++++++
 blivet/devices/md.py          | 15 ++++++++++---
 tests/devices_test/md_test.py | 41 +++++++++++++++++++++++++++++++++--
 3 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/blivet/devicelibs/raid.py b/blivet/devicelibs/raid.py
index 19c3fae98..a9e241c7a 100644
--- a/blivet/devicelibs/raid.py
+++ b/blivet/devicelibs/raid.py
@@ -462,6 +462,18 @@ def _pad(self, size, chunk_size):
     def _get_recommended_stride(self, member_count):
         return None
 
+    def get_size(self, member_sizes, num_members=None, chunk_size=None, superblock_size_func=None):
+        if not member_sizes:
+            return Size(0)
+
+        if num_members is None:
+            num_members = len(member_sizes)
+
+        min_size = min(member_sizes)
+        superblock_size = superblock_size_func(min_size)
+        min_data_size = self._trim(min_size - superblock_size, chunk_size)
+        return self.get_net_array_size(num_members, min_data_size)
+
 
 RAID1 = RAID1()
 ALL_LEVELS.add_raid_level(RAID1)
diff --git a/blivet/devices/md.py b/blivet/devices/md.py
index 69eee93a5..d1a2faf1f 100644
--- a/blivet/devices/md.py
+++ b/blivet/devices/md.py
@@ -138,7 +138,7 @@ def __init__(self, name, level=None, major=None, minor=None, size=None,
         if self.exists:
             self._chunk_size = self.read_chunk_size()
         else:
-            self._chunk_size = chunk_size or mdraid.MD_CHUNK_SIZE
+            self.chunk_size = chunk_size or Size(0)
 
         if not self.exists and not isinstance(metadata_version, str):
             self.metadata_version = "default"
@@ -208,8 +208,14 @@ def sector_size(self):
 
     @property
     def chunk_size(self):
-        if self.exists and self._chunk_size == Size(0):
-            self._chunk_size = self.read_chunk_size()
+        if self._chunk_size == Size(0):
+            if self.exists:
+                return self.read_chunk_size()
+            else:
+                if self.level == raid.RAID1:
+                    return self._chunk_size
+                else:
+                    return mdraid.MD_CHUNK_SIZE
         return self._chunk_size
 
     @chunk_size.setter
@@ -223,6 +229,9 @@ def chunk_size(self, newsize):
         if self.exists:
             raise ValueError("cannot set chunk size for an existing device")
 
+        if self.level == raid.RAID1 and newsize != Size(0):
+            raise ValueError("specifying chunk size is not allowed for raid1")
+
         self._chunk_size = newsize
 
     def read_chunk_size(self):
diff --git a/tests/devices_test/md_test.py b/tests/devices_test/md_test.py
index 46df76d3d..47a0fa0cc 100644
--- a/tests/devices_test/md_test.py
+++ b/tests/devices_test/md_test.py
@@ -1,6 +1,11 @@
 import six
 import unittest
 
+try:
+    from unittest.mock import patch
+except ImportError:
+    from mock import patch
+
 import blivet
 
 from blivet.devices import StorageDevice
@@ -27,9 +32,27 @@ def test_chunk_size1(self):
         raid_array = MDRaidArrayDevice(name="raid", level="raid0", member_devices=2,
                                        total_devices=2, parents=[member1, member2])
 
-        # no chunk_size specified -- default value
+        # no chunk_size specified and RAID0 -- default value
         self.assertEqual(raid_array.chunk_size, mdraid.MD_CHUNK_SIZE)
 
+        with patch("blivet.devices.md.blockdev.md.create") as md_create:
+            raid_array._create()
+            md_create.assert_called_with("/dev/md/raid", "raid0", ["/dev/member1", "/dev/member2"],
+                                         0, version="default", bitmap=False,
+                                         chunk_size=mdraid.MD_CHUNK_SIZE)
+
+        raid_array = MDRaidArrayDevice(name="raid", level="raid1", member_devices=2,
+                                       total_devices=2, parents=[member1, member2])
+
+        # no chunk_size specified and RAID1 -- no chunk size set (0)
+        self.assertEqual(raid_array.chunk_size, Size(0))
+
+        with patch("blivet.devices.md.blockdev.md.create") as md_create:
+            raid_array._create()
+            md_create.assert_called_with("/dev/md/raid", "raid1", ["/dev/member1", "/dev/member2"],
+                                         0, version="default", bitmap=True,
+                                         chunk_size=0)
+
     def test_chunk_size2(self):
 
         member1 = StorageDevice("member1", fmt=blivet.formats.get_format("mdmember"),
@@ -40,11 +63,25 @@ def test_chunk_size2(self):
         raid_array = MDRaidArrayDevice(name="raid", level="raid0", member_devices=2,
                                        total_devices=2, parents=[member1, member2],
                                        chunk_size=Size("1024 KiB"))
-
         self.assertEqual(raid_array.chunk_size, Size("1024 KiB"))
 
+        # for raid0 setting chunk_size = 0 means "default"
+        raid_array.chunk_size = Size(0)
+        self.assertEqual(raid_array.chunk_size, mdraid.MD_CHUNK_SIZE)
+
         with six.assertRaisesRegex(self, ValueError, "new chunk size must be of type Size"):
             raid_array.chunk_size = 1
 
         with six.assertRaisesRegex(self, ValueError, "new chunk size must be multiple of 4 KiB"):
             raid_array.chunk_size = Size("5 KiB")
+
+        with six.assertRaisesRegex(self, ValueError, "specifying chunk size is not allowed for raid1"):
+            MDRaidArrayDevice(name="raid", level="raid1", member_devices=2,
+                              total_devices=2, parents=[member1, member2],
+                              chunk_size=Size("1024 KiB"))
+
+        raid_array = MDRaidArrayDevice(name="raid", level="raid1", member_devices=2,
+                                       total_devices=2, parents=[member1, member2])
+
+        with six.assertRaisesRegex(self, ValueError, "specifying chunk size is not allowed for raid1"):
+            raid_array.chunk_size = Size("512 KiB")