Florian Eckert 8767ba8698 base-files: add the possibility to set sysctl via uci
With this change, sysctl parameters can be set via the uci. The
following unamed section musst be set in '/etc/config/sysctl'.

config sysctl
	option enable '1'
	option section 'net.ipv4.fib_multipath_hash_policy'
	option value '2'

If the unamed section gets disabled, then the default value gets set,
that was saved during boot.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
2024-05-17 10:58:45 +02:00

79 lines
1.9 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org
START=11
apply_defaults() {
local mem="$(awk '/^MemTotal:/ {print $2}' /proc/meminfo)"
local min_free frag_low_thresh frag_high_thresh
if [ "$mem" -gt 65536 ]; then # 128M
min_free=16384
elif [ "$mem" -gt 32768 ]; then # 64M
min_free=8192
else
min_free=1024
frag_low_thresh=393216
frag_high_thresh=524288
fi
sysctl -qw vm.min_free_kbytes="$min_free"
[ "$frag_low_thresh" ] && sysctl -qw \
net.ipv4.ipfrag_low_thresh="$frag_low_thresh" \
net.ipv4.ipfrag_high_thresh="$frag_high_thresh" \
net.ipv6.ip6frag_low_thresh="$frag_low_thresh" \
net.ipv6.ip6frag_high_thresh="$frag_high_thresh" \
net.netfilter.nf_conntrack_frag6_low_thresh="$frag_low_thresh" \
net.netfilter.nf_conntrack_frag6_high_thresh="$frag_high_thresh"
# first set default, then all interfaces to avoid races with appearing interfaces
if [ -d /proc/sys/net/ipv6/conf ]; then
echo 0 > /proc/sys/net/ipv6/conf/default/accept_ra
for iface in /proc/sys/net/ipv6/conf/*/accept_ra; do
echo 0 > "$iface"
done
fi
}
handle_sysctl() {
local cfg="$1"
local sysctl="$2"
local enable section value
config_get section "$cfg" section
[ -z "$section" ] && return
# check if section should be set and if not reset to default value
config_get_bool enable "$cfg" enable '0'
if [ "$enable" = '0' ]; then
value="$(grep "$section =" /tmp/sysctl-default.conf)"
value="${value##*= }"
else
config_get value "$cfg" value
fi
[ -z "$value" ] && return
echo "${section}=${value}" >> "$sysctl"
}
apply_uci() {
mkdir -p /tmp/run/sysctl.d
rm -f /tmp/run/sysctl.d/01-uci.conf
config_load sysctl
config_foreach handle_sysctl sysctl "/tmp/run/sysctl.d/01-uci.conf"
}
start() {
apply_defaults
[ -f /tmp/sysctl-default.conf ] || sysctl -a 1>/tmp/sysctl-default.conf 2>/dev/null
apply_uci
for CONF in /etc/sysctl.d/*.conf /etc/sysctl.conf /tmp/run/sysctl.d/*.conf; do
[ -f "$CONF" ] && sysctl -e -p "$CONF" >&-
done
}