[Buildroot] [PATCH 4/4] package/busybox: add service to load kernel modules at boot

Angelo Compagnucci angelo at amarulasolutions.com
Mon Jun 20 21:40:58 UTC 2022


In cases where no hotplug is available (by choice or by the lack of a
proper hotplug method for a device), this service can be used to load
kernel module drivers by reading the /etc/modules file.

Signed-off-by: Angelo Compagnucci <angelo at amarulasolutions.com>
---
 package/busybox/S02modules | 56 ++++++++++++++++++++++++++++++++++++++
 package/busybox/busybox.mk |  2 ++
 2 files changed, 58 insertions(+)
 create mode 100644 package/busybox/S02modules

diff --git a/package/busybox/S02modules b/package/busybox/S02modules
new file mode 100644
index 0000000000..f25712e1ca
--- /dev/null
+++ b/package/busybox/S02modules
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+MODULES="modules"
+
+load_unload() {
+	[ ! -f /etc/${MODULES} ] && echo ' OK' && exit 0
+
+	while read module args; do
+
+		case "$module" in
+			""|"#"*) continue ;;
+		esac
+
+		if [ "$1" = "load" ]; then
+			modprobe -q ${module} ${args} >/dev/null && \
+				printf ' %s success,' "$module" ||
+				printf ' %s failed,' "$module"
+		else
+			rmmod ${module} >/dev/null
+		fi
+
+	done < /etc/${MODULES}
+}
+
+start() {
+	printf 'Starting %s:' "$MODULES"
+
+	load_unload load
+
+	echo ' OK'
+}
+
+stop() {
+	printf 'Stopping %s: ' "$MODULES"
+
+	load_unload unload
+
+	echo 'OK'
+}
+
+restart() {
+	stop
+	sleep 1
+	start
+}
+
+case "$1" in
+	start|stop|restart)
+		"$1";;
+	reload)
+		# Restart, since there is no true "reload" feature.
+		restart;;
+	*)
+		echo "Usage: $0 {start|stop|restart|reload}"
+		exit 1
+esac
diff --git a/package/busybox/busybox.mk b/package/busybox/busybox.mk
index 3e49de0a84..40490e866a 100644
--- a/package/busybox/busybox.mk
+++ b/package/busybox/busybox.mk
@@ -382,6 +382,8 @@ define BUSYBOX_INSTALL_TARGET_CMDS
 	$(BUSYBOX_INSTALL_UDHCPC_SCRIPT)
 	$(BUSYBOX_INSTALL_ZCIP_SCRIPT)
 	$(BUSYBOX_INSTALL_MDEV_CONF)
+	$(INSTALL) -m 0755 -D package/busybox/S02modules \
+			$(TARGET_DIR)/etc/init.d/S02modules;
 endef
 
 # Install the sysvinit scripts, for the moment, but not those that already
-- 
2.25.1




More information about the buildroot mailing list