-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
97 lines (78 loc) · 2.47 KB
/
Makefile
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
###################### Linux Kernel Module Makefile #######################
# Author : Aravind Potluri <[email protected]>
# Arguments :
# all - Builds all module.
# module - Builds the defined module.
# clean - Cleans the module build artifacts.
# sign - Signs the module. [Requires *signmod]
# lsmod - List the last few installed modules.
# insert - Inserts the module for testing.
# remove - Removes the inserted module.
# install - Module will be placed in source tree.
# uninstall - Module will be removed from source tree.
# load - Load the source tree module. [insert]
# unload - Unload the module from kernel. [remove]
# info - Show the information of loaded module.
# dmesg - Prints the live kernel ring buffer.
#
# *signmod More info : https://github.com/cipherswami/signmod
##########################################################################
######## Select Module ########
MODULE ?= hello_world
###############################
# Makefile Variables
include .config
ARCH ?= x86_64
CROSS_COMPILE ?= x86_64-linux-gnu-
export
# Directories
SDIR := $(PWD)/modules
MDIR := $(SDIR)/$(MODULE)
HDIR := /lib/modules/$(shell uname -r)
KDIR := $(HDIR)/build
IDIR := $(HDIR)/updates
# Flags
FLAGS += EXTRA_CFLAGS:=-I$(PWD)/include
# Default target: build the kernel modules
all:
$(MAKE) -C $(KDIR) M=$(SDIR) $(FLAGS) modules
# Build the module defined by "MODULE" variable
module:
$(MAKE) -C $(KDIR) M=$(MDIR) $(FLAGS) modules
# Cleans the build artifacts
clean:
$(MAKE) -C $(KDIR) M=$(SDIR) clean
# Sign the kernel module (requires signmod external tool)
sign:
sudo signmod $(MDIR)/$(MODULE).ko
# List the top 5 loaded modules
lsmod:
lsmod | head -n 6
# Insert the kernel module
insert: sign
sudo insmod $(MDIR)/$(MODULE).ko
# Remove the kernel module
remove:
sudo rmmod $(MODULE)
# Install the module to the kernel directory
install: sign
sudo cp $(MDIR)/$(MODULE).ko $(IDIR) && sudo depmod -a
# Uninstall the module
uninstall:
sudo rm -f $(IDIR)/$(MODULE).ko && sudo depmod -a
# Load the module using modprobe
load:
sudo modprobe $(MODULE)
# Unload the module using modprobe
unload:
sudo modprobe -r $(MODULE)
# Show module information
info:
modinfo $(MDIR)/$(MODULE).ko
# Clear ans show the Ring buffer
dmesg:
clear && sudo dmesg -C && sudo dmesg -wHT || true
# Target for debugging
debug:
@echo $(FLAGS)
.PHONY: all clean sign lsmod insert remove install uninstall load unload info dmesg debug