#!/bin/bash # lkm : a silly kernel module dev - build, load, unload - helper wrapper script # License: MIT # Kaiwan NB # Turn on unofficial Bash 'strict mode'! V useful # "Convert many kinds of hidden, intermittent, or subtle bugs into immediate, glaringly obvious errors" # ref: http://redsymbol.net/articles/unofficial-bash-strict-mode/ set -euo pipefail unset ARCH unset CROSS_COMPILE name=$(basename "${0}") #-------------- r u n c m d ------------------------------------------- # Display and run the provided command. # Parameter(s): the command to run runcmd() { local SEP="------------------------------" [[ $# -eq 0 ]] && return echo "${SEP} $* ${SEP}" eval "$@" [[ $? -ne 0 ]] && echo " ^--[FAILED]" } ### "main" here [[ $# -ne 1 ]] && { echo "Usage: ${name} name-of-kernel-module-file (without the .c)" exit 1 } [[ "${1}" = *"."* ]] && { echo "Usage: ${name} name-of-kernel-module-file ONLY (do NOT put any extension)." exit 1 } echo "Version info:" which lsb_release >/dev/null 2>&1 && { echo -n "Distro: " lsb_release -a 2>/dev/null |grep "Description" |awk -F':' '{print $2}' } echo -n "Kernel: " ; uname -r runcmd "sudo rmmod $1 2> /dev/null" || true #runcmd "make clean" runcmd "sudo dmesg -C" || true runcmd "make || exit 1" || true [[ ! -f "$1".ko ]] && { echo "[!] ${name}: \"$1.ko\" has not been built, aborting... (Note though, that this can happen when the module's named differently)." exit 1 } runcmd "sudo insmod ./$1.ko && lsmod|grep $1" || true # Ubuntu 20.10 onward has enabled CONFIG_SECURITY_DMESG_RESTRICT ! That's good for security # So we need to 'sudo' dmesg; thanks to @gregbuchholz for pointing this out runcmd "sudo dmesg" || true exit 0