neil / rpms / python-blivet

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