diff options
author | Bill Nottingham <notting@redhat.com> | 2009-08-04 11:02:23 -0400 |
---|---|---|
committer | Bill Nottingham <notting@redhat.com> | 2009-08-04 11:02:23 -0400 |
commit | 1ee4b82d95023a03d94df23769c425312d12c276 (patch) | |
tree | 1693c9863511658f11bbf6fc2b1007a6e09b7af9 | |
parent | 0b2370db0ab684e169765a3f498a2f9d4947ee1e (diff) | |
parent | 86b25083c61c583c866c8bbb93c18cf3c8f57804 (diff) | |
download | anaconda-1ee4b82d95023a03d94df23769c425312d12c276.tar.gz anaconda-1ee4b82d95023a03d94df23769c425312d12c276.tar.xz anaconda-1ee4b82d95023a03d94df23769c425312d12c276.zip |
Merge branch 'master' of ssh://git.fedoraproject.org/git/anaconda
-rwxr-xr-x | anaconda | 2 | ||||
-rw-r--r-- | baseudev.py | 136 | ||||
-rwxr-xr-x | command-stubs/list-harddrives-stub | 36 | ||||
-rwxr-xr-x | isys/isys.py | 4 | ||||
-rw-r--r-- | storage/udev.py | 117 |
5 files changed, 161 insertions, 134 deletions
@@ -562,7 +562,7 @@ if __name__ == "__main__": # automatically, I don't know. but we need to trigger so that we # have all the information about netdevs that we care about for # NetworkManager in the udev database - from storage.udev import udev_trigger, udev_settle + from baseudev import udev_trigger, udev_settle udev_trigger("net") udev_settle() # and for added fun, once doesn't seem to be enough? so we diff --git a/baseudev.py b/baseudev.py new file mode 100644 index 000000000..f441bceaa --- /dev/null +++ b/baseudev.py @@ -0,0 +1,136 @@ +# udev.py +# Python module for querying the udev database for device information. +# +# 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@redhat.com> +# Chris Lumens <clumens@redhat.com> +# + +import iutil +import os + +import logging +log = logging.getLogger("storage") + +def udev_enumerate_devices(deviceClass="block"): + top_dir = "/sys/class/%s" % deviceClass + devices = [] + for dev_name in os.listdir(top_dir): + full_path = os.path.join(top_dir, dev_name) + link_ref = os.readlink(full_path) + real_path = os.path.join(top_dir, link_ref) + sysfs_path = os.path.normpath(real_path) + devices.append(sysfs_path) + return devices + +def udev_get_device(sysfs_path): + if not os.path.exists(sysfs_path): + log.debug("%s does not exist" % sysfs_path) + return None + + db_entry = sysfs_path[4:].replace("/", "\\x2f") + db_root = "/dev/.udev/db" + db_path = os.path.normpath("%s/%s" % (db_root, db_entry)) + if not os.access(db_path, os.R_OK): + log.debug("db entry %s does not exist" % db_path) + return None + + entry = open(db_path).read() + dev = udev_parse_entry(entry) + if dev.has_key("name"): + # XXX why do we do this? is /sys going to move during installation? + dev['sysfs_path'] = sysfs_path[4:] # strip off the leading '/sys' + dev = udev_parse_uevent_file(dev) + + # now add in the contents of the uevent file since they're handy + return dev + +def udev_get_devices(deviceClass="block"): + udev_settle(timeout=30) + entries = [] + for path in udev_enumerate_devices(deviceClass): + entry = udev_get_device(path) + if entry: + entries.append(entry) + return entries + +def udev_parse_entry(buf): + dev = {} + + for line in buf.splitlines(): + line.strip() + (tag, sep, val) = line.partition(":") + if not sep: + continue + + if tag == "N": + dev['name'] = val + elif tag == "S": + if dev.has_key('symlinks'): + dev['symlinks'].append(val) + else: + dev['symlinks'] = [val] + elif tag == "E": + if val.count("=") > 1 and val.count(" ") > 0: + # eg: LVM2_LV_NAME when querying the VG for its LVs + vars = val.split() + vals = [] + var_name = None + for (index, subval) in enumerate(vars): + (var_name, sep, var_val) = subval.partition("=") + if sep: + vals.append(var_val) + + dev[var_name] = vals + else: + (var_name, sep, var_val) = val.partition("=") + if not sep: + continue + + dev[var_name] = var_val + + return dev + +def udev_parse_uevent_file(dev): + path = os.path.normpath("/sys/%s/uevent" % dev['sysfs_path']) + if not os.access(path, os.R_OK): + return dev + + with open(path) as f: + for line in f.readlines(): + (key, equals, value) = line.strip().partition("=") + if not equals: + continue + + dev[key] = value + + return dev + +def udev_settle(timeout=None): + argv = ["settle"] + if timeout: + argv.append("--timeout=%d" % int(timeout)) + + iutil.execWithRedirect("udevadm", argv, stderr="/dev/null", searchPath=1) + +def udev_trigger(subsystem=None): + argv = ["trigger"] + if subsystem: + argv.append("--subsystem-match=%s" % subsystem) + + iutil.execWithRedirect("udevadm", argv, stderr="/dev/null", searchPath=1) diff --git a/command-stubs/list-harddrives-stub b/command-stubs/list-harddrives-stub index a8060b15b..ae7e31dba 100755 --- a/command-stubs/list-harddrives-stub +++ b/command-stubs/list-harddrives-stub @@ -18,43 +18,17 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. # -import dbus import sys - -HAL_INTERFACE = "org.freedesktop.Hal" -HAL_MANAGER_PATH = "/org/freedesktop/Hal/Manager" -HAL_MANAGER_INTERFACE = "org.freedesktop.Hal.Manager" -HAL_DEVICE_INTERFACE = "org.freedesktop.Hal.Device" +import parted def main(argv): lst = set() - try: - bus = dbus.SystemBus() - hal = dbus.Interface(bus.get_object(HAL_INTERFACE, - HAL_MANAGER_PATH), - HAL_MANAGER_INTERFACE) - except: - sys.exit(1) - - for udi in hal.FindDeviceByCapability("volume"): - try: - haldev = dbus.Interface(bus.get_object(HAL_INTERFACE, udi), - HAL_DEVICE_INTERFACE) - props = haldev.GetAllProperties() - except dbus.exceptions.DBusException: - continue - - if not props.has_key('volume.size'): - continue - - size = str(props['volume.size'] / (1024 * 1024)) - if props.has_key('block.device'): - devnode = props['block.device'].encode('utf-8') - elif props.has_key('linux.device_file'): - devnode = props['linux.device_file'].encode('utf-8') + for dev in filter(lambda d: d.type <= parted.DEVICE_SX8, parted.getAllDevices()): + disk = parted.Disk(dev) - lst.add("%s %s" % (devnode, size,)) + for part in disk.partitions: + lst.add("%s %s" % (part.path, int(part.getSize()))) lst = list(lst) lst.sort() diff --git a/isys/isys.py b/isys/isys.py index f1003b59b..13ba807d8 100755 --- a/isys/isys.py +++ b/isys/isys.py @@ -581,7 +581,7 @@ def getMacAddress(dev): # Get a description string for a network device (e.g., eth0) def getNetDevDesc(dev): - from storage.udev import udev_get_block_device + from baseudev import udev_get_device desc = "Network Interface" if dev == '' or dev is None: @@ -599,7 +599,7 @@ def getNetDevDesc(dev): if dev == device_props['Interface']: # This is the sysfs path (for now). udev_path = device_props['Udi'] - dev = udev_get_block_device(udev_path, requireName=False) + dev = udev_get_device(udev_path) if dev is None: log.debug("weird, we have a None dev with path %s" % path) diff --git a/storage/udev.py b/storage/udev.py index 051dc21e9..450b54aa6 100644 --- a/storage/udev.py +++ b/storage/udev.py @@ -25,6 +25,7 @@ import stat import iutil from errors import * +from baseudev import * import logging log = logging.getLogger("storage") @@ -56,7 +57,7 @@ def udev_resolve_devspec(devspec): def udev_get_block_devices(): udev_settle(timeout=30) entries = [] - for path in enumerate_block_devices(): + for path in udev_enumerate_block_devices(): entry = udev_get_block_device(path) if entry: entries.append(entry) @@ -87,112 +88,28 @@ def __is_blacklisted_blockdev(dev_name): return False -def enumerate_block_devices(): - top_dir = "/sys/class/block" - devices = [] - for dev_name in os.listdir(top_dir): - if __is_blacklisted_blockdev(dev_name): - continue - full_path = os.path.join(top_dir, dev_name) - link_ref = os.readlink(full_path) - real_path = os.path.join(top_dir, link_ref) - sysfs_path = os.path.normpath(real_path) - devices.append(sysfs_path) - return devices - -def udev_get_block_device(sysfs_path, requireName=True): - if not os.path.exists(sysfs_path): - log.debug("%s does not exist" % sysfs_path) - return None - - db_entry = sysfs_path[4:].replace("/", "\\x2f") - db_root = "/dev/.udev/db" - db_path = os.path.normpath("%s/%s" % (db_root, db_entry)) - if not os.access(db_path, os.R_OK): - log.debug("db entry %s does not exist" % db_path) - return None +def udev_enumerate_block_devices(): + import os.path - entry = open(db_path).read() - dev = udev_parse_block_entry(entry) - if requireName and dev.has_key("name"): - # XXX why do we do this? is /sys going to move during installation? - dev['sysfs_path'] = sysfs_path[4:] # strip off the leading '/sys' - dev = udev_parse_uevent_file(dev) + return filter(lambda d: not __is_blacklisted_blockdev(os.path.basename(d)), + udev_enumerate_devices(deviceClass="block")) - # now add in the contents of the uevent file since they're handy - return dev - -def udev_parse_uevent_file(dev): - path = os.path.normpath("/sys/%s/uevent" % dev['sysfs_path']) - if not os.access(path, os.R_OK): +def udev_get_block_device(sysfs_path): + dev = udev_get_device(sysfs_path) + if not dev or not dev.has_key("name"): + return {"name": None, "symlinks": []} + else: return dev - with open(path) as f: - for line in f.readlines(): - (key, equals, value) = line.strip().partition("=") - if not equals: - continue - - dev[key] = value - - return dev - def udev_parse_block_entry(buf): - dev = {} - - for line in buf.splitlines(): - line.strip() - (tag, sep, val) = line.partition(":") - if not sep: - continue - - if tag == "N": - dev['name'] = val - elif tag == "S": - if dev.has_key('symlinks'): - dev['symlinks'].append(val) - else: - dev['symlinks'] = [val] - elif tag == "E": - if val.count("=") > 1 and val.count(" ") > 0: - # eg: LVM2_LV_NAME when querying the VG for its LVs - vars = val.split() - vals = [] - var_name = None - for (index, subval) in enumerate(vars): - (var_name, sep, var_val) = subval.partition("=") - if sep: - vals.append(var_val) - - dev[var_name] = vals - else: - (var_name, sep, var_val) = val.partition("=") - if not sep: - continue - - # Skip splitting for any keys matching MODEL or VENDOR, since - # those values could include embedded, unquoted spaces. - if var_val.count(" ") and var_name.find("MODEL") == -1 and var_name.find("VENDOR") == -1: - # eg: DEVLINKS - var_val = var_val.split() - - dev[var_name] = var_val - - return dev + dev = udev_parse_entry(buf) -def udev_settle(timeout=None): - argv = ["settle"] - if timeout: - argv.append("--timeout=%d" % int(timeout)) + for (key, value) in dev.iteritems(): + if value.count(" "): + # eg: DEVLINKS + dev.update({key: value.split()}) - iutil.execWithRedirect("udevadm", argv, stderr="/dev/null", searchPath=1) - -def udev_trigger(subsystem=None): - argv = ["trigger"] - if subsystem: - argv.append("--subsystem-match=%s" % subsystem) - - iutil.execWithRedirect("udevadm", argv, stderr="/dev/null", searchPath=1) + return dev # These are functions for retrieving specific pieces of information from |