+# check if a specific kernel module is loaded
+def module_is_loaded(module):
+ global loaded_modules
+
+ if loaded_modules:
+ return module in loaded_modules
+
+ # Get list of sysfs modules (both built-in and dynamically loaded)
+ sysfs_path = '/sys/module/'
+
+ # Get the list of directories in sysfs_path
+ sysfs_mods = [m for m in os.listdir(sysfs_path)
+ if os.path.isdir(os.path.join(sysfs_path, m))]
+
+ # special case for vfio_pci (module is named vfio-pci,
+ # but its .ko is named vfio_pci)
+ sysfs_mods = [a if a != 'vfio_pci' else 'vfio-pci' for a in sysfs_mods]
+
+ loaded_modules = sysfs_mods
+
+ return module in sysfs_mods
+