[PATCH 05/11] Use BlockDev's loop plugin instead of devicelibs/loop.py

Vratislav Podzimek vpodzime at redhat.com
Sun Feb 1 18:15:55 UTC 2015


Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 blivet/devicelibs/loop.py | 121 ----------------------------------------------
 blivet/devices/loop.py    |  11 ++---
 blivet/devicetree.py      |   5 +-
 blivet/util.py            |   4 +-
 4 files changed, 9 insertions(+), 132 deletions(-)
 delete mode 100644 blivet/devicelibs/loop.py

diff --git a/blivet/devicelibs/loop.py b/blivet/devicelibs/loop.py
deleted file mode 100644
index bd6d099..0000000
--- a/blivet/devicelibs/loop.py
+++ /dev/null
@@ -1,121 +0,0 @@
-#
-# loop.py
-# loop device functions
-#
-# Copyright (C) 2010  Red Hat, Inc.  All rights reserved.
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty 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, see <http://www.gnu.org/licenses/>.
-#
-# Author(s): David Lehman <dlehman at redhat.com>
-#
-
-import os
-
-from .. import util
-from ..errors import DeviceError, LoopError
-
-import logging
-log = logging.getLogger("blivet")
-
-
-def losetup(args, capture=False):
-    """ run losetup
-
-    :param list args: Arguments to pass to losetup
-    :param bool capture: When True the output is captured and returned
-    :returns: returncode when capture is False (default) or output when it is True.
-    :raises: LoopError if there is an OSError running losetup
-    """
-    if capture:
-        exec_func = util.capture_output
-    else:
-        exec_func = util.run_program
-
-    try:
-        # ask losetup what this loop device's backing device is
-        ret = exec_func(["losetup"] + args)
-    except OSError as e:
-        raise LoopError(e.strerror)
-
-    return ret
-
-def get_backing_file(name):
-    """ Get the backing file for a loop device
-
-    :param str name: Name of loop device (loop0, loop1, etc.)
-    :returns: path of the backing file or ""
-    """
-    path = ""
-    sys_path  = "/sys/class/block/%s/loop/backing_file" % name
-    if os.access(sys_path, os.R_OK):
-        path = open(sys_path).read().strip()
-
-    return path
-
-def get_loop_name(path):
-    """ Get the name of the loop device associated with a file
-
-    :param str path: Path to the file
-    :returns: First loop device or ""
-
-    If multiple loop devices are associated with path this will return the first one.
-    """
-    args = ["-j", path]
-    buf = losetup(args, capture=True)
-
-    entries = buf.splitlines()
-    if not entries:
-        return ""
-
-    first_entry = entries[0]
-    if len(entries) > 1:
-        # If there are multiple loop devices use the first one
-        log.warning("multiple loops associated with %s. Using %s", path, first_entry)
-
-    name = os.path.basename(first_entry.split(":")[0])
-    return name
-
-def loop_setup(path):
-    """ Setup a loop device backed by a file
-
-    :param str path: Path to the file to setup the loop device on
-    :raises: LoopError if there was an error setting up the loop device
-    """
-    args = ["-f", path]
-    msg = None
-    try:
-        msg = losetup(args)
-    except LoopError as e:
-        msg = str(e)
-
-    if msg:
-        raise LoopError("failed to set up loop for %s: %s" % (path, msg))
-
-def loop_teardown(path):
-    """ Teardown the loop device associated with a file
-
-    :param str path: Path to the loop device
-    :raises: LoopError if there was an error tearing down the loop device
-    """
-    args = ["-d", path]
-    msg = None
-    try:
-        msg = losetup(args)
-    except LoopError as e:
-        msg = str(e)
-
-    if msg:
-        raise DeviceError("failed to tear down loop %s: %s" % (path, msg))
-
-
diff --git a/blivet/devices/loop.py b/blivet/devices/loop.py
index 6768e64..7d8c30e 100644
--- a/blivet/devices/loop.py
+++ b/blivet/devices/loop.py
@@ -20,8 +20,7 @@
 #
 
 import os
-
-from ..devicelibs import loop
+from gi.repository import BlockDev
 
 from .. import errors
 from ..storage_log import log_method_call
@@ -74,7 +73,7 @@ class LoopDevice(StorageDevice):
             # if our name is loopN we must already be active
             return self.name
 
-        name = loop.get_loop_name(self.slave.path)
+        name = BlockDev.loop_get_loop_name(self.slave.path)
         if name.startswith("loop"):
             self.name = name
 
@@ -84,7 +83,7 @@ class LoopDevice(StorageDevice):
     def status(self):
         return (self.slave.status and
                 self.name.startswith("loop") and
-                loop.get_loop_name(self.slave.path) == self.name)
+                BlockDev.loop_get_loop_name(self.slave.path) == self.name)
 
     @property
     def size(self):
@@ -99,7 +98,7 @@ class LoopDevice(StorageDevice):
         """ Open, or set up, a device. """
         log_method_call(self, self.name, orig=orig, status=self.status,
                         controllable=self.controllable)
-        loop.loop_setup(self.slave.path)
+        BlockDev.loop_setup(self.slave.path)
 
     def _postSetup(self):
         StorageDevice._postSetup(self)
@@ -110,7 +109,7 @@ class LoopDevice(StorageDevice):
         """ Close, or tear down, a device. """
         log_method_call(self, self.name, status=self.status,
                         controllable=self.controllable)
-        loop.loop_teardown(self.path)
+        BlockDev.loop_teardown(self.path)
 
     def _postTeardown(self, recursive=False):
         StorageDevice._postTeardown(self, recursive=recursive)
diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index 85b378b..a6e372f 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -46,7 +46,6 @@ from .formats import getFormat
 from .formats.fs import nodev_filesystems
 from .devicelibs import dm
 from .devicelibs import lvm
-from .devicelibs import loop
 from .devicelibs import edd
 from . import udev
 from . import util
@@ -641,7 +640,7 @@ class DeviceTree(object):
 
         if name.startswith("loop"):
             # ignore loop devices unless they're backed by a file
-            return (not loop.get_backing_file(name))
+            return (not BlockDev.loop_get_backing_file(name))
 
         # FIXME: check for virtual devices whose slaves are on the ignore list
 
@@ -2021,7 +2020,7 @@ class DeviceTree(object):
                 filedev.setup()
                 log.debug("%s", filedev)
 
-                loop_name = loop.get_loop_name(filedev.path)
+                loop_name = BlockDev.loop_get_loop_name(filedev.path)
                 loop_sysfs = None
                 if loop_name:
                     loop_sysfs = "/class/block/%s" % loop_name
diff --git a/blivet/util.py b/blivet/util.py
index 77fe773..1d58f53 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -11,6 +11,7 @@ import tempfile
 import uuid
 from decimal import Decimal
 from contextlib import contextmanager
+from gi.repository import BlockDev
 
 import six
 
@@ -137,9 +138,8 @@ def get_mount_device(mountpoint):
             break
 
     if mount_device and re.match(r'/dev/loop\d+$', mount_device):
-        from blivet.devicelibs import loop
         loop_name = os.path.basename(mount_device)
-        mount_device = loop.get_backing_file(loop_name)
+        mount_device = BlockDev.loop_get_backing_file(loop_name)
         log.debug("found backing file %s for loop device %s", mount_device,
                                                               loop_name)
 
-- 
2.1.0



More information about the anaconda-patches mailing list