2023-04-03 18:32:58 +02:00
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
set -o pipefail
MODULES_LOAD_BIN = "/usr/lib/systemd/systemd-modules-load"
CONFIG_FILE = "/run/modules-load.d/99-test.conf"
at_exit( ) {
rm -rfv " ${ CONFIG_FILE : ? } "
}
trap at_exit EXIT
if systemd-detect-virt -cq; then
echo "Running in a container, skipping the systemd-modules-load test..."
exit 0
fi
2024-02-22 13:00:48 +00:00
ORIG_MODULES_LOAD_CONFIG = " $( systemd-analyze cat-config modules-load.d) "
2023-04-03 18:32:58 +02:00
# Check if we have required kernel modules
2024-05-03 15:56:52 +02:00
modprobe --all --resolve-alias dummy
2023-04-03 18:32:58 +02:00
mkdir -p /run/modules-load.d/
" $MODULES_LOAD_BIN "
" $MODULES_LOAD_BIN " --help
" $MODULES_LOAD_BIN " --version
# Explicit config file
2024-05-03 15:56:52 +02:00
modprobe -v --all --remove dummy
printf "dummy" >" $CONFIG_FILE "
2023-04-03 18:32:58 +02:00
" $MODULES_LOAD_BIN " " $CONFIG_FILE " | & tee /tmp/out.log
grep -E "Inserted module .*dummy" /tmp/out.log
# Implicit config file
2024-05-03 15:56:52 +02:00
modprobe -v --all --remove dummy
printf "dummy" >" $CONFIG_FILE "
2023-04-03 18:32:58 +02:00
" $MODULES_LOAD_BIN " | & tee /tmp/out.log
grep -E "Inserted module .*dummy" /tmp/out.log
# Valid & invalid data mixed together
2024-05-03 15:56:52 +02:00
modprobe -v --all --remove dummy
2023-04-03 18:32:58 +02:00
cat >" $CONFIG_FILE " <<EOF
2024-05-03 15:56:52 +02:00
dummy
dummy
dummy
dummy
2023-04-03 18:32:58 +02:00
dummy
\\ n\n \n \\ \\ \\
2024-05-03 15:56:52 +02:00
dumm!@@123##2455
2023-04-03 18:32:58 +02:00
# This is a comment
$( printf "%.0sx" { 0..4096} )
dummy
2024-05-03 15:56:52 +02:00
dummy
2023-04-03 18:32:58 +02:00
foo-bar-baz
1
"
'
EOF
" $MODULES_LOAD_BIN " | & tee /tmp/out.log
grep -E "^Inserted module .*dummy" /tmp/out.log
grep -E "^Failed to find module .*foo-bar-baz" /tmp/out.log
( ! grep -E "This is a comment" /tmp/out.log)
# Each module should be loaded only once, even if specified multiple times
2024-05-03 15:56:52 +02:00
[ [ " $( grep -Ec "^Inserted module" /tmp/out.log) " -eq 1 ] ]
2023-04-03 18:32:58 +02:00
[ [ " $( grep -Ec "^Failed to find module" /tmp/out.log) " -eq 7 ] ]
# Command line arguments
2024-05-03 15:56:52 +02:00
modprobe -v --all --remove dummy
2023-04-03 18:32:58 +02:00
# Make sure we have no config files left over that might interfere with
# following tests
rm -fv " $CONFIG_FILE "
2024-02-22 13:00:48 +00:00
[ [ " $ORIG_MODULES_LOAD_CONFIG " = = " $( systemd-analyze cat-config modules-load.d) " ] ]
2024-05-03 15:56:52 +02:00
CMDLINE = "ro root= modules_load= modules_load=, / = modules_load=foo-bar-baz,dummy modules_load=dummy,dummy,dummy"
2023-04-03 18:32:58 +02:00
SYSTEMD_PROC_CMDLINE = " $CMDLINE " " $MODULES_LOAD_BIN " | & tee /tmp/out.log
grep -E "^Inserted module .*dummy" /tmp/out.log
grep -E "^Failed to find module .*foo-bar-baz" /tmp/out.log
# Each module should be loaded only once, even if specified multiple times
2024-05-03 15:56:52 +02:00
[ [ " $( grep -Ec "^Inserted module" /tmp/out.log) " -eq 1 ] ]
2023-04-03 18:32:58 +02:00
( ! " $MODULES_LOAD_BIN " --nope)
( ! " $MODULES_LOAD_BIN " /foo/bar/baz)