[PATCH 4/20] Use BlockDev's swap plugin instead of devicelibs/swap.py

vpodzime installerbot-noreply at redhat.com
Fri Feb 27 15:31:52 UTC 2015


From: Vratislav Podzimek <vpodzime at redhat.com>

---
 blivet/devicelibs/swap.py | 114 ----------------------------------------------
 blivet/errors.py          |  12 -----
 blivet/formats/swap.py    |  19 ++++----
 3 files changed, 10 insertions(+), 135 deletions(-)
 delete mode 100644 blivet/devicelibs/swap.py

diff --git a/blivet/devicelibs/swap.py b/blivet/devicelibs/swap.py
deleted file mode 100644
index 5314810..0000000
--- a/blivet/devicelibs/swap.py
+++ /dev/null
@@ -1,114 +0,0 @@
-# swap.py
-# Python module for managing swap devices.
-#
-# Copyright (C) 2009  Red Hat, Inc.
-#
-# This copyrighted material is made available to anyone wishing to use,
-# modify, copy, or redistribute it subject to the terms and conditions of
-# the GNU General Public License v.2, or (at your option) any later version.
-# This program is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY expressed or implied, including the implied warranties of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
-# Public License for more details.  You should have received a copy of the
-# GNU General Public License along with this program; if not, write to the
-# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
-# source code or documentation are not subject to the GNU General Public
-# License and may only be used or replicated with the express permission of
-# Red Hat, Inc.
-#
-# Red Hat Author(s): Dave Lehman <dlehman at redhat.com>
-#
-
-import resource
-import os
-
-from ..errors import DMError, OldSwapError, SuspendError, SwapError, UnknownSwapError
-from .. import util
-from . import dm
-
-import logging
-log = logging.getLogger("blivet")
-
-def mkswap(device, label=None):
-    # We use -f to force since mkswap tends to refuse creation on lvs with
-    # a message about erasing bootbits sectors on whole disks. Bah.
-    argv = ["-f"]
-    if label is not None:
-        argv.extend(["-L", label])
-    argv.append(device)
-
-    ret = util.run_program(["mkswap"] + argv)
-
-    if ret:
-        raise SwapError("mkswap failed for '%s'" % device)
-
-def swapon(device, priority=None):
-    pagesize = resource.getpagesize()
-    buf = None
-    sig = None
-
-    if pagesize > 2048:
-        num = pagesize
-    else:
-        num = 2048
-
-    try:
-        fd = os.open(device, os.O_RDONLY)
-        buf = os.read(fd, num)
-    except OSError:
-        pass
-    finally:
-        try:
-            os.close(fd)
-        except (OSError, UnboundLocalError):
-            pass
-
-    if buf is not None and len(buf) == pagesize:
-        sig = buf[pagesize - 10:]
-        if sig == 'SWAP-SPACE':
-            raise OldSwapError
-        if sig == 'S1SUSPEND\x00' or sig == 'S2SUSPEND\x00':
-            raise SuspendError
-
-    if sig != 'SWAPSPACE2':
-        raise UnknownSwapError
-
-    argv = []
-    if isinstance(priority, int) and 0 <= priority <= 32767:
-        argv.extend(["-p", "%d" % priority])
-    argv.append(device)
-
-    rc = util.run_program(["swapon"] + argv)
-
-    if rc:
-        raise SwapError("swapon failed for '%s'" % device)
-
-def swapoff(device):
-    rc = util.run_program(["swapoff", device])
-
-    if rc:
-        raise SwapError("swapoff failed for '%s'" % device)
-
-def swapstatus(device):
-    alt_dev = None
-    if device.startswith("/dev/mapper/"):
-        # get the real device node for device-mapper devices since the ones
-        # with meaningful names are just symlinks
-        try:
-            alt_dev = "/dev/%s" % dm.dm_node_from_name(device.split("/")[-1])
-        except DMError:
-            alt_dev = None
-
-    lines = open("/proc/swaps").readlines()
-    status = False
-    for line in lines:
-        if not line.strip():
-            continue
-
-        swap_dev = line.split()[0]
-        if swap_dev in [device, alt_dev]:
-            status = True
-            break
-
-    return status
diff --git a/blivet/errors.py b/blivet/errors.py
index 34d6fd1..d814ffb 100644
--- a/blivet/errors.py
+++ b/blivet/errors.py
@@ -105,18 +105,6 @@ class DiskLabelCommitError(DiskLabelError):
     pass
 
 # devicelibs
-class SwapError(StorageError):
-    pass
-
-class SuspendError(SwapError):
-    pass
-
-class OldSwapError(SwapError):
-    pass
-
-class UnknownSwapError(SwapError):
-    pass
-
 class MDRaidError(StorageError):
     pass
 
diff --git a/blivet/formats/swap.py b/blivet/formats/swap.py
index bb3eaff..ce0a9df 100644
--- a/blivet/formats/swap.py
+++ b/blivet/formats/swap.py
@@ -23,9 +23,9 @@
 from parted import PARTITION_SWAP, fileSystemType
 from ..storage_log import log_method_call
 from ..errors import SwapSpaceError
-from ..devicelibs import swap
 from . import DeviceFormat, register_device_format
 from ..size import Size
+from gi.repository import BlockDev as blockdev
 
 import logging
 log = logging.getLogger("blivet")
@@ -66,7 +66,7 @@ def __init__(self, **kwargs):
         log_method_call(self, **kwargs)
         DeviceFormat.__init__(self, **kwargs)
 
-        self.priority = kwargs.get("priority")
+        self.priority = kwargs.get("priority", -1)
         self.label = kwargs.get("label")
 
     def __repr__(self):
@@ -97,11 +97,12 @@ def labelFormatOK(cls, label):
     def _setPriority(self, priority):
         # pylint: disable=attribute-defined-outside-init
         if priority is None:
-            self._priority = None
+            self._priority = -1
             return
 
-        if not isinstance(priority, int) or not 0 <= priority <= 32767:
-            raise ValueError("swap priority must be an integer between 0 and 32767")
+        if not isinstance(priority, int) or not -1 <= priority <= 32767:
+            # -1 means "unspecified"
+            raise ValueError("swap priority must be an integer between -1 and 32767")
 
         self._priority = priority
 
@@ -134,7 +135,7 @@ def _setOptions(self, opts):
     @property
     def status(self):
         """ Device status. """
-        return self.exists and swap.swapstatus(self.device)
+        return self.exists and blockdev.swap_swapstatus(self.device)
 
     def setup(self, **kwargs):
         """ Activate the formatting.
@@ -158,7 +159,7 @@ def setup(self, **kwargs):
             return
 
         DeviceFormat.setup(self, **kwargs)
-        swap.swapon(self.device, priority=self.priority)
+        blockdev.swap_swapon(self.device, priority=self.priority)
 
     def teardown(self):
         """ Close, or tear down, a device. """
@@ -168,7 +169,7 @@ def teardown(self):
             raise SwapSpaceError("format has not been created")
 
         if self.status:
-            swap.swapoff(self.device)
+            blockdev.swap_swapoff(self.device)
 
     def create(self, **kwargs):
         """ Write the formatting to the specified block device.
@@ -190,7 +191,7 @@ def create(self, **kwargs):
 
         try:
             DeviceFormat.create(self, **kwargs)
-            swap.mkswap(self.device, label=self.label)
+            blockdev.swap_mkswap(self.device, label=self.label)
         except Exception:
             raise
         else:


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


More information about the anaconda-patches mailing list