[master 16/19] Abstract aligning the target size.

mulkieran installerbot-noreply at redhat.com
Thu Jun 18 21:04:17 UTC 2015


From: mulhern <amulhern at redhat.com>

Related: #56

Eliminate FS._preResize which seems to be more generic than I had originally
thought.

Raise an exception in _preResize if resize operation is unavailable.
Consistantly treat inability to resize by raising an exception.
Return False only if there is no point in resizing because the
resize operation will have no effect.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/formats/__init__.py | 78 ++++++++++++++++++++++++----------------------
 blivet/formats/fs.py       | 24 ++------------
 2 files changed, 42 insertions(+), 60 deletions(-)

diff --git a/blivet/formats/__init__.py b/blivet/formats/__init__.py
index 0a69ef8..dd45984 100644
--- a/blivet/formats/__init__.py
+++ b/blivet/formats/__init__.py
@@ -377,6 +377,35 @@ def _alignToResizeUnit(self, newsize, direction=ROUND_DOWN):
             log.warning("Trying to align size to resize unit, but no resize unit available.")
             return newsize
 
+    def _alignTargetSize(self):
+        """ Align the targetSize within various bounds.
+
+            This is a last minute step before actually resizing the format.
+
+            Makes sure that:
+            * The targetSize is no less than the minimum size.
+            * The targetSize is either equal to the current size or differs
+              from the current size by at least one resize unit.
+
+            and if the above constraints do not conflict:
+            * The targetSize is rounded down to the resize unit
+        """
+        # We always round down because the format has to fit on whatever device
+        # contains it. To round up would risk quietly setting a target size too
+        # large for the device to hold.
+        newsize = self._alignToResizeUnit(self.targetSize)
+
+        if newsize < self.minSize:
+            log.info("Setting target size on %s to minimum size %s", self.device, newsize)
+            newsize = self.minSize
+
+        if newsize < self.currentSize < self.targetSize:
+            log.info("rounding target size down to next %s obviated resize of "
+                     "format on %s", unitStr(self._resizeTask.unit), self.device)
+            newsize = self.currentSize
+
+        self.targetSize = newsize
+
     def _deviceCheck(self, devspec):
         """ Verifies that device spec has a proper format.
 
@@ -618,35 +647,6 @@ def resize(self, **kwargs):
 
     def _resize(self, **kwargs):
         """ Do generic resizing actions. """
-
-        # We always round down because the fs has to fit on whatever device
-        # contains it. To round up would risk quietly setting a target size too
-        # large for the device to hold.
-        rounded = self._alignToResizeUnit(self.targetSize)
-
-        # 1. target size was between the min size and max size values prior to
-        #    rounding (see _setTargetSize)
-        # 2. we've just rounded the target size down (or not at all)
-        # 3. the minimum size is already either rounded (see _getMinSize) or is
-        #    equal to the current size (see updateSizeInfo)
-        # 5. the minimum size is less than or equal to the current size (see
-        #    _getMinSize)
-        #
-        # This, I think, is sufficient to guarantee that the rounded target size
-        # is greater than or equal to the minimum size.
-
-        # It is possible that rounding down a target size greater than the
-        # current size would move it below the current size, thus changing the
-        # direction of the resize. That means the target size was less than one
-        # unit larger than the current size, and we should do nothing and return
-        # early.
-        if self.targetSize > self.currentSize and rounded < self.currentSize:
-            log.info("rounding target size down to next %s obviated resize of "
-                     "format on %s", unitStr(self._resizeTask.unit), self.device)
-            return
-        else:
-            self.targetSize = rounded
-
         self._resizeTask.doTask()
 
     def _deviceRequiredForResize(self):
@@ -661,24 +661,26 @@ def _preResize(self, **kwargs):
         """ Do all preparatory checks for resizing a format.
 
             :rtype: bool
-            :returns: True if resize should proceed, otherwise False
+            :returns: False if resize would be pointless, otherwise True
+            :raises DeviceFormatError: if it is not possible to resize
         """
         if not self.exists:
             raise DeviceFormatError("format has not been created")
 
-        if not self.resizable:
-            raise DeviceFormatError("format is not resizable")
-
-        if self.targetSize == self.currentSize:
-            return False
-
         if not self._resizeTask.available:
-            return False
+            raise DeviceFormatError("no facility for resizing format")
 
         if self._deviceRequiredForResize() and not os.path.exists(self.device):
             raise DeviceFormatError("device %s does not exist" % self.device)
 
-        return True
+        self.updateSizeInfo()
+
+        if not self.resizable:
+            raise DeviceFormatError("format is not resizable")
+
+        self._alignTargetSize()
+
+        return self.targetSize != self.currentSize
 
     def _postResize(self, **kwargs):
         """ Do what must be done after resizing a format. """
diff --git a/blivet/formats/fs.py b/blivet/formats/fs.py
index bf23ba9..b4548e9 100644
--- a/blivet/formats/fs.py
+++ b/blivet/formats/fs.py
@@ -38,7 +38,7 @@
 from ..tasks import fssync
 from ..tasks import fswritelabel
 from ..errors import FormatCreateError, FSError, FSReadLabelError
-from ..errors import FSWriteLabelError, FSResizeError
+from ..errors import FSWriteLabelError
 from . import DeviceFormat, register_device_format
 from .. import util
 from .. import platform
@@ -329,7 +329,7 @@ def _postCreate(self, **kwargs):
     def doResize(self):
         """ Resize this filesystem based on this instance's targetSize attr.
 
-            :raises: FSResizeError, FSError, DeviceFormatError
+            :raises: FSError, DeviceFormatError
 
             .. versionchanged:: 1.5
                Raises :class:`~.errors.DeviceFormatError`
@@ -339,26 +339,6 @@ def doResize(self):
         """
         self.resize()
 
-    def _preResize(self, **kwargs):
-        if not super(FS, self)._preResize(**kwargs):
-            return False
-
-        # The first minimum size can be incorrect if the fs was not
-        # properly unmounted. After doCheck the minimum size will be correct
-        # so run the check one last time and bump up the size if it was too
-        # small.
-        self.updateSizeInfo()
-
-        # Check again if resizable is True, as updateSizeInfo() can change that
-        if not self.resizable:
-            raise FSResizeError("filesystem not resizable", self.device)
-
-        if self.targetSize < self.minSize:
-            self.targetSize = self.minSize
-            log.info("Minimum size changed, setting targetSize on %s to %s",
-                     self.device, self.targetSize)
-        return True
-
     def _postResize(self, **kwargs):
         self.doCheck()
         super(FS, self)._postResize(**kwargs)


-- 
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/0ba1aba6281fea4e900a2a462b07404bfaefbd61


More information about the anaconda-patches mailing list