如何针对新源码编译内核模块

4

我正在尝试编译一个Hello World模块。我的系统是新安装的Ubuntu,没有任何已编译的内核。

我的内核版本是:

2.6.32-34-generic

我使用了以下Makefile,并出现了错误:

obj-m += hello-1.o
all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

# make
make -C /lib/modules/2.6.32-34-generic/build M=/home/james/Desktop/hello modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-34-generic'
make[2]: *** No rule to make target `/home/james/Desktop/hello/hello-1.c', needed by `/home/james/Desktop/hello/hello-1.o'.  Stop.
make[1]: *** [_module_/home/james/Desktop/hello] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-34-generic'
make: *** [all] Error 2

我的 /lib/modules/2.6.32-34-generic 目录下的内容如下:
total 3864
lrwxrwxrwx  1 root root     40 2011-11-05 15:55 build -> /usr/src/linux-headers-2.6.32-34-generic
drwxr-xr-x  2 root root   4096 2011-11-05 15:49 initrd
drwxr-xr-x 10 root root   4096 2011-11-05 15:49 kernel
.......................................................
.......................................................

文件夹 /usr/src/linux-headers-2.6.32-34-generic 存在。

由于它没有起作用,我从Ubuntu下载了linux-headers-2.6.32-34-generic源代码并编译,然后更改了我的Makefile为:

obj-m += hello-1.o
all:
    make -C /usr/src/linux-2.6.32/ M=$(PWD) modules

clean:
    make -C /usr/src/linux-2.6.32/ M=$(PWD) clean

#make
make -C /usr/src/linux-2.6.32/ M=/home/james/Desktop/hello modules
make[1]: Entering directory `/usr/src/linux-2.6.32'

  ERROR: Kernel configuration is invalid.
         include/linux/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.


  WARNING: Symbol version dump /usr/src/linux-2.6.32/Module.symvers
           is missing; modules will have no dependencies and modversions.

make[2]: *** No rule to make target `/home/james/Desktop/hello/hello-1.c', needed by `/home/james/Desktop/hello/hello-1.o'.  Stop.
make[1]: *** [_module_/home/james/Desktop/hello] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.32'
make: *** [all] Error 2

请问有人能帮我解决这个问题吗?http://packages.ubuntu.com/lucid-updates/devel/linux-headers-2.6.32-34-generic

我有一些通用的问题要问。

在重新安装完毕后,编译内核的最佳方法是什么?在我编译内核并构建模块之后,以前它可以无缝运行。但是在这种情况下,我不知道该怎么做。

4个回答

3

错误信息:

ERROR: Kernel configuration is invalid.
         include/linux/autoconf.h or include/config/auto.conf are missing.
         Run 'make oldconfig && make prepare' on kernel src to fix it.


  WARNING: Symbol version dump /usr/src/linux-2.6.32/Module.symvers
           is missing; modules will have no dependencies and modversions.

简单地说,这是因为你的内核源代码是新下载和未编译过的。
以下是应该如何编译任何内核模块:
在下载内核源代码后,您必须准备将其添加到任何模块中。从/boot/目录中复制旧内核的“config-xxxx”文件到新内核源目录中,并将其重命名为“.config”。
然后执行“make oldconfig”,它将备份.config到.config.old,并基于新内核源生成新的.config。只需为所有默认设置输入“ENTER”(有很多)。
接下来要做的是“make”(并等待一段时间),它将生成一个新的内核文件“vmlinux”,以及许多其他由模块编译过程读取的文件。
现在您可以转到内核模块源代码所在的目录,并基于以下Makefile进行操作:
obj-m += hello-1.o

default: modules

modules:

    make -C /kernel_source/ M=$(PWD) modules

clean:
    make -C /kernel_source/ M=$(PWD) clean

与Makefile一起的是你的头文件和源文件,即hello-1.c文件在同一个位置。

只需运行"make"命令,你的内核模块就会成功生成。


2
您需要在 Fedora 上安装一些软件包,比如“kernel-devel”(抱歉,我不是 Ubuntu 用户),它提供了编译您的内核模块所需的头文件和 .config 文件。请注意保留 HTML 标签。

1
make[2]: *** 没有规则可制作目标“/home/james/Desktop/hello/hello-1.c”,需要的是“/home/james/Desktop/hello/hello-1.o”。停止。 您在第一次编译中遇到此错误,因为“/home/james/Desktop/hello/”目录中缺少“hello-1.c”文件。

0
  1. 检查 /home/james/Desktop/hello/ 目录中是否存在 hello-1.c 文件。
  2. 您需要在内核中启用 modules_enabled。您需要编译一个新的内核来完成此操作。 以下文章解释了如何很好地构建内核。在内核构建的配置中启用模块。

    http://kernelnewbies.org/FAQ/KernelCompilation


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接