3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2007 The Trustees of Princeton University
8 # expected /proc/partitions format
16 from Exceptions import *
18 import BootServerRequest
24 Download core + extensions bootstrapfs tarballs and install on the hard drive
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
32 Sets the following variables:
33 TEMP_BOOTCD_PATH where the boot cd is remounted in the temp
35 ROOT_MOUNTED set to 1 when the the base logical volumes
39 log.write( "\n\nStep: Install: bootstrapfs tarball.\n" )
41 # make sure we have the variables we need
43 SYSIMG_PATH= vars["SYSIMG_PATH"]
45 raise ValueError, "SYSIMG_PATH"
47 PARTITIONS= vars["PARTITIONS"]
48 if PARTITIONS == None:
49 raise ValueError, "PARTITIONS"
51 NODE_ID= vars["NODE_ID"]
53 raise ValueError, "NODE_ID"
55 VERSION=vars['VERSION'] or 'unknown'
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
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 )
72 bs_request= BootServerRequest.BootServerRequest(vars)
74 log.write( "turning on swap space\n" )
75 utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
77 # make sure the sysimg dir is present
78 utils.makedirs( SYSIMG_PATH )
80 log.write( "mounting root file system\n" )
81 utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
83 fstype = 'ext3' if vars['virt']=='vs' else 'btrfs'
85 one_partition = vars['ONE_PARTITION']=='1'
87 if (not one_partition):
88 log.write( "mounting vserver partition in root file system (type %s)\n"%fstype )
89 utils.makedirs( SYSIMG_PATH + "/vservers" )
90 utils.sysexec( "mount -t %s %s %s/vservers" % \
91 (fstype, PARTITIONS["vservers"], SYSIMG_PATH), log )
93 if vars['virt']=='lxc':
94 # NOTE: btrfs quota is supported from version: >= btrfs-progs-0.20 (f18+)
95 # older versions will not recongize the 'quota' command.
96 log.write( "Enabling btrfs quota on %s/vservers\n"%SYSIMG_PATH )
97 utils.sysexec_noerr( "btrfs quota enable %s/vservers" % SYSIMG_PATH )
99 vars['ROOT_MOUNTED']= 1
101 # this is now retrieved in GetAndUpdateNodeDetails
102 nodefamily = vars['nodefamily']
103 extensions = vars['extensions']
104 # the 'plain' option is for tests mostly
105 plain = vars['plain']
107 download_suffix=".tar"
109 log.write("Using plain bootstrapfs images\n")
111 download_suffix=".tar.bz2"
112 uncompress_option="-j"
113 log.write("Using compressed bootstrapfs images\n")
115 log.write ("Using nodefamily=%s\n"%(nodefamily))
117 log.write("Installing only core software\n")
119 log.write("Requested extensions %r\n" % extensions)
121 bootstrapfs_names = [ nodefamily ] + extensions
123 for name in bootstrapfs_names:
124 tarball = "bootstrapfs-%s%s"%(name,download_suffix)
125 source_file= "/boot/%s" % (tarball)
126 dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
128 source_hash_file= "/boot/%s.sha1sum" % (tarball)
129 dest_hash_file= "%s/%s.sha1sum" % (SYSIMG_PATH, tarball)
132 log.write( "downloading %s\n" % source_file )
133 # 30 is the connect timeout, 14400 is the max transfer time in
135 result = bs_request.DownloadFile( source_file, None, None,
139 duration=int(time_end-time_beg)
140 log.write("Done downloading (%s seconds)\n"%duration)
142 # Download SHA1 checksum file
143 log.write( "downloading sha1sum for %s\n"%source_file)
144 result = bs_request.DownloadFile( source_hash_file, None, None,
145 1, 1, dest_hash_file,
148 log.write( "verifying sha1sum for %s\n"%source_file)
149 if not utils.check_file_hash(dest_file, dest_hash_file):
150 raise BootManagerException, "FATAL: SHA1 checksum does not match between %s and %s" % (source_file, source_hash_file)
154 log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
155 result = utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,uncompress_option), log )
157 duration=int(time_end-time_beg)
158 log.write( "Done extracting (%s seconds)\n"%duration)
159 utils.removefile( dest_file )
161 # the main tarball is required
162 if name == nodefamily:
163 raise BootManagerException, "FATAL: Unable to download main tarball %s from server." % \
165 # for extensions, just print a warning
167 log.write("WARNING: tarball for extension %s not found\n"%(name))
169 # copy resolv.conf from the base system into our temp dir
170 # so DNS lookups work correctly while we are chrooted
171 log.write( "Copying resolv.conf to temp dir\n" )
172 utils.sysexec( "cp /etc/resolv.conf %s/etc/" % SYSIMG_PATH, log )
174 # Copy the boot server certificate(s) and GPG public key to
175 # /usr/boot in the temp dir.
176 log.write( "Copying boot server certificates and public key\n" )
178 if os.path.exists("/usr/boot"):
179 utils.makedirs(SYSIMG_PATH + "/usr")
180 shutil.copytree("/usr/boot", SYSIMG_PATH + "/usr/boot")
181 elif os.path.exists("/usr/bootme"):
182 utils.makedirs(SYSIMG_PATH + "/usr/boot")
183 boot_server = file("/usr/bootme/BOOTSERVER").readline().strip()
184 shutil.copy("/usr/bootme/cacert/" + boot_server + "/cacert.pem",
185 SYSIMG_PATH + "/usr/boot/cacert.pem")
186 file(SYSIMG_PATH + "/usr/boot/boot_server", "w").write(boot_server)
187 shutil.copy("/usr/bootme/pubring.gpg", SYSIMG_PATH + "/usr/boot/pubring.gpg")
189 # For backward compatibility
190 if os.path.exists("/usr/bootme"):
191 utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
192 shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
194 # ONE_PARTITION => new distribution type
195 if (vars['ONE_PARTITION']!='1'):
196 # Import the GPG key into the RPM database so that RPMS can be verified
197 utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
198 utils.sysexec("gpg --homedir=/root --export --armor" \
199 " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
200 " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH), log)
201 utils.sysexec_chroot(SYSIMG_PATH, "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab", log)
203 # keep a log on the installed hdd
204 stamp=file(SYSIMG_PATH + "/bm-install.txt",'w')
205 now=time.strftime("%Y-%b-%d @ %H:%M %Z", time.gmtime())
206 stamp.write("Hard drive installed by BootManager %s\n"%VERSION)
207 stamp.write("Finished extraction of bootstrapfs on %s\n"%now)
208 stamp.write("Using nodefamily %s\n"%nodefamily)