-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathlkm
executable file
·62 lines (53 loc) · 1.66 KB
/
lkm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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