Skip to content

Commit

Permalink
ns-api: add load-kernel-modules
Browse files Browse the repository at this point in the history
New helper to load all configured kernel modules
and set their parameters.

If the script fails to set a parameter, it exists
with special code 99
  • Loading branch information
gsanchietti committed May 21, 2024
1 parent 104e54b commit e8a171c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/ns-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ define Package/ns-api/install
$(INSTALL_CONF) files/msmtp.keep $(1)/lib/upgrade/keep.d/msmtp
$(INSTALL_CONF) files/nat-helpers.keep $(1)/lib/upgrade/keep.d/nat-helpers
$(LN) /usr/bin/msmtp $(1)/usr/sbin/sendmail
$(INSTALL_BIN) ./files/load-kernel-modules $(1)/usr/sbin/load-kernel-modules
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_CONF) ./files/config $(1)/etc/config/ns-api
$(INSTALL_CONF) ./files/templates $(1)/etc/config/
Expand Down
30 changes: 30 additions & 0 deletions packages/ns-api/files/load-kernel-modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

# Load all kernel modules from /etc/modules.d/ns-nathelpers
# Example:
# nf_conntrack_sip sip_external_media=1 sip_direct_media=1

exit_code=0

# Load all module
for line in "$(grep -v '^#' /etc/modules.d/ns-nathelpers)"; do
module=$(echo $line | awk '{print $1}')
modprobe $module
for param in $(echo $line | awk '{for(i=2;i<=NF;++i)print $i}'); do
# Set parameter using /sys since modprobe doesn't support parameters
key=$(echo $param | cut -d= -f1)
value=$(echo $param | cut -d= -f2)
echo $value > /sys/module/$module/parameters/$key
if [ $? -ne 0 ]; then
exit_code=99
fi
done
done

# Special exit code 99 means that at least one parameter failed to be set
exit $exit_code
31 changes: 31 additions & 0 deletions packages/ns-api/files/load-modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-2.0-only
#

# Load all kernel modules from /etc/modules.d/ns-nathelpers
# Example:
# nf_conntrack_sip sip_external_media=1 sip_direct_media=1

exit_code=0

# Load all module
for line in "$(grep -v '^#' /etc/modules.d/ns-nathelpers)"; do
module=$(echo $line | awk '{print $1}')
modprobe $module
exit_code=$?
for param in $(echo $line | awk '{for(i=2;i<=NF;++i)print $i}'); do
# Set parameter using /sys since modprobe doesn't support parameters
key=$(echo $param | cut -d= -f1)
value=$(echo $param | cut -d= -f2)
echo $value > /sys/module/$module/parameters/$key
if [ $? -ne 0 ]; then
exit_code=99
fi
done
done

# Special exit code 99 means that at least one parameter failed to be set
exit $exit_code

0 comments on commit e8a171c

Please sign in to comment.