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

发布于 2024-12-18 00:47:12 字数 2530 浏览 2 评论 0原文

我正在尝试编译一个 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 存在。

由于它不起作用,我下载了 linux-headers -2.6.32-34-来自 Ubuntu 的通用源 并编译并将我的 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

有人可以帮我解决吗this.http://packages.ubuntu.com/lucid-updates/devel/linux-headers-2.6.32-34-generic

我有一些一般性问题要问。

全新安装后编译内核的最佳方法是什么。在我编译了内核并构建了一个模块之后,它之前就完美地工作了。但我不知道在这种情况下该怎么办

I am trying to compile a Hello World module. I am having a fresh Ubuntu in my system which doesn't have any compiled kernel.

My kernel is:

2.6.32-34-generic

I gave the following Makefile and got the error:

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

The contents of my /lib/modules/2.6.32-34-generic are

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
.......................................................
.......................................................

The folder /usr/src/linux-headers-2.6.32-34-generic exists.

Since it didnt work, I downloaded the linux-headers-2.6.32-34-generic source from Ubuntu and compiled and changed my Makefile to:

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

Could someone help me solving this.http://packages.ubuntu.com/lucid-updates/devel/linux-headers-2.6.32-34-generic

I have some general questions to ask.

After a fresh install what is the best way of compiling a kernel. After I compiled the kernel and built a module it worked flawlessly earlier. But I couldnt know what to do this in this situation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

窝囊感情。 2024-12-25 00:47:12

错误:

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.

只是因为您的内核源是新下载的并且之前未编译。

这就是你应该如何编译任何内核模块。

下载内核源代码后,您必须准备好向其中添加任何模块。

将旧内核的“config-xxxx”文件从 /boot/ 目录复制到新内核源目录,并将其重命名为“.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”,您的内核模块就应该成功生成。

The error:

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.

simply is because your kernel source is newly downloaded and uncompiled before.

This is how u should compile any kernel module.

After download the kernel source, you must prepare it for adding any modules to it.

Copy the older-kernel's "config-xxxx" file from the /boot/ directory into the new kernel source directory, and rename it as ".config".

Then execute "make oldconfig", which will take a backup of the .config into .config.old, and regenerate a new .config based on the new kernel source. Just enter "ENTER" for all the default settings (lots of them).

Next is to do a "make" (and wait for some time) - it will generate a new kernel file "vmlinux", together with many other files which is read by the modules compilation process.

Now you can go to your directory where the kernel module source code is located, and based on the following Makefile:

obj-m += hello-1.o

default: modules

modules:

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

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

Together with Makefile are your header and source file, which is hello-1.c located together.

Just "make" and your kernel modules should be generated successfully.

苯莒 2024-12-25 00:47:12

你需要在 Fedora 上安装一些像“kernel-devel”这样的包(抱歉我不是 Ubuntu 用户),它提供了头文件和 .config 来编译你的内核模块。

You need to install some package like 'kernel-devel' on Fedora (sorry I am not a Ubuntu user), it provides the headers and .config to compile your kernel module.

失退 2024-12-25 00:47:12

make[2]: * 没有创建目标的规则
/home/james/Desktop/hello/hello-1.c',需要/home/james/Desktop/hello/hello-1.o'。停止

您在第一次编译时遇到此错误,因为 /home/james/Desktop/hello/ 目录中缺少 hello-1.c 文件。

make[2]: * No rule to make target
/home/james/Desktop/hello/hello-1.c', needed by/home/james/Desktop/hello/hello-1.o'. Stop

Your are facing this error in the first compilation because hello-1.c file is missing in /home/james/Desktop/hello/ directory.

哎呦我呸! 2024-12-25 00:47:12
  1. 检查 /home/james/Desktop/hello/ 目录中是否存在 hello-1.c。
  2. 您需要在内核中启用modules_enabled。您需要编译一个新的内核才能执行此操作。
    下面的文章解释了如何很好地构建内核。在内核构建的配置中启用模块。

    http://kernelnewbies.org/FAQ/KernelCompilation

  1. Check if hello-1.c exists in /home/james/Desktop/hello/ directory.
  2. You need to have modules_enabled in your kernel. You need to compile a fresh kernel to do this.
    Following post explains how to build kernel nicely. Enable modules in configuration of kernel build.

    http://kernelnewbies.org/FAQ/KernelCompilation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文