Files
linux-kernel-module-cheat/rootfs_overlay/lkmc/memfile.sh
Ciro Santilli e4847e4b40 kernel modules: add a quick scull port from LDD3
Also:

* fix fops.c on both kernels:
  * 5.9: the out of space error code was 1 not 8
  * 6.6: for whatever reason we can't read the user buffer as before on the
         diagnostic print, it leads to segfault and oops
* create memfile.c which is like fops.c but of unlimited size
2025-04-30 13:15:27 +01:00

52 lines
895 B
Bash
Executable File

#!/bin/sh
set -e
# Helpers
odraw() (
od -A n -t x1 -v "$@" | tr -d '\n' | cut -c 2-
)
# Setup
f=/sys/kernel/debug/lkmc_memfile
mod="${1:-memfile.ko}"
shift
insmod "$mod" "$@"
# Starts off empty
[ -z "$(cat "$f")" ]
# write and check it is there
printf 12 > "$f"
[ 12 = "$(cat "$f")" ]
# Append and check that it is there
printf 34 >> "$f"
[ 1234 = "$(cat "$f")" ]
# Restart
printf 56 > "$f"
[ 56 = "$(cat "$f")" ]
# skip
printf 1234 > "$f"
[ 23 = "$(dd if="$f" bs=1 count=2 skip=1)" ]
# seek
printf 1234 > "$f"
printf xy | dd bs=1 of="$f" seek=1 conv=notrunc
[ 1xy4 = "$(cat "$f")" ]
# seek past the end
printf 1234 > "$f"
printf xy | dd bs=1 of="$f" seek=6 conv=notrunc
[ '31 32 33 34 00 00 78 79' = "$(odraw "$f")" ]
# Allocate 1 GB for fun.
dd if=/dev/zero of="$f" bs=1k count=1M
[ '00 00' = "$(dd if="$f" bs=1 count=2 skip=500M | odraw)" ]
# Teardown
rmmod memfile
echo passed