7354893d480b7a511d23940a550f9efbad65bd9b
[bootmanager.git] / source / steps / InstallBootstrapFS.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2007 The Trustees of Princeton University
7 # All rights reserved.
8 # expected /proc/partitions format
9
10 import os, string
11 import popen2
12 import shutil
13 import traceback 
14 import time
15
16 from Exceptions import *
17 import utils
18 import BootServerRequest
19 import BootAPI
20
21
22 def Run( vars, log ):
23     """
24     Download core + extensions bootstrapfs tarballs and install on the hard drive
25     
26     Expect the following variables from the store:
27     SYSIMG_PATH          the path where the system image will be mounted
28     PARTITIONS           dictionary of generic part. types (root/swap)
29                          and their associated devices.
30     NODE_ID              the id of this machine
31     
32     Sets the following variables:
33     TEMP_BOOTCD_PATH     where the boot cd is remounted in the temp
34                          path
35     ROOT_MOUNTED         set to 1 when the the base logical volumes
36                          are mounted.
37     """
38
39     log.write( "\n\nStep: Install: bootstrapfs tarball.\n" )
40
41     # make sure we have the variables we need
42     try:
43         SYSIMG_PATH= vars["SYSIMG_PATH"]
44         if SYSIMG_PATH == "":
45             raise ValueError, "SYSIMG_PATH"
46
47         PARTITIONS= vars["PARTITIONS"]
48         if PARTITIONS == None:
49             raise ValueError, "PARTITIONS"
50
51         NODE_ID= vars["NODE_ID"]
52         if NODE_ID == "":
53             raise ValueError, "NODE_ID"
54
55         VERSION=vars['VERSION'] or 'unknown'
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     fstype = 'ext3' if vars['virt']=='vs' else 'btrfs'
84     log.write( "mounting vserver partition in root file system (type %s)\n"%fstype )
85     utils.makedirs( SYSIMG_PATH + "/vservers" )
86     utils.sysexec( "mount -t %s %s %s/vservers" % \
87                        (fstype, PARTITIONS["vservers"], SYSIMG_PATH), log )
88
89     if vars['virt']=='lxc':
90         # NOTE: btrfs quota is supported from version: >= btrfs-progs-0.20 (f18+)
91         #       older versions will not recongize the 'quota' command.
92         log.write( "Enabling btrfs quota on %s/vservers\n"%SYSIMG_PATH )
93         utils.sysexec_noerr( "btrfs quota enable %s/vservers" % SYSIMG_PATH )
94
95     vars['ROOT_MOUNTED']= 1
96
97     # this is now retrieved in GetAndUpdateNodeDetails
98     nodefamily = vars['nodefamily']
99     extensions = vars['extensions']
100     # the 'plain' option is for tests mostly
101     plain = vars['plain']
102     if plain:
103         download_suffix=".tar"
104         uncompress_option=""
105         log.write("Using plain bootstrapfs images\n")
106     else:
107         download_suffix=".tar.bz2"
108         uncompress_option="-j"
109         log.write("Using compressed bootstrapfs images\n")
110
111     log.write ("Using nodefamily=%s\n"%(nodefamily))
112     if not extensions:
113         log.write("Installing only core software\n")
114     else:
115         log.write("Requested extensions %r\n" % extensions)
116     
117     bootstrapfs_names = [ nodefamily ] + extensions
118
119     for name in bootstrapfs_names:
120         tarball = "bootstrapfs-%s%s"%(name,download_suffix)
121         source_file= "/boot/%s" % (tarball)
122         dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
123
124         source_hash_file= "/boot/%s.sha1sum" % (tarball)
125         dest_hash_file= "%s/%s.sha1sum" % (SYSIMG_PATH, tarball)
126
127         time_beg=time.time()
128         log.write( "downloading %s\n" % source_file )
129         # 30 is the connect timeout, 14400 is the max transfer time in
130         # seconds (4 hours)
131         result = bs_request.DownloadFile( source_file, None, None,
132                                          1, 1, dest_file,
133                                          30, 14400)
134         time_end=time.time()
135         duration=int(time_end-time_beg)
136         log.write("Done downloading (%s seconds)\n"%duration)
137         if result:
138             # Download SHA1 checksum file
139             log.write( "downloading sha1sum for %s\n"%source_file)
140             result = bs_request.DownloadFile( source_hash_file, None, None,
141                                          1, 1, dest_hash_file,
142                                          30, 14400)
143  
144             log.write( "verifying sha1sum for %s\n"%source_file)
145             if not utils.check_file_hash(dest_file, dest_hash_file):
146                 raise BootManagerException, "FATAL: SHA1 checksum does not match between %s and %s" % (source_file, source_hash_file)
147                 
148             
149             time_beg=time.time()
150             log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
151             result = utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,uncompress_option), log )
152             time_end=time.time()
153             duration=int(time_end-time_beg)
154             log.write( "Done extracting (%s seconds)\n"%duration)
155             utils.removefile( dest_file )
156         else:
157             # the main tarball is required
158             if name == nodefamily:
159                 raise BootManagerException, "FATAL: Unable to download main tarball %s from server." % \
160                     source_file
161             # for extensions, just print a warning
162             else:
163                 log.write("WARNING: tarball for extension %s not found\n"%(name))
164
165     # copy resolv.conf from the base system into our temp dir
166     # so DNS lookups work correctly while we are chrooted
167     log.write( "Copying resolv.conf to temp dir\n" )
168     utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
169
170     # Copy the boot server certificate(s) and GPG public key to
171     # /usr/boot in the temp dir.
172     log.write( "Copying boot server certificates and public key\n" )
173
174     if os.path.exists("/usr/boot"):
175         utils.makedirs(SYSIMG_PATH + "/usr")
176         shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
177     elif os.path.exists("/usr/bootme"):
178         utils.makedirs(SYSIMG_PATH + "/usr/boot")
179         boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
180         shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
181                     SYSIMG_PATH + "/usr/boot/cacert.pem")
182         file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
183         shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
184         
185     # For backward compatibility
186     if os.path.exists("/usr/bootme"):
187         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
188         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
189
190     # Import the GPG key into the RPM database so that RPMS can be verified
191     utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
192     utils.sysexec("gpg --homedir=/root --export --armor" \
193                   " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
194                   " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH), log)
195     utils.sysexec_chroot(SYSIMG_PATH, "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab", log)
196
197     # keep a log on the installed hdd
198     stamp=file(SYSIMG_PATH + "/bm-install.txt",'w')
199     now=time.strftime("%Y-%b-%d @ %H:%M %Z", time.gmtime())
200     stamp.write("Hard drive installed by BootManager %s\n"%VERSION)
201     stamp.write("Finished extraction of bootstrapfs on %s\n"%now)
202     stamp.write("Using nodefamily %s\n"%nodefamily)
203     stamp.close()
204
205     return 1