svn:keywords
[bootmanager.git] / source / steps / InstallWriteConfig.py
1 #!/usr/bin/python
2 #
3 # $Id$
4 # $URL$
5 #
6 # Copyright (c) 2003 Intel Corporation
7 # All rights reserved.
8 #
9 # Copyright (c) 2004-2006 The Trustees of Princeton University
10 # All rights reserved.
11 # expected /proc/partitions format
12
13 import os, string
14
15 from Exceptions import *
16 import utils
17 import systeminfo
18 import BootAPI
19 import ModelOptions
20
21 def Run( vars, log ):
22
23     """
24     Writes out the following configuration files for the node:
25     /etc/fstab
26     /etc/resolv.conf (if applicable)
27     /etc/ssh/ssh_host_key
28     /etc/ssh/ssh_host_rsa_key
29     /etc/ssh/ssh_host_dsa_key
30     
31     Expect the following variables from the store:
32     VERSION                 the version of the install
33     SYSIMG_PATH             the path where the system image will be mounted
34                             (always starts with TEMP_PATH)
35     PARTITIONS              dictionary of generic part. types (root/swap)
36                             and their associated devices.
37     PLCONF_DIR              The directory to store the configuration file in
38     INTERFACE_SETTINGS  A dictionary of the values from the network
39                                 configuration file
40     Sets the following variables:
41     None
42     
43     """
44
45     log.write( "\n\nStep: Install: Writing configuration files.\n" )
46     
47     # make sure we have the variables we need
48     try:
49         VERSION= vars["VERSION"]
50         if VERSION == "":
51             raise ValueError, "VERSION"
52
53         SYSIMG_PATH= vars["SYSIMG_PATH"]
54         if SYSIMG_PATH == "":
55             raise ValueError, "SYSIMG_PATH"
56
57         PARTITIONS= vars["PARTITIONS"]
58         if PARTITIONS == None:
59             raise ValueError, "PARTITIONS"
60
61         PLCONF_DIR= vars["PLCONF_DIR"]
62         if PLCONF_DIR == "":
63             raise ValueError, "PLCONF_DIR"
64
65         INTERFACE_SETTINGS= vars["INTERFACE_SETTINGS"]
66         if INTERFACE_SETTINGS == "":
67             raise ValueError, "INTERFACE_SETTINGS"
68
69     except KeyError, var:
70         raise BootManagerException, "Missing variable in vars: %s\n" % var
71     except ValueError, var:
72         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
73
74     log.write( "Setting local time to UTC\n" )
75     utils.sysexec_chroot( SYSIMG_PATH,
76         "ln -sf /usr/share/zoneinfo/UTC /etc/localtime", log )
77
78     log.write( "Enabling ntp at boot\n" )
79     utils.sysexec_chroot( SYSIMG_PATH, "chkconfig ntpd on", log )
80
81     log.write( "Creating system directory %s\n" % PLCONF_DIR )
82     if not utils.makedirs( "%s/%s" % (SYSIMG_PATH,PLCONF_DIR) ):
83         log.write( "Unable to create directory\n" )
84         return 0
85
86     log.write( "Writing system /etc/fstab\n" )
87     fstab= file( "%s/etc/fstab" % SYSIMG_PATH, "w" )
88     fstab.write( "%s           none        swap      sw        0 0\n" % \
89                  PARTITIONS["mapper-swap"] )
90     fstab.write( "%s           /           ext3      defaults  1 1\n" % \
91                  PARTITIONS["mapper-root"] )
92     fstab.write( "%s           /vservers   ext3      tagxid,defaults  1 2\n" % \
93                  PARTITIONS["mapper-vservers"] )
94     fstab.write( "none         /proc       proc      defaults  0 0\n" )
95     fstab.write( "none         /dev/shm    tmpfs     defaults  0 0\n" )
96     fstab.write( "none         /dev/pts    devpts    defaults  0 0\n" )
97     fstab.close()
98
99     log.write( "Writing system /etc/issue\n" )
100     issue= file( "%s/etc/issue" % SYSIMG_PATH, "w" )
101     issue.write( "PlanetLab Node: \\n\n" )
102     issue.write( "Kernel \\r on an \\m\n" )
103     issue.write( "http://www.planet-lab.org\n\n" )
104     issue.close()
105
106     log.write( "Setting up authentication (non-ssh)\n" )
107     utils.sysexec_chroot( SYSIMG_PATH, "authconfig --nostart --kickstart --enablemd5 " \
108                    "--enableshadow", log )
109     utils.sysexec( "sed -e 's/^root\:\:/root\:*\:/g' " \
110                    "%s/etc/shadow > %s/etc/shadow.new" % \
111                    (SYSIMG_PATH,SYSIMG_PATH), log )
112     utils.sysexec_chroot( SYSIMG_PATH, "mv " \
113                    "/etc/shadow.new /etc/shadow", log )
114     utils.sysexec_chroot( SYSIMG_PATH, "chmod 400 /etc/shadow", log )
115
116     # if we are setup with dhcp, copy the current /etc/resolv.conf into
117     # the system image so we can run programs inside that need network access
118     method= ""
119     try:
120         method= vars['INTERFACE_SETTINGS']['method']
121     except:
122         pass
123     
124     if method == "dhcp":
125         utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
126
127     log.write( "Writing node install version\n" )
128     utils.makedirs( "%s/etc/planetlab" % SYSIMG_PATH )
129     ver= file( "%s/etc/planetlab/install_version" % SYSIMG_PATH, "w" )
130     ver.write( "%s\n" % VERSION )
131     ver.close()
132
133     log.write( "Creating ssh host keys\n" )
134     key_gen_prog= "/usr/bin/ssh-keygen"
135
136     log.write( "Generating SSH1 RSA host key:\n" )
137     key_file= "/etc/ssh/ssh_host_key"
138     utils.sysexec_chroot( SYSIMG_PATH, "%s -q -t rsa1 -f %s -C '' -N ''" %
139                    (key_gen_prog,key_file), log )
140     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
141     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
142     
143     log.write( "Generating SSH2 RSA host key:\n" )
144     key_file= "/etc/ssh/ssh_host_rsa_key"
145     utils.sysexec_chroot( SYSIMG_PATH, "%s -q -t rsa -f %s -C '' -N ''" %
146                    (key_gen_prog,key_file), log )
147     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
148     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
149     
150     log.write( "Generating SSH2 DSA host key:\n" )
151     key_file= "/etc/ssh/ssh_host_dsa_key"
152     utils.sysexec_chroot( SYSIMG_PATH, "%s -q -t dsa -f %s -C '' -N ''" %
153                    (key_gen_prog,key_file), log )
154     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
155     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
156
157     return 1