fix typo
[bootmanager.git] / source / steps / InstallBootstrapFS.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-2007 The Trustees of Princeton University
10 # All rights reserved.
11 # expected /proc/partitions format
12
13 import os, sys, string
14 import popen2
15 import shutil
16 import traceback 
17
18 from Exceptions import *
19 import utils
20 import BootServerRequest
21 import BootAPI
22
23
24 def Run( vars, log ):
25     """
26     Download core + extensions bootstrapfs tarballs and install on the hard drive
27     
28     Expect the following variables from the store:
29     SYSIMG_PATH          the path where the system image will be mounted
30     PARTITIONS           dictionary of generic part. types (root/swap)
31                          and their associated devices.
32     NODE_ID              the id of this machine
33     
34     Sets the following variables:
35     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
36                          path
37     ROOT_MOUNTED         set to 1 when the the base logical volumes
38                          are mounted.
39     """
40
41     log.write( "\n\nStep: Install: bootstrapfs tarball.\n" )
42
43     # make sure we have the variables we need
44     try:
45         SYSIMG_PATH= vars["SYSIMG_PATH"]
46         if SYSIMG_PATH == "":
47             raise ValueError, "SYSIMG_PATH"
48
49         PARTITIONS= vars["PARTITIONS"]
50         if PARTITIONS == None:
51             raise ValueError, "PARTITIONS"
52
53         NODE_ID= vars["NODE_ID"]
54         if NODE_ID == "":
55             raise ValueError, "NODE_ID"
56
57     except KeyError, var:
58         raise BootManagerException, "Missing variable in vars: %s\n" % var
59     except ValueError, var:
60         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
61
62
63     try:
64         # make sure the required partitions exist
65         val= PARTITIONS["root"]
66         val= PARTITIONS["swap"]
67         val= PARTITIONS["vservers"]
68     except KeyError, part:
69         log.write( "Missing partition in PARTITIONS: %s\n" % part )
70         return 0   
71
72     bs_request= BootServerRequest.BootServerRequest(vars)
73     
74     log.write( "turning on swap space\n" )
75     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
76
77     # make sure the sysimg dir is present
78     utils.makedirs( SYSIMG_PATH )
79
80     log.write( "mounting root file system\n" )
81     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
82
83     log.write( "mounting vserver partition in root file system\n" )
84     utils.makedirs( SYSIMG_PATH + "/vservers" )
85     utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
86                                                      SYSIMG_PATH), log )
87
88     vars['ROOT_MOUNTED']= 1
89
90     # call getNodeFlavour
91     try:
92         node_flavour = BootAPI.call_api_function(vars, "GetNodeFlavour", (NODE_ID,) )
93         nodefamily = node_flavour['nodefamily']
94         extensions = node_flavour['extensions']
95         plain = node_flavour['plain']
96     except:
97         raise BootManagerException ("Could not call GetNodeFlavour - need PLCAPI-5.0")
98     
99     # the 'plain' option is for tests mostly
100     if plain:
101         download_suffix=".tar"
102         uncompress_option=""
103         log.write("Using plain bootstrapfs images\n")
104     else:
105         download_suffix=".tar.bz2"
106         uncompress_option="-j"
107         log.write("Using compressed bootstrapfs images\n")
108
109     log.write ("Using nodefamily=%s\n"%(nodefamily))
110     if not extensions:
111         log.write("Installing only core software\n")
112     else:
113         log.write("Requested extensions %r" % extensions)
114     
115     bootstrapfs_names = [ nodefamily ] + extensions
116
117     for name in bootstrapfs_names:
118         tarball = "bootstrapfs-%s%s"%(name,download_suffix)
119         source_file= "/boot/%s" % (tarball)
120         dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
121
122         # 30 is the connect timeout, 14400 is the max transfer time in
123         # seconds (4 hours)
124         log.write( "downloading %s\n" % source_file )
125         result= bs_request.DownloadFile( source_file, None, None,
126                                          1, 1, dest_file,
127                                          30, 14400)
128         if result:
129             log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
130             result= utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,uncompress_option), log )
131             log.write( "Done\n")
132             utils.removefile( dest_file )
133         else:
134             # the main tarball is required
135             if name == nodefamily:
136                 raise BootManagerException, "FATAL: Unable to download main tarball %s from server." % \
137                     source_file
138             # for extensions, just print a warning
139             else:
140                 log.write("WARNING: tarball for extension %s not found\n"%(name))
141
142     # copy resolv.conf from the base system into our temp dir
143     # so DNS lookups work correctly while we are chrooted
144     log.write( "Copying resolv.conf to temp dir\n" )
145     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
146
147     # Copy the boot server certificate(s) and GPG public key to
148     # /usr/boot in the temp dir.
149     log.write( "Copying boot server certificates and public key\n" )
150
151     if os.path.exists("/usr/boot"):
152         utils.makedirs(SYSIMG_PATH + "/usr")
153         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
154     elif os.path.exists("/usr/bootme"):
155         utils.makedirs(SYSIMG_PATH + "/usr/boot")
156         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
157         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
158                     SYSIMG_PATH + "/usr/boot/cacert.pem")
159         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
160         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
161         
162     # For backward compatibility
163     if os.path.exists("/usr/bootme"):
164         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
165         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
166
167     # Import the GPG key into the RPM database so that RPMS can be verified
168     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
169     utils.sysexec("gpg --homedir=/root --export --armor" \
170                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
171                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
172     utils.sysexec_chroot(SYSIMG_PATH, "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab")
173
174     return 1