源码
Kernel git库:https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
下载速度太慢,使用清华大学的镜像:
git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux.git
DPDK git库:https://git.dpdk.org/dpdk
git clone git://dpdk.org/dpdk
增加新模块
以netfilter为例
hello.c
#include <linux/netfilter_ipv4/ip_tables.h>
#include <net/ip.h>
static unsigned int hook_func(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct iphdr *iph = skb->nh.iph;
/* we can read/write iph here */
return NF_ACCEPT;
}
static struct nf_hook_ops op = {
.hook = hook_func,
.owner = THIS_MODULE,
.pf = PF_INET,
.hooknum = NF_INET_FORWARD,
.priority = NF_IP_PRI_FIRST,
};
static int __init hook_init(void)
{
return nf_register_hook(&op);
}
static void __exit hook_exit(void)
{
nf_unregister_hook(&op);
}
module_init(hook_init);
module_exit(hook_exit);
Makefile
# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)
# Assume the source tree is where the running kernel was built
# You should set KERNELDIR in the environment if it's elsewhere
KERNELDIR ?= /lib/modules/`uname -r`/build
# The current directory is passed to sub-makes as argument
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
# called from kernel build system: just declare what our modules are
obj-m := hello.o
endif
编译
make
insmod hello.ko
rmmod hello
修改旧模块
1、安装源码
wget http://vault.centos.org/7.5.1804/os/Source/SPackages/kernel-3.10.0-862.el7.src.rpm
useradd -s /sbin/nologin mockbuild
rpm -ivh kernel-3.10.0-862.el7.src.rpm
cd rpmbuild/SPECS/
rpmbuild -bp kernel.spec
cd ../BUILD/kernel-3.10.0-862.el7/linux-3.10.0-862.el7.centos.x86_64/
安装完成后,源码放在rpmbuild/BUILD/kernel-3.10.0-862.el7/linux-3.10.0-862.el7.centos.x86_64/目录下
2、安装kernel-devel
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/kernel-devel-3.10.0-862.el7.x86_64.rpm
rpm -ivh kernel-devel-3.10.0-862.el7.x86_64.rpm
安装完成后,在/usr/src/kernels/目录下出现3.10.0-862.el7.x86_64/目录,其中包含重要的Module.symvers文件
3、进入源码目录,将Module.symvers文件拷贝到当前目录下
4、编译指定目录下的模块
make M=drivers/net/usb/
5、进入指定目录,将ko文件压缩成xz文件
xz -zk cdc_ether.ko
6、使用新的xz文件替换/lib/modules/3.10.0-862.el7.x86_64/kernel/drivers/net/usb/目录下旧的xz文件
cp cdc_ether.ko.xz /lib/modules/3.10.0-862.el7.x86_64/kernel/drivers/net/usb/
7、重启
修改旧组件
rpm -ivh iputils-20160308-10.el7.src.rpm
cd rpmbuild/SPECS/
rpmbuild -bp iputils.spec
cd ../BUILD/iputils-s20160308/