neil / rpms / python-blivet

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