3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
12 from Exceptions import *
14 import BootServerRequest
21 Write out the network configuration for this machine:
23 /etc/sysconfig/network-scripts/ifcfg-eth0
24 /etc/resolv.conf (if applicable)
25 /etc/sysconfig/network
27 The values to be used for the network settings are to be set in vars
28 in the variable 'NETWORK_SETTINGS', which is a dictionary
31 Key Used by this function
32 -----------------------------------------------
47 Expect the following variables from the store:
48 SYSIMG_PATH the path where the system image will be mounted
49 (always starts with TEMP_PATH)
50 NETWORK_SETTINGS A dictionary of the values from the network
52 NODE_NETWORKS All the network associated with this node
53 Sets the following variables:
57 log.write( "\n\nStep: Install: Writing Network Configuration files.\n" )
60 SYSIMG_PATH= vars["SYSIMG_PATH"]
62 raise ValueError, "SYSIMG_PATH"
65 raise BootManagerException, "Missing variable in vars: %s\n" % var
66 except ValueError, var:
67 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
71 network_settings= vars['NETWORK_SETTINGS']
73 raise BootManagerException, "No network settings found in vars."
76 hostname= network_settings['hostname']
77 domainname= network_settings['domainname']
78 method= network_settings['method']
79 ip= network_settings['ip']
80 gateway= network_settings['gateway']
81 network= network_settings['network']
82 netmask= network_settings['netmask']
83 dns1= network_settings['dns1']
84 mac= network_settings['mac']
86 raise BootManagerException, "Missing value %s in network settings." % str(e)
90 dns2= network_settings['dns2']
95 # Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST
96 log.write("Writing /etc/planetlab/plc_config\n")
97 utils.makedirs("%s/etc/planetlab" % SYSIMG_PATH)
98 plc_config = file("%s/etc/planetlab/plc_config" % SYSIMG_PATH, "w")
100 bs= BootServerRequest.BootServerRequest()
101 if bs.BOOTSERVER_CERTS:
102 print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
104 api_url = vars['BOOT_API_SERVER']
105 (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
106 parts = netloc.split(':')
112 print >> plc_config, "PLC_API_HOST='%s'" % host
113 print >> plc_config, "PLC_API_PORT='%s'" % port
114 print >> plc_config, "PLC_API_PATH='%s'" % path
119 log.write( "Writing /etc/hosts\n" )
120 hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )
121 hosts_file.write( "127.0.0.1 localhost\n" )
122 if method == "static":
123 hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
128 if method == "static":
129 log.write( "Writing /etc/resolv.conf\n" )
130 resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
132 resolv_file.write( "nameserver %s\n" % dns1 )
134 resolv_file.write( "nameserver %s\n" % dns2 )
135 resolv_file.write( "search %s\n" % domainname )
139 log.write( "Writing /etc/sysconfig/network\n" )
140 network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
141 network_file.write( "NETWORKING=yes\n" )
142 network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
143 if method == "static":
144 network_file.write( "GATEWAY=%s\n" % gateway )
150 for network in vars['NODE_NETWORKS']:
151 if method == "static" or method == "dhcp":
152 if network['is_primary'] == 1:
160 int['HWADDR'] = network['mac']
162 if network['method'] == "static":
163 int['BOOTPROTO'] = "static"
164 int['IPADDR'] = network['ip']
165 int['NETMASK'] = network['netmask']
167 elif network['method'] == "dhcp":
168 int['BOOTPROTO'] = "dhcp"
169 if network['hostname']:
170 int['DHCP_HOSTNAME'] = network['hostname']
172 int['DHCP_HOSTNAME'] = hostname
173 if not network['is_primary']:
174 int['DHCLIENTARGS'] = "-R subnet-mask"
178 if len(network['nodenetwork_setting_ids']) > 0:
179 settings = BootAPI.call_api_function(vars, "GetNodeNetworkSettings",
180 ({'nodenetwork_setting_id': network['nodenetwork_setting_ids']},))
181 for setting in settings:
182 # to explicitly set interface name
183 if setting['name'].upper() == "IFNAME":
184 ifname=setting['value']
185 elif setting['name'].upper() == "DRIVER":
186 # xxx not sure how to do that yet - probably add a line in modprobe.conf
188 elif setting['name'].upper() == "ALIAS":
189 alias = ":" + setting['value']
191 # a hack for testing before a new setting is hardcoded here
192 # use the backdoor setting and put as a value 'var=value'
193 elif setting['name'].upper() == "BACKDOOR":
194 [var,value]=setting['value'].split('=',1)
197 elif setting['name'].upper() in \
198 [ "mode", "essid", "nw", "freq", "channel", "sens", "rate",
199 "key", "key1", "key2", "key3", "key4", "securitymode",
200 "iwconfig", "iwpriv" ] :
201 int [setting['name'].upper()] = setting['value']
202 int ['TYPE']=Wireless
204 log.write("Warning - ignored setting named %s"%setting['name'])
206 if alias and 'HWADDR' in int:
207 for (dev, i) in interfaces.iteritems():
208 if i['HWADDR'] == int['HWADDR']:
211 interfaces[dev + alias] = int
215 ifname="eth%d" % ifnum
218 interfaces[ifname] = int
220 for (dev, int) in interfaces.iteritems():
221 path = "%s/etc/sysconfig/network-scripts/ifcfg-%s" % (
224 log.write("Writing %s\n" % path.replace(SYSIMG_PATH, ""))
226 f.write("DEVICE=%s\n" % dev)
227 f.write("ONBOOT=yes\n")
228 f.write("USERCTL=no\n")
229 for (key, val) in int.iteritems():
230 f.write('%s="%s"\n' % (key, val))