45c71560d6b5dcf3765097a55fd91783280a90f7
[bootmanager.git] / source / steps / InstallWriteConfig.py
1 # Copyright (c) 2003 Intel Corporation
2 # All rights reserved.
3
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7
8 #     * Redistributions of source code must retain the above copyright
9 #       notice, this list of conditions and the following disclaimer.
10
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.
15
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.
19
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.
31
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
40 # goods and services.
41
42
43 import os, string
44
45 from Exceptions import *
46 import utils
47 from systeminfo import systeminfo
48 import BootAPI
49
50
51 def Run( vars, log ):
52
53     """
54     Writes out the following configuration files for the node:
55     /etc/fstab
56     /etc/hosts
57     /etc/sysconfig/network-scripts/ifcfg-eth0
58     /etc/resolv.conf (if applicable)
59     /etc/sysconfig/network
60     /etc/modprobe.conf
61     /etc/ssh/ssh_host_key
62     /etc/ssh/ssh_host_rsa_key
63     /etc/ssh/ssh_host_dsa_key
64     
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
73                                 configuration file
74     BOOT_CD_VERSION          A tuple of the current bootcd version
75     
76     Sets the following variables:
77     None
78     
79     """
80
81     log.write( "\n\nStep: Install: Writing configuration files.\n" )
82     
83     # make sure we have the variables we need
84     try:
85         VERSION= vars["VERSION"]
86         if VERSION == "":
87             raise ValueError, "VERSION"
88
89         SYSIMG_PATH= vars["SYSIMG_PATH"]
90         if SYSIMG_PATH == "":
91             raise ValueError, "SYSIMG_PATH"
92
93         PARTITIONS= vars["PARTITIONS"]
94         if PARTITIONS == None:
95             raise ValueError, "PARTITIONS"
96
97         PLCONF_DIR= vars["PLCONF_DIR"]
98         if PLCONF_DIR == "":
99             raise ValueError, "PLCONF_DIR"
100
101         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
102         if NETWORK_SETTINGS == "":
103             raise ValueError, "NETWORK_SETTINGS"
104
105         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
106         if BOOT_CD_VERSION == "":
107             raise ValueError, "BOOT_CD_VERSION"
108
109     except KeyError, var:
110         raise BootManagerException, "Missing variable in vars: %s\n" % var
111     except ValueError, var:
112         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
113
114     try:
115         # we need to keys in PARTITIONS, root and swap, make sure
116         # they exist
117         val= PARTITIONS["root"]
118         val= PARTITIONS["swap"]
119         val= PARTITIONS["vservers"]
120         val= PARTITIONS["mapper-root"]
121         val= PARTITIONS["mapper-swap"]
122         val= PARTITIONS["mapper-vservers"]
123     except KeyError, part:
124         log.write( "Missing partition in PARTITIONS: %s\n" % part )
125         return 0
126     
127
128     log.write( "Setting local time to UTC\n" )
129     utils.sysexec( "chroot %s ln -sf /usr/share/zoneinfo/UTC /etc/localtime" % \
130                    SYSIMG_PATH, log )
131
132
133     log.write( "Enabling ntp at boot\n" )
134     utils.sysexec( "chroot %s chkconfig ntpd on" % SYSIMG_PATH, log )
135
136     log.write( "Creating system directory %s\n" % PLCONF_DIR )
137     if not utils.makedirs( "%s/%s" % (SYSIMG_PATH,PLCONF_DIR) ):
138         log.write( "Unable to create directory\n" )
139         return 0
140
141
142     log.write( "Writing network configuration\n" )
143     write_network_configuration( vars, log )
144
145     # write out the modprobe.conf file for the system. make sure
146     # the order of the ethernet devices are listed in the same order
147     # as the boot cd loaded the modules. this is found in /tmp/loadedmodules
148     # ultimately, the order will only match the boot cd order if
149     # the kernel modules have the same name - which should be true for the later
150     # version boot cds because they use the same kernel version.
151     # older boot cds use a 2.4.19 kernel, and its possible some of the network
152     # module names have changed, in which case the system might not boot
153     # if the network modules are activated in a different order that the
154     # boot cd.
155     log.write( "Writing /etc/modprobe.conf\n" )
156
157     # get the kernel version
158     initrd= os.readlink( "%s/boot/initrd-boot" % SYSIMG_PATH )
159     kernel_version= initrd.replace("initrd-", "").replace(".img", "")
160
161     sysinfo= systeminfo()
162     sysmods= sysinfo.get_system_modules(SYSIMG_PATH, kernel_version)
163     if sysmods is None:
164         raise BootManagerException, "Unable to get list of system modules."
165         
166     eth_count= 0
167     scsi_count= 0
168
169     modulesconf_file= file("%s/etc/modprobe.conf" % SYSIMG_PATH, "w" )
170
171     for type in sysmods:
172         if type == sysinfo.MODULE_CLASS_SCSI:
173             for a_mod in sysmods[type]:
174                 if scsi_count == 0:
175                     modulesconf_file.write( "alias scsi_hostadapter %s\n" %
176                                             a_mod )
177                 else:
178                     modulesconf_file.write( "alias scsi_hostadapter%d %s\n" %
179                                             (scsi_count,a_mod) )
180                 scsi_count= scsi_count + 1
181
182         elif type == sysinfo.MODULE_CLASS_NETWORK:
183             for a_mod in sysmods[type]:
184                 modulesconf_file.write( "alias eth%d %s\n" %
185                                         (eth_count,a_mod) )
186                 eth_count= eth_count + 1
187
188     modulesconf_file.close()
189     modulesconf_file= None
190
191
192     # dump the modprobe.conf file to the log (not to screen)
193     log.write( "Contents of new modprobe.conf file:\n" )
194     modulesconf_file= file("%s/etc/modprobe.conf" % SYSIMG_PATH, "r" )
195     contents= modulesconf_file.read()
196     log.write( contents + "\n" )
197     modulesconf_file.close()
198     modulesconf_file= None
199     log.write( "End contents of new modprobe.conf file.\n" )
200
201     log.write( "Writing system /etc/fstab\n" )
202     fstab= file( "%s/etc/fstab" % SYSIMG_PATH, "w" )
203     fstab.write( "%s           none        swap      sw        0 0\n" % \
204                  PARTITIONS["mapper-swap"] )
205     fstab.write( "%s           /           ext3      defaults  0 0\n" % \
206                  PARTITIONS["mapper-root"] )
207     fstab.write( "%s           /vservers   ext3      tagxid,defaults  0 0\n" % \
208                  PARTITIONS["mapper-vservers"] )
209     fstab.write( "none         /proc       proc      defaults  0 0\n" )
210     fstab.write( "none         /dev/shm    tmpfs     defaults  0 0\n" )
211     fstab.write( "none         /dev/pts    devpts    defaults  0 0\n" )
212     fstab.write( "none         /rcfs       rcfs      defaults  0 0\n" )
213     fstab.close()
214
215
216     log.write( "Writing system /etc/issue\n" )
217     issue= file( "%s/etc/issue" % SYSIMG_PATH, "w" )
218     issue.write( "PlanetLab Node: \\n\n" )
219     issue.write( "Kernel \\r on an \\m\n" )
220     issue.write( "http://www.planet-lab.org\n\n" )
221     issue.close()
222
223     log.write( "Setting up authentication (non-ssh)\n" )
224     utils.sysexec( "chroot %s authconfig --nostart --kickstart --enablemd5 " \
225                    "--enableshadow" % SYSIMG_PATH, log )
226     utils.sysexec( "sed -e 's/^root\:\:/root\:*\:/g' " \
227                    "%s/etc/shadow > %s/etc/shadow.new" % \
228                    (SYSIMG_PATH,SYSIMG_PATH), log )
229     utils.sysexec( "chroot %s mv " \
230                    "/etc/shadow.new /etc/shadow" % SYSIMG_PATH, log )
231     utils.sysexec( "chroot %s chmod 400 /etc/shadow" % SYSIMG_PATH, log )
232
233     # if we are setup with dhcp, copy the current /etc/resolv.conf into
234     # the system image so we can run programs inside that need network access
235     method= ""
236     try:
237         method= vars['NETWORK_SETTINGS']['method']
238     except:
239         pass
240     
241     if method == "dhcp":
242         utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
243
244     # the kernel rpm should have already done this, so don't fail the
245     # install if it fails
246     log.write( "Mounting /proc in system image\n" )
247     utils.sysexec_noerr( "mount -t proc proc %s/proc" % SYSIMG_PATH, log )
248
249     # mkinitrd references both /etc/modprobe.conf and /etc/fstab
250     # as well as /proc/lvm/global. The kernel RPM installation
251     # likely created an improper initrd since these files did not
252     # yet exist. Re-create the initrd here.
253     log.write( "Making initrd\n" )
254
255     # trick mkinitrd in case the current environment does not have device mapper
256     fake_root_lvm= 0
257     if not os.path.exists( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) ):
258         fake_root_lvm= 1
259         utils.makedirs( "%s/dev/mapper" % SYSIMG_PATH )
260         rootdev= file( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]), "w" )
261         rootdev.close()
262
263     # initrd set above
264     utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
265     utils.sysexec( "chroot %s mkinitrd /boot/initrd-%s.img %s" % \
266                    (SYSIMG_PATH, kernel_version, kernel_version), log )
267
268     if fake_root_lvm == 1:
269         utils.removefile( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) )
270
271     log.write( "Writing node install version\n" )
272     utils.makedirs( "%s/etc/planetlab" % SYSIMG_PATH )
273     ver= file( "%s/etc/planetlab/install_version" % SYSIMG_PATH, "w" )
274     ver.write( "%s\n" % VERSION )
275     ver.close()
276
277     log.write( "Creating ssh host keys\n" )
278     key_gen_prog= "/usr/bin/ssh-keygen"
279
280     log.write( "Generating SSH1 RSA host key:\n" )
281     key_file= "/etc/ssh/ssh_host_key"
282     utils.sysexec( "chroot %s %s -q -t rsa1 -f %s -C '' -N ''" %
283                    (SYSIMG_PATH,key_gen_prog,key_file), log )
284     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
285     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
286     
287     log.write( "Generating SSH2 RSA host key:\n" )
288     key_file= "/etc/ssh/ssh_host_rsa_key"
289     utils.sysexec( "chroot %s %s -q -t rsa -f %s -C '' -N ''" %
290                    (SYSIMG_PATH,key_gen_prog,key_file), log )
291     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
292     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
293     
294     log.write( "Generating SSH2 DSA host key:\n" )
295     key_file= "/etc/ssh/ssh_host_dsa_key"
296     utils.sysexec( "chroot %s %s -q -t dsa -f %s -C '' -N ''" %
297                    (SYSIMG_PATH,key_gen_prog,key_file), log )
298     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
299     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
300
301     return 1
302
303
304
305 def write_network_configuration( vars, log ):
306     """
307     Write out the network configuration for this machine:
308     /etc/hosts
309     /etc/sysconfig/network-scripts/ifcfg-eth0
310     /etc/resolv.conf (if applicable)
311     /etc/sysconfig/network
312
313     It is assumed the caller mounted the root partition and the vserver partition
314     starting on SYSIMG_PATH - it is not checked here.
315
316     The values to be used for the network settings are to be set in vars
317     in the variable 'NETWORK_SETTINGS', which is a dictionary
318     with keys:
319
320      Key               Used by this function
321      -----------------------------------------------
322      node_id
323      node_key
324      method            x
325      ip                x
326      mac               x (optional)
327      gateway           x
328      network           x
329      broadcast         x
330      netmask           x
331      dns1              x
332      dns2              x (optional)
333      hostname          x
334      domainname        x
335     """
336
337     try:
338         SYSIMG_PATH= vars["SYSIMG_PATH"]
339         if SYSIMG_PATH == "":
340             raise ValueError, "SYSIMG_PATH"
341
342     except KeyError, var:
343         raise BootManagerException, "Missing variable in vars: %s\n" % var
344     except ValueError, var:
345         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
346
347
348     try:
349         network_settings= vars['NETWORK_SETTINGS']
350     except KeyError, e:
351         raise BootManagerException, "No network settings found in vars."
352
353     try:
354         hostname= network_settings['hostname']
355         domainname= network_settings['domainname']
356         method= network_settings['method']
357         ip= network_settings['ip']
358         gateway= network_settings['gateway']
359         network= network_settings['network']
360         netmask= network_settings['netmask']
361         dns1= network_settings['dns1']
362     except KeyError, e:
363         raise BootManagerException, "Missing value %s in network settings." % str(e)
364
365     try:
366         dns2= ''
367         dns2= network_settings['dns2']
368     except KeyError, e:
369         pass
370
371         
372     log.write( "Writing /etc/hosts\n" )
373     hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )    
374     hosts_file.write( "127.0.0.1       localhost\n" )
375     if method == "static":
376         hosts_file.write( "%s %s.%s\n" % (ip, hostname, domainname) )
377     hosts_file.close()
378     hosts_file= None
379     
380
381     log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
382     eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
383                     SYSIMG_PATH, "w" )
384     eth0_file.write( "DEVICE=eth0\n" )
385     if method == "static":
386         eth0_file.write( "BOOTPROTO=static\n" )
387         eth0_file.write( "IPADDR=%s\n" % ip )
388         eth0_file.write( "NETMASK=%s\n" % netmask )
389         eth0_file.write( "GATEWAY=%s\n" % gateway )
390     else:
391         eth0_file.write( "BOOTPROTO=dhcp\n" )
392         eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
393     eth0_file.write( "ONBOOT=yes\n" )
394     eth0_file.write( "USERCTL=no\n" )
395     eth0_file.close()
396     eth0_file= None
397
398     if method == "static":
399         log.write( "Writing /etc/resolv.conf\n" )
400         resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
401         if dns1 != "":
402             resolv_file.write( "nameserver %s\n" % dns1 )
403         if dns2 != "":
404             resolv_file.write( "nameserver %s\n" % dns2 )
405         resolv_file.write( "search %s\n" % domainname )
406         resolv_file.close()
407         resolv_file= None
408
409     log.write( "Writing /etc/sysconfig/network\n" )
410     network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
411     network_file.write( "NETWORKING=yes\n" )
412     network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
413     if method == "static":
414         network_file.write( "GATEWAY=%s\n" % gateway )
415     network_file.close()
416     network_file= None
417