d8b5a6ecfc262cfb6bd2fec7a3a30fbcedc1496b
[bootmanager.git] / source / steps / InstallWriteConfig.py
1 #!/usr/bin/python
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     INTERFACE_SETTINGS  A dictionary of the values from the network
36                                 configuration file
37     Sets the following variables:
38     None
39     
40     """
41
42     log.write( "\n\nStep: Install: Writing configuration files.\n" )
43     
44     # make sure we have the variables we need
45     try:
46         VERSION= vars["VERSION"]
47         if VERSION == "":
48             raise ValueError, "VERSION"
49
50         SYSIMG_PATH= vars["SYSIMG_PATH"]
51         if SYSIMG_PATH == "":
52             raise ValueError, "SYSIMG_PATH"
53
54         PARTITIONS= vars["PARTITIONS"]
55         if PARTITIONS == None:
56             raise ValueError, "PARTITIONS"
57
58         PLCONF_DIR= vars["PLCONF_DIR"]
59         if PLCONF_DIR == "":
60             raise ValueError, "PLCONF_DIR"
61
62         INTERFACE_SETTINGS= vars["INTERFACE_SETTINGS"]
63         if INTERFACE_SETTINGS == "":
64             raise ValueError, "INTERFACE_SETTINGS"
65
66     except KeyError, var:
67         raise BootManagerException, "Missing variable in vars: %s\n" % var
68     except ValueError, var:
69         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
70
71     log.write( "Setting local time to UTC\n" )
72     utils.sysexec_chroot( SYSIMG_PATH,
73         "ln -sf /usr/share/zoneinfo/UTC /etc/localtime", log )
74
75     log.write( "Enabling ntp at boot\n" )
76     utils.sysexec_chroot( SYSIMG_PATH, "chkconfig ntpd on", log )
77
78     log.write( "Creating system directory %s\n" % PLCONF_DIR )
79     if not utils.makedirs( "%s/%s" % (SYSIMG_PATH,PLCONF_DIR) ):
80         log.write( "Unable to create directory\n" )
81         return 0
82
83     log.write( "Writing system /etc/fstab\n" )
84     fstab= file( "%s/etc/fstab" % SYSIMG_PATH, "w" )
85     fstab.write( "%s           none        swap      sw        0 0\n" % \
86                  PARTITIONS["mapper-swap"] )
87     fstab.write( "%s           /           ext3      defaults  1 1\n" % \
88                  PARTITIONS["mapper-root"] )
89     fstab.write( "%s           /vservers   ext3      tagxid,defaults  1 2\n" % \
90                  PARTITIONS["mapper-vservers"] )
91     fstab.write( "none         /proc       proc      defaults  0 0\n" )
92     fstab.write( "none         /dev/shm    tmpfs     defaults  0 0\n" )
93     fstab.write( "none         /dev/pts    devpts    defaults  0 0\n" )
94     fstab.close()
95
96     log.write( "Writing system /etc/issue\n" )
97     issue= file( "%s/etc/issue" % SYSIMG_PATH, "w" )
98     issue.write( "PlanetLab Node: \\n\n" )
99     issue.write( "Kernel \\r on an \\m\n" )
100     issue.write( "http://www.planet-lab.org\n\n" )
101     issue.close()
102
103     log.write( "Setting up authentication (non-ssh)\n" )
104     utils.sysexec_chroot( SYSIMG_PATH, "authconfig --nostart --kickstart --enablemd5 " \
105                    "--enableshadow", log )
106     utils.sysexec( "sed -e 's/^root\:\:/root\:*\:/g' " \
107                    "%s/etc/shadow > %s/etc/shadow.new" % \
108                    (SYSIMG_PATH,SYSIMG_PATH), log )
109     utils.sysexec_chroot( SYSIMG_PATH, "mv " \
110                    "/etc/shadow.new /etc/shadow", log )
111     utils.sysexec_chroot( SYSIMG_PATH, "chmod 400 /etc/shadow", log )
112
113     # if we are setup with dhcp, copy the current /etc/resolv.conf into
114     # the system image so we can run programs inside that need network access
115     method= ""
116     try:
117         method= vars['INTERFACE_SETTINGS']['method']
118     except:
119         pass
120     
121     if method == "dhcp":
122         utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
123
124     log.write( "Writing node install version\n" )
125     utils.makedirs( "%s/etc/planetlab" % SYSIMG_PATH )
126     ver= file( "%s/etc/planetlab/install_version" % SYSIMG_PATH, "w" )
127     ver.write( "%s\n" % VERSION )
128     ver.close()
129
130     log.write( "Creating ssh host keys\n" )
131     key_gen_prog= "/usr/bin/ssh-keygen"
132
133     log.write( "Generating SSH1 RSA host key:\n" )
134     key_file= "/etc/ssh/ssh_host_key"
135     utils.sysexec_chroot( SYSIMG_PATH, "%s -q -t rsa1 -f %s -C '' -N ''" %
136                    (key_gen_prog,key_file), log )
137     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
138     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
139     
140     log.write( "Generating SSH2 RSA host key:\n" )
141     key_file= "/etc/ssh/ssh_host_rsa_key"
142     utils.sysexec_chroot( SYSIMG_PATH, "%s -q -t rsa -f %s -C '' -N ''" %
143                    (key_gen_prog,key_file), log )
144     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
145     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
146     
147     log.write( "Generating SSH2 DSA host key:\n" )
148     key_file= "/etc/ssh/ssh_host_dsa_key"
149     utils.sysexec_chroot( SYSIMG_PATH, "%s -q -t dsa -f %s -C '' -N ''" %
150                    (key_gen_prog,key_file), log )
151     utils.sysexec( "chmod 600 %s/%s" % (SYSIMG_PATH,key_file), log )
152     utils.sysexec( "chmod 644 %s/%s.pub" % (SYSIMG_PATH,key_file), log )
153
154     return 1