neil / rpms / python-blivet

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