# -*- coding: utf-8 -*- import sys , os, commands , glob, time, re import logging import mv_common global etcnetdir etcnetdir = "/etc/net/ifaces" def setup_MYTH_IP(systemconfig): default_interface = systemconfig["default_interface"] try: defaultip = systemconfig["Hostip"+default_interface] except: logging.debug(" Error occured finding the defaultip") defaultip = "127.0.0.1" logging.debug(" Using %s as default ip", defaultip) return defaultip def setup_MYTH_DHCP(systemconfig): default_interface = systemconfig["default_interface"] try: defaultdhcp = systemconfig["HostUSEDHCP"+default_interface] except: logging.critical(" *Error occured finding default dhcp") defaultdhcp = "0" logging.debug(" Using %s as dhcp value for %s", default_interface, defaultdhcp) return defaultdhcp def flush(netdev): logging.debug(" Flushing %s",netdev) cmd = '''ip address flush dev %s''' %netdev mv_common.runcmd(cmd) def kill_dhcp(basedir): logging.debug(" _Start of kill_dhcp") logging.debug(" Killing off dhcpd") stddir = os.getcwd() piddir = ("%s/var/run/") %basedir try: os.chdir(piddir) for FILE in glob.glob("dhcpcd-*.pid"): f = open(FILE,'r') pid = f.readline() f.close cmd = "kill -9 %s" %pid mv_common.runcmd(cmd) os.remove(FILE) os.chdir(stddir) except: pass def setup_nameserver(dns): logging.info(" Adding %s for DNS", dns) cmd = '''grep -q %s /etc/resolv.conf''' %dns status = mv_common.runcmd(cmd) if not status == 0 : cmd = ''' echo "nameserver %s" >> /etc/resolv.conf ''' %dns mv_common.runcmd(cmd) def setup_hostname(systemconfig): logging.debug(" _Start of setup_hostname") try: hostname = systemconfig["hostname"] except: logging.critical(" *Hostname could not be set") logging.info(" Using default value of me") hostname = "me" logging.info(" Setting the hostname to %s", hostname) cmd = "cat /etc/hosts | grep -v 127.0.0.1 | grep -v %s > /tmp/hosts" %hostname mv_common.runcmd(cmd) if systemconfig["mythdhcp"] == "1" : logging.debug(" not using dhcp") cmd = ''' echo 127.0.0.1 localhost > /etc/hosts ''' mv_common.runcmd(cmd) cmd = ''' echo %s %s > /etc/hosts ''' %(systemconfig["mythip"], systemconfig["hostname"]) mv_common.runcmd(cmd) else: cmd = ''' echo 127.0.0.1 %s localhost > /etc/hosts ''' %systemconfig["hostname"] mv_common.runcmd(cmd) cmd = '''cat /tmp/hosts >> /etc/hosts ''' mv_common.runcmd(cmd) def write_wpafile(line,netdev): logging.debug(" writing out wpa_supplicant.conf file") logging.debug(line) wpafile = etcnetdir+"/"+netdev+"/wpa_supplicant.conf" try: f = open(wpafile,'w') f.write(line) f.close() except: logging.debug(" Couldn't write to %s",wpafile) def setup_wpa(netinfo,netdev): logging.info(" %s will use wpa for encryption",netdev) line = ''' #This file was automaticly generated, any changes may be lost ctrl_interface=/var/run/wpa_supplicant network={ ssid="%s" # Preshared key as an ASCII passphrase psk="%s" scan_ssid=1 key_mgmt=WPA-EAP WPA-PSK NONE pairwise=CCMP TKIP proto=WPA RSN }''' %(netinfo["ESSID"],netinfo["KEY"]) write_wpafile(line,netdev) def setup_wep(netinfo,netdev): logging.info(" %s will use wep for encryption",netdev) logging.debug(" When a packet comes along, you must wep it") line=''' #This file was automaticly generated, any changes may be lost ctrl_interface=/var/run/wpa_supplicant network={ ssid="%s" key_mgmt=NONE wep_key0=%s wep_tx_keyidx=0 scan_ssid=1 auth_alg=SHARED } ''' %(netinfo["ESSID"],netinfo["KEY"]) write_wpafile(line,netdev) def setup_none(netinfo,netdev): logging.info(" %s will not use encryption",netdev) line = ''' #This file was automaticly generated, any changes may be lost ctrl_interface=/var/run/wpa_supplicant network={ ssid="%s" key_mgmt=NONE } ''' %netinfo["ESSID"] write_wpafile(line,netdev) def setup_interface(netdev,systemconfig): logging.debug(" _Start of setup_interface for %s",netdev) nettrans = {'Hostip':'ip', 'Hostnetmask':'netmask', 'HostActive':'isactive', 'HostDNS':'dns', 'HostUSEDHCP':'UseDHCP', 'HostGW':'GW', 'HostMTU':'mtu', 'HOST_iswireless':"wireless", 'HostESSID':'ESSID', 'HostKey':'KEY', 'HostUseEncryption':'ENCRYPT'} netinfo = {} #populate the netinfo dict for netitem in nettrans: if systemconfig[netitem+netdev]: netinfo[nettrans[netitem]] = systemconfig[netitem+netdev] else: netinfo[nettrans[netitem]] = False logging.debug(" %s:%s",netitem,netinfo[nettrans[netitem]]) logging.info(" %s wireless: %s", netdev,netinfo["wireless"]) try: os.makedirs(etcnetdir) except: logging.debug(" Could not create %s",etcnetdir) optionfile=etcnetdir+"/"+netdev+"/options" if not os.path.exists(optionfile): os.makedirs(etcnetdir+"/"+netdev) mv_common.cp_and_log(systemconfig["TEMPLATES"]+"/etcnet/eth/options", optionfile) if netinfo["isactive"] == "1" : change_iface_state(netdev,"enabled") else: change_iface_state(netdev,"disabled") if netinfo["wireless"] == "1" : logging.info(" Enabling wireless extensions") cmd = ''' sed -i -e 's/^CONFIG_WIRELESS=.*$/CONFIG_WIRLESS=yes/g' %s/%s/options''' %(etcnetdir,netdev) mv_common.runcmd(cmd) if netinfo["ENCRYPT"] == "WEP": setup_wep(netinfo,netdev) elif netinfo["ENCRYPT"] == "WPA": setup_wpa(netinfo,netdev) elif netinfo["ENCRYPT"] == "none": setup_none(netinfo,netdev) else: logging.info(" Disabling wireless extensions") cmd = '''sed -i -e 's/^CONFIG_WIRLESS=.*$/CONFIG_WIRLESS=no/g' %s/%s/options''' %(etcnetdir, netdev) mv_common.runcmd(cmd) try: wpafile=etcnetdir+"/"+netdev+"/wpa_supplicant.conf" os.remove(wpafile) except: logging.debug(" Couldn't remove %s",wpafile) if netinfo["UseDHCP"] == "0" : logging.info(" Enabling DHCP support") cmd = ''' sed -i -e 's/^BOOTPROTO=.*$/BOOTPROTO=dhcp/g' %s/%s/options''' %(etcnetdir, netdev) mv_common.runcmd(cmd) else: logging.info(" Using static ip address of %s",netinfo["ip"]) cmd = ''' sed -i -e 's/^BOOTPROTO=.*$/BOOTPROTO=static/g' %s/%s/options''' %(etcnetdir, netdev) mv_common.runcmd(cmd) cmd = '''echo "default via %s" > %s/%s/ipv4route''' %(netinfo["GW"], etcnetdir, netdev) mv_common.runcmd(cmd) cmd = '''echo "%s%s" > %s/%s/ipv4address ''' %(netinfo["ip"], netinfo["netmask"].split()[0], etcnetdir, netdev) mv_common.runcmd(cmd) setup_nameserver(netinfo["dns"]) if netinfo["mtu"] : logging.info(" Setting mtu to %s", netinfo["mtu"]) cmd = '''sed -i '/^mtu.*$/d' %s/%s/iplink''' %(etcnetdir,netdev) mv_common.runcmd(cmd) cmd = ''' echo "mtu %s" >> %s/%s/iplink''' %(netinfo["mtu"], etcnetdir,netdev) mv_common.runcmd(cmd) else: logging.info(" Using default mtu value") cmd = '''sed -i '/^mtu.*$/d' %s/%s/iplink''' %(etcnetdir,netdev) mv_common.runcmd(cmd) def change_iface_state(netdev, state): if state == "enabled": logging.info(" Activating %s", netdev) cmd = '''sed -i -e 's/^ONBOOT=.*$/ONBOOT=yes/g' %s/%s/options''' %(etcnetdir,netdev) mv_common.runcmd(cmd) cmd = '''sed -i -e 's/^DISABLED=.*$/DISABLED=no/g' %s/%s/options ''' %(etcnetdir,netdev) mv_common.runcmd(cmd) else: logging.info(" Disabling %s", netdev) cmd = '''sed -i -e 's/^ONBOOT=.*$/ONBOOT=no/g' %s/%s/options''' %(etcnetdir,netdev) mv_common.runcmd(cmd) cmd = '''sed -i -e 's/^DISABLED=.*$/DISABLED=yes/g' %s/%s/options ''' %(etcnetdir,netdev) mv_common.runcmd(cmd) cmd = "/sbin/ifconfig %s down" %netdev mv_common.runcmd(cmd) cmd = "ip address flush %s" %netdev mv_common.runcmd(cmd) def devcheck(netdev): cmd = ''' /sbin/ifconfig %s''' %netdev rc = mv_common.runcmd(cmd) if rc == 0 : return True else: return False def find_active(systemconfig): interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1', 'ath0') logging.debug(" _Start of find_active") if systemconfig["mythdhcp"] == "1": try: os.remove("/etc/resolv.conf") except: logging.debug(" couldn't remove /etc/resolv.conf") cmd = ''' echo search lan > /etc/resolv.conf ''' mv_common.runcmd(cmd) cmd = ''' echo nameserver 127.0.0.1 >> /etc/resolv.conf ''' mv_common.runcmd(cmd) for netdev in interfacelist: currentnet = "HostActive" + netdev try: systemconfig[currentnet] except: logging.debug(" %s is not defined",currentnet) change_iface_state(netdev, "disabled") continue if systemconfig[currentnet] == "1" : #check if device is present if devcheck(netdev) : setup_interface(netdev,systemconfig) else: logging.debug(" Interface %s not found in config", netdev) #change_iface_state(netdev, "disabled") else: change_iface_state(netdev, "disabled") def vnc_netboot_check(): logging.debug(" Start of VNC/NETBOOT check") vnc = False netboot = False try: f = open('/proc/cmdline', 'r') bootoptions = f.readlines() f.close() except: logging.critical(" *Couldn't open /proc/cmdline") logging.debug(" Assuming it's ok to fiddle with the network") return True bootoptions = bootoptions[0] if re.match("vnc",bootoptions) != None : logging.debug(" Found VNC option") vnc = True if re.match("nfsroot",bootoptions) != None : logging.debug(" Found netboot option") netboot = True return (vnc or netboot) def start_network(): if not vnc_netboot_check(): logging.info(" Restarting network") cmd ="/etc/net/scripts/network.init reload" mv_common.runcmd(cmd) cmd ="/etc/net/scripts/network.init restart" mv_common.runcmd(cmd) else: logging.info(" Will not restart network due to netboot/vnc") def stop_network(): if not vnc_netboot_check(): logging.info(" Stopping network") cmd ="/etc/net/scripts/network.init stop" mv_common.runcmd(cmd) kill_dhcp("") interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1', 'ath0') for i in interfacelist: flush(i) else: logging.info(" Will not stop network due to netboot/vnc") pass def setup_network (systemconfig): logging.info("____Start of network____") logging.info(" Setting up the network") setup_hostname(systemconfig) find_active(systemconfig) start_network() logging.info("__End of network\n") ##this is used by the install process to start the network def install_network_setup(systemconfig): logging.info("____Start of network_install____") logging.info(" Setting up the network") #setup_MYTH_vars setup_hostname(systemconfig) stop_network() find_active(systemconfig) start_network() logging.info("__End of network install \n")