Skip to content

Instantly share code, notes, and snippets.

@raspi
Created March 26, 2021 22:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspi/3b251c6c6f6578208cb0ff812a941e3d to your computer and use it in GitHub Desktop.
Save raspi/3b251c6c6f6578208cb0ff812a941e3d to your computer and use it in GitHub Desktop.
List all loadable kernel modules
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