From: David Lehman <dlehman(a)redhat.com>
---
blivet/devices/__init__.py | 6 +++---
blivet/devices/dm.py | 4 +++-
blivet/devices/lib.py | 43 ++++++++++++++++++++++++++++++++++++++-----
blivet/devices/md.py | 3 +++
4 files changed, 47 insertions(+), 9 deletions(-)
diff --git a/blivet/devices/__init__.py b/blivet/devices/__init__.py
index 4f697ca..4bdea37 100644
--- a/blivet/devices/__init__.py
+++ b/blivet/devices/__init__.py
@@ -19,15 +19,15 @@
# Red Hat Author(s): David Lehman <dlehman(a)redhat.com>
#
-from .lib import get_device_majors, device_path_to_name, device_name_to_disk_by_path,
ParentList
+from .lib import device_path_to_name, device_name_to_disk_by_path, ParentList
from .device import Device
from .storage import StorageDevice
from .disk import DiskDevice, DiskFile, DMRaidArrayDevice, MultipathDevice,
iScsiDiskDevice, FcoeDiskDevice, DASDDevice, ZFCPDiskDevice
from .partition import PartitionDevice
-from .dm import DMDevice, DMLinearDevice, DMCryptDevice
+from .dm import DMDevice, DMLinearDevice, DMCryptDevice, DM_MAJORS
from .luks import LUKSDevice
from .lvm import LVMVolumeGroupDevice, LVMLogicalVolumeDevice, LVMSnapShotDevice,
LVMThinPoolDevice, LVMThinLogicalVolumeDevice, LVMThinSnapShotDevice
-from .md import MDBiosRaidArrayDevice, MDContainerDevice, MDRaidArrayDevice
+from .md import MDBiosRaidArrayDevice, MDContainerDevice, MDRaidArrayDevice, MD_MAJORS
from .btrfs import BTRFSDevice, BTRFSVolumeDevice, BTRFSSubVolumeDevice,
BTRFSSnapShotDevice
from .file import FileDevice, DirectoryDevice, SparseFileDevice
from .loop import LoopDevice
diff --git a/blivet/devices/dm.py b/blivet/devices/dm.py
index f76ff3a..e7efddf 100644
--- a/blivet/devices/dm.py
+++ b/blivet/devices/dm.py
@@ -37,7 +37,9 @@
log = logging.getLogger("blivet")
from .storage import StorageDevice
-from .lib import LINUX_SECTOR_SIZE
+from .lib import LINUX_SECTOR_SIZE, get_majors_by_device_type
+
+DM_MAJORS = get_majors_by_device_type("device-mapper")
class DMDevice(StorageDevice):
diff --git a/blivet/devices/lib.py b/blivet/devices/lib.py
index 5edb398..33e90e2 100644
--- a/blivet/devices/lib.py
+++ b/blivet/devices/lib.py
@@ -28,19 +28,52 @@
LINUX_SECTOR_SIZE = Size(512)
-def get_device_majors():
- majors = {}
+def _collect_device_major_data():
+ by_major = {}
+ by_device = {}
for line in open("/proc/devices").readlines():
try:
(major, device) = line.split()
except ValueError:
continue
try:
- majors[int(major)] = device
+ by_major[int(major)] = device
+ if device not in by_device:
+ by_device[device] = []
+
+ by_device[device].append(int(major))
except ValueError:
continue
- return majors
-device_majors = get_device_majors()
+ return (by_major, by_device)
+_devices_by_major, _majors_by_device = _collect_device_major_data()
+
+
+def get_majors_by_device_type(device_type):
+ """ Return a list of major numbers for the given device type.
+
+ :param str device_type: device type string (eg: 'device-mapper',
'md')
+ :rtype: list of int
+
+ .. note::
+
+ Type strings are taken directly from /proc/devices.
+
+ """
+ return _majors_by_device.get(device_type, [])
+
+
+def get_device_type_by_major(major):
+ """ Return a device-type string for the given major number.
+
+ :param int major: major number
+ :rtype: str
+
+ .. note::
+
+ Type strings are taken directly from /proc/devices.
+
+ """
+ return _devices_by_major.get(major)
def device_path_to_name(device_path):
diff --git a/blivet/devices/md.py b/blivet/devices/md.py
index 7bc2531..579126a 100644
--- a/blivet/devices/md.py
+++ b/blivet/devices/md.py
@@ -43,6 +43,9 @@
from .storage import StorageDevice
from .container import ContainerDevice
from .raid import RaidDevice
+from .lib import get_majors_by_device_type
+
+MD_MAJORS = get_majors_by_device_type("md")
class MDRaidArrayDevice(ContainerDevice, RaidDevice):
--
To view this commit on github, visit
https://github.com/rhinstaller/blivet/commit/611f0d4b2109380981e1539f5232...