patch from Marc F to optionally pull in extra kexec args from /kargs.txt
[bootmanager.git] / source / steps / ChainBootNode.py
index 9cc16b8..a83474a 100644 (file)
@@ -1,4 +1,5 @@
 import string
+import re
 
 from Exceptions import *
 import utils
@@ -101,16 +102,20 @@ def Run( vars, log ):
     ROOT_MOUNTED= 0
     vars['ROOT_MOUNTED']= 0
 
-    if BOOT_CD_VERSION[0] == 2:
-        log.write( "Unloading modules and chaining booting to new kernel.\n" )
-    else:
-        log.write( "Chaining booting to new kernel.\n" )
+    log.write( "Unloading modules and chain booting to new kernel.\n" )
 
     # further use of log after Upload will only output to screen
     log.Upload()
 
-    # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything to
-    # get kexec to work correctly
+    # regardless of whether kexec works or not, we need to stop trying to
+    # run anything
+    cancel_boot_flag= "/tmp/CANCEL_BOOT"
+    utils.sysexec( "touch %s" % cancel_boot_flag, log )
+
+    # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
+    # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
+    # there are a few buggy drivers that don't disable their hardware
+    # correctly unless they are first unloaded.
     
     utils.sysexec_noerr( "ifconfig eth0 down", log )
 
@@ -128,11 +133,81 @@ def Run( vars, log ):
         for line in modules:
             module= string.strip(line)
             if module != "":
+                log.write( "Unloading %s\n" % module )
                 utils.sysexec_noerr( "modprobe -r %s" % module, log )
+
+        modules.close()
     except IOError:
-        log.write( "Couldn't load /tmp/loadedmodules to unload, continuing.\n" )
-    
-    utils.sysexec( "kexec --force --initrd=/tmp/initrd " \
-                   "--append=ramdisk_size=8192 /tmp/kernel" )
+        log.write( "Couldn't read /tmp/loadedmodules, continuing.\n" )
+
+    try:
+        modules= file("/proc/modules", "r")
+
+        # Get usage count for USB
+        usb_usage = 0
+        for line in modules:
+            try:
+                # Module Size UsageCount UsedBy State LoadAddress
+                parts= string.split(line)
+
+                if parts[0] == "usb_storage":
+                    usb_usage += int(parts[2])
+            except IndexError, e:
+                log.write( "Couldn't parse /proc/modules, continuing.\n" )
+
+        modules.seek(0)
+
+        for line in modules:
+            try:
+                # Module Size UsageCount UsedBy State LoadAddress
+                parts= string.split(line)
+
+                # While we would like to remove all "unused" modules,
+                # you can't trust usage count, especially for things
+                # like network drivers or RAID array drivers. Just try
+                # and unload a few specific modules that we know cause
+                # problems during chain boot, such as USB host
+                # controller drivers (HCDs) (PL6577).
+                # if int(parts[2]) == 0:
+                if re.search('_hcd$', parts[0]):
+                    if usb_usage > 0:
+                        log.write( "NOT unloading %s since USB may be in use\n" % parts[0] )
+                    else:
+                        log.write( "Unloading %s\n" % parts[0] )
+                        utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
+            except IndexError, e:
+                log.write( "Couldn't parse /proc/modules, continuing.\n" )
+    except IOError:
+        log.write( "Couldn't read /proc/modules, continuing.\n" )
+
+
+    kargs = "ramdisk_size=8192"
+    try:
+        kargsfb = open("/kargs.txt","r")
+        moreargs = kargsfb.readline()
+        kargsfb.close()
+        moreargs = moreargs.strip()
+        log.write( 'Parsed in "%s" kexec args from /kargs.txt\n' % moreargs )
+        kargs = kargs + " " + moreargs
+    except IOError:
+        # /kargs.txt does not exist, which is fine. Just kexec with default
+        # kargs, which is ramdisk_size=8192
+        pass 
+
+    try:
+        utils.sysexec( 'kexec --force --initrd=/tmp/initrd ' \
+                       '--append="%s" /tmp/kernel' % kargs)
+    except BootManagerException, e:
+        # if kexec fails, we've shut the machine down to a point where nothing
+        # can run usefully anymore (network down, all modules unloaded, file
+        # systems unmounted. write out the error, and cancel the boot process
+
+        log.write( "\n\n" )
+        log.write( "-------------------------------------------------------\n" )
+        log.write( "kexec failed with the following error. Please report\n" )
+        log.write( "this problem to support@planet-lab.org.\n\n" )
+        log.write( str(e) + "\n\n" )
+        log.write( "The boot process has been canceled.\n" )
+        log.write( "-------------------------------------------------------\n\n" )
 
     return