diff mbox series

[v4,5/5] package/initscripts: add service to load kernel modules at boot

Message ID 20221004110421.137795-6-angelo@amarulasolutions.com
State Superseded
Headers show
Series Configure default wifi through kconfig | expand

Commit Message

Angelo Compagnucci Oct. 4, 2022, 11:04 a.m. UTC
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-load.d/*.conf files.
The modules files matches the one used by systemd, which in turn has
a builtin mechanism to load a module at boot, therefore making systemv
init on par with systemd features.

Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
---
v2:
* Moved script to initscripts (Arnout)
* Moved script to S11modules, after S10[mu]dev (Andreas)
* Use /etc/modules-load.d/ to share the same setup with systemd (me)
v4:
* Select only .conf file (Peter)
* Better ouput: printing module name only when loaded successfully,
  print a FAIL when at least one module fails (Peter) 

 package/initscripts/init.d/S11modules | 59 +++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 package/initscripts/init.d/S11modules

Comments

Arnout Vandecappelle Oct. 7, 2023, 8:40 p.m. UTC | #1
On 04/10/2022 13:04, Angelo Compagnucci wrote:
> 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-load.d/*.conf files.
> The modules files matches the one used by systemd, which in turn has
> a builtin mechanism to load a module at boot, therefore making systemv
> init on par with systemd features.
> 
> Signed-off-by: Angelo Compagnucci <angelo@amarulasolutions.com>
> ---
> v2:
> * Moved script to initscripts (Arnout)
> * Moved script to S11modules, after S10[mu]dev (Andreas)
> * Use /etc/modules-load.d/ to share the same setup with systemd (me)
> v4:
> * Select only .conf file (Peter)
> * Better ouput: printing module name only when loaded successfully,
>    print a FAIL when at least one module fails (Peter)
> 
>   package/initscripts/init.d/S11modules | 59 +++++++++++++++++++++++++++
>   1 file changed, 59 insertions(+)
>   create mode 100644 package/initscripts/init.d/S11modules
> 
> diff --git a/package/initscripts/init.d/S11modules b/package/initscripts/init.d/S11modules
> new file mode 100644
> index 0000000000..dcc2e79135
> --- /dev/null
> +++ b/package/initscripts/init.d/S11modules
> @@ -0,0 +1,59 @@
> +#!/bin/sh
> +
> +MODULES_DIR="/etc/modules-load.d/"

  The script was failing check-package because there's no DAEMON variable. Of 
course, it's not appropriate to have a DAEMON variable in this init script 
because there's no daemon to start...

  On option would be to set a dummy DAEMON variable, but that would be rather 
pointless. So instead, I used the check-package exception comment to avoid it. 
However, that feature didn't exist yet for global checks (done in the after() 
function). So I added that feature to check-package.

  Because the change became a bit too big to apply directly, I resent. Of 
course, stupid me forgot to add versioning information :-(

> +
> +[ -z "$(ls -A ${MODULES_DIR} 2> /dev/null)" ] && exit 0
> +
> +load_unload() {
> +	for module_file in $(ls -1 ${MODULES_DIR}/*.conf); do

  This (and a few other lines) triggered a shellcheck warning. I fixed those. 
Could you test with the patch I sent?

  Regards,
  Arnout

> +		while read module args; do
> +
> +			case "$module" in
> +				""|"#"*) continue ;;
> +			esac
> +
> +			if [ "$1" = "load" ]; then
> +				modprobe -q ${module} ${args} >/dev/null && \
> +					printf '%s ' "$module" || RET='FAIL'
> +			else
> +				rmmod ${module} >/dev/null
> +			fi
> +
> +		done < ${module_file}
> +	done
> +
> +	RET='OK'
> +}
> +
> +start() {
> +	printf 'Starting modules: '
> +
> +	load_unload load
> +
> +	echo $RET
> +}
> +
> +stop() {
> +	printf 'Stopping modules: '
> +
> +	load_unload unload
> +
> +	echo $RET
> +}
> +
> +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 mbox series

Patch

diff --git a/package/initscripts/init.d/S11modules b/package/initscripts/init.d/S11modules
new file mode 100644
index 0000000000..dcc2e79135
--- /dev/null
+++ b/package/initscripts/init.d/S11modules
@@ -0,0 +1,59 @@ 
+#!/bin/sh
+
+MODULES_DIR="/etc/modules-load.d/"
+
+[ -z "$(ls -A ${MODULES_DIR} 2> /dev/null)" ] && exit 0
+
+load_unload() {
+	for module_file in $(ls -1 ${MODULES_DIR}/*.conf); do
+		while read module args; do
+
+			case "$module" in
+				""|"#"*) continue ;;
+			esac
+
+			if [ "$1" = "load" ]; then
+				modprobe -q ${module} ${args} >/dev/null && \
+					printf '%s ' "$module" || RET='FAIL'
+			else
+				rmmod ${module} >/dev/null
+			fi
+
+		done < ${module_file}
+	done
+
+	RET='OK'
+}
+
+start() {
+	printf 'Starting modules: '
+
+	load_unload load
+
+	echo $RET
+}
+
+stop() {
+	printf 'Stopping modules: '
+
+	load_unload unload
+
+	echo $RET
+}
+
+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