3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
14 from Exceptions import *
18 # if this file is present in the vservers /etc directory,
19 # the resolv.conf and hosts files will automatically be updated
21 UPDATE_FILE_FLAG= "AUTO_UPDATE_NET_FILES"
23 # the name of the vserver-reference directory
24 VSERVER_REFERENCE_DIR_NAME='vserver-reference'
29 Setup directories for building vserver reference image.
31 Except the following variables from the store:
32 SYSIMG_PATH the path where the system image will be mounted
33 (always starts with TEMP_PATH)
34 NETWORK_SETTINGS A dictionary of the values from the network
37 Sets the following variables:
42 log.write( "\n\nStep: Install: Setting up VServer image.\n" )
44 # make sure we have the variables we need
46 SYSIMG_PATH= vars["SYSIMG_PATH"]
48 raise ValueError, "SYSIMG_PATH"
50 NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
51 if NETWORK_SETTINGS == "":
52 raise ValueError, "NETWORK_SETTINGS"
55 raise BootManagerException, "Missing variable in vars: %s\n" % var
56 except ValueError, var:
57 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
59 vserver_ref_dir= "/vservers/vserver-reference"
60 full_vserver_ref_path= "%s/%s" % (SYSIMG_PATH,vserver_ref_dir)
62 utils.makedirs( full_vserver_ref_path )
63 utils.makedirs( "%s/etc" % full_vserver_ref_path )
65 log.write( "Setting permissions on directories\n" )
66 utils.sysexec( "chmod 0000 %s/vservers/" % SYSIMG_PATH, log )
72 def update_vserver_network_files( vserver_dir, vars, log ):
74 Update the /etc/resolv.conf and /etc/hosts files in the specified
75 vserver directory. If the files do not exist, write them out. If they
76 do exist, rewrite them with new values if the file UPDATE_FILE_FLAG
77 exists it /etc. if this is called with the vserver-reference directory,
78 always update the network config files and create the UPDATE_FILE_FLAG.
80 This is currently called when setting up the initial vserver reference,
81 and later when nodes boot to update existing vserver images.
83 Expect the following variables from the store:
84 SYSIMG_PATH the path where the system image will be mounted
85 (always starts with TEMP_PATH)
86 NETWORK_SETTINGS A dictionary of the values from the network
91 SYSIMG_PATH= vars["SYSIMG_PATH"]
93 raise ValueError, "SYSIMG_PATH"
95 NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
96 if NETWORK_SETTINGS == "":
97 raise ValueError, "NETWORK_SETTINGS"
100 raise BootManagerException, "Missing variable in vars: %s\n" % var
101 except ValueError, var:
102 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
105 ip= NETWORK_SETTINGS['ip']
106 method= NETWORK_SETTINGS['method']
107 hostname= NETWORK_SETTINGS['hostname']
108 domainname= NETWORK_SETTINGS['domainname']
109 except KeyError, var:
110 raise BootManagerException, \
111 "Missing network value %s in var NETWORK_SETTINGS\n" % var
114 os.listdir(vserver_dir)
116 log.write( "Directory %s does not exist to write network conf in.\n" %
120 file_path= "%s/etc/%s" % (vserver_dir,UPDATE_FILE_FLAG)
122 if os.access(file_path,os.F_OK):
126 if vserver_dir.find(VSERVER_REFERENCE_DIR_NAME) != -1:
127 log.write( "Forcing update on vserver-reference directory:\n%s\n" %
129 utils.sysexec_noerr( "echo '%s' > %s/etc/%s" %
130 (UPDATE_FILE_FLAG,vserver_dir,UPDATE_FILE_FLAG),
136 log.write( "Updating network files in %s.\n" % vserver_dir )
138 file_path= "%s/etc/hosts" % vserver_dir
139 hosts_file= file(file_path, "w" )
140 hosts_file.write( "127.0.0.1 localhost\n" )
141 if method == "static":
142 hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
147 file_path= "%s/etc/resolv.conf" % vserver_dir
149 # copy the resolv.conf from the boot cd env.
150 utils.sysexec( "cp /etc/resolv.conf %s/etc" % vserver_dir, log )
152 # copy the generated resolv.conf from the system image, since
153 # we generated it via static settings
154 utils.sysexec( "cp %s/etc/resolv.conf %s/etc" % \
155 (SYSIMG_PATH,vserver_dir), log )