1 # Copyright (c) 2003 Intel Corporation
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials provided
14 # with the distribution.
16 # * Neither the name of the Intel Corporation nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
33 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
34 # export regulations applicable in licensee's jurisdiction. Under
35 # CURRENT (May 2000) U.S. export regulations this software is eligible
36 # for export from the U.S. and can be downloaded by or otherwise
37 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
38 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
39 # Afghanistan and any other country to which the U.S. has embargoed
45 from Exceptions import *
47 from systeminfo import systeminfo
54 Writes out the following configuration files for the node:
57 /etc/sysconfig/network-scripts/ifcfg-eth0
58 /etc/resolv.conf (if applicable)
59 /etc/sysconfig/network
62 /etc/ssh/ssh_host_rsa_key
63 /etc/ssh/ssh_host_dsa_key
65 Expect the following variables from the store:
66 VERSION the version of the install
67 SYSIMG_PATH the path where the system image will be mounted
68 (always starts with TEMP_PATH)
69 PARTITIONS dictionary of generic part. types (root/swap)
70 and their associated devices.
71 PLCONF_DIR The directory to store the configuration file in
72 NETWORK_SETTINGS A dictionary of the values from the network
74 BOOT_CD_VERSION A tuple of the current bootcd version
76 Sets the following variables:
81 log.write( "\n\nStep: Install: Writing configuration files.\n" )
83 # make sure we have the variables we need
85 VERSION= vars["VERSION"]
87 raise ValueError, "VERSION"
89 SYSIMG_PATH= vars["SYSIMG_PATH"]
91 raise ValueError, "SYSIMG_PATH"
93 PARTITIONS= vars["PARTITIONS"]
94 if PARTITIONS == None:
95 raise ValueError, "PARTITIONS"
97 PLCONF_DIR= vars["PLCONF_DIR"]
99 raise ValueError, "PLCONF_DIR"
101 NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
102 if NETWORK_SETTINGS == "":
103 raise ValueError, "NETWORK_SETTINGS"
105 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
106 if BOOT_CD_VERSION == "":
107 raise ValueError, "BOOT_CD_VERSION"
109 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
111 except KeyError, var:
112 raise BootManagerException, "Missing variable in vars: %s\n" % var
113 except ValueError, var:
114 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
117 # we need to keys in PARTITIONS, root and swap, make sure
119 val= PARTITIONS["root"]
120 val= PARTITIONS["swap"]
121 val= PARTITIONS["vservers"]
122 val= PARTITIONS["mapper-root"]
123 val= PARTITIONS["mapper-swap"]
124 val= PARTITIONS["mapper-vservers"]
125 except KeyError, part:
126 log.write( "Missing partition in PARTITIONS: %s\n" % part )
130 log.write( "Setting local time to UTC\n" )
131 utils.sysexec( "chroot %s ln -sf /usr/share/zoneinfo/UTC /etc/localtime" % \
135 log.write( "Enabling ntp at boot\n" )
136 utils.sysexec( "chroot %s chkconfig ntpd on" % SYSIMG_PATH, log )
138 log.write( "Creating system directory %s\n" % PLCONF_DIR )
139 if not utils.makedirs( "%s/%s" % (SYSIMG_PATH,PLCONF_DIR) ):
140 log.write( "Unable to create directory\n" )
144 log.write( "Writing network configuration\n" )
145 write_network_configuration( vars, log )
147 # write out the modprobe.conf file for the system. make sure
148 # the order of the ethernet devices are listed in the same order
149 # as the boot cd loaded the modules. this is found in /tmp/loadedmodules
150 # ultimately, the order will only match the boot cd order if
151 # the kernel modules have the same name - which should be true for the later
152 # version boot cds because they use the same kernel version.
153 # older boot cds use a 2.4.19 kernel, and its possible some of the network
154 # module names have changed, in which case the system might not boot
155 # if the network modules are activated in a different order that the
157 log.write( "Writing /etc/modprobe.conf\n" )
158 write_modprobeconf_file( vars, log )
160 # dump the modprobe.conf file to the log (not to screen)
161 log.write( "Contents of new modprobe.conf file:\n" )
162 modulesconf_file= file("%s/etc/modprobe.conf" % SYSIMG_PATH, "r" )
163 contents= modulesconf_file.read()
164 log.write( contents + "\n" )
165 modulesconf_file.close()
166 modulesconf_file= None
167 log.write( "End contents of new modprobe.conf file.\n" )
169 log.write( "Writing system /etc/fstab\n" )
170 fstab= file( "%s/etc/fstab" % SYSIMG_PATH, "w" )
171 fstab.write( "%s none swap sw 0 0\n" % \
172 PARTITIONS["mapper-swap"] )
173 fstab.write( "%s / ext3 defaults 0 0\n" % \
174 PARTITIONS["mapper-root"] )
175 fstab.write( "%s /vservers ext3 tagxid,defaults 0 0\n" % \
176 PARTITIONS["mapper-vservers"] )
177 fstab.write( "none /proc proc defaults 0 0\n" )
178 fstab.write( "none /dev/shm tmpfs defaults 0 0\n" )
179 fstab.write( "none /dev/pts devpts defaults 0 0\n" )
181 # fstab.write( "none /rcfs rcfs defaults 0 0\n" )
185 log.write( "Writing system /etc/issue\n" )
186 issue= file( "%s/etc/issue" % SYSIMG_PATH, "w" )
187 issue.write( "PlanetLab Node: \\n\n" )
188 issue.write( "Kernel \\r on an \\m\n" )
189 issue.write( "http://www.planet-lab.org\n\n" )
192 log.write( "Setting up authentication (non-ssh)\n" )
193 utils.sysexec( "chroot %s authconfig --nostart --kickstart --enablemd5 " \
194 "--enableshadow" % SYSIMG_PATH, log )
195 utils.sysexec( "sed -e 's/^root\:\:/root\:*\:/g' " \
196 "%s/etc/shadow > %s/etc/shadow.new" % \
197 (SYSIMG_PATH,SYSIMG_PATH), log )
198 utils.sysexec( "chroot %s mv " \
199 "/etc/shadow.new /etc/shadow" % SYSIMG_PATH, log )
200 utils.sysexec( "chroot %s chmod 400 /etc/shadow" % SYSIMG_PATH, log )
202 # if we are setup with dhcp, copy the current /etc/resolv.conf into
203 # the system image so we can run programs inside that need network access
206 method= vars['NETWORK_SETTINGS']['method']
211 utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
213 # the kernel rpm should have already done this, so don't fail the
214 # install if it fails
215 log.write( "Mounting /proc in system image\n" )
216 utils.sysexec_noerr( "mount -t proc proc %s/proc" % SYSIMG_PATH, log )
218 # mkinitrd references both /etc/modprobe.conf and /etc/fstab
219 # as well as /proc/lvm/global. The kernel RPM installation
220 # likely created an improper initrd since these files did not
221 # yet exist. Re-create the initrd here.
222 log.write( "Making initrd\n" )
224 # trick mkinitrd in case the current environment does not have device mapper
226 if not os.path.exists( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) ):
228 utils.makedirs( "%s/dev/mapper" % SYSIMG_PATH )
229 rootdev= file( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]), "w" )
233 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
235 initrd= os.readlink( "%s/boot/initrd-boot%s" % (SYSIMG_PATH,option) )
236 kernel_version= initrd.replace("initrd-", "").replace(".img", "")
237 utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
238 utils.sysexec( "chroot %s mkinitrd /boot/initrd-%s.img %s" % \
239 (SYSIMG_PATH, kernel_version, kernel_version), log )
241 if fake_root_lvm == 1:
242 utils.removefile( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) )
244 log.write( "Writing node install version\n" )
245 utils.makedirs( "%s/etc/planetlab" % SYSIMG_PATH )
246 ver= file( "%s/etc/planetlab/install_version" % SYSIMG_PATH, "w" )
247 ver.write( "%s\n" % VERSION )
250 log.write( "Creating ssh host keys\n" )
251 key_gen_prog= "/usr/bin/ssh-keygen"
253 log.write( "Generating SSH1 RSA host key:\n" )
254 key_file= "/etc/ssh/ssh_host_key"
255 utils.sysexec( "chroot %s %s -q -t rsa1 -f %s -C '' -N ''" %
256 (SYSIMG_PATH,key_gen_prog,key_file), log )
257 utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
258 utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
260 log.write( "Generating SSH2 RSA host key:\n" )
261 key_file= "/etc/ssh/ssh_host_rsa_key"
262 utils.sysexec( "chroot %s %s -q -t rsa -f %s -C '' -N ''" %
263 (SYSIMG_PATH,key_gen_prog,key_file), log )
264 utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
265 utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
267 log.write( "Generating SSH2 DSA host key:\n" )
268 key_file= "/etc/ssh/ssh_host_dsa_key"
269 utils.sysexec( "chroot %s %s -q -t dsa -f %s -C '' -N ''" %
270 (SYSIMG_PATH,key_gen_prog,key_file), log )
271 utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
272 utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
278 def write_network_configuration( vars, log ):
280 Write out the network configuration for this machine:
282 /etc/sysconfig/network-scripts/ifcfg-eth0
283 /etc/resolv.conf (if applicable)
284 /etc/sysconfig/network
286 It is assumed the caller mounted the root partition and the vserver partition
287 starting on SYSIMG_PATH - it is not checked here.
289 The values to be used for the network settings are to be set in vars
290 in the variable 'NETWORK_SETTINGS', which is a dictionary
293 Key Used by this function
294 -----------------------------------------------
311 SYSIMG_PATH= vars["SYSIMG_PATH"]
312 if SYSIMG_PATH == "":
313 raise ValueError, "SYSIMG_PATH"
315 except KeyError, var:
316 raise BootManagerException, "Missing variable in vars: %s\n" % var
317 except ValueError, var:
318 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
322 network_settings= vars['NETWORK_SETTINGS']
324 raise BootManagerException, "No network settings found in vars."
327 hostname= network_settings['hostname']
328 domainname= network_settings['domainname']
329 method= network_settings['method']
330 ip= network_settings['ip']
331 gateway= network_settings['gateway']
332 network= network_settings['network']
333 netmask= network_settings['netmask']
334 dns1= network_settings['dns1']
335 mac= network_settings['mac']
337 raise BootManagerException, "Missing value %s in network settings." % str(e)
341 dns2= network_settings['dns2']
346 log.write( "Writing /etc/hosts\n" )
347 hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )
348 hosts_file.write( "127.0.0.1 localhost\n" )
349 if method == "static":
350 hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
355 log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
356 eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
358 eth0_file.write( "DEVICE=eth0\n" )
359 if method == "static":
360 eth0_file.write( "BOOTPROTO=static\n" )
361 eth0_file.write( "IPADDR=%s\n" % ip )
362 eth0_file.write( "NETMASK=%s\n" % netmask )
363 eth0_file.write( "GATEWAY=%s\n" % gateway )
365 eth0_file.write( "BOOTPROTO=dhcp\n" )
366 eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
368 eth0_file.write( "HWADDR=%s\n" % mac )
369 eth0_file.write( "ONBOOT=yes\n" )
370 eth0_file.write( "USERCTL=no\n" )
374 if method == "static":
375 log.write( "Writing /etc/resolv.conf\n" )
376 resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
378 resolv_file.write( "nameserver %s\n" % dns1 )
380 resolv_file.write( "nameserver %s\n" % dns2 )
381 resolv_file.write( "search %s\n" % domainname )
385 log.write( "Writing /etc/sysconfig/network\n" )
386 network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
387 network_file.write( "NETWORKING=yes\n" )
388 network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
389 if method == "static":
390 network_file.write( "GATEWAY=%s\n" % gateway )
396 def write_modprobeconf_file( vars, log, filename = "/etc/modprobe.conf"):
398 write out the system file /etc/modprobe.conf with the current
401 returns a tuple of the number of network driver lines and storage
402 driver lines written as (networkcount,storagecount)
405 # make sure we have this class loaded
406 from systeminfo import systeminfo
409 SYSIMG_PATH= vars["SYSIMG_PATH"]
410 if SYSIMG_PATH == "":
411 raise ValueError, "SYSIMG_PATH"
413 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
415 except KeyError, var:
416 raise BootManagerException, "Missing variable in vars: %s\n" % var
417 except ValueError, var:
418 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
421 # get the kernel version
423 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
425 initrd= os.readlink( "%s/boot/initrd-boot%s" % (SYSIMG_PATH,option) )
426 kernel_version= initrd.replace("initrd-", "").replace(".img", "")
428 sysinfo= systeminfo()
429 sysmods= sysinfo.get_system_modules(SYSIMG_PATH, kernel_version)
431 raise BootManagerException, "Unable to get list of system modules."
436 modulesconf_file= file("%s/%s" % (SYSIMG_PATH,filename), "w" )
439 if type == sysinfo.MODULE_CLASS_SCSI:
440 for a_mod in sysmods[type]:
442 modulesconf_file.write( "alias scsi_hostadapter %s\n" %
445 modulesconf_file.write( "alias scsi_hostadapter%d %s\n" %
447 scsi_count= scsi_count + 1
449 elif type == sysinfo.MODULE_CLASS_NETWORK:
450 for a_mod in sysmods[type]:
451 modulesconf_file.write( "alias eth%d %s\n" %
453 eth_count= eth_count + 1
455 modulesconf_file.close()
456 modulesconf_file= None
458 return (eth_count,scsi_count)