From 1fc4aadb5d87643d6e64805b19a5a9e7671737ba Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 29 May 2020 22:22:55 +0200 Subject: udev: T2490: add persistent USB device files During testing it was discovered that on 5 out of 10 reboots the USB enumeration/mapping from physical port to /dev/ttyUSB is different. The root cause is that it's a FIFO so first found/loaded driver module will be assigned ttyUSB0. This mixed up the serial interfaces of my FTDI chips and my connected Sierra Wireless MC7710 card which was no longer functioning as it now was mapped to a different USB interface. The solution is a udev rule which persistently maps the USB-tree-device to a device file in /dev. Wait? isn't this what /dev/serial/by-{id,path} is for? Correct, it does the very same thing but the problem is as follows: * by-path uses device file names which also incorporate the parent bus system, this results in "pci-0000:00:10.0-usb-0:2.4:1.0-port0" * by-id will overwrite the assigned device symlink if a new USB device with the same name appears. This happens to some FTDI devices with no serial number programmed so the device added last wins and will be the only one in the by-id folder - cruel world! This commit adds a new directory /dev/serial/by-bus which holds the following device files (as example): $ ls -1 /dev/serial/by-bus/ usb0b1.3p1.0 usb0b1.3p1.2 usb0b1.3p1.3 usb0b2.4p1.0 usb0b2.4p1.1 usb0b2.4p1.2 usb0b2.4p1.3 --- src/etc/udev/rules.d/90-vyos-serial.rules | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/etc/udev/rules.d/90-vyos-serial.rules (limited to 'src') diff --git a/src/etc/udev/rules.d/90-vyos-serial.rules b/src/etc/udev/rules.d/90-vyos-serial.rules new file mode 100644 index 000000000..3f10f4924 --- /dev/null +++ b/src/etc/udev/rules.d/90-vyos-serial.rules @@ -0,0 +1,28 @@ +# do not edit this file, it will be overwritten on update + +ACTION=="remove", GOTO="serial_end" +SUBSYSTEM!="tty", GOTO="serial_end" + +SUBSYSTEMS=="pci", ENV{ID_BUS}="pci", ENV{ID_VENDOR_ID}="$attr{vendor}", ENV{ID_MODEL_ID}="$attr{device}" +SUBSYSTEMS=="pci", IMPORT{builtin}="hwdb --subsystem=pci" +SUBSYSTEMS=="usb", IMPORT{builtin}="usb_id", IMPORT{builtin}="hwdb --subsystem=usb" + +# /dev/serial/by-path/, /dev/serial/by-id/ for USB devices +KERNEL!="ttyUSB[0-9]*|ttyACM[0-9]*", GOTO="serial_end" + +SUBSYSTEMS=="usb-serial", ENV{.ID_PORT}="$attr{port_number}" + +IMPORT{builtin}="path_id", IMPORT{builtin}="usb_id" + +# Change the name of the usb id to a "more" human redable format. +# +# - $env{ID_PATH} usually is a name like: "pci-0000:00:10.0-usb-0:2.3.3.4:1.0-port0" so we strip the "pci-*" +# portion and only use the usb part +# - Transform the USB "speach" to the tree like structure so we start with "usb0" as root-complex 0. +# (tr -d -) does the replacement +# - Replace the first group after ":" to represent the bus relation (sed -e 0,/:/s//b/) indicated by "b" +# - Replace the next group after ":" to represent the port relation (sed -e 0,/:/s//p/) indicated by "p" +ENV{ID_PATH}=="?*", ENV{.ID_PORT}=="", PROGRAM="/bin/sh -c 'D=$env{ID_PATH}; echo ${D:17} | tr -d - | sed -e 0,/:/s//b/ | sed -e 0,/:/s//p/'", SYMLINK+="serial/by-bus/$result" +ENV{ID_PATH}=="?*", ENV{.ID_PORT}=="?*", PROGRAM="/bin/sh -c 'D=$env{ID_PATH}; echo ${D:17} | tr -d - | sed -e 0,/:/s//b/ | sed -e 0,/:/s//p/'", SYMLINK+="serial/by-bus/$result" + +LABEL="serial_end" -- cgit v1.2.3 From 1c7d7cbd3963428888068af679946e6329567451 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 7 Jun 2020 14:04:36 +0200 Subject: wwan: T2529: migrate device from ttyUSB to usbXbY.YpZ.Z During testing it was discovered that there is a well known problem (we had for ethernet interfaces) also in the serial port world. They will be enumerated and mapped to /dev/ttyUSBxxx differently from boot to boot. This is especially painful on my development APU4 board which also has a Sierra Wireless MC7710 LTE module installed. The serial port will toggle between ttyUSB2 and ttyUSB5 depending on the amount of serial port extenders attached (FT4232H). The shipped udev rule (/usr/lib/udev/rules.d/60-serial.rules) partly solves this by enumerating the devices into /dev/serial/by-id folder with their name and serial number - it's a very good idea but I've found that not all of the FT4232H dongles have a serial number programmed - this leads to the situation that when you plug in two cables with both having serial number 0 - only one device symlink will appear - the previous one is always overwritten by the latter one. Derive /usr/lib/udev/rules.d/60-serial.rules and create a /dev/serial/by-bus directory and group devices by attached USB root port. vyos@vyos:~$ find /dev/serial/by-bus/ -name usb* -exec basename {} \; | sort usb0b1.3p1.0 usb0b1.3p1.2 usb0b1.3p1.3 usb0b2.4p1.0 usb0b2.4p1.1 usb0b2.4p1.2 usb0b2.4p1.3 So we have USB root 0 with bus 1.3 and port 1.0. The enumeration is constant accross reboots. --- data/templates/wwan/peer.tmpl | 2 +- .../interfaces-wirelessmodem.xml.in | 10 ++-- src/conf_mode/interfaces-wirelessmodem.py | 32 ++++++++++--- src/migration-scripts/interfaces/10-to-11 | 55 ++++++++++++++++++++++ 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100755 src/migration-scripts/interfaces/10-to-11 (limited to 'src') diff --git a/data/templates/wwan/peer.tmpl b/data/templates/wwan/peer.tmpl index 04ab4f844..b8a7d10ae 100644 --- a/data/templates/wwan/peer.tmpl +++ b/data/templates/wwan/peer.tmpl @@ -10,7 +10,7 @@ linkname {{ intf }} usepeerdns {%- endif %} # physical device -/dev/{{ device }} +{{ device }} lcp-echo-failure 0 115200 debug diff --git a/interface-definitions/interfaces-wirelessmodem.xml.in b/interface-definitions/interfaces-wirelessmodem.xml.in index 91eee56ab..d5f2e04d7 100644 --- a/interface-definitions/interfaces-wirelessmodem.xml.in +++ b/interface-definitions/interfaces-wirelessmodem.xml.in @@ -46,12 +46,16 @@ #include - System device name (default: ttyUSB0) + Serial device - + - ttyXXX + ttySXX + System TTY device name + + + usbXbY.YpZ.Z System TTY device name diff --git a/src/conf_mode/interfaces-wirelessmodem.py b/src/conf_mode/interfaces-wirelessmodem.py index a13c70990..f16457a7f 100755 --- a/src/conf_mode/interfaces-wirelessmodem.py +++ b/src/conf_mode/interfaces-wirelessmodem.py @@ -16,9 +16,10 @@ import os -from sys import exit from copy import deepcopy +from fnmatch import fnmatch from netifaces import interfaces +from sys import exit from vyos.config import Config from vyos.ifconfig import BridgeIf, Section @@ -36,7 +37,7 @@ default_config_data = { 'chat_script': '', 'deleted': False, 'description': '', - 'device': 'ttyUSB0', + 'device': '', 'disable': False, 'disable_link_detect': 1, 'on_demand': False, @@ -56,6 +57,16 @@ def check_kmod(): if call(f'modprobe {module}') != 0: raise ConfigError(f'Loading Kernel module {module} failed') +def find_device_file(device): + """ Recurively search /dev for the given device file and return its full path. + If no device file was found 'None' is returned """ + for root, dirs, files in os.walk('/dev'): + for basename in files: + if fnmatch(basename, device): + return os.path.join(root, basename) + + return None + def get_config(): wwan = deepcopy(default_config_data) conf = Config() @@ -93,7 +104,13 @@ def get_config(): # System device name if conf.exists(['device']): - wwan['device'] = conf.return_value(['device']) + tmp = conf.return_value(['device']) + wwan['device'] = find_device_file(tmp) + # If device file was not found in /dev we will just re-use + # the plain device name, thus we can trigger the exception + # in verify() as it's a non existent file + if wwan['device'] == None: + wwan['device'] = tmp # disable interface if conf.exists('disable'): @@ -131,7 +148,10 @@ def verify(wwan): return None if not wwan['apn']: - raise ConfigError(f"APN for {wwan['intf']} not configured") + raise ConfigError('No APN configured for "{intf}"'.format(**wwan)) + + if not wwan['device']: + raise ConfigError('Physical "device" must be configured') # we can not use isfile() here as Linux device files are no regular files # thus the check will return False @@ -169,7 +189,7 @@ def generate(wwan): script_wwan_ip_up, script_wwan_ip_down] # Always hang-up WWAN connection prior generating new configuration file - cmd(f'systemctl stop ppp@{intf}.service') + call(f'systemctl stop ppp@{intf}.service') if wwan['deleted']: # Delete PPP configuration files @@ -205,9 +225,9 @@ def apply(wwan): if not wwan['disable']: # "dial" WWAN connection intf = wwan['intf'] - cmd(f'systemctl start ppp@{intf}.service') # make logfile owned by root / vyattacfg chown(wwan['logfile'], 'root', 'vyattacfg') + call(f'systemctl start ppp@{intf}.service') # re-add ourselves to any bridge we might have fallen out of # FIXME: wwan isn't under vyos.ifconfig so we can't call diff --git a/src/migration-scripts/interfaces/10-to-11 b/src/migration-scripts/interfaces/10-to-11 new file mode 100755 index 000000000..6b8e49ed9 --- /dev/null +++ b/src/migration-scripts/interfaces/10-to-11 @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2020 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# 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 . + +# rename WWAN (wirelessmodem) serial interface from non persistent ttyUSB2 to +# a bus like name, e.g. "usb0b1.3p1.3" + +import os + +from sys import exit, argv +from vyos.configtree import ConfigTree + +if __name__ == '__main__': + if (len(argv) < 1): + print("Must specify file name!") + exit(1) + + file_name = argv[1] + with open(file_name, 'r') as f: + config_file = f.read() + + config = ConfigTree(config_file) + base = ['interfaces', 'wirelessmodem'] + if not config.exists(base): + # Nothing to do + exit(0) + + for wwan in config.list_nodes(base): + if config.exists(base + [wwan, 'device']): + device = config.return_value(base + [wwan, 'device']) + + for root, dirs, files in os.walk('/dev/serial/by-bus'): + for file in files: + device_file = os.path.realpath(os.path.join(root, file)) + if os.path.basename(device_file) == device: + config.set(base + [wwan, 'device'], value=file, replace=True) + + try: + with open(file_name, 'w') as f: + f.write(config.to_string()) + except OSError as e: + print("Failed to save the modified config: {}".format(e)) + exit(1) -- cgit v1.2.3 From d70f1538f215f065855a89b1afe4cd8b26cd9cc2 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 7 Jun 2020 14:12:28 +0200 Subject: wwan: T2488: remove generation of dedicated logfile ... all information are present in journald. --- data/templates/wwan/peer.tmpl | 1 - src/conf_mode/interfaces-wirelessmodem.py | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) (limited to 'src') diff --git a/data/templates/wwan/peer.tmpl b/data/templates/wwan/peer.tmpl index b8a7d10ae..0168283fd 100644 --- a/data/templates/wwan/peer.tmpl +++ b/data/templates/wwan/peer.tmpl @@ -14,7 +14,6 @@ usepeerdns lcp-echo-failure 0 115200 debug -logfile {{ logfile }} nodefaultroute ipcp-max-failure 4 ipcp-accept-local diff --git a/src/conf_mode/interfaces-wirelessmodem.py b/src/conf_mode/interfaces-wirelessmodem.py index f16457a7f..571969f4d 100755 --- a/src/conf_mode/interfaces-wirelessmodem.py +++ b/src/conf_mode/interfaces-wirelessmodem.py @@ -24,7 +24,7 @@ from sys import exit from vyos.config import Config from vyos.ifconfig import BridgeIf, Section from vyos.template import render -from vyos.util import chown, chmod_755, cmd, call +from vyos.util import call from vyos.validate import is_member from vyos import ConfigError @@ -41,7 +41,6 @@ default_config_data = { 'disable': False, 'disable_link_detect': 1, 'on_demand': False, - 'logfile': '', 'metric': '10', 'mtu': '1500', 'name_server': True, @@ -76,7 +75,6 @@ def get_config(): raise ConfigError('Interface (VYOS_TAGNODE_VALUE) not specified') wwan['intf'] = os.environ['VYOS_TAGNODE_VALUE'] - wwan['logfile'] = f"/var/log/vyatta/ppp_{wwan['intf']}.log" wwan['chat_script'] = f"/etc/ppp/peers/chat.{wwan['intf']}" # check if interface is member if a bridge @@ -225,8 +223,6 @@ def apply(wwan): if not wwan['disable']: # "dial" WWAN connection intf = wwan['intf'] - # make logfile owned by root / vyattacfg - chown(wwan['logfile'], 'root', 'vyattacfg') call(f'systemctl start ppp@{intf}.service') # re-add ourselves to any bridge we might have fallen out of -- cgit v1.2.3 From cceffc8725271b15107f21ae26f4e477751df6bc Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 7 Jun 2020 14:14:57 +0200 Subject: wwan: T2241: interface is not bond- or bridgeable Commit 2cb806271928 ("wirelessmodem: T2241: make VRF and bond/bridge membership mutually exclusive") added some logic which is not forseen/neither makes sense on a dialup interface, thus it's removed again --- src/conf_mode/interfaces-wirelessmodem.py | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/conf_mode/interfaces-wirelessmodem.py b/src/conf_mode/interfaces-wirelessmodem.py index 571969f4d..c05ca684e 100755 --- a/src/conf_mode/interfaces-wirelessmodem.py +++ b/src/conf_mode/interfaces-wirelessmodem.py @@ -32,7 +32,6 @@ from vyos import airbag airbag.enable() default_config_data = { - 'address': [], 'apn': '', 'chat_script': '', 'deleted': False, @@ -77,9 +76,6 @@ def get_config(): wwan['intf'] = os.environ['VYOS_TAGNODE_VALUE'] wwan['chat_script'] = f"/etc/ppp/peers/chat.{wwan['intf']}" - # check if interface is member if a bridge - wwan['is_bridge_member'] = is_member(conf, wwan['intf'], 'bridge') - # Check if interface has been removed if not conf.exists('interfaces wirelessmodem ' + wwan['intf']): wwan['deleted'] = True @@ -138,11 +134,6 @@ def get_config(): def verify(wwan): if wwan['deleted']: - if wwan['is_bridge_member']: - raise ConfigError(( - f'Cannot delete interface "{wwan["intf"]}" as it is a ' - f'member of bridge "{wwan["is_bridge_member"]}"!')) - return None if not wwan['apn']: @@ -153,23 +144,11 @@ def verify(wwan): # we can not use isfile() here as Linux device files are no regular files # thus the check will return False - if not os.path.exists(f"/dev/{wwan['device']}"): - raise ConfigError(f"Device {wwan['device']} does not exist") - - if wwan['is_bridge_member'] and wwan['address']: - raise ConfigError(( - f'Cannot assign address to interface "{wwan["intf"]}" ' - f'as it is a member of bridge "{wwan["is_bridge_member"]}"!')) - - if wwan['vrf']: - if wwan['vrf'] not in interfaces(): - raise ConfigError(f'VRF "{wwan["vrf"]}" does not exist') - - if wwan['is_bridge_member']: - raise ConfigError(( - f'Interface "{wwan["intf"]}" cannot be member of VRF ' - f'"{wwan["vrf"]}" and bridge {wwan["is_bridge_member"]} ' - f'at the same time!')) + if not os.path.exists('{device}'.format(**wwan)): + raise ConfigError('Device "{device}" does not exist'.format(**wwan)) + + if wwan['vrf'] not in interfaces(): + raise ConfigError('VRF "{vrf}" does not exist'.format(**wwan)) return None -- cgit v1.2.3 From b5cc8f4ae4178761d64c3e01e133c79219c2d756 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 7 Jun 2020 16:31:30 +0200 Subject: usb: op-mode: T2560: display USB interface information vyos@vyos:~$ show system usb /: Bus 03.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M |__ Port 3: Dev 4, If 0, Class=Vendor Specific Class, Driver=qcserial, 480M |__ Port 3: Dev 4, If 2, Class=Vendor Specific Class, Driver=qcserial, 480M |__ Port 3: Dev 4, If 3, Class=Vendor Specific Class, Driver=qcserial, 480M |__ Port 3: Dev 4, If 8, Class=Vendor Specific Class, Driver=qmi_wwan, 480M vyos@vyos:~$ show system usb serial No USB to serial converter connected vyos@vyos:~$ show system usb serial Device Model Vendor ------ ------ ------ usb0b1.3.3.4p1.0 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.3.4p1.1 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.3.4p1.2 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.3.4p1.3 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.4p1.0 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.4p1.1 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.4p1.2 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3.4p1.3 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.3p1.0 MC7710 Sierra Wireless, Inc. usb0b1.3p1.2 MC7710 Sierra Wireless, Inc. usb0b1.3p1.3 MC7710 Sierra Wireless, Inc. usb0b1.4p1.0 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.4p1.1 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.4p1.2 Quad_RS232-HS Future Technology Devices International, Ltd usb0b1.4p1.3 Quad_RS232-HS Future Technology Devices International, Ltd --- debian/control | 1 + op-mode-definitions/show-system-info.xml | 174 ------------------------------ op-mode-definitions/show-system.xml | 179 +++++++++++++++++++++++++++++++ src/op_mode/show_usb_serial.py | 57 ++++++++++ 4 files changed, 237 insertions(+), 174 deletions(-) delete mode 100644 op-mode-definitions/show-system-info.xml create mode 100644 op-mode-definitions/show-system.xml create mode 100755 src/op_mode/show_usb_serial.py (limited to 'src') diff --git a/debian/control b/debian/control index 20423aee1..7c1555416 100644 --- a/debian/control +++ b/debian/control @@ -34,6 +34,7 @@ Depends: python3, python3-zmq, python3-jmespath, python3-xmltodict, + python3-pyudev, bsdmainutils, cron, etherwake, diff --git a/op-mode-definitions/show-system-info.xml b/op-mode-definitions/show-system-info.xml deleted file mode 100644 index 61c947bbe..000000000 --- a/op-mode-definitions/show-system-info.xml +++ /dev/null @@ -1,174 +0,0 @@ - - - - - - - Show system information - - - - - - Show active network connections on the system - - netstat -an - - - - Show TCP connection information - - ss -t -r - - - - Show all TCP connections - - ss -t -a - - - - Show TCP connection without resolving names - - ss -t -n - - - - - - Show UDP socket information - - ss -u -a -r - - - - Show UDP socket information without resolving names - - ss -u -a -n - - - - - - - - - Checks overall system integrity - - sudo ${vyos_op_scripts_dir}/system_integrity.py - - - - - Show messages in kernel ring buffer - - sudo dmesg - - - - - Show user accounts - - - - - Show user account information - - ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py - - - - Show information about all accounts - - ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py all - - - - Show information about locked accounts - - ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py locked - - - - Show information about non VyOS user accounts - - ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py other - - - - Show information about VyOS user accounts - - ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py vyos - - - - - - - - - Show system memory usage - - ${vyos_op_scripts_dir}/show_ram.sh - - - - Show kernel cache information - - sudo slabtop -o - - - - Show detailed system memory usage - - cat /proc/meminfo - - - - - - - Show system processes - - ps ax - - - - Show extensive process info - - top -b -n1 - - - - Show summary of system processes - - uptime - - - - Show process tree - - ps -ejH - - - - - - - Show filesystem usage - - df -h -x squashfs - - - - - Show how long the system has been up - - uptime - - - - - - - diff --git a/op-mode-definitions/show-system.xml b/op-mode-definitions/show-system.xml new file mode 100644 index 000000000..b7f56ae20 --- /dev/null +++ b/op-mode-definitions/show-system.xml @@ -0,0 +1,179 @@ + + + + + + + Show system information + + + + + Show active network connections on the system + + netstat -an + + + + Show TCP connection information + + ss -t -r + + + + Show all TCP connections + + ss -t -a + + + + Show TCP connection without resolving names + + ss -t -n + + + + + + Show UDP socket information + + ss -u -a -r + + + + Show UDP socket information without resolving names + + ss -u -a -n + + + + + + + + Checks overall system integrity + + sudo ${vyos_op_scripts_dir}/system_integrity.py + + + + Show messages in kernel ring buffer + + sudo dmesg + + + + Show user accounts + + + + + Show user account information + + ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py + + + + Show information about all accounts + + ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py all + + + + Show information about locked accounts + + ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py locked + + + + Show information about non VyOS user accounts + + ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py other + + + + Show information about VyOS user accounts + + ${vyos_libexec_dir}/vyos-sudo.py ${vyos_op_scripts_dir}/show_users.py vyos + + + + + + + + Show system memory usage + + ${vyos_op_scripts_dir}/show_ram.sh + + + + Show kernel cache information + + sudo slabtop -o + + + + Show detailed system memory usage + + cat /proc/meminfo + + + + + + Show system processes + + ps ax + + + + Show extensive process info + + top -b -n1 + + + + Show summary of system processes + + uptime + + + + Show process tree + + ps -ejH + + + + + + Show filesystem usage + + df -h -x squashfs + + + + Show how long the system has been up + + uptime + + + + Show information about Universal Serial Bus (USB) + + /usr/bin/lsusb -t + + + + Show information about connected USB serial ports + + ${vyos_op_scripts_dir}/show_usb_serial.py + + + + + + + + diff --git a/src/op_mode/show_usb_serial.py b/src/op_mode/show_usb_serial.py new file mode 100755 index 000000000..776898c25 --- /dev/null +++ b/src/op_mode/show_usb_serial.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2018 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# 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 . + +import os + +from jinja2 import Template +from pyudev import Context, Devices +from sys import exit + +OUT_TMPL_SRC = """Device Model Vendor +------ ------ ------ +{%- for d in devices %} +{{ "%-16s" | format(d.device) }} {{ "%-19s" | format(d.model)}} {{d.vendor}} +{%- endfor %} + +""" + +data = { + 'devices': [] +} + + +base_directory = '/dev/serial/by-bus' +if not os.path.isdir(base_directory): + print("No USB to serial converter connected") + exit(0) + +context = Context() +for root, dirs, files in os.walk(base_directory): + for basename in files: + os.path.join(root, basename) + device = Devices.from_device_file(context, os.path.join(root, basename)) + tmp = { + 'device': basename, + 'model': device.properties.get('ID_MODEL'), + 'vendor': device.properties.get('ID_VENDOR_FROM_DATABASE') + } + data['devices'].append(tmp) + +data['devices'] = sorted(data['devices'], key = lambda i: i['device']) +tmpl = Template(OUT_TMPL_SRC) +print(tmpl.render(data)) + +exit(0) -- cgit v1.2.3