# -*- 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 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) #set the key and essid cmd = ''' echo "essid %s" > %s/%s/iwconfig ''' %(netinfo["ESSID"], etcnetdir, netdev) mv_common.runcmd(cmd) if netinfo["ENCRYPT"] == "1": logging.info(" Enabling encryption") cmd = '''echo "key %s" >> %s/%s/iwconfig ''' %(netinfo["KEY"], etcnetdir, netdev) mv_common.runcmd(cmd) 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) 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"], 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 find_active(systemconfig): interfacelist=('eth0', 'eth1', 'wlan0', 'wlan1') 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) continue if systemconfig[currentnet] == "1" : setup_interface(netdev,systemconfig) 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 reload" 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') 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) 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")