From 80be18474546dc9f5415bee0545c1be4982cdf72 Mon Sep 17 00:00:00 2001 From: Marcello Rinaldo Martina <39562568+marcellorinaldo@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:42:48 +0200 Subject: [PATCH] feat(distrib): network interface names retrieved without `nmcli` (#4920) * refactor(comment_interfaces_file.py): removed unused imports Signed-off-by: Marcello Martina * feat(distrib): changed way of retrieving network interface names and types Signed-off-by: Marcello Martina * fix(distrib): NetworkManager is no more required to start at installation Signed-off-by: Marcello Martina * enh: removed unuseful comments Signed-off-by: Marcello Martina --------- Signed-off-by: Marcello Martina --- .../common/comment_interfaces_file.py | 2 - .../resources/common/find_net_interfaces.py | 60 +++++++++---------- .../resources/generic-aarch64/kura_install.sh | 1 - .../resources/generic-arm32/kura_install.sh | 1 - .../resources/generic-x86_64/kura_install.sh | 1 - 5 files changed, 27 insertions(+), 38 deletions(-) diff --git a/kura/distrib/src/main/resources/common/comment_interfaces_file.py b/kura/distrib/src/main/resources/common/comment_interfaces_file.py index 2c542cd7773..b55a58cc7a1 100644 --- a/kura/distrib/src/main/resources/common/comment_interfaces_file.py +++ b/kura/distrib/src/main/resources/common/comment_interfaces_file.py @@ -11,8 +11,6 @@ # Contributors: # Eurotech # -import subprocess -import sys import shutil import logging from os.path import exists diff --git a/kura/distrib/src/main/resources/common/find_net_interfaces.py b/kura/distrib/src/main/resources/common/find_net_interfaces.py index fbcc22a5851..d5018fb02b9 100644 --- a/kura/distrib/src/main/resources/common/find_net_interfaces.py +++ b/kura/distrib/src/main/resources/common/find_net_interfaces.py @@ -11,57 +11,51 @@ # Contributors: # Eurotech # -import subprocess import sys import logging +import os -ETHERNET_TYPES = ['ethernet', 'eth', 'wired'] -WIRELESS_TYPES = ['wifi', 'wireless'] +def get_interface_names(): + if not os.path.exists("/sys/class/net"): + logging.error("'/sys/class/net' does not exists, unable to read interface names. Exiting.") + sys.exit(1) + + return os.listdir("/sys/class/net") def get_eth_wlan_interfaces_names(): - """Reads the network interface names using 'nmcli dev' command. + """Reads the network interface names present on the device. It is assumed that at least one ethernet interface is present. + Requirements: + "/sys/class/net" directory must exist + Returns: tuple of lists (eth_names, wlan_names) where: 'eth_names' are the found ethernet interface names sorted by name; 'wlan_names' are the found wireless interface names sorted by name, might be an empty list. """ - cmd_output = subprocess.check_output(['nmcli', 'dev']).decode(sys.stdout.encoding).strip() - # list comprehension to remove empty items - lines = [x.strip() for x in cmd_output.split('\n') if len(x.strip()) > 0] - - # removing header - del lines[0] - - ethernet_inteface_names = list() - wireless_inteface_names = list() + ethernet_interface_names = list() + wireless_interface_names = list() - for line in lines: - row = [x.strip() for x in line.split(' ') if len(x.strip()) > 0] - - interface_name = row[0].lower() - interface_type = row[1].lower() - - if interface_type in ETHERNET_TYPES: - ethernet_inteface_names.append(interface_name) - - if interface_type in WIRELESS_TYPES: - wireless_inteface_names.append(interface_name) + interface_names = get_interface_names() - if len(ethernet_inteface_names) < 1: - logging.info("ERROR: no ethernet interfaces found") - sys.exit(1) + for ifname in interface_names: + if ifname.startswith("en") or ifname.startswith("et"): + ethernet_interface_names.append(ifname) + if ifname.startswith("wl"): + wireless_interface_names.append(ifname) - ethernet_inteface_names.sort() - wireless_inteface_names.sort() + ethernet_interface_names.sort() + wireless_interface_names.sort() - logging.info("Found ethernet interfaces: %s", ethernet_inteface_names) - logging.info("Found wireless interfaces: %s", wireless_inteface_names) - - return (ethernet_inteface_names, wireless_inteface_names) + if len(ethernet_interface_names) < 1: + logging.error("No ethernet interfaces found, exiting.") + sys.exit(1) + logging.info("Found ethernet interfaces: %s", ethernet_interface_names) + logging.info("Found wireless interfaces: %s", wireless_interface_names) + return (ethernet_interface_names, wireless_interface_names) def main(): logging.basicConfig( diff --git a/kura/distrib/src/main/resources/generic-aarch64/kura_install.sh b/kura/distrib/src/main/resources/generic-aarch64/kura_install.sh index f7dd73c8f29..818bd9bd524 100755 --- a/kura/distrib/src/main/resources/generic-aarch64/kura_install.sh +++ b/kura/distrib/src/main/resources/generic-aarch64/kura_install.sh @@ -67,7 +67,6 @@ systemctl disable systemd-timesyncd systemctl stop chrony systemctl disable chrony systemctl enable NetworkManager -systemctl start NetworkManager systemctl enable ModemManager systemctl stop dnsmasq systemctl disable dnsmasq diff --git a/kura/distrib/src/main/resources/generic-arm32/kura_install.sh b/kura/distrib/src/main/resources/generic-arm32/kura_install.sh index f7dd73c8f29..818bd9bd524 100755 --- a/kura/distrib/src/main/resources/generic-arm32/kura_install.sh +++ b/kura/distrib/src/main/resources/generic-arm32/kura_install.sh @@ -67,7 +67,6 @@ systemctl disable systemd-timesyncd systemctl stop chrony systemctl disable chrony systemctl enable NetworkManager -systemctl start NetworkManager systemctl enable ModemManager systemctl stop dnsmasq systemctl disable dnsmasq diff --git a/kura/distrib/src/main/resources/generic-x86_64/kura_install.sh b/kura/distrib/src/main/resources/generic-x86_64/kura_install.sh index f7dd73c8f29..818bd9bd524 100755 --- a/kura/distrib/src/main/resources/generic-x86_64/kura_install.sh +++ b/kura/distrib/src/main/resources/generic-x86_64/kura_install.sh @@ -67,7 +67,6 @@ systemctl disable systemd-timesyncd systemctl stop chrony systemctl disable chrony systemctl enable NetworkManager -systemctl start NetworkManager systemctl enable ModemManager systemctl stop dnsmasq systemctl disable dnsmasq