Detangled steps. No step makes calls into another step.
[bootmanager.git] / source / steps / InstallWriteConfig.py
1 #!/usr/bin/python2
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8 # expected /proc/partitions format
9
10 import os, string
11
12 from Exceptions import *
13 import utils
14 import systeminfo
15 import BootAPI
16 import ModelOptions
17
18 def Run( vars, log ):
19
20     """
21     Writes out the following configuration files for the node:
22     /etc/fstab
23     /etc/resolv.conf (if applicable)
24     /etc/ssh/ssh_host_key
25     /etc/ssh/ssh_host_rsa_key
26     /etc/ssh/ssh_host_dsa_key
27     
28     Expect the following variables from the store:
29     VERSION                 the version of the install
30     SYSIMG_PATH             the path where the system image will be mounted
31                             (always starts with TEMP_PATH)
32     PARTITIONS              dictionary of generic part. types (root/swap)
33                             and their associated devices.
34     PLCONF_DIR              The directory to store the configuration file in
35     NETWORK_SETTINGS  A dictionary of the values from the network
36                                 configuration file
37     BOOT_CD_VERSION          A tuple of the current bootcd version
38     
39     Sets the following variables:
40     None
41     
42     """
43
44     log.write( "\n\nStep: Install: Writing configuration files.\n" )
45     
46     # make sure we have the variables we need
47     try:
48         VERSION= vars["VERSION"]
49         if VERSION == "":
50             raise ValueError, "VERSION"
51
52         SYSIMG_PATH= vars["SYSIMG_PATH"]
53         if SYSIMG_PATH == "":
54             raise ValueError, "SYSIMG_PATH"
55
56         PARTITIONS= vars["PARTITIONS"]
57         if PARTITIONS == None:
58             raise ValueError, "PARTITIONS"
59
60         PLCONF_DIR= vars["PLCONF_DIR"]
61         if PLCONF_DIR == "":
62             raise ValueError, "PLCONF_DIR"
63
64         NETWORK_SETTINGS= vars["NETWORK_SETTINGS"]
65         if NETWORK_SETTINGS == "":
66             raise ValueError, "NETWORK_SETTINGS"
67
68         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
69         if BOOT_CD_VERSION == "":
70             raise ValueError, "BOOT_CD_VERSION"
71
72     except KeyError, var:
73         raise BootManagerException, "Missing variable in vars: %s\n" % var
74     except ValueError, var:
75         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
76
77     log.write( "Setting local time to UTC\n" )
78     utils.sysexec( "chroot %s ln -sf /usr/share/zoneinfo/UTC /etc/localtime" % \
79                    SYSIMG_PATH, log )
80
81     log.write( "Enabling ntp at boot\n" )
82     utils.sysexec( "chroot %s chkconfig ntpd on" % SYSIMG_PATH, log )
83
84     log.write( "Creating system directory %s\n" % PLCONF_DIR )
85     if not utils.makedirs( "%s/%s" % (SYSIMG_PATH,PLCONF_DIR) ):
86         log.write( "Unable to create directory\n" )
87         return 0
88
89     log.write( "Writing system /etc/fstab\n" )
90     fstab= file( "%s/etc/fstab" % SYSIMG_PATH, "w" )
91     fstab.write( "%s           none        swap      sw        0 0\n" % \
92                  PARTITIONS["mapper-swap"] )
93     fstab.write( "%s           /           ext3      defaults  0 0\n" % \
94                  PARTITIONS["mapper-root"] )
95     fstab.write( "%s           /vservers   ext3      tagxid,defaults  0 0\n" % \
96                  PARTITIONS["mapper-vservers"] )
97     fstab.write( "none         /proc       proc      defaults  0 0\n" )
98     fstab.write( "none         /dev/shm    tmpfs     defaults  0 0\n" )
99     fstab.write( "none         /dev/pts    devpts    defaults  0 0\n" )
100     # no longer needed
101     # fstab.write( "none         /rcfs       rcfs      defaults  0 0\n" )
102     fstab.close()
103
104
105     log.write( "Writing system /etc/issue\n" )
106     issue= file( "%s/etc/issue" % SYSIMG_PATH, "w" )
107     issue.write( "PlanetLab Node: \\n\n" )
108     issue.write( "Kernel \\r on an \\m\n" )
109     issue.write( "http://www.planet-lab.org\n\n" )
110     issue.close()
111
112     log.write( "Setting up authentication (non-ssh)\n" )
113     utils.sysexec( "chroot %s authconfig --nostart --kickstart --enablemd5 " \
114                    "--enableshadow" % SYSIMG_PATH, log )
115     utils.sysexec( "sed -e 's/^root\:\:/root\:*\:/g' " \
116                    "%s/etc/shadow > %s/etc/shadow.new" % \
117                    (SYSIMG_PATH,SYSIMG_PATH), log )
118     utils.sysexec( "chroot %s mv " \
119                    "/etc/shadow.new /etc/shadow" % SYSIMG_PATH, log )
120     utils.sysexec( "chroot %s chmod 400 /etc/shadow" % SYSIMG_PATH, log )
121
122     # if we are setup with dhcp, copy the current /etc/resolv.conf into
123     # the system image so we can run programs inside that need network access
124     method= ""
125     try:
126         method= vars['NETWORK_SETTINGS']['method']
127     except:
128         pass
129     
130     if method == "dhcp":
131         utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
132
133     # the kernel rpm should have already done this, so don't fail the
134     # install if it fails
135     log.write( "Mounting /proc in system image\n" )
136     utils.sysexec_noerr( "mount -t proc proc %s/proc" % SYSIMG_PATH, log )
137
138     # mkinitrd references both /etc/modprobe.conf and /etc/fstab
139     # as well as /proc/lvm/global. The kernel RPM installation
140     # likely created an improper initrd since these files did not
141     # yet exist. Re-create the initrd here.
142     log.write( "Making initrd\n" )
143
144     # trick mkinitrd in case the current environment does not have device mapper
145     fake_root_lvm= False
146     if not os.path.exists( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) ):
147         fake_root_lvm= True
148         utils.makedirs( "%s/dev/mapper" % SYSIMG_PATH )
149         rootdev= file( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]), "w" )
150         rootdev.close()
151
152     initrd, kernel_version= systeminfo.getKernelVersion(vars,log)
153     utils.removefile( "%s/boot/%s" % (SYSIMG_PATH, initrd) )
154     utils.sysexec( "chroot %s mkinitrd /boot/initrd-%s.img %s" % \
155                    (SYSIMG_PATH, kernel_version, kernel_version), log )
156
157     if fake_root_lvm == True:
158         utils.removefile( "%s/%s" % (SYSIMG_PATH,PARTITIONS["mapper-root"]) )
159
160     log.write( "Writing node install version\n" )
161     utils.makedirs( "%s/etc/planetlab" % SYSIMG_PATH )
162     ver= file( "%s/etc/planetlab/install_version" % SYSIMG_PATH, "w" )
163     ver.write( "%s\n" % VERSION )
164     ver.close()
165
166     log.write( "Creating ssh host keys\n" )
167     key_gen_prog= "/usr/bin/ssh-keygen"
168
169     log.write( "Generating SSH1 RSA host key:\n" )
170     key_file= "/etc/ssh/ssh_host_key"
171     utils.sysexec( "chroot %s %s -q -t rsa1 -f %s -C '' -N ''" %
172                    (SYSIMG_PATH,key_gen_prog,key_file), log )
173     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
174     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
175     
176     log.write( "Generating SSH2 RSA host key:\n" )
177     key_file= "/etc/ssh/ssh_host_rsa_key"
178     utils.sysexec( "chroot %s %s -q -t rsa -f %s -C '' -N ''" %
179                    (SYSIMG_PATH,key_gen_prog,key_file), log )
180     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
181     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
182     
183     log.write( "Generating SSH2 DSA host key:\n" )
184     key_file= "/etc/ssh/ssh_host_dsa_key"
185     utils.sysexec( "chroot %s %s -q -t dsa -f %s -C '' -N ''" %
186                    (SYSIMG_PATH,key_gen_prog,key_file), log )
187     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
188     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
189
190     return 1