389. Modules and sysctl
This page is for anyone who needs a driver the machine does not load on its own, wants to pass a driver an option, or wants to change a kernel setting. It covers the two ways of changing the running kernel without rebuilding it: loading kernel modules (drivers and other kernel parts kept in separate files) and passing them options, and setting kernel parameters through sysctl. It says which files KDOS reads for each at boot, how to make a change for this boot only or for every boot, and how to find out why a driver did not load. Building a different kernel is in The kernel. Every command on this page runs on a running KDOS machine.
Installation
- linux installs the kernel and its modules under
/lib/modules/<version>. Which drivers are modules and which are built in is set by the port’skdos.config. - kmod installs
kmod, withmodprobe,insmod,rmmod,lsmod,modinfoanddepmodas links to it, and the manual pagesmodprobe(8)andmodprobe.d(5). - eudev loads the driver for each device it sees, through libkmod.
- procps-ng installs
sysctland the manual pagessysctl(8)andsysctl.conf(5). - The base filesystem supplies the boot scripts that apply both at boot,
01_udev.sh,02_modules.shand10_sysctl.sh, and the files they read. - Ports add module configuration: hwdata installs
/usr/lib/modprobe.d/dist-blacklist.conf, stlink installs/etc/modprobe.d/stlink_v1.conf, and ndctl installs upstream’s/etc/modprobe.d/nvdimm-security.conf, which loads the NVDIMM keys beforelibnvdimm. - The installer, and kdos-settingsd on a running system when the time zone changes, write
/etc/modprobe.d/kdos-regdom.conf, the Wi-Fi country.
Every service involved is enabled on every image. On the live medium a file added under /etc is kept only when a persistence store exists (kdos persist; see Live persistence).
Configuration
/etc/modules-load.d/
02_modules.sh loads every module named in /etc/modules-load.d/*.conf, one name per line, # starting a comment. It reads no other directory: a file under /usr/lib/modules-load.d or /run/modules-load.d is not read. The script:
# Load kernel modules listed in /etc/modules-load.d/*.conf.
# Runs after udev so module aliases are resolvable.
CONF_DIR="/etc/modules-load.d"
[ -d "$CONF_DIR" ] || exit 0
case "$1" in
start)
echo "[KDOS] Loading kernel modules from $CONF_DIR..."
for f in "$CONF_DIR"/*.conf; do
[ -r "$f" ] || continue
while IFS= read -r mod; do
# strip comments and whitespace
mod="${mod%%#*}"
mod="${mod//[[:space:]]/}"
[ -z "$mod" ] && continue
# modprobe's own message is the reason: absent hardware,
# a misspelt name, a module this kernel does not carry
# and a refused option all fail here, and only the
# message tells them apart.
if _err=$(modprobe "$mod" 2>&1); then
echo "[KDOS] loaded: $mod"
else
_err="${_err%%$'\n'*}"
echo "[KDOS] skip: $mod (${_err:-modprobe failed})"
fi
done < "$f"
done
;;
stop|status)
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
The four files the image ships, each with its reason:
# Kernel modules for the container layer (podman + distrobox).
# tun: pasta, podman's rootless network, opens /dev/net/tun for its tap
# device, and so does the standalone slirp4netns.
# overlay: the kernel's overlayfs. kdos-packd composes every pack box with it,
# and so does rootless podman on an installed system, where
# kdos-appbox gives the user a storage.conf without a
# mount_program. /etc/containers/storage.conf pins fuse-overlayfs
# for every other store, which the live ISO needs: $HOME there is
# itself on an overlay.
tun
overlay
fs/etc/modules-load.d/kdos-containers.conf
# Virtual machines' DRM drivers, loaded by name at boot rather than left to
# udev's modalias match. virtio_gpu and bochs are built into the kernel, so
# their lines load nothing and succeed; qxl and vmwgfx are modules, and on a
# machine without their device they load, bind nothing and sit idle.
virtio_gpu
qxl
bochs
vmwgfx
fs/etc/modules-load.d/kdos-drm.conf
# The hwmon drivers no uevent can name. A desktop board's fans, voltage rails
# and motherboard temperatures sit behind a Super-I/O chip — Nuvoton on most
# boards, ITE on most of the rest — and a SATA disk's temperature behind
# drivetemp. None of the three declares a modalias, so udev's autoload never
# reaches them, and without this file `sensors` and System Monitor show a desktop's
# CPU and GPU temperatures and nothing else.
#
# Each probes and binds only what it finds. On hardware without the chip, or
# where the firmware's ACPI tables have claimed its ports, the load fails with
# ENODEV or EBUSY and 02_modules.sh reports a skip. sensors-detect is not the
# route: it probes the SMBus by writing to it, and these three do not.
nct6775
it87
drivetemp
fs/etc/modules-load.d/kdos-hwmon.conf
# The character-device interface to the I2C buses, which is how ddcutil reaches
# a monitor's DDC/CI line. Built as a module and never autoloaded: i2c-dev
# declares no modalias, so no uevent names it and udev's autoload path cannot
# reach it. Without this line /dev/i2c-* does not exist and
# 70-kdos-i2c.rules has nothing to grant.
#
# ddcutil ships /usr/lib/modules-load.d/ddcutil.conf saying the same thing.
# 02_modules.sh reads /etc/modules-load.d only, so that copy is inert here.
i2c-dev
fs/etc/modules-load.d/kdos-i2c.conf
/etc/modprobe.d/
modprobe reads module options, aliases and blacklists from every *.conf file in these directories, in this order, the first of two files with the same name winning: /etc/modprobe.d, /run/modprobe.d, /usr/local/lib/modprobe.d, /usr/lib/modprobe.d and /lib/modprobe.d, which is the same directory as /usr/lib/modprobe.d on KDOS. The format is modprobe.d(5). The base filesystem ships one file:
# The kernel claims an RTL2832U as a DVB-T tuner on plug-in, which leaves
# librtlsdr unable to open a device that is present and enumerated.
# /etc/udev/rules.d/70-kdos-sdr.rules is the access half of the same fix.
blacklist dvb_usb_rtl28xxu
fs/etc/modprobe.d/kdos-sdr.conf
The hwdata port installs upstream’s blacklist of drivers that are not to load on their own:
./configure --prefix=/usr --sysconfdir=/etc --disable-blacklist
make
make DESTDIR=$PKG install
install -Dm644 blacklist.conf "$PKG/usr/lib/modprobe.d/dist-blacklist.conf"
ports/core/hardware/hwdata/build.sh, lines 12–15
The installer’s file is written from the time zone’s country:
if (cc[0]) {
mkpath(TARGET "/etc/modprobe.d");
wr("/etc/modprobe.d/kdos-regdom.conf",
"# Written by the KDOS installer from the timezone (%s):\n"
"# the country the Wi-Fi radio's channels and transmit\n"
"# power are set for.\n"
"options cfg80211 ieee80211_regdom=%s\n",
cfg.tz_label, cc);
}
src/system/kinstall-core/install.c, lines 3074–3082
kdos-settingsd writes the same options cfg80211 line when the zone changes, and removes the file for a zone with no country, such as UTC. What the country changes is on Wireless regulatory domain.
Udev and coldplug
01_udev.sh starts udevd and replays every device already present, which is what loads the drivers for the hardware the machine boots with:
"$DAEMON" --daemon
# --action=add, and not the default. udevadm trigger replays every
# device with action "change", but /lib/udev/rules.d/80-drivers.rules
# — the rule that modprobes a driver from MODALIAS — opens with
# ACTION!="add", GOTO="drivers_end". A plain trigger therefore loads
# NO module at all: the sound controller stays unclaimed and ALSA
# finds no card. Subsystems before devices, so a bus module is in
# place before its children are replayed.
/usr/sbin/udevadm trigger --action=add --type=subsystems
/usr/sbin/udevadm trigger --action=add --type=devices
/usr/sbin/udevadm settle
fs/etc/init.d/01_udev.sh, lines 33–43
/etc/sysctl.conf
10_sysctl.sh runs sysctl --system at boot. That applies every drop-in file under /etc/sysctl.d, /run/sysctl.d, /usr/local/lib/sysctl.d and /usr/lib/sysctl.d, in file-name order, and then /etc/sysctl.conf last, so a key set in /etc/sysctl.conf wins over the same key in a drop-in. A program that raises a limit for good writes a drop-in: System Settings’ inotify page, for one, writes /etc/sysctl.d/50-kde-inotify-survey-max_user_{watches,instances}.conf. The image ships no drop-in. /etc/sysctl.conf, with the reason for each setting:
net.ipv4.ping_group_range = 0 2147483647
# A PROGRAM THAT DIES SAYS SO IN THE KERNEL LOG.
#
# Without this a segmentation fault is silent: the process is gone, its window
# with it, and nothing anywhere names the program or the address. On a desktop
# whose surfaces are separate processes that is the difference between "the
# terminal closed" and a fault with a faulting instruction — and the log is
# `dmesg`, which survives the session restarting. The kernel is built with
# CONFIG_SECURITY_DMESG_RESTRICT, so reading it takes root: `sudo dmesg`.
kernel.print-fatal-signals = 1
# LINKS IN A STICKY WORLD-WRITABLE DIRECTORY ARE NOT FOLLOWED FOR SOMEONE ELSE.
#
# /tmp is a shared 1777 tmpfs, and every init script, timer and package hook
# runs as root. The kernel leaves all four of these at 0, and at 0 a user who
# plants a symlink, hardlink, FIFO or file at a name a root job is about to
# write redirects that write onto any file on the machine.
fs.protected_symlinks = 1
fs.protected_hardlinks = 1
fs.protected_fifos = 1
fs.protected_regular = 2
# PER-TASK DELAY ACCOUNTING IS ON.
#
# The kernel is built with it and leaves it off. Off, iotop has no IO% or
# SWAPIN% column and htop's CPU, IO and swap delay columns read N/A. On, the
# scheduler reads the clock around every wait it accounts — a small cost on
# every context switch, and the reason the kernel ships it off.
kernel.task_delayacct = 1
# NO ROUTE IS TAKEN FROM AN ICMP REDIRECT, AND NONE IS SENT.
#
# A redirect rewrites this machine's route to a destination on the word of
# whoever sends it, so on a shared Wi-Fi any host on the link can put itself in
# the path. The kernel accepts them by default. This machine is a router only
# for its hotspot and its containers, whose clients have no other route to
# correct, so it sends none either.
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0
# A PACKET IS DROPPED WHEN NO ROUTE LEADS BACK TO ITS SOURCE.
#
# 2 is the loose check: the source must be reachable through some interface,
# not necessarily the one the packet came in on. It drops spoofed sources from
# nowhere and leaves alone the asymmetric routes a VPN, a hotspot, podman's
# bridge and a Babel mesh all make; the strict check, 1, breaks each of those.
net.ipv4.conf.all.rp_filter = 2
net.ipv4.conf.default.rp_filter = 2
# KERNEL ADDRESSES ARE HIDDEN FROM EVERY ACCOUNT BUT ROOT.
#
# At 0 /proc/kallsyms, /proc/modules and the kernel log's %pK pointers give
# any user the layout that address randomisation hides. At 1 they read as
# zeroes to an account without CAP_SYSLOG; root's perf and crash tools still
# see them.
kernel.kptr_restrict = 1
What each setting changes is also listed in Services, under 10_sysctl.
One more parameter is set at boot outside this file: 12_zram.sh sets vm.page-cluster to 0 while the compressed swap device is active, and back to the kernel’s default when it stops; see /etc/kdos/zram.conf.
Usage
Looking at modules
List the loaded modules:
lsmodShow what a module is, its options and its firmware:
modinfo i2c-dev
Loading a module
Load it now, with what it depends on.
snd-aloop, the ALSA loopback card, is a module KDOS builds and does not load:sudo modprobe -v snd-aloopUnload it:
sudo modprobe -r snd-aloopLoad it at every boot: write its name on a line of a new file, such as
/etc/modules-load.d/local.conf, and check the file with the script that reads it:sudo /etc/init.d/02_modules.sh startIt prints a line for every name in every file, such as
[KDOS] loaded: snd-aloop, and for one that did not load askip:line carrying the first line ofmodprobe’s own error, such as[KDOS] skip: nct6775 (modprobe: ERROR: could not insert 'nct6775': No such device). Running it again is harmless: a module already loaded is reportedloaded:once more.
Passing a module options
Write an
optionsline into a file under/etc/modprobe.d/, such as/etc/modprobe.d/local.conf:options <module> <parameter>=<value>The options apply the next time the module loads. For a module that is loaded and not in use, reload it:
sudo modprobe -r <module> sudo modprobe <module>Check the value the module took, where it exports one:
cat /sys/module/<module>/parameters/<parameter>
A module the initramfs loads, such as a filesystem or a disk controller driver, loads before the root filesystem is mounted, and the initramfs carries no /etc/modprobe.d. Such a module takes its options from the kernel command line instead, as <module>.<parameter>=<value>; modprobe reads those from /proc/cmdline too. How to add a word to the command line, for one boot or for every boot, is on Boot menu and kernel parameters.
Keeping a module from loading
Write a
blacklistline, askdos-sdr.confdoes, into a file under/etc/modprobe.d/:blacklist <module>This stops the module loading for a device;
modprobe <module>by name still loads it.For one boot, add
modprobe.blacklist=<module>to the kernel command line in the boot menu’s entry editor (Eon an entry; see Adding a parameter for one boot).
Reading and setting a kernel parameter
Read one, or all:
sysctl kernel.kptr_restrict sysctl -aSet one for this boot:
sudo sysctl -w vm.swappiness=20Set it at every boot: add the line
vm.swappiness = 20to/etc/sysctl.conf, or put it in a file of its own such as/etc/sysctl.d/60-local.conf. Then apply the file you wrote, eithersudo sysctl -p /etc/sysctl.confor
sudo sysctl -p /etc/sysctl.d/60-local.confA key set in
/etc/sysctl.confoverrides the same key in a drop-in, because the boot reads/etc/sysctl.conflast.sudo service start sysctlapplies every file the boot applies, withsysctl --system.
Tips
kdos why /etc/sysctl.confreports that no package owns the file. It comes from the base filesystem, so no package upgrade replaces an edit to it.kdos explain coldplugprints why01_udev.shreplays devices with--action=add.02_modules.shprintsskip: <name> (<reason>)for every module that fails to load, where the reason is the first line ofmodprobe’s own error, ormodprobe failedwhen it printed none. Absent hardware, a misspelt name, a module this kernel does not carry and a refused option all fail alike, and only the reason tells them apart;sudo modprobe -v <name>shows the rest of the message.- The kernel’s modules are compressed with zstd;
modinfoandmodproberead them as they are. - Every key is a file under
/proc/sys, with the dots as slashes:sysctl -w vm.swappiness=20is the same as writing20to/proc/sys/vm/swappiness.
Troubleshooting
A device is present but has no driver. Check whether the kernel has a module for it:
lspci -k
sudo dmesg | tail -n 30
lspci -k names the module each PCI device can use and the one in use. If a module is named but not loaded, sudo modprobe -v <module> shows why. A module that declares no modalias, such as the sensor drivers in kdos-hwmon.conf, is never loaded for a device and has to be listed in /etc/modules-load.d.
Coldplug loaded nothing at boot: no sound card, no network. Replay the devices as 01_udev.sh does:
sudo udevadm trigger --action=add --type=subsystems
sudo udevadm trigger --action=add --type=devices
sudo udevadm settle
A plain udevadm trigger replays them as change, and the rule that loads drivers acts only on add.
A module refuses an option. The kernel log says which parameter it did not know: sudo dmesg | grep <module>. modinfo -p <module> lists the parameters it takes.
A setting in /etc/sysctl.conf is not in effect after boot. Read what sysctl --system printed:
cat /run/kdos-init.sysctl.log
A key the running kernel lacks is reported there. A setting a later script or program changes, as 12_zram.sh does for vm.page-cluster, wins over the file.
Known gaps
- Root can load an unsigned module; see Security
- linux-firmware leaves out what an x86 kernel cannot load
- Packet radio has no kernel network interface
- btop’s GPU panel is empty on Intel graphics unless btop runs as root:
kernel.perf_event_paranoidstays at the kernel’s default
See also
- Services:
01_udev,02_modulesand10_sysctlin the boot order - The device groups and the udev rules: who may open which device
- Other machine files:
/etc/sysctl.conf,/etc/modules-load.dand/etc/modprobe.d - The initramfs: which modules load before the root is mounted
- Firmware: the firmware files drivers load
- Services, Init and runlevels, Logs, Boot menu and kernel parameters, Microcode and firmware, zram and swap