From 2fadf63da8dd477ba2856f543e9c62e78b3d9f11 Mon Sep 17 00:00:00 2001 From: LIN SE Date: Thu, 8 Apr 2021 07:02:57 +0200 Subject: [PATCH] ifcfg scripts are now created from scratch - ifcfg script do not contain UUID any more --- install.py | 107 +++++++----------- servercfg/{apuslave2.cfg => apuslave3.cfg} | 2 +- servercfg/dilsc.cfg | 6 +- to_system/etc/profile.d/welcome.sh | 2 +- .../sysconfig/network-scripts/ifcfg-enp1s0 | 8 ++ .../sysconfig/network-scripts/ifcfg-enp2s0 | 8 ++ .../sysconfig/network-scripts/ifcfg-enp3s0 | 8 ++ .../sysconfig/network-scripts/ifcfg-enp4s0 | 5 + 8 files changed, 78 insertions(+), 68 deletions(-) rename servercfg/{apuslave2.cfg => apuslave3.cfg} (86%) create mode 100644 to_system/etc/sysconfig/network-scripts/ifcfg-enp1s0 create mode 100644 to_system/etc/sysconfig/network-scripts/ifcfg-enp2s0 create mode 100644 to_system/etc/sysconfig/network-scripts/ifcfg-enp3s0 create mode 100644 to_system/etc/sysconfig/network-scripts/ifcfg-enp4s0 diff --git a/install.py b/install.py index c53f92a..a9745f1 100755 --- a/install.py +++ b/install.py @@ -17,6 +17,28 @@ os.chdir('to_system') DEL = '__to_delete__' CFGPATH = '/root/aputools/servercfg/%s.cfg' +def create_if(name, mac, cfg): + result=dict(TYPE='Ethernet', NAME=name, DEVICE=name, + BOOTPROTO='none', ONBOOT='yes') + if cfg == 'off': + result['ONBOOT']='no' + elif cfg == 'dhcp': + result['BOOTPROTO']='dhcp' + else: + interface = IPv4Interface(cfg) + if '/' not in cfg: + if interface < IPv4Interface('128.0.0.0'): + cfg += '/8' + elif interface < IPv4Interface('192.0.0.0'): + cfg += '/16' + else: + cfg += '/24' + interface = IPv4Interface(cfg) + result['IPADDR'], result['NETMASK'] = interface.with_netmask.split('/') + _, result['PREFIX'] = interface.with_prefixlen.split('/') + return result + + def walk(action): for dirpath, _, files in os.walk('.'): syspath = dirpath[1:] # remove leading '.' @@ -83,11 +105,15 @@ class Do: os.remove(join(self.syspath, file)) +IFNAMES = ['enp%ds0' % i for i in range(1,5)] + def network(doit): - result = None - dirty = None - with open('/sys/class/net/enp1s0/address') as f: - netaddr = f.read().strip().lower() + result = True + netaddr_dict = {} + for ifname in IFNAMES: + with open('/sys/class/net/%s/address' % ifname) as f: + netaddr_dict[ifname] = f.read().strip().lower() + netaddr = netaddr_dict[IFNAMES[0]] action = 'doit' if doit else 'check' addrdict = {} for cfgfile in glob(CFGPATH % '*'): @@ -99,15 +125,14 @@ def network(doit): print(cfgfile) print(addrdict[address]) print('ERROR: duplicate address %s in above files' % address) - result = 'failed' addrdict[address] = cfgfile except Exception as e: print('ERROR: can not read %s: %r' % (cfgfile, e)) - result = 'failed' + result = False cfgfile = addrdict.get(netaddr) if cfgfile is None: print('can not find cfg file for %s' % netaddr) - result = 'failed' + result = False for ntry in range(1 + bool(doit)): with open('/etc/hostname') as f: hostname = f.read().strip() @@ -118,12 +143,11 @@ def network(doit): os.system('sh sethostname.sh') else: print('ERROR: can not set host name') - result = 'failed' + result = False else: print('host name does not match') - dirty = 'dirty' break - if result: + if not result: return False ifname = '' parser = ConfigParser() @@ -132,67 +156,24 @@ def network(doit): network = dict(parser['NETWORK']) address = network.pop('address', None) main = None - for ifname, value in network.items(): - try: - with open('/etc/sysconfig/network-scripts/ifcfg-%s' % ifname) as f: - original = f.read().strip().split('\n') - except FileNotFoundError: - raise ValueError('unknown interface %s' % ifname) - content = {} - for line in original: - key, _, val = line.strip().partition('=') - if val: - content[key] = val - if content['NAME'] != ifname: - print('ERROR: name %s does not match' % content['NAME']) - result = 'failed' - original = dict(content) - if value == 'dhcp': - if main: - print('ERROR: only one interface with DHCP allowed') - result = 'failed' - main = ifname - content.pop('IPADDR', None) - content['ONBOOT'] = 'yes' - content['BOOTPROTO'] = 'dhcp' - else: - interface = IPv4Interface(value) - if '/' not in value: - if interface < IPv4Interface('128.0.0.0'): - value += '/8' - elif interface < IPv4Interface('192.0.0.0'): - value += '/16' - else: - value += '/24' - interface = IPv4Interface(value) - content['IPADDR'], content['NETMASK'] = interface.with_netmask.split('/') - _, content['PREFIX'] = interface.with_prefixlen.split('/') - content['ONBOOT'] = 'yes' - content['BOOTPROTO'] = 'none' - if content != original: - if doit: - with open('/etc/sysconfig/network-scripts/ifcfg-%s' % ifname, 'w') as f: - f.write('%s\n' % '\n'.join('%s=%s' % kv for kv in content.items())) - else: - dirty = 'dirty' - print('%s changed:' % ifname) - diff = dict(content) - diff.update({k: '' for k in original if k not in diff}) - print(' %s' % ', '.join('%s=%s' % (k, v) for k, v in diff.items() if v != original.get(k))) + for ifname in IFNAMES: + content = create_if(ifname, netaddr_dict[ifname], + network.get(ifname, 'off')) + with open('etc/sysconfig/network-scripts/ifcfg-%s' % ifname, 'w') as f: + f.write('\n'.join('%s=%s' % kv for kv in content.items())+ '\n') except Exception as e: print('ERROR: can not handle %s %s: %r' % (hostname, ifname, e)) - result = 'failed' - return result or dirty - + result = False + return result print('---') show = Show() -walk(show) result = network(False) +walk(show) -if result == 'failed': +if not result: print('fix first above errors') -elif result == 'dirty' or show.dirty: +elif show.dirty: print('---') answer = input('do above? ') if answer.lower().startswith('y'): diff --git a/servercfg/apuslave2.cfg b/servercfg/apuslave3.cfg similarity index 86% rename from servercfg/apuslave2.cfg rename to servercfg/apuslave3.cfg index 8ae37bb..3739da9 100644 --- a/servercfg/apuslave2.cfg +++ b/servercfg/apuslave3.cfg @@ -1,5 +1,5 @@ [NETWORK] -address=00:0d:b9:5a:58:e4 +address=00:0d:b9:5a:4c:90 enp1s0=dhcp enp2s0=192.168.2.2/24 enp3s0=192.168.2.3/24 diff --git a/servercfg/dilsc.cfg b/servercfg/dilsc.cfg index 3739da9..0f36cd5 100644 --- a/servercfg/dilsc.cfg +++ b/servercfg/dilsc.cfg @@ -1,9 +1,9 @@ [NETWORK] -address=00:0d:b9:5a:4c:90 -enp1s0=dhcp +address=00:0d:b9:5a:58:e4 +enp1s0=192.168.2.1/24 enp2s0=192.168.2.2/24 enp3s0=192.168.2.3/24 -enp4s0=192.168.127.4/24 +enp4s0=dhcp [ROUTER] 3000=/dev/ttyUSB0 diff --git a/to_system/etc/profile.d/welcome.sh b/to_system/etc/profile.d/welcome.sh index a6dd1c0..7ea343f 100755 --- a/to_system/etc/profile.d/welcome.sh +++ b/to_system/etc/profile.d/welcome.sh @@ -1,3 +1,3 @@ echo "Welcome to $HOSTNAME $(hostname -I)" -echo "$(cat /sys/class/net/enp1s0/address)" +echo "$(cat /sys/class/net/enp4s0/address)" export EDITOR=nano diff --git a/to_system/etc/sysconfig/network-scripts/ifcfg-enp1s0 b/to_system/etc/sysconfig/network-scripts/ifcfg-enp1s0 new file mode 100644 index 0000000..7a3aa5c --- /dev/null +++ b/to_system/etc/sysconfig/network-scripts/ifcfg-enp1s0 @@ -0,0 +1,8 @@ +TYPE=Ethernet +NAME=enp1s0 +DEVICE=enp1s0 +BOOTPROTO=none +ONBOOT=yes +IPADDR=192.168.2.1 +NETMASK=255.255.255.0 +PREFIX=24 diff --git a/to_system/etc/sysconfig/network-scripts/ifcfg-enp2s0 b/to_system/etc/sysconfig/network-scripts/ifcfg-enp2s0 new file mode 100644 index 0000000..749bd14 --- /dev/null +++ b/to_system/etc/sysconfig/network-scripts/ifcfg-enp2s0 @@ -0,0 +1,8 @@ +TYPE=Ethernet +NAME=enp2s0 +DEVICE=enp2s0 +BOOTPROTO=none +ONBOOT=yes +IPADDR=192.168.2.2 +NETMASK=255.255.255.0 +PREFIX=24 diff --git a/to_system/etc/sysconfig/network-scripts/ifcfg-enp3s0 b/to_system/etc/sysconfig/network-scripts/ifcfg-enp3s0 new file mode 100644 index 0000000..edde62a --- /dev/null +++ b/to_system/etc/sysconfig/network-scripts/ifcfg-enp3s0 @@ -0,0 +1,8 @@ +TYPE=Ethernet +NAME=enp3s0 +DEVICE=enp3s0 +BOOTPROTO=none +ONBOOT=yes +IPADDR=192.168.2.3 +NETMASK=255.255.255.0 +PREFIX=24 diff --git a/to_system/etc/sysconfig/network-scripts/ifcfg-enp4s0 b/to_system/etc/sysconfig/network-scripts/ifcfg-enp4s0 new file mode 100644 index 0000000..33eeb64 --- /dev/null +++ b/to_system/etc/sysconfig/network-scripts/ifcfg-enp4s0 @@ -0,0 +1,5 @@ +TYPE=Ethernet +NAME=enp4s0 +DEVICE=enp4s0 +BOOTPROTO=dhcp +ONBOOT=yes