Support non-rpm distributions
[bootmanager.git] / source / steps / InstallBootstrapFS.py
index 291ffa6..26fd0d3 100644 (file)
@@ -1,5 +1,5 @@
 #!/usr/bin/python
-
+#
 # Copyright (c) 2003 Intel Corporation
 # All rights reserved.
 #
@@ -7,9 +7,11 @@
 # All rights reserved.
 # expected /proc/partitions format
 
-import os, sys, string
+import os, string
 import popen2
 import shutil
+import traceback 
+import time
 
 from Exceptions import *
 import utils
@@ -19,15 +21,12 @@ import BootAPI
 
 def Run( vars, log ):
     """
-    Download enough files to run rpm and yum from a chroot in
-    the system image directory
+    Download core + extensions bootstrapfs tarballs and install on the hard drive
     
     Expect the following variables from the store:
     SYSIMG_PATH          the path where the system image will be mounted
     PARTITIONS           dictionary of generic part. types (root/swap)
                          and their associated devices.
-    SUPPORT_FILE_DIR     directory on the boot servers containing
-                         scripts and support files
     NODE_ID              the id of this machine
     
     Sets the following variables:
@@ -49,14 +48,12 @@ def Run( vars, log ):
         if PARTITIONS == None:
             raise ValueError, "PARTITIONS"
 
-        SUPPORT_FILE_DIR= vars["SUPPORT_FILE_DIR"]
-        if SUPPORT_FILE_DIR == None:
-            raise ValueError, "SUPPORT_FILE_DIR"
-
         NODE_ID= vars["NODE_ID"]
         if NODE_ID == "":
             raise ValueError, "NODE_ID"
 
+        VERSION=vars['VERSION'] or 'unknown'
+
     except KeyError, var:
         raise BootManagerException, "Missing variable in vars: %s\n" % var
     except ValueError, var:
@@ -72,7 +69,7 @@ def Run( vars, log ):
         log.write( "Missing partition in PARTITIONS: %s\n" % part )
         return 0   
 
-    bs_request= BootServerRequest.BootServerRequest()
+    bs_request= BootServerRequest.BootServerRequest(vars)
     
     log.write( "turning on swap space\n" )
     utils.sysexec( "swapon %s" % PARTITIONS["swap"], log )
@@ -83,92 +80,91 @@ def Run( vars, log ):
     log.write( "mounting root file system\n" )
     utils.sysexec( "mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH), log )
 
-    log.write( "mounting vserver partition in root file system\n" )
-    utils.makedirs( SYSIMG_PATH + "/vservers" )
-    utils.sysexec( "mount -t ext3 %s %s/vservers" % (PARTITIONS["vservers"],
-                                                     SYSIMG_PATH), log )
+    fstype = 'ext3' if vars['virt']=='vs' else 'btrfs'
 
-    vars['ROOT_MOUNTED']= 1
+    one_partition = vars['ONE_PARTITION']=='1'
 
-    # check which nodegroups we are part of (>=4.0)
-    utils.breakpoint("querying nodegroups for loading extensions")
-    try:
-        nodes = BootAPI.call_api_function(vars, "GetNodes", ([NODE_ID], ['nodegroup_ids']))
-        node = nodes[0]
-        nodegroups = BootAPI.call_api_function(vars, "GetNodeGroups", (node['nodegroup_ids'], ['groupname']))
-        nodegroupnames = [ nodegroup['groupname'].lower() for nodegroup in nodegroups ]
-
-    except:
-        log.write("WARNING : Failed to query nodegroups - installing only core software\n")
-        nodegroupnames = []
-        pass
-
-    # see also GetBootMedium in PLCAPI that does similar things
-    # figuring the default node family:
-    # (1) look at /etc/planetlab/nodefamily on the bootcd
-    # (2) if that fails, set to planetlab-i386
-    try:
-        (pldistro,arch) = file("/etc/planetlab/nodefamily").read().strip().split("-")
-    except:
-        (pldistro,arch) = ("planetlab","i386")
-
-    # scan nodegroupnames - temporary, as most of this nodegroup-based info 
-    # should be more adequately defined in the nodes data model
-    known_archs = [ 'i386', 'x86_64' ]
-    extensions = []
-    # (1) if groupname == arch, nodefamily becomes pldistro-groupname
-    # (2) else if groupname looks like pldistro-arch, it is taken as a nodefamily
-    # (3) otherwise groupname is taken as an extension
-    for nodegroupname in nodegroupnames:
-        if nodegroupname in known_archs:
-            arch = nodegroupname
-        else:
-            is_nodefamily = False
-            for known_arch in known_archs:
-                try:
-                    (api_pldistro,api_arch)=nodegroupname.split("-")
-                    # sanity check
-                    if api_arch != known_arch: raise Exception,"mismatch"
-                    (pldistro,arch) = (api_pldistro, api_arch)
-                    is_nodefamily = True
-                    break
-                except:
-                    pass
-            if not is_nodefamily:
-                extensions.append(nodegroupname)
-            
-    bootstrapfs_names = [ pldistro ] + extensions
+    if (not one_partition):
+        log.write( "mounting vserver partition in root file system (type %s)\n"%fstype )
+        utils.makedirs( SYSIMG_PATH + "/vservers" )
+        utils.sysexec( "mount -t %s %s %s/vservers" % \
+                           (fstype, PARTITIONS["vservers"], SYSIMG_PATH), log )
 
-    # download and extract support tarball for this step, which has
-    # everything we need to successfully run
+        if vars['virt']=='lxc':
+            # NOTE: btrfs quota is supported from version: >= btrfs-progs-0.20 (f18+)
+            #       older versions will not recongize the 'quota' command.
+            log.write( "Enabling btrfs quota on %s/vservers\n"%SYSIMG_PATH )
+            utils.sysexec_noerr( "btrfs quota enable %s/vservers" % SYSIMG_PATH )
 
-    # we first try to find a tarball, if it is not found we use yum instead
-    yum_extensions = []
-    # download and extract support tarball for this step, 
-    for bootstrapfs_name in bootstrapfs_names:
-        tarball = "bootstrapfs-%s-%s.tar.bz2"%(bootstrapfs_name,arch)
-        source_file= "%s/%s" % (SUPPORT_FILE_DIR,tarball)
+    vars['ROOT_MOUNTED']= 1
+
+    # this is now retrieved in GetAndUpdateNodeDetails
+    nodefamily = vars['nodefamily']
+    extensions = vars['extensions']
+    # the 'plain' option is for tests mostly
+    plain = vars['plain']
+    if plain:
+        download_suffix=".tar"
+        uncompress_option=""
+        log.write("Using plain bootstrapfs images\n")
+    else:
+        download_suffix=".tar.bz2"
+        uncompress_option="-j"
+        log.write("Using compressed bootstrapfs images\n")
+
+    log.write ("Using nodefamily=%s\n"%(nodefamily))
+    if not extensions:
+        log.write("Installing only core software\n")
+    else:
+        log.write("Requested extensions %r\n" % extensions)
+    
+    bootstrapfs_names = [ nodefamily ] + extensions
+
+    for name in bootstrapfs_names:
+        tarball = "bootstrapfs-%s%s"%(name,download_suffix)
+        source_file= "/boot/%s" % (tarball)
         dest_file= "%s/%s" % (SYSIMG_PATH, tarball)
 
+        source_hash_file= "/boot/%s.sha1sum" % (tarball)
+        dest_hash_file= "%s/%s.sha1sum" % (SYSIMG_PATH, tarball)
+
+        time_beg=time.time()
+        log.write( "downloading %s\n" % source_file )
         # 30 is the connect timeout, 14400 is the max transfer time in
         # seconds (4 hours)
-        log.write( "downloading %s\n" % source_file )
-        result= bs_request.DownloadFile( source_file, None, None,
+        result = bs_request.DownloadFile( source_file, None, None,
                                          1, 1, dest_file,
                                          30, 14400)
+        time_end=time.time()
+        duration=int(time_end-time_beg)
+        log.write("Done downloading (%s seconds)\n"%duration)
         if result:
+            # Download SHA1 checksum file
+            log.write( "downloading sha1sum for %s\n"%source_file)
+            result = bs_request.DownloadFile( source_hash_file, None, None,
+                                         1, 1, dest_hash_file,
+                                         30, 14400)
+            log.write( "verifying sha1sum for %s\n"%source_file)
+            if not utils.check_file_hash(dest_file, dest_hash_file):
+                raise BootManagerException, "FATAL: SHA1 checksum does not match between %s and %s" % (source_file, source_hash_file)
+                
+            
+            time_beg=time.time()
             log.write( "extracting %s in %s\n" % (dest_file,SYSIMG_PATH) )
-            result= utils.sysexec( "tar -C %s -xpjf %s" % (SYSIMG_PATH,dest_file), log )
-            log.write( "Done\n")
+            result = utils.sysexec( "tar -C %s -xpf %s %s" % (SYSIMG_PATH,dest_file,uncompress_option), log )
+            time_end=time.time()
+            duration=int(time_end-time_beg)
+            log.write( "Done extracting (%s seconds)\n"%duration)
             utils.removefile( dest_file )
         else:
             # the main tarball is required
-            if bootstrapfs_name == pldistro:
-                raise BootManagerException, "Unable to download main tarball %s from server." % \
+            if name == nodefamily:
+                raise BootManagerException, "FATAL: Unable to download main tarball %s from server." % \
                     source_file
+            # for extensions, just print a warning
             else:
-                log.write("tarball for %s-%s not found, scheduling a yum attempt\n"%(bootstrapfs_name,arch))
-                yum_extensions.append(bootstrapfs_name)
+                log.write("WARNING: tarball for extension %s not found\n"%(name))
 
     # copy resolv.conf from the base system into our temp dir
     # so DNS lookups work correctly while we are chrooted
@@ -195,44 +191,21 @@ def Run( vars, log ):
         utils.makedirs(SYSIMG_PATH + "/mnt/cdrom")
         shutil.copytree("/usr/bootme", SYSIMG_PATH + "/mnt/cdrom/bootme")
 
-    # Import the GPG key into the RPM database so that RPMS can be verified
-    utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
-    utils.sysexec("gpg --homedir=/root --export --armor" \
-                  " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
-                  " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH))
-    utils.sysexec("chroot %s rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % \
-                  SYSIMG_PATH)
-
-    # yum-based extensions:
-    # before we can use yum, yum.conf needs to get installed
-    # xxx this should probably depend on the node's nodegroup, at least among alpha, beta ..
-    # however there does not seem to be a clear interface for that in yum.conf.php
-    # so let's keep it simple for the bootstrap phase, as yum.conf will get overwritten anyway
-    if yum_extensions:
-        getDict = {'gpgcheck':1,'arch':arch}
-        url="PlanetLabConf/yum.conf.php"
-        dest="%s/etc/yum.conf"%SYSIMG_PATH
-        log.write("downloading bootstrap yum.conf\n")
-        yumconf=bs_request.DownloadFile (url,getDict,None,
-                                         1, 1, dest)
-        if not yumconf:
-            log.write("Cannot fetch %s from %s - aborting yum extensions"%(dest,url))
-            # failures here should not stop the install process
-            return 1
-
-        # yum also needs /proc to be mounted 
-        # do it here so as to not break the tarballs-only case
-        cmd = "mount -t proc none %s/proc" % SYSIMG_PATH
-        utils.sysexec( cmd, log )
-        # we now just need to yum groupinstall everything
-        for extension in yum_extensions:
-            yum_command="yum groupinstall extension%s"%extension
-            utils.breakpoint ("before chroot %s %s"%(SYSIMG_PATH,yum_command))
-            log.write("Attempting to install extension %s through yum\n"%extension)
-            utils.sysexec_noerr("chroot %s %s" % (SYSIMG_PATH,yum_command))
-            # xxx how to check that this completed correctly ?
-        # let's cleanup
-        utils.sysexec_noerr( "umount %s/proc" % SYSIMG_PATH, log )
-        utils.breakpoint ("Done with yum extensions")
+    # ONE_PARTITION => new distribution type
+    if (vars['ONE_PARTITION']!='1'):
+        # Import the GPG key into the RPM database so that RPMS can be verified
+        utils.makedirs(SYSIMG_PATH + "/etc/pki/rpm-gpg")
+        utils.sysexec("gpg --homedir=/root --export --armor" \
+                      " --no-default-keyring --keyring %s/usr/boot/pubring.gpg" \
+                      " >%s/etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab" % (SYSIMG_PATH, SYSIMG_PATH), log)
+        utils.sysexec_chroot(SYSIMG_PATH, "rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-planetlab", log)
+
+    # keep a log on the installed hdd
+    stamp=file(SYSIMG_PATH + "/bm-install.txt",'w')
+    now=time.strftime("%Y-%b-%d @ %H:%M %Z", time.gmtime())
+    stamp.write("Hard drive installed by BootManager %s\n"%VERSION)
+    stamp.write("Finished extraction of bootstrapfs on %s\n"%now)
+    stamp.write("Using nodefamily %s\n"%nodefamily)
+    stamp.close()
 
     return 1