def __init__(self, bus_name): gobject.GObject.__init__(self) self.bus_name = bus_name self.device_list = {} self.__devtable = DeviceTable.DeviceTable() self.udev_client = gudev.Client(["tty"]) self.udev_client_usb = gudev.Client(["usb/usb_device"]) thread.start_new_thread(self.__check_available_devices, ())
def fget(self): try: c = gudev.Client(["tty"]) return c.query_by_device_file( self.io.modem_path).get_parent().get_driver() except: return ''
def mount_live(): ret = os.system('cat /proc/mounts|grep -q "none /live"') if ret == 0: os.system("umount /live") live_dev = "/dev/live" if not os.path.exists(live_dev): ret = os.system("losetup /dev/loop0|grep -q '\.iso'") if ret == 0: # PXE boot live_dev = "/dev/loop0" else: # /dev/live if not exist alternative client = gudev.Client(['block']) version = open("/etc/default/version") for line in version.readlines(): if "PACKAGE" in line: pkg, pkg_name = line.split("=") for device in client.query_by_subsystem("block"): if device.has_property("ID_CDROM"): dev = device.get_property("DEVNAME") blkid_cmd = "blkid '%s'|grep -q '%s'" % (dev, pkg_name) ret = os.system(blkid_cmd) if ret == 0: live_dev = dev os.system("mkdir -p /live") os.system("mount -r " + live_dev + " /live &>/dev/null") if os.path.ismount("/live"): return True else: return False
def get_live_disk(): live_disk = "" if os.path.exists("/dev/live"): live_disk = os.path.dirname(udev_info("/dev/live", "path")) if "block" in live_disk: live_disk = os.path.basename(udev_info("/dev/live", "path")).strip() elif os.path.exists("/dev/disk/by-label/LIVE"): live_disk = os.path.dirname( udev_info("/dev/disk/by-label/LIVE", "path")) if "block" in live_disk: live_disk = os.path.basename( udev_info("/dev/disk/by-label/LIVE", "path")).strip() else: ret = os.system("losetup /dev/loop0|grep -q '\.iso'") if ret != 0: client = gudev.Client(['block']) version = open("/etc/default/version") for line in version.readlines(): if "PACKAGE" in line: pkg, pkg_name = line.split("=") for device in client.query_by_subsystem("block"): if device.has_property("ID_CDROM"): dev = device.get_property("DEVNAME") blkid_cmd = "blkid '%s'|grep -q '%s' " % (dev, pkg_name) ret = os.system(blkid_cmd) if ret == 0: live_disk = os.path.basename(dev) return live_disk
class UdevNICInfo(base.Base): """Gather NIC infos form udev """ _client = gudev.Client(['net']) _cached_device = None ifname = None def __init__(self, iface): super(UdevNICInfo, self).__init__() self.ifname = iface @property def _udev_device(self): if not self._cached_device: for d in self._client.query_by_subsystem("net"): if d.get_property("INTERFACE") == self.ifname: self._cached_device = d if not self._cached_device: self.logger.debug("udev has no infos for %s" % self.ifname) return self._cached_device def __get_property(self, name): return self._udev_device.get_property(name) if self.exists() \ else None def exists(self): return self._udev_device is not None @property def name(self): return self.__get_property("INTERFACE") @property def vendor(self): vendor = self.__get_property("ID_VENDOR_FROM_DATABASE") if not vendor: # fallback method for older udev versions try: dpath = self.__get_property("DEVPATH") pci_addr = dpath.split("/")[-3] cmd = ["lspci", "-m", "-s", pci_addr] lspci_out = process.pipe(cmd, check=True) # shelx needs str not unicode vendor = shlex.split(str(lspci_out))[2] except: self.logger.debug("Failed to fetch vendor name for %s" % dpath, exc_info=True) return vendor @property def devtype(self): return self.__get_property("DEVTYPE") @property def devpath(self): return self.__get_property("DEVPATH")
def __init__(self) : service.Service.__init__(self) ##### self.__udev_client = gudev.Client(["block"]) self.__disks_smart = DisksSmart(DISKS_SMART_OBJECT_NAME, self)
def getUdevDevs(self, options): """ Method: getUdevDevs Method gets info about FC HBA's for systems with Udev @type options: dict @param options: passed options @rtype: void """ # getting fc_host devices, these are HBA ports import gudev client = gudev.Client(["fc_host"]) devs = client.query_by_subsystem("fc_host") # getting information all pci cards, and from those we will select our HBAs pciinfo = systeminfo.proc.pci.Pci() pciinfo.getData(options) for dev in devs: props = {} parentdev = dev.get_parent() grandparent = parentdev.get_parent() # getting vendor hex number, we will match it with info from Pci vendorhex = grandparent.get_sysfs_attr('vendor') vendorhex = string.replace(vendorhex, '0x', '') # getting address hostregex = re.compile('\/([a-zA-Z0-9:.]+)\/host[0-9]+') hostmatch = hostregex.search(dev.get_sysfs_path()) if hostmatch: props['pcicard'] = hostmatch.group(1) props['pci.vendor'] = pciinfo.pciids['vendors'][vendorhex] props['linux.sysfs_path'] = dev.get_sysfs_path() props['nodename'] = dev.get_sysfs_attr('node_name') props['portname'] = dev.get_sysfs_attr('port_name') props['portstate'] = dev.get_sysfs_attr('port_state') props['porttype'] = dev.get_sysfs_attr('port_type') props['speed'] = dev.get_sysfs_attr('speed') props['driver'] = grandparent.get_property('DRIVER') props['irq'] = grandparent.get_sysfs_attr('irq') props['numanode'] = grandparent.get_sysfs_attr('numa_node') props['localcpulist'] = grandparent.get_sysfs_attr('local_cpulist') props['localcpus'] = grandparent.get_sysfs_attr('local_cpus') props['fabricname'] = dev.get_sysfs_attr('fabric_name') props['supportedclasses'] = dev.get_sysfs_attr('supported_classes') props['supportedspeeds'] = dev.get_sysfs_attr('supported_speeds') props['maxnpivvports'] = dev.get_sysfs_attr('max_npiv_vports') props['npivvportsinuse'] = dev.get_sysfs_attr('npiv_vports_inuse') props['portid'] = dev.get_sysfs_attr('port_id') props['symbolicname'] = dev.get_sysfs_attr('symbolic_name') props['toolindex'] = props['pcicard'] self.asset_info.append(props)
def watch_removed_device(self): # look for removed device loop = glib.MainLoop() device_listener = DeviceListener(self, loop, "remove") client = gudev.Client(["usb/usb_device"]) client.connect("uevent", device_listener.callback, None) loop.run() return device_listener.device_found
def configure_device(self): # binding process starts here loop = glib.MainLoop() device_listener = DeviceListener(self, loop, 'add') client = gudev.Client(["usb/usb_device"]) client.connect("uevent", device_listener.callback, None) loop.run() return device_listener.device_info
def get_subsystems(): client = gudev.Client('') subsys = [] for device in client.query_by_subsystem('*'): device_subsystem = device.get_subsystem() if not device_subsystem in subsys: subsys.append(device_subsystem) return subsys
def getUdevDevs(self, options): """ Method: getUdevDevs Getting information for systems with Udev @type options: dict @param options: passed parameters @rtype: void """ # getting info about pci devices, matching against pci ids parsed earlier import gudev client = gudev.Client(["pci"]) devs = client.query_by_subsystem("pci") for dev in devs: props = {} vendorhex = dev.get_sysfs_attr('vendor') devhex = dev.get_sysfs_attr('device') classhex = dev.get_sysfs_attr('class') subvendhex = dev.get_sysfs_attr('subsystem_vendor') subdevhex = dev.get_sysfs_attr('subsystem_device') vendorhex = string.replace(vendorhex, '0x', '') devhex = string.replace(devhex, '0x', '') classhex = string.replace(classhex, '0x', '') subvendhex = string.replace(subvendhex, '0x', '') subdevhex = string.replace(subdevhex, '0x', '') subdevid = subvendhex + subdevhex subdevice = '' classreg = re.search('^(\w{2})(\w{2})(\w{2})', classhex) if vendorhex in self.pciids['subdevs'].keys(): if subdevid in self.pciids['subdevs'][vendorhex].keys(): subdevice = self.pciids['subdevs'][vendorhex][subdevid] props['addr'] = dev.get_property('PCI_SLOT_NAME') props['vendor'] = self.pciids['vendors'][vendorhex] props['device'] = self.pciids['devices'][vendorhex][devhex] props['class'] = self.pciids['classes'][classreg.group(1)] props['subclass'] = self.pciids['subclasses'][classreg.group(1)][classreg.group(2)] props['subdevice'] = subdevice props['driver'] = dev.get_property('DRIVER') props['sysfspath'] = dev.get_sysfs_path() props['localcpus'] = dev.get_sysfs_attr('local_cpus') props['irq'] = dev.get_sysfs_attr('irq') props['numanode'] = dev.get_sysfs_attr('numa_node') props['localcpulist'] = dev.get_sysfs_attr('local_cpulist') props['toolindex'] = props['addr'] self.asset_info.append(props)
def getUdevDevs(self, options): """ Method: getUdevDevs Method gets info about FC HBA's for systems with Udev @type options: dict @param options: passed options @rtype: void """ # getting fc_host devices, these are HBA ports import gudev client = gudev.Client(["net"]) devs = client.query_by_subsystem("net") # getting information all pci cards, and from those we will select our HBAs pciinfo = systeminfo.proc.pci.Pci(self.confDir, self.cacheDir, self.viewDir) for dev in devs: props = {} parentdev = dev.get_parent() if parentdev: classhex = parentdev.get_sysfs_attr('class') if classhex: classhex = string.replace(classhex, '0x', '') classreg = re.search('^(\w{2})(\w{2})(\w{2})', classhex) parentClassId = classreg.group(1) parentSubClassId = classreg.group(2) if (parentClassId == '02' and (parentSubClassId == '00' or parentSubClassId == '80')): devsysfspath = dev.get_sysfs_path() duplex = systeminfo.io.file.readFile(devsysfspath + '/' + 'duplex') speed = systeminfo.io.file.readFile(devsysfspath + '/' + 'speed') props = pciinfo.getUdevPciDevInfo(parentdev) props['mac'] = dev.get_sysfs_attr('address') props['operstate'] = dev.get_sysfs_attr('operstate') props['mtu'] = dev.get_sysfs_attr('mtu') props['intf'] = dev.get_property('INTERFACE') props['duplex'] = duplex[0].strip() props['speed'] = speed[0].strip() self.asset_info.append(props)
def watch_added_device(self): loop = glib.MainLoop() device_listener = DeviceListener(self, loop, "add") client = gudev.Client(["usb/usb_device"]) # client = gudev.Client([]) # all types client.connect("uevent", device_listener.callback, None) loop.run() return device_listener.device_found
def loop(args, machine_secret): """Wait for YubiKeys to be inserted, udev triggers.""" def usb_event(client, event, device): """GUDev handler when an USB event triggers.""" if (device.get_action() == 'add' and device.get_property('ID_VENDOR') == 'Yubico' and device.get_property('ID_MODEL') == 'Yubico_Yubikey_II'): yubikey_inserted(args, machine_secret) client = gudev.Client(['usb']) client.connect('uevent', usb_event) gobject.MainLoop().run()
def lista_discos(self): ''' devuelve los discos que están conectados al equipo ''' l = [] d = gudev.Client(['block']) c = d.query_by_subsystem('block') for i in c: if i.get_devtype() == 'disk': if i.get_property('ID_TYPE') == 'disk': l.append(i.get_device_file()) return l
def run(): client = gudev.Client(["usb/usb_device"]) client.connect("uevent", callback, None) if args.enable_leds: led_process_start() loop = glib.MainLoop() loop.run() if args.enable_leds: led_process_stop()
def __init__(self, subsystems=["usb"]): ''' Create a new DeviceFinder and attach to the udev system to listen for events. ''' TcloudObject.__init__(self) self.client = gudev.Client(subsystems) self.subsystems = subsystems self.devices_tree = {} self.devices_list = [] self.client.connect('uevent', self.uevent)
def __init__(self, subsystems='', parent_tree=False): ''' Create a new DeviceFinder and attach to the udev system to listen for events. ''' GObject.GObject.__init__(self) self.client = gudev.Client(subsystems) self.subsystems = subsystems self.parent_tree = parent_tree self.devices_tree = {} self.devices_list = [] self.client.connect('uevent', self.event)
def fget(self): try: c = gudev.Client(["tty"]) parent = c.query_by_device_file( self.io.modem_path).get_parent() for x in range(0, 3): if parent.get_subsystem() == "usb": return parent.get_parent().get_sysfs_path() else: parent = parent.get_parent() return '' except: return ''
def get_udev_devices(self): self.disk_dict = {} client = gudev.Client(['block']) for device in client.query_by_subsystem("block"): dev_name = device.get_property("DEVNAME") dev_bus = device.get_property("ID_BUS") dev_model = device.get_property("ID_MODEL") dev_serial = device.get_property("ID_SERIAL") dev_desc = device.get_property("ID_SCSI_COMPAT") dev_size_cmd = "sfdisk -s %s 2>/dev/null" % dev_name dev_size = _functions.subprocess_closefds(dev_size_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) dev_size = dev_size.stdout.read() size_failed = 0 if not device.get_property("ID_CDROM"): try: dev_size = int(dev_size) / 1024 / 1024 except: size_failed = 1 if not dev_desc: if "/dev/vd" in dev_name: dev_desc = "virtio disk" elif dev_serial is not None: dev_desc = dev_serial else: dev_desc = "unknown" if (not device.get_property("ID_CDROM") and not "/dev/dm-" in dev_name and not "/dev/loop" in dev_name and size_failed == 0): dev_name = _functions.translate_multipath_device(dev_name) busmap = { \ "usb": "USB Device ", \ "ata": "Local / FibreChannel", \ "scsi": "Local / FibreChannel", \ "cciss": "CCISS " \ } if dev_bus in busmap: dev_bus = busmap[dev_bus] elif "/dev/vd" in dev_name: dev_bus = "Local (Virtio) " else: dev_bus = " " self.disk_dict[dev_name] = "%s,%s,%s,%s,%s,%s" % ( dev_bus, dev_name, dev_size, dev_desc, dev_serial, dev_model) devs = self.get_dev_name() return (sorted(devs), self.disk_dict)
def start(self): logging.info("brickd started") self.usb_notifier = USBNotifier() if gudev_imported: self.gudev_client = gudev.Client(["usb"]) self.gudev_client.connect("uevent", self.notify_udev) reactor.listenTCP(config.PORT, BrickProtocolFactory()) try: reactor.run(installSignalHandlers=True) except KeyboardInterrupt: reactor.stop()
def logical_to_physical_networks(): networks = {} client = gudev.Client(['net']) for device in client.query_by_subsystem("net"): try: dev_interface = device.get_property("INTERFACE") dev_address = get_mac_address(dev_interface) bridge_cmd = "/files/etc/sysconfig/network-scripts/ifcfg-%s/BRIDGE" % str( dev_interface) dev_bridge = augtool_get(bridge_cmd) except: pass if not dev_bridge is None: networks[dev_bridge] = (dev_interface, dev_address) return networks
def _get_gudev_devices(self): client = gudev.Client(["tty", 'usb-serial']) devices = [] # usb serial devices for dev in client.query_by_subsystem("usb-serial"): devices.append(SerialDevice('/dev/' + dev.get_name())) # serial tty devices for dev in client.query_by_subsystem("tty"): parent = dev.get_parent() if parent is None: continue if parent.get_driver() != 'serial': continue devices.append(SerialDevice('/dev/' + dev.get_name())) return devices
class UdevNICInfo(base.Base): """Gather NIC infos form udev """ _client = gudev.Client(['net']) _cached_device = None ifname = None def __init__(self, iface): super(UdevNICInfo, self).__init__() self.ifname = iface @property def _udev_device(self): if not self._cached_device: for d in self._client.query_by_subsystem("net"): if d.get_property("INTERFACE") == self.ifname: self._cached_device = d if not self._cached_device: self.logger.debug("udev has no infos for %s" % self.ifname) return self._cached_device def __get_property(self, name): return self._udev_device.get_property(name) if self.exists() \ else None def exists(self): return self._udev_device is not None @property def name(self): return self.__get_property("INTERFACE") @property def vendor(self): return self.__get_property("ID_VENDOR_FROM_DATABASE") @property def devtype(self): return self.__get_property("DEVTYPE") @property def devpath(self): return self.__get_property("DEVPATH")
def check_for_dvd(): """scan udev list for dvd drives and fund the name of the dvd""" dvdname = None client = gudev.Client(['block']) for device in client.query_by_subsystem("block"): #find device if device.has_property("ID_CDROM"): loginfo = "Found CD/DVD drive at %s" % device.get_device_file() write_log(loginfo) # check for content if device.has_property("ID_FS_LABEL"): loginfo = "Found disk: %s" % device.get_property("ID_FS_LABEL") dvdname = device.get_property("ID_FS_LABEL") elif device.has_property("ID_FS_TYPE"): loginfo = "Found disc but it has no name!" else: loginfo = "No disc" write_log(loginfo) return dvdname
def scan_subsystems(self, subsystems='', parent_tree=False): self.client = gudev.Client(subsystems) self.subsystems = subsystems self.devices_tree = {} self.devices_list = [] if subsystems == '': subsystems = ['*'] for subsystem in subsystems: for gudevice in self.client.query_by_subsystem(subsystem): if parent_tree: self.__explore_parent(gudevice, self.devices_tree, self.devices_list) else: self.devices_list.append( device.get_device_object(gudevice)) self.devices_tree[gudevice.get_sysfs_path()] = \ device.get_device_object(gudevice) self.client.connect('uevent', self.event) self.parent_tree = parent_tree
def getUdevDevs(self, options): """ Method: getUdevDevs Getting information for systems with Udev @type options: dict @param options: passed parameters @rtype: void """ # getting info about pci devices, matching against pci ids parsed earlier import gudev client = gudev.Client(["pci"]) devs = client.query_by_subsystem("pci") for dev in devs: props = self.getUdevPciDevInfo(dev) self.asset_info.append(props)
def getUdevDevs(self, options): """ Method: getUdevDevs Method gets info about FC HBA's for systems with Udev @type options: dict @param options: passed options @rtype: void """ # getting fc_host devices, these are HBA ports import gudev client = gudev.Client(["fc_host"]) devs = client.query_by_subsystem("fc_host") # getting information all pci cards, and from those we will select our HBAs pciinfo = systeminfo.proc.pci.Pci(self.confDir, self.cacheDir, self.viewDir) for dev in devs: props = {} parentdev = dev.get_parent() grandparent = parentdev.get_parent() props = pciinfo.getUdevPciDevInfo(grandparent) props['nodename'] = dev.get_sysfs_attr('node_name') props['portname'] = dev.get_sysfs_attr('port_name') props['portstate'] = dev.get_sysfs_attr('port_state') props['porttype'] = dev.get_sysfs_attr('port_type') props['speed'] = dev.get_sysfs_attr('speed') props['fabricname'] = dev.get_sysfs_attr('fabric_name') props['supportedclasses'] = dev.get_sysfs_attr('supported_classes') props['supportedspeeds'] = dev.get_sysfs_attr('supported_speeds') props['maxnpivvports'] = dev.get_sysfs_attr('max_npiv_vports') props['npivvportsinuse'] = dev.get_sysfs_attr('npiv_vports_inuse') props['portid'] = dev.get_sysfs_attr('port_id') props['symbolicname'] = dev.get_sysfs_attr('symbolic_name') self.asset_info.append(props)
def __init__(self): """ Create a new DVDFinder and attach to the udev system to listen for events. """ self.__gobject_init__() self.client = gudev.Client(["video4linux", "block"]) self.drives = {} self.capture_devices = {} for device in self.client.query_by_subsystem("video4linux"): block = device.get_device_file() self.capture_devices[block] = V4LDevice(device) for device in self.client.query_by_subsystem("block"): if device.has_property("ID_CDROM"): block = device.get_device_file() self.drives[block] = DVDDevice(device) self.client.connect("uevent", self.event)
g = int(max(0, min(255, g))) b = int(max(0, min(255, b))) rainbow.set_pixel(x, y, r, g, b) rainbow.show() time.sleep(0.02) def clear(): for y in range(8): for x in range(8): rainbow.set_pixel(x, y, 0, 0, 0) rainbow.show() def callback(client, action, device, user_data): if action == "add": effect() if action == "remove": clear() client = gudev.Client(["usb/usb_device"]) client.connect("uevent", callback, None) loop = glib.MainLoop() loop.run()