From 056b800c8eb42c4112dce54bd1a7336d52ead2ab Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Thu, 20 Aug 2020 17:08:13 +0000 Subject: Starting to add networking helpers in archinstall. First up is archinstall.getHwAddr(ifname) which returns the mac of a interface name. second is archinstall.list_interfaces() which lists all the local MAC addresses and which interface it is bound to. Also starting to add the unattended installer back step by step. Currently with one MAC profile. The MAC profile filtering/detection has also been added in archinstall.list_profiles() - it will filter out all MAC address-specific profiles when called, unless a MAC matches a profile or filter_irrelevant_macs=False is given. --- archinstall/lib/networking.py | 20 ++++++++++++++++++++ archinstall/lib/profiles.py | 16 +++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 archinstall/lib/networking.py (limited to 'archinstall/lib') diff --git a/archinstall/lib/networking.py b/archinstall/lib/networking.py new file mode 100644 index 00000000..ae4126a9 --- /dev/null +++ b/archinstall/lib/networking.py @@ -0,0 +1,20 @@ +import fcntl +import socket +import struct +from collections import OrderedDict + +from .exceptions import * + +def getHwAddr(ifname): + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname, 'utf-8')[:15])) + return ':'.join('%02x' % b for b in info[18:24]) + +def list_interfaces(skip_loopback=True): + interfaces = OrderedDict() + for index, iface in socket.if_nameindex(): + if skip_loopback and iface == 'lo': continue + + mac = getHwAddr(iface).replace(':', '-') + interfaces[mac] = iface + return interfaces diff --git a/archinstall/lib/profiles.py b/archinstall/lib/profiles.py index 47d6eaf8..b63a26c4 100644 --- a/archinstall/lib/profiles.py +++ b/archinstall/lib/profiles.py @@ -1,8 +1,9 @@ -import os, urllib.request, urllib.parse, ssl, json +import os, urllib.request, urllib.parse, ssl, json, re import importlib.util, sys from collections import OrderedDict from .general import multisplit, sys_command, log from .exceptions import * +from .networking import * UPSTREAM_URL = 'https://raw.githubusercontent.com/Torxed/archinstall/master/profiles' @@ -14,19 +15,28 @@ def grab_url_data(path): response = urllib.request.urlopen(safe_path, context=ssl_context) return response.read() -def list_profiles(base='./profiles/'): +def list_profiles(base='./profiles/', filter_irrelevant_macs=True): # TODO: Grab from github page as well, not just local static files + if filter_irrelevant_macs: + local_macs = list_interfaces() + cache = {} for root, folders, files in os.walk(base): for file in files: + tailored = False if os.path.splitext(file)[1] == '.py': + if len(mac := re.findall('(([a-zA-z0-9]{2}[-:]){5}([a-zA-z0-9]{2}))', file)): + if filter_irrelevant_macs and mac[0][0] not in local_macs:: + continue + tailored = True + description = '' with open(os.path.join(root, file), 'r') as fh: first_line = fh.readline() if first_line[0] == '#': description = first_line[1:].strip() - cache[file] = {'path' : os.path.join(root, file), 'description' : description} + cache[file] = {'path' : os.path.join(root, file), 'description' : description, 'tailored' : tailored} break return cache -- cgit v1.2.3-70-g09d2