From: Vratislav Podzimek <vpodzime(a)redhat.com>
---
blivet/devicelibs/loop.py | 121 ----------------------------------------------
blivet/devices/loop.py | 11 ++---
blivet/devicetree.py | 5 +-
blivet/errors.py | 3 --
blivet/util.py | 4 +-
5 files changed, 9 insertions(+), 135 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(a)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..6410443 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 as blockdev
from .. import errors
from ..storage_log import log_method_call
@@ -74,7 +73,7 @@ def updateName(self):
# 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 @@ def updateName(self):
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 @@ def _setup(self, orig=False):
""" 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 @@ def _teardown(self, recursive=False):
""" 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 6663037..2960985 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -47,7 +47,6 @@
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
@@ -642,7 +641,7 @@ def isIgnored(self, info):
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
@@ -2027,7 +2026,7 @@ def setupDiskImages(self):
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/errors.py b/blivet/errors.py
index 33fb906..36b1a78 100644
--- a/blivet/errors.py
+++ b/blivet/errors.py
@@ -120,9 +120,6 @@ class CryptoError(StorageError):
class MPathError(StorageError):
pass
-class LoopError(StorageError):
- pass
-
class BTRFSError(StorageError):
pass
diff --git a/blivet/util.py b/blivet/util.py
index 5c13858..ecd3693 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -11,6 +11,7 @@
import uuid
from decimal import Decimal
from contextlib import contextmanager
+from gi.repository import BlockDev as 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 .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)
--
To view this commit on github, visit
https://github.com/rhinstaller/blivet/commit/a16ff7ca2c0332fb881082318408...