Skip to content

Commit bcd3b48

Browse files
committed
auto enable the usb kmod module if a usb storage device is detected
Signed-off-by: Zen <[email protected]>
1 parent 1200315 commit bcd3b48

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

src/ugrd/fs/mounts.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "6.2.1"
2+
__version__ = "6.3.0"
33

44
from pathlib import Path
55
from typing import Union
@@ -535,9 +535,7 @@ def _resolve_dev(self, device_path) -> str:
535535
for device in self["_blkid_info"]:
536536
check_major, check_minor = _get_device_id(device)
537537
if (major, minor) == (check_major, check_minor):
538-
self.logger.info(
539-
"Resolved device: %s -> %s" % (device_path, colorize(device, "cyan"))
540-
)
538+
self.logger.info("Resolved device: %s -> %s" % (device_path, colorize(device, "cyan")))
541539
return device
542540
self.logger.critical("Failed to resolve device: %s" % colorize(device_path, "red", bold=True))
543541
self.logger.error("Blkid info: %s" % pretty_print(self["_blkid_info"]))
@@ -546,12 +544,13 @@ def _resolve_dev(self, device_path) -> str:
546544

547545

548546
def _resolve_device_mountpoint(self, device) -> str:
549-
""" Gets the mountpoint of a device based on the device path."""
547+
"""Gets the mountpoint of a device based on the device path."""
550548
for mountpoint, mount_info in self["_mounts"].items():
551549
if str(device) == mount_info["device"]:
552550
return mountpoint
553551
raise AutodetectError("Device mountpoint not found: %s" % device)
554552

553+
555554
def _resolve_overlay_lower_dir(self, mountpoint) -> str:
556555
for option in self["_mounts"][mountpoint]["options"]:
557556
if option.startswith("lowerdir="):
@@ -614,7 +613,7 @@ def _autodetect_mount(self, mountpoint) -> None:
614613
self.logger.error("Host mounts: %s" % pretty_print(self["_mounts"]))
615614
raise AutodetectError("auto_mount mountpoint not found in host mounts: %s" % mountpoint)
616615

617-
mount_device = _resolve_overlay_lower_device(self, mountpoint) # Just resolve the overlayfs device
616+
mount_device = _resolve_overlay_lower_device(self, mountpoint) # Just resolve the overlayfs device
618617

619618
if ":" in mount_device: # Handle bcachefs
620619
mount_device = mount_device.split(":")[0]
@@ -805,34 +804,49 @@ def export_mount_info(self) -> None:
805804

806805
def autodetect_mount_kmods(self, device) -> None:
807806
"""Autodetects the kernel modules for a block device."""
807+
if "/" not in str(device):
808+
device = f"/dev/{device}"
809+
808810
if device_kmods := resolve_blkdev_kmod(self, device):
809811
self.logger.info("Auto-enabling kernel modules for device: %s" % colorize(", ".join(device_kmods), "cyan"))
810812
self["_kmod_auto"] = device_kmods
811813

812814

813815
def resolve_blkdev_kmod(self, device) -> list[str]:
814816
"""Gets the kmod name for a block device."""
817+
kmods = []
815818
dev = Path(device)
816819
while dev.is_symlink():
817820
dev = dev.resolve()
821+
822+
if dev.is_block_device():
823+
major, minor = _get_device_id(device)
824+
sys_dev = str(Path(f"/sys/dev/block/{major}:{minor}").resolve())
825+
if "/usb" in sys_dev:
826+
if "ugrd.kmod.usb" not in self["modules"]:
827+
self.logger.info(
828+
"Auto-enabling %s for USB device: %s" % (colorize("ugrd.kmod.usb", bold=True), colorize(device, "cyan"))
829+
)
830+
self["modules"] = "ugrd.kmod.usb"
818831
device_name = dev.name
832+
819833
if device_name.startswith("dm-") or dev.parent.name == "mapper" or dev.parent.name.startswith("vg"):
820-
return ["dm_mod"]
834+
kmods.append("dm_mod")
821835
elif device_name.startswith("nvme"):
822-
return ["nvme"]
836+
kmods.append("nvme")
823837
elif device_name.startswith("vd"):
824-
return ["virtio_blk"]
838+
kmods.append("virtio_blk")
825839
elif device_name.startswith("sd"):
826-
return ["sd_mod"]
840+
kmods.append("sd_mod")
827841
elif device_name.startswith("mmcblk"):
828-
return ["mmc_block"]
842+
kmods.append("mmc_block")
829843
elif device_name.startswith("sr"):
830-
return ["sr_mod"]
844+
kmods.append("sr_mod")
831845
elif device_name.startswith("md"):
832-
return ["md_mod"]
846+
kmods.append("md_mod")
833847
else:
834848
self.logger.error(
835849
"[%s] Unable to determine kernel module for block device: %s"
836850
% (device_name, colorize(device, "red", bold=True))
837851
)
838-
return []
852+
return kmods

0 commit comments

Comments
 (0)