3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
12 from Exceptions import *
16 # if this file is present in the vservers /etc directory,
17 # the resolv.conf and hosts files will automatically be updated
19 UPDATE_FILE_FLAG= "AUTO_UPDATE_NET_FILES"
24 Reconfigure a node if necessary, including rewriting any network init
25 scripts based on what PLC has. Also, update any slivers on the machine
26 incase their network files are out of date (primarily /etc/hosts).
28 Also write out /etc/planetlab/session, a random string that gets
29 a new value at every request of BootGetNodeDetails (ie, every boot)
31 This step expects the root to be already mounted on SYSIMG_PATH.
33 Except the following keys to be set:
34 SYSIMG_PATH the path where the system image will be mounted
35 (always starts with TEMP_PATH)
36 ROOT_MOUNTED the node root file system is mounted
37 INTERFACE_SETTINGS A dictionary of the values from the network
41 log.write( "\n\nStep: Updating node configuration.\n" )
43 # make sure we have the variables we need
45 INTERFACE_SETTINGS= vars["INTERFACE_SETTINGS"]
46 if INTERFACE_SETTINGS == "":
47 raise ValueError, "INTERFACE_SETTINGS"
49 SYSIMG_PATH= vars["SYSIMG_PATH"]
51 raise ValueError, "SYSIMG_PATH"
53 ROOT_MOUNTED= vars["ROOT_MOUNTED"]
54 if ROOT_MOUNTED == "":
55 raise ValueError, "ROOT_MOUNTED"
58 raise BootManagerException, "Missing variable in vars: %s\n" % var
59 except ValueError, var:
60 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
63 ip= INTERFACE_SETTINGS['ip']
64 method= INTERFACE_SETTINGS['method']
65 hostname= INTERFACE_SETTINGS['hostname']
66 domainname= INTERFACE_SETTINGS['domainname']
68 raise BootManagerException, \
69 "Missing network value %s in var INTERFACE_SETTINGS\n" % var
73 raise BootManagerException, "Root isn't mounted on SYSIMG_PATH\n"
75 log.write( "Updating vserver's /etc/hosts and /etc/resolv.conf files\n" )
77 # create a list of the full directory paths of all the vserver images that
81 for base_dir in ('/vservers','/vservers/.vref','/vservers/.vcache'):
83 full_dir_path= "%s/%s" % (SYSIMG_PATH,base_dir)
84 slices= os.listdir( full_dir_path )
87 slices.remove("lost+found")
91 update_path_list= update_path_list + map(lambda x: \
98 log.write( "Updating network configuration in:\n" )
99 if len(update_path_list) == 0:
100 log.write( "No vserver images found to update.\n" )
102 for base_dir in update_path_list:
103 log.write( "%s\n" % base_dir )
106 # now, update /etc/hosts and /etc/resolv.conf in each dir if
107 # the update flag is there
108 for base_dir in update_path_list:
109 update_vserver_network_files(base_dir,vars,log)
115 def update_vserver_network_files( vserver_dir, vars, log ):
117 Update the /etc/resolv.conf and /etc/hosts files in the specified
118 vserver directory. If the files do not exist, write them out. If they
119 do exist, rewrite them with new values if the file UPDATE_FILE_FLAG
120 exists it /etc. if this is called with the vserver-reference directory,
121 always update the network config files and create the UPDATE_FILE_FLAG.
123 This is currently called when setting up the initial vserver reference,
124 and later when nodes boot to update existing vserver images.
126 Expect the following variables from the store:
127 SYSIMG_PATH the path where the system image will be mounted
128 (always starts with TEMP_PATH)
129 INTERFACE_SETTINGS A dictionary of the values from the network
134 SYSIMG_PATH= vars["SYSIMG_PATH"]
135 if SYSIMG_PATH == "":
136 raise ValueError, "SYSIMG_PATH"
138 INTERFACE_SETTINGS= vars["INTERFACE_SETTINGS"]
139 if INTERFACE_SETTINGS == "":
140 raise ValueError, "INTERFACE_SETTINGS"
142 except KeyError, var:
143 raise BootManagerException, "Missing variable in vars: %s\n" % var
144 except ValueError, var:
145 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
148 ip= INTERFACE_SETTINGS['ip']
149 method= INTERFACE_SETTINGS['method']
150 hostname= INTERFACE_SETTINGS['hostname']
151 domainname= INTERFACE_SETTINGS['domainname']
152 except KeyError, var:
153 raise BootManagerException, \
154 "Missing network value %s in var INTERFACE_SETTINGS\n" % var
157 os.listdir(vserver_dir)
159 log.write( "Directory %s does not exist to write network conf in.\n" %
163 file_path= "%s/etc/%s" % (vserver_dir,UPDATE_FILE_FLAG)
165 if os.access(file_path,os.F_OK):
169 # Thierry - 2012/03 - I'm renaming vserver-reference into sliceimage
170 # however I can't quite grasp the reason for this test below, very likely
171 # compatibility with very old node images or something
172 if '/.vref/' in vserver_dir or \
173 '/.vcache/' in vserver_dir or \
174 '/vserver-reference' in vserver_dir:
175 log.write( "Forcing update on vserver reference directory:\n%s\n" %
177 utils.sysexec_noerr( "echo '%s' > %s/etc/%s" %
178 (UPDATE_FILE_FLAG,vserver_dir,UPDATE_FILE_FLAG),
184 log.write( "Updating network files in %s.\n" % vserver_dir )
186 # NOTE: this works around a recurring problem on public pl,
187 # suspected to be due to mismatch between 2.6.12 bootcd and
188 # 2.6.22/f8 root environment. files randomly show up with the
189 # immutible attribute set. this clears it before trying to write
191 utils.sysexec( "chattr -i %s/etc/hosts" % vserver_dir , log )
192 utils.sysexec( "chattr -i %s/etc/resolv.conf" % vserver_dir , log )
197 file_path= "%s/etc/hosts" % vserver_dir
198 hosts_file= file(file_path, "w" )
199 hosts_file.write( "127.0.0.1 localhost\n" )
200 if method == "static":
201 hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
205 file_path= "%s/etc/resolv.conf" % vserver_dir
207 # copy the resolv.conf from the boot cd env.
208 utils.sysexec( "cp /etc/resolv.conf %s/etc" % vserver_dir, log )
210 # copy the generated resolv.conf from the system image, since
211 # we generated it via static settings
212 utils.sysexec( "cp %s/etc/resolv.conf %s/etc" % \
213 (SYSIMG_PATH,vserver_dir), log )