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