Skip to contentSkip to conversation

When kmodpy is not available call kmod binary directly

PATCH In review Michal Suchanek 2 participants 3 messages Mar 21, 2025 – Jul 7 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 When kmodpy is not available call kmod binary directly

kmodpy is an unmaintained project. Python is not really good at backwards compatibility. With the upstream project not keeping up with python churn using the library is a maintenance burden.

nvmet does not use the python library in any substantial way, it only loads a module once. This can be easily accomplished without any library using the modprobe tool directly.

---
 nvmet/nvme.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
nvmet/nvme.py +14 -3
index 59efdb5b7c1b..397f103abcdb 100644
@@ -256,9 +256,20 @@ class Root(CFSNode):
             # Try the ctypes library included with the libkmod itself.
             try:
                 import kmod
-                kmod.Kmod().modprobe(modname)
-            except Exception as e:
-                pass
+
+                try:
+                    kmod.Kmod().modprobe(modname)
+                except Exception as e:
+                    pass
+            except ImportError:
+                # Try the binary specified in /proc
+                try:
+                    kmod = None
+                    with open('/proc/sys/kernel/modprobe', 'r') as f:
+                        kmod = f.read().rstrip()
+                    os.system(kmod + ' ' + modname)
+                except Exception as e:
+                    pass
 
     def _list_subsystems(self):
         self._check_self()
Signature · 1 line
2.47.1
#1 lore raw
Quoted 3 lines · Michal Suchanek

kmodpy is an unmaintained project. Python is not really good at backwards compatibility. With the upstream project not keeping up with python churn using the library is a maintenance burden.

Well, the original maintainer abandoned it, I did what I could to make kmod able to run with latest Cython v3

https://lore.kernel.org/target-devel/CAFL455=r4SuND7vL1Sr2vKzLTsgg-KL7GNzzJZPe2wDDH-NQyg@mail.gmail.com/

However I think that the fallback is ok and I will do the same on targetcli too, soon or later.

Maurizio

Quoted 36 lines

nvmet does not use the python library in any substantial way, it only loads a module once. This can be easily accomplished without any library using the modprobe tool directly.

---
 nvmet/nvme.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
nvmet/nvme.py +14 -3
index 59efdb5b7c1b..397f103abcdb 100644
@@ -256,9 +256,20 @@ class Root(CFSNode):
             # Try the ctypes library included with the libkmod itself.
             try:
                 import kmod
-                kmod.Kmod().modprobe(modname)
-            except Exception as e:
-                pass
+
+                try:
+                    kmod.Kmod().modprobe(modname)
+                except Exception as e:
+                    pass
+            except ImportError:
+                # Try the binary specified in /proc
+                try:
+                    kmod = None
+                    with open('/proc/sys/kernel/modprobe', 'r') as f:
+                        kmod = f.read().rstrip()
+                    os.system(kmod + ' ' + modname)
+                except Exception as e:
+                    pass
 
     def _list_subsystems(self):
         self._check_self()
#2 lore raw
In reply to Michal Suchanek
Quoted 7 lines · Michal Suchanek

kmodpy is an unmaintained project. Python is not really good at backwards compatibility. With the upstream project not keeping up with python churn using the library is a maintenance burden.

nvmet does not use the python library in any substantial way, it only loads a module once. This can be easily accomplished without any library using the modprobe tool directly.

Forgot

Quoted 35 lines
---
 nvmet/nvme.py | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
nvmet/nvme.py +14 -3
index 59efdb5b7c1b..397f103abcdb 100644
@@ -256,9 +256,20 @@ class Root(CFSNode):
             # Try the ctypes library included with the libkmod itself.
             try:
                 import kmod
-                kmod.Kmod().modprobe(modname)
-            except Exception as e:
-                pass
+
+                try:
+                    kmod.Kmod().modprobe(modname)
+                except Exception as e:
+                    pass
+            except ImportError:
+                # Try the binary specified in /proc
+                try:
+                    kmod = None
+                    with open('/proc/sys/kernel/modprobe', 'r') as f:
+                        kmod = f.read().rstrip()
+                    os.system(kmod + ' ' + modname)
+                except Exception as e:
+                    pass
 
     def _list_subsystems(self):
         self._check_self()
Signature · 1 line
2.47.1