Vojtech Trefny 165b85
From eb16230427fc1081f8515e6ad69ccf99ca521e5d Mon Sep 17 00:00:00 2001
Vojtech Trefny 165b85
From: Vojtech Trefny <vtrefny@redhat.com>
Vojtech Trefny 165b85
Date: Tue, 4 Apr 2023 13:31:40 +0200
Vojtech Trefny 165b85
Subject: [PATCH 1/2] Add support for filesystem online resize
Vojtech Trefny 165b85
Vojtech Trefny 165b85
Resolves: rhbz#2168680
Vojtech Trefny 165b85
---
Vojtech Trefny 165b85
 blivet/devices/lvm.py       | 13 ++++++++-----
Vojtech Trefny 165b85
 blivet/devices/partition.py | 11 ++++++-----
Vojtech Trefny 165b85
 blivet/flags.py             |  3 +++
Vojtech Trefny 165b85
 blivet/formats/fs.py        | 32 ++++++++++++++++++++++++++++----
Vojtech Trefny 165b85
 blivet/formats/fslib.py     |  7 +++++++
Vojtech Trefny 165b85
 5 files changed, 52 insertions(+), 14 deletions(-)
Vojtech Trefny 165b85
Vojtech Trefny 165b85
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
Vojtech Trefny 165b85
index c3132457..ca45c4b5 100644
Vojtech Trefny 165b85
--- a/blivet/devices/lvm.py
Vojtech Trefny 165b85
+++ b/blivet/devices/lvm.py
Vojtech Trefny 165b85
@@ -42,6 +42,7 @@ from .. import errors
Vojtech Trefny 165b85
 from .. import util
Vojtech Trefny 165b85
 from ..storage_log import log_method_call
Vojtech Trefny 165b85
 from .. import udev
Vojtech Trefny 165b85
+from ..flags import flags
Vojtech Trefny 165b85
 from ..size import Size, KiB, MiB, ROUND_UP, ROUND_DOWN
Vojtech Trefny 165b85
 from ..static_data.lvm_info import lvs_info
Vojtech Trefny 165b85
 from ..tasks import availability
Vojtech Trefny 165b85
@@ -2729,12 +2730,14 @@ class LVMLogicalVolumeDevice(LVMLogicalVolumeBase, LVMInternalLogicalVolumeMixin
Vojtech Trefny 165b85
         # Setup VG parents (in case they are dmraid partitions for example)
Vojtech Trefny 165b85
         self.vg.setup_parents(orig=True)
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
-        if self.original_format.exists:
Vojtech Trefny 165b85
-            self.original_format.teardown()
Vojtech Trefny 165b85
-        if self.format.exists:
Vojtech Trefny 165b85
-            self.format.teardown()
Vojtech Trefny 165b85
+        if not flags.allow_online_fs_resize:
Vojtech Trefny 165b85
+            if self.original_format.exists:
Vojtech Trefny 165b85
+                self.original_format.teardown()
Vojtech Trefny 165b85
+            if self.format.exists:
Vojtech Trefny 165b85
+                self.format.teardown()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+            udev.settle()
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
-        udev.settle()
Vojtech Trefny 165b85
         blockdev.lvm.lvresize(self.vg.name, self._name, self.size)
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
     @type_specific
Vojtech Trefny 165b85
diff --git a/blivet/devices/partition.py b/blivet/devices/partition.py
Vojtech Trefny 165b85
index 0e9250ce..6ae4b8d3 100644
Vojtech Trefny 165b85
--- a/blivet/devices/partition.py
Vojtech Trefny 165b85
+++ b/blivet/devices/partition.py
Vojtech Trefny 165b85
@@ -745,11 +745,12 @@ class PartitionDevice(StorageDevice):
Vojtech Trefny 165b85
         if not self.exists:
Vojtech Trefny 165b85
             raise errors.DeviceError("device has not been created")
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
-        # don't teardown when resizing luks
Vojtech Trefny 165b85
-        if self.format.type == "luks" and self.children:
Vojtech Trefny 165b85
-            self.children[0].format.teardown()
Vojtech Trefny 165b85
-        else:
Vojtech Trefny 165b85
-            self.teardown()
Vojtech Trefny 165b85
+        if not flags.allow_online_fs_resize:
Vojtech Trefny 165b85
+            # don't teardown when resizing luks
Vojtech Trefny 165b85
+            if self.format.type == "luks" and self.children:
Vojtech Trefny 165b85
+                self.children[0].format.teardown()
Vojtech Trefny 165b85
+            else:
Vojtech Trefny 165b85
+                self.teardown()
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
         if not self.sysfs_path:
Vojtech Trefny 165b85
             return
Vojtech Trefny 165b85
diff --git a/blivet/flags.py b/blivet/flags.py
Vojtech Trefny 165b85
index 6364164d..ecfa7ad7 100644
Vojtech Trefny 165b85
--- a/blivet/flags.py
Vojtech Trefny 165b85
+++ b/blivet/flags.py
Vojtech Trefny 165b85
@@ -91,6 +91,9 @@ class Flags(object):
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
         self.debug_threads = False
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
+        # Allow online filesystem resizes
Vojtech Trefny 165b85
+        self.allow_online_fs_resize = False
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
     def get_boot_cmdline(self):
Vojtech Trefny 165b85
         with open("/proc/cmdline") as f:
Vojtech Trefny 165b85
             buf = f.read().strip()
Vojtech Trefny 165b85
diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
Vojtech Trefny 165b85
index 33922f3a..3f553eb0 100644
Vojtech Trefny 165b85
--- a/blivet/formats/fs.py
Vojtech Trefny 165b85
+++ b/blivet/formats/fs.py
Vojtech Trefny 165b85
@@ -56,7 +56,7 @@ from ..i18n import N_
Vojtech Trefny 165b85
 from .. import udev
Vojtech Trefny 165b85
 from ..mounts import mounts_cache
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
-from .fslib import kernel_filesystems
Vojtech Trefny 165b85
+from .fslib import kernel_filesystems, FSResize
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
 import logging
Vojtech Trefny 165b85
 log = logging.getLogger("blivet")
Vojtech Trefny 165b85
@@ -88,6 +88,9 @@ class FS(DeviceFormat):
Vojtech Trefny 165b85
     # value is already unpredictable and can change in the future...
Vojtech Trefny 165b85
     _metadata_size_factor = 1.0
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
+    # support for resize: grow/shrink, online/offline
Vojtech Trefny 165b85
+    _resize_support = 0
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
     config_actions_map = {"label": "write_label"}
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
     def __init__(self, **kwargs):
Vojtech Trefny 165b85
@@ -436,12 +439,27 @@ class FS(DeviceFormat):
Vojtech Trefny 165b85
             self.write_uuid()
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
     def _pre_resize(self):
Vojtech Trefny 165b85
-        # file systems need a check before being resized
Vojtech Trefny 165b85
-        self.do_check()
Vojtech Trefny 165b85
+        if self.status:
Vojtech Trefny 165b85
+            if flags.allow_online_fs_resize:
Vojtech Trefny 165b85
+                if self.target_size > self.size and not self._resize_support & FSResize.ONLINE_GROW:
Vojtech Trefny 165b85
+                    raise FSError("This filesystem doesn't support online growing")
Vojtech Trefny 165b85
+                if self.target_size < self.size and not self._resize_support & FSResize.ONLINE_SHRINK:
Vojtech Trefny 165b85
+                    raise FSError("This filesystem doesn't support online shrinking")
Vojtech Trefny 165b85
+            else:
Vojtech Trefny 165b85
+                raise FSError("Resizing of mounted filesystems is disabled")
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+        if self.status:
Vojtech Trefny 165b85
+            # fsck tools in general don't allow checks on mounted filesystems
Vojtech Trefny 165b85
+            log.debug("Filesystem on %s is mounted, not checking", self.device)
Vojtech Trefny 165b85
+        else:
Vojtech Trefny 165b85
+            # file systems need a check before being resized
Vojtech Trefny 165b85
+            self.do_check()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
         super(FS, self)._pre_resize()
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
     def _post_resize(self):
Vojtech Trefny 165b85
-        self.do_check()
Vojtech Trefny 165b85
+        if not self.status:
Vojtech Trefny 165b85
+            self.do_check()
Vojtech Trefny 165b85
         super(FS, self)._post_resize()
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
     def do_check(self):
Vojtech Trefny 165b85
@@ -838,6 +856,7 @@ class Ext2FS(FS):
Vojtech Trefny 165b85
     _formattable = True
Vojtech Trefny 165b85
     _supported = True
Vojtech Trefny 165b85
     _resizable = True
Vojtech Trefny 165b85
+    _resize_support = FSResize.ONLINE_GROW | FSResize.OFFLINE_GROW | FSResize.OFFLINE_SHRINK
Vojtech Trefny 165b85
     _linux_native = True
Vojtech Trefny 165b85
     _max_size = Size("8 TiB")
Vojtech Trefny 165b85
     _dump = True
Vojtech Trefny 165b85
@@ -1097,6 +1116,7 @@ class XFS(FS):
Vojtech Trefny 165b85
     _linux_native = True
Vojtech Trefny 165b85
     _supported = True
Vojtech Trefny 165b85
     _resizable = True
Vojtech Trefny 165b85
+    _resize_support = FSResize.ONLINE_GROW | FSResize.OFFLINE_GROW
Vojtech Trefny 165b85
     _packages = ["xfsprogs"]
Vojtech Trefny 165b85
     _fsck_class = fsck.XFSCK
Vojtech Trefny 165b85
     _info_class = fsinfo.XFSInfo
Vojtech Trefny 165b85
@@ -1247,6 +1267,7 @@ class NTFS(FS):
Vojtech Trefny 165b85
     _labelfs = fslabeling.NTFSLabeling()
Vojtech Trefny 165b85
     _uuidfs = fsuuid.NTFSUUID()
Vojtech Trefny 165b85
     _resizable = True
Vojtech Trefny 165b85
+    _resize_support = FSResize.OFFLINE_GROW | FSResize.OFFLINE_SHRINK
Vojtech Trefny 165b85
     _formattable = True
Vojtech Trefny 165b85
     _supported = True
Vojtech Trefny 165b85
     _min_size = Size("1 MiB")
Vojtech Trefny 165b85
@@ -1490,6 +1511,9 @@ class TmpFS(NoDevFS):
Vojtech Trefny 165b85
         # same, nothing actually needs to be set
Vojtech Trefny 165b85
         pass
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
+    def _pre_resize(self):
Vojtech Trefny 165b85
+        self.do_check()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
     def do_resize(self):
Vojtech Trefny 165b85
         # Override superclass method to record whether mount options
Vojtech Trefny 165b85
         # should include an explicit size specification.
Vojtech Trefny 165b85
diff --git a/blivet/formats/fslib.py b/blivet/formats/fslib.py
Vojtech Trefny 165b85
index ea93b1fd..8722e942 100644
Vojtech Trefny 165b85
--- a/blivet/formats/fslib.py
Vojtech Trefny 165b85
+++ b/blivet/formats/fslib.py
Vojtech Trefny 165b85
@@ -36,3 +36,10 @@ def update_kernel_filesystems():
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
 update_kernel_filesystems()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+class FSResize():
Vojtech Trefny 165b85
+    OFFLINE_SHRINK = 1 << 1
Vojtech Trefny 165b85
+    OFFLINE_GROW = 1 << 2
Vojtech Trefny 165b85
+    ONLINE_SHRINK = 1 << 3
Vojtech Trefny 165b85
+    ONLINE_GROW = 1 << 4
Vojtech Trefny 165b85
-- 
Vojtech Trefny 165b85
2.40.1
Vojtech Trefny 165b85
Vojtech Trefny 165b85
Vojtech Trefny 165b85
From 3fce5d0bfd7b09a976ff49feed15077477c6a425 Mon Sep 17 00:00:00 2001
Vojtech Trefny 165b85
From: Vojtech Trefny <vtrefny@redhat.com>
Vojtech Trefny 165b85
Date: Thu, 6 Apr 2023 14:02:11 +0200
Vojtech Trefny 165b85
Subject: [PATCH 2/2] Add a test case for filesystem online resize
Vojtech Trefny 165b85
Vojtech Trefny 165b85
Related: rhbz#2168680
Vojtech Trefny 165b85
---
Vojtech Trefny 165b85
 tests/storage_tests/formats_test/fs_test.py | 43 ++++++++++++++++++++-
Vojtech Trefny 165b85
 1 file changed, 42 insertions(+), 1 deletion(-)
Vojtech Trefny 165b85
Vojtech Trefny 165b85
diff --git a/tests/storage_tests/formats_test/fs_test.py b/tests/storage_tests/formats_test/fs_test.py
Vojtech Trefny 165b85
index 97f4cbbe..1d42dc21 100644
Vojtech Trefny 165b85
--- a/tests/storage_tests/formats_test/fs_test.py
Vojtech Trefny 165b85
+++ b/tests/storage_tests/formats_test/fs_test.py
Vojtech Trefny 165b85
@@ -6,9 +6,10 @@ import parted
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
 import blivet.formats.fs as fs
Vojtech Trefny 165b85
 from blivet.size import Size, ROUND_DOWN
Vojtech Trefny 165b85
-from blivet.errors import DeviceFormatError
Vojtech Trefny 165b85
+from blivet.errors import DeviceFormatError, FSError
Vojtech Trefny 165b85
 from blivet.formats import get_format
Vojtech Trefny 165b85
 from blivet.devices import PartitionDevice, DiskDevice
Vojtech Trefny 165b85
+from blivet.flags import flags
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
 from .loopbackedtestcase import LoopBackedTestCase
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
@@ -26,6 +27,46 @@ class Ext3FSTestCase(Ext2FSTestCase):
Vojtech Trefny 165b85
 class Ext4FSTestCase(Ext3FSTestCase):
Vojtech Trefny 165b85
     _fs_class = fs.Ext4FS
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
+    def test_online_resize(self):
Vojtech Trefny 165b85
+        an_fs = self._fs_class()
Vojtech Trefny 165b85
+        if not an_fs.formattable:
Vojtech Trefny 165b85
+            self.skipTest("can not create filesystem %s" % an_fs.name)
Vojtech Trefny 165b85
+        an_fs.device = self.loop_devices[0]
Vojtech Trefny 165b85
+        self.assertIsNone(an_fs.create())
Vojtech Trefny 165b85
+        an_fs.update_size_info()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+        if not self.can_resize(an_fs):
Vojtech Trefny 165b85
+            self.skipTest("filesystem is not resizable")
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+        # shrink offline first (ext doesn't support online shrinking)
Vojtech Trefny 165b85
+        TARGET_SIZE = Size("64 MiB")
Vojtech Trefny 165b85
+        an_fs.target_size = TARGET_SIZE
Vojtech Trefny 165b85
+        self.assertEqual(an_fs.target_size, TARGET_SIZE)
Vojtech Trefny 165b85
+        self.assertNotEqual(an_fs._size, TARGET_SIZE)
Vojtech Trefny 165b85
+        self.assertIsNone(an_fs.do_resize())
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+        with tempfile.TemporaryDirectory() as mountpoint:
Vojtech Trefny 165b85
+            an_fs.mount(mountpoint=mountpoint)
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+            # grow back when mounted
Vojtech Trefny 165b85
+            TARGET_SIZE = Size("100 MiB")
Vojtech Trefny 165b85
+            an_fs.target_size = TARGET_SIZE
Vojtech Trefny 165b85
+            self.assertEqual(an_fs.target_size, TARGET_SIZE)
Vojtech Trefny 165b85
+            self.assertNotEqual(an_fs._size, TARGET_SIZE)
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+            # should fail, online resize disabled by default
Vojtech Trefny 165b85
+            with self.assertRaisesRegex(FSError, "Resizing of mounted filesystems is disabled"):
Vojtech Trefny 165b85
+                an_fs.do_resize()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+            # enable online resize
Vojtech Trefny 165b85
+            flags.allow_online_fs_resize = True
Vojtech Trefny 165b85
+            an_fs.do_resize()
Vojtech Trefny 165b85
+            flags.allow_online_fs_resize = False
Vojtech Trefny 165b85
+            self._test_sizes(an_fs)
Vojtech Trefny 165b85
+            self.assertEqual(an_fs.system_mountpoint, mountpoint)
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
+            an_fs.unmount()
Vojtech Trefny 165b85
+
Vojtech Trefny 165b85
 
Vojtech Trefny 165b85
 class FATFSTestCase(fstesting.FSAsRoot):
Vojtech Trefny 165b85
     _fs_class = fs.FATFS
Vojtech Trefny 165b85
-- 
Vojtech Trefny 165b85
2.40.1
Vojtech Trefny 165b85