change if names from enp*s0 to eth*

including all cfg files!
This commit is contained in:
2024-03-19 15:56:53 +01:00
parent ad130f16f4
commit 9c9719bff4
14 changed files with 88 additions and 69 deletions

View File

@@ -20,7 +20,7 @@ from glob import glob
from ipaddress import IPv4Interface
from configparser import ConfigParser
from os.path import join, getmtime, exists, basename
from utils import get_config, ifname_mapping
from utils import get_config
if os.geteuid() != 0:
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
@@ -43,10 +43,10 @@ COMMENT = "; please refer to README.md for help"
CONFIG_TEMPLATE = f"""[NETWORK]
{COMMENT}
enp1s0=192.168.127.254
enp2s0=192.168.2.2
enp3s0=192.168.3.3
enp4s0=dhcp
eth0=192.168.127.254
eth1=192.168.2.2
eth2=192.168.3.3
eth3=dhcp
[ROUTER]
{COMMENT}
@@ -55,10 +55,10 @@ enp4s0=dhcp
CONTROLBOX_TEMPLATE = f"""[NETWORK]
{COMMENT}
enp1s0=dhcp
enp2s0=192.168.1.1
enp3s0=192.168.2.2
enp4s0=192.168.3.3
eth0=dhcp
eth1=192.168.1.1
eth2=192.168.2.2
eth3=192.168.3.3
[DISPLAY]
{COMMENT}
@@ -123,18 +123,23 @@ main_info = { # info to be determined depending on cfg
'hostname': socket.gethostname() # effective or given host name
}
change_if_names = False
for netif in os.scandir('/sys/class/net'):
if netif.name != 'lo': # do not consider loopback interface
with open(os.path.join(netif.path, 'address')) as f:
addr = f.read().strip().lower()
net_addr[netif.name] = addr
n = netif.name
if n.startswith('enp'):
change_if_names = True
n = f'eth{int(n[3]) - 1}'
net_addr[n] = addr
sorted_if = sorted(net_addr)
boxaddr = net_addr[sorted_if[0]]
boxid = int(''.join(boxaddr.split(':')[-3:]), 16) & 0xffffff
boxtype = BOX_TYPES.get(boxaddr[:8])
if boxtype != 'apu':
ifname_mapping.clear()
TO_SYSTEM = f'{TOOLS}/to_{boxtype}'
if not exists(TO_SYSTEM):
TO_SYSTEM = f'{TOOLS}/to_system'
@@ -295,7 +300,6 @@ def create_if(name, cfg):
else:
cfgip = IPv4Interface(cfg)
network = cfgip.network
print('N', network, network.prefixlen)
if network.prefixlen == 32: # or no prefix specified
otherip = IPv4Interface('%s/24' % cfgip.ip)
network = otherip.network
@@ -410,7 +414,7 @@ def handle_config():
cfgfiles.append(file)
if boxtype == 'apu':
for i in [1, 2, 3]:
bad = glob(CFGPATH % ('*', apuid+i))
bad = glob(CFGPATH % ('*', boxid+i))
if bad:
print('cfg files found with bad apu id (use net addr of leftmost plug)')
print(bad)
@@ -460,6 +464,10 @@ def handle_config():
config = get_config()
try:
netcfg = config['NETWORK']
for name in netcfg:
if name not in net_addr:
print(f'{name} is not a valid network interface name')
raise RuntimeError('network interface name system does not match')
for ifname in net_addr:
content = create_if(ifname, netcfg.get(ifname, 'off'))
# content = '\n'.join('%s=%s' % kv for kv in content.items())
@@ -469,7 +477,6 @@ def handle_config():
show.dirty = True
to_start[ifname] = 'if_restart'
if dhcp_server_cfg:
print(dhcp_server_cfg)
content = [DHCP_HEADER]
for subnet, rangelist in dhcp_server_cfg:
try:
@@ -565,6 +572,18 @@ def handle_config():
if action == 'restart':
unix_cmd('systemctl restart %s' % service)
unix_cmd('systemctl enable %s' % service)
if change_if_names:
with open('/etc/default/grub') as f:
content = f.read()
prev = 'GRUB_CMDLINE_LINUX_DEFAULT="quiet"'
repl = 'GRUB_CMDLINE_LINUX_DEFAULT="quiet net.ifnames=0"'
newcontent = content.replace(prev, repl)
if repl not in newcontent:
write_when_new('/etc/default/grub', newcontent)
unix_cmd('update-grub')
result = [f'config file:\n {cfgfile}']
for section, section_dict in config.items():
result.append(section)