编写hello内核模块--fedora21环境

1、编写内核模块

参考:https://blog.csdn.net/sh21_/article/details/60878812

hello.c --- 注意函数参数中void  ----hello_init(void)

#include 
#include 
static int hello_init(void)
{
    printk("hello  world\n");
    return 0;
}
static void hello_exit(void)
{printk("exit\n");
}
module_init(hello_init);
module_exit(hello_exit);

Makefile文件

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR:=/lib/modules/3.17.4/build
all:
	make -C $(KDIR) M=$(PWD) modules
clean:
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers
endif

这里编译出错,原因是我以前修改了 ~/.bashrc文件,所以提示找不到arm-linx-gcc

希望找到原本的bashrc,于是这篇文章帮了我:https://blog.csdn.net/yucicheung/article/details/79334998

/etc/skel是Ubuntu的各种初始配置文件的存放目录.

原始bashrc位置: /etc/skel/.bashrc

我现有的bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
export ANDROID_HOME=/home/pengfei/android/sdk
export CROSS_COMPILE=/run/media/pengfei/6CC2286EC2283F28/asop/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8/bin/arm-eabi-
export ARMH=arm
export SUBARCH=arm
export eabi_home=/run/media/pengfei/6CC2286EC2283F28/asop/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8
#export PYTHONPATH=/run/media/pengfei/6CC2286EC2283F28/asop/prebuilts/python/linux-x86/2.7.5/lib/python2.7/site-packages:$PYTHONPATH
export ANDROID_SWT=/run/media/pengfei/6CC2286EC2283F28/asop/out/host/linux-x86/framework
export ANDROID_PRODUCT_OUT=/run/media/pengfei/6CC2286EC2283F28/asop/out/target/product/generic
PATH=$eabi_home/bin:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools:$PATH
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS="@im=fcitx"
export USE_CCACHE=1

初始的bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

替换之后make,生成hello.ko

插入hello模块

编写hello内核模块--fedora21环境_第1张图片

2、内核编程insmod后,fedora查看日志无/var/log/messages

fedora 21上没有/var/log/message了,取而代之的是journal.
参考:https://fedoraproject.org/wiki/Changes/NoDefaultSyslog

"cat /var/log/messages" will now become "journalctl".

"tail -f /var/log/messages" will now become "journalctl -f".

"grep foobar /var/log/messages" will now become "journalctl | grep foobar".


journalctl -f

5月 16 22:03:03 pengfei kernel: hello  world

5月 16 22:03:47 pengfei kernel: exit

你可能感兴趣的:(Linux,C语言)