more harmless space-related matters
[bootcd.git] / initscripts / pl_hwinit
index 80f2849..16763c0 100755 (executable)
@@ -1,21 +1,24 @@
 #!/usr/bin/python
 
+# xxx could use a port to python3..
+
 import sys
 import pypci
 import pypcimap
 import os
 import time
 
-loadedmodules = None
-
 def now():
-    format="%H:%M:%S(%Z)"
-    return time.strftime(format,time.localtime())
+    format = "%H:%M:%S(%Z)"
+    return time.strftime(format, time.localtime())
+
+def verbose_message(single):
+    print now(), single
 
-def modprobe(module, args = ""):
-    ret = os.system("/sbin/modprobe %s %s" % (module, args))
+def modprobe(module, summary_file, args = ""):
+    ret = os.system("/sbin/modprobe {} {}".format(module, args))
     if os.WEXITSTATUS(ret) == 0:
-        globals()['loadedmodules'].write("%s\n" % module)
+        summary_file.write("{}\n".format(module))
         return True
     else:
         return False
@@ -29,56 +32,71 @@ def main(argv):
     if os.path.exists(kernel):
         path = kernel
     else:
-        path = "/lib/modules/%s/modules.pcimap" % kernel
+        path = "/lib/modules/{}/modules.pcimap".format(kernel)
+
+    blacklisted_modules = []
+    blacklists = os.listdir("/etc/modprobe.d")
+    for blacklist in blacklists:
+        blf = "/etc/modprobe.d/{}".format(blacklist)
+        if os.path.exists(blf):
+            f = open(blf)
+            for i in f.readlines():
+                if i.startswith("blacklist"):
+                    blacklisted_modules.append(i.split()[1])
+    # unify the list
+    blacklisted_modules = list(set(blacklisted_modules))
 
     pcimap = pypcimap.PCIMap(path)
-    print now(),"pl_hwinit: loading applicable modules"
+    verbose_message("pl_hwinit: loading applicable modules")
     devices = pypci.get_devices()
     storage_devices = 0
     network_devices = 0
     missing = []
-    globals()['loadedmodules'] = file('/tmp/loadedmodules', 'w')
-    for slot in sorted(devices.keys()):
-        dev = devices[slot]
-        modules = pcimap.get(dev)
-        base = (dev[4] & 0xff0000) >> 16
-        if len(modules) == 0:
-            if base == 0x01 or base == 0x02:
-                # storage or network device, in that order
-                missing.append((slot, dev))
-        else:
-            if base == 0x01:
-                storage_devices += 1
-            elif base == 0x02:
-                network_devices += 1
+    with open('/tmp/loadedmodules', 'w') as loadedmodules:
+        for slot in sorted(devices.keys()):
+            dev = devices[slot]
+            modules = pcimap.get(dev)
+            base = (dev[4] & 0xff0000) >> 16
+            if len(modules) == 0:
+                if base == 0x01 or base == 0x02:
+                    # storage or network device, in that order
+                    missing.append((slot, dev))
+            else:
+                if base == 0x01:
+                    storage_devices += 1
+                elif base == 0x02:
+                    network_devices += 1
 
-            # FIXME: This needs improved logic in the case of multiple matching modules
-            for module in modules:
-                print now(),"pl_hwinit: found and loading module %s (%s)" % (module, slot)
-                modprobe(module)
+                # FIXME: This needs improved logic in the case of multiple matching modules
+                for module in modules:
+                    if module not in blacklisted_modules:
+                        verbose_message("pl_hwinit: found and loading module {} (%s)" % (module, slot))
+                        modprobe(module, loadedmodules)
 
-    if network_devices == 0:
-        print now(),"pl_hwinit: no supported network devices found!"
-        print now(),"pl_hwinit: the following devices were found, but have no driver:"
-        print now(),"pl_hwinit: ", "\npl_hwinit: ".join(missing)
+        if network_devices == 0:
+            verbose_message("pl_hwinit: no supported network devices found!")
+            verbose_message("pl_hwinit: the following devices were found, but have no driver:")
+            lines = [ "{} x {}".format(slot, dev) for slot, dev in missing ]
+            for line in lines:
+                verbose_message("pl_hwinit: missing " + line)
 
-    # XXX: could check for storage devices too, but older kernels have a lot of that built-in
+        # XXX: could check for storage devices too, but older kernels have a lot of that built-in
 
-    # sd_mod won't get loaded automatically
-    print now(),"pl_hwinit: loading sd_mod"
-    modprobe("sd_mod")
+        # sd_mod won't get loaded automatically
+        verbose_message("pl_hwinit: loading sd_mod")
+        modprobe("sd_mod", loadedmodules)
 
-    # load usb_storage to support node conf files on flash disks
-    print now(),"pl_hwinit: loading usb_storage"
-    modprobe("usb_storage")
+        # load usb_storage to support node conf files on flash disks
+        verbose_message("pl_hwinit: loading usb_storage")
+        modprobe("usb_storage", loadedmodules)
 
-    print now(),"pl_hwinit: loading floppy device driver"
-    modprobe("floppy","floppy=0,allowed_drive_mask")
+        verbose_message("pl_hwinit: loading floppy device driver")
+        modprobe("floppy", loadedmodules, "floppy=0,allowed_drive_mask")
 
     # always wait a bit between loading the usb drivers, and checking /sys/
     # for usb devices (this isn't necessarily for waiting for mass storage files,
     # that is done below)
-    print now(),"pl_hwinit: waiting for usb system to initialize."
+    verbose_message("pl_hwinit: waiting for usb system to initialize.")
     time.sleep(10)
 
     # sometimes, flash devices take a while to initialize. in fact, the kernel
@@ -88,7 +106,7 @@ def main(argv):
 
     # the way this will be done is to look for files in /sys/devices that are named
     # 'bInterfaceClass', these will be a list of the usb devices on the system, and
-    # their primary usb device interface class ids. The base directory these files 
+    # their primary usb device interface class ids. The base directory these files
     # exist in will be the full path to the /sys/device entry for that device.
     # for each mass storage devices (they have an interface class value of 08),
     # we wait for a new symbolic link named 'driver' to exist in that directory,
@@ -119,15 +137,15 @@ def main(argv):
     os.path.walk("/sys/devices", filter_and_add, wait_dev_list)
 
     if len(wait_dev_list) > 0:
-        print now(),"pl_hwinit: found USB mass storage device(s). Attempting to wait"
-        print now(),"pl_hwinit: up to %d seconds for them to come online." % MAX_USB_WAIT_TIME
+        verbose_message("pl_hwinit: found USB mass storage device(s). Attempting to wait")
+        verbose_message("pl_hwinit: up to %d seconds for them to come online." % MAX_USB_WAIT_TIME)
 
         total_wait_time = 0
         success = False
-        while total_wait_time < MAX_USB_WAIT_TIME:    
+        while total_wait_time < MAX_USB_WAIT_TIME:
             total_wait_time += PER_CHECK_USB_WAIT_TIME
 
-            print now(),"pl_hwinit: waiting %d seconds." % PER_CHECK_USB_WAIT_TIME
+            verbose_message("pl_hwinit: waiting {} seconds.".format(PER_CHECK_USB_WAIT_TIME))
             time.sleep(PER_CHECK_USB_WAIT_TIME)
 
             all_devices_online = True
@@ -137,17 +155,17 @@ def main(argv):
 
             if all_devices_online:
                 success = True
-                print now(),"pl_hwinit: looks like the devices are now online."
+                verbose_message("pl_hwinit: looks like the devices are now online.")
                 break
             else:
-                print now(),"pl_hwinit: not all devices online yet, waiting..."
+                verbose_message("pl_hwinit: not all devices online yet, waiting...")
 
         if success:
-            print now(),"pl_hwinit: Succesfully waited for USB mass storage devices"
-            print now(),"pl_hwinit: to come online."
+            verbose_message("pl_hwinit: Succesfully waited for USB mass storage devices")
+            verbose_message("pl_hwinit: to come online.")
         else:
-            print now(),"pl_hwinit: One or more USB mass storage devices did not"
-            print now(),"pl_hwinit: initialize in time. Continuing anyway."
+            verbose_message("pl_hwinit: One or more USB mass storage devices did not")
+            verbose_message("pl_hwinit: initialize in time. Continuing anyway.")
 
 if __name__ == "__main__":
     main(sys.argv[1:])