Created
March 26, 2021 22:01
-
-
Save raspi/3b251c6c6f6578208cb0ff812a941e3d to your computer and use it in GitHub Desktop.
List all loadable kernel modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import subprocess | |
import sys | |
from pathlib import Path | |
skipped_keys = [ | |
'sig_id', | |
'signer', | |
'sig_key', | |
'sig_hashalgo', | |
'signature', | |
'srcversion', | |
'vermagic', | |
'filename', | |
'license', | |
'author', | |
] | |
if __name__ == '__main__': | |
kversion: str = sys.argv[1] | |
modules: list = [] | |
for module in Path(f"/lib/modules/{kversion}").rglob("*.ko*"): | |
cmd = ["modinfo", module.absolute()] | |
sp = subprocess.run(cmd, capture_output=True) | |
obj: dict = {} | |
key: str = "" | |
for line in sp.stdout.decode('utf8').split("\n"): | |
if line.find("\t") == 0: | |
continue | |
if line.find(":") == -1: | |
continue | |
key, value = line.split(":", 1) | |
value = value.strip() | |
if value == "": | |
value = None | |
if value is None: | |
continue | |
if key in skipped_keys: | |
continue | |
if key in ['depends']: | |
if line.find(",") != -1: | |
value = value.split(",") | |
if key in ['intree', 'retpoline']: | |
if value == "Y": | |
value = True | |
else: | |
value = False | |
if key not in obj: | |
obj[key] = value | |
else: | |
if not isinstance(obj[key], list): | |
obj[key] = [obj[key]] | |
obj[key].append(value) | |
if 'parm' in obj: | |
parms = {} | |
if isinstance(obj['parm'], str): | |
obj['parm'] = [obj['parm']] | |
for i in obj['parm']: | |
k, v = i.split(":", 1) | |
v = v.strip() | |
parms[k] = v | |
obj['parm'] = parms | |
modules.append(obj) | |
print(json.dumps(modules, indent=" ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment