为 openvz VM 编译内核模块?

标签 c linux makefile virtual-machine openvz

我有一个 openvz 机器,我是 root,它是一个虚拟机,我是 ssh 到:

>uname -a
Linux molo 2.6.32-042stab084.25 #1 SMP Wed Feb 12 16:04:42 MSK 2014 x86_64 x86_64 x86_64 GNU/Linux

我正在尝试构建一个 hello world 内核模块:

你好.c:

#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */
#include <linux/init.h>         /* Needed for the macros */
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
module_init(hello_start);
module_exit(hello_end);

生成文件:

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

当我尝试编译 hello.c 时:

#make
make -C /lib/modules/2.6.32-042stab084.25/build M=/local/my_modules modules
make: *** /lib/modules/2.6.32-042stab084.25/build: No such file or directory.  Stop.
make: *** [all] Error 2

那是内核版本uname -r报告

#uname -r
2.6.32-042stab084.25

以下也没有帮助:

$sudo apt-get install "linux-headers-$(uname -r)"
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package linux-headers-2.6.32-042stab084.25
E: Couldn't find any package by regex 'linux-headers-2.6.32-042stab084.25'

这里是/lib/modules目录:

ls/lib/modules/2.6.32-042stab084.25/

modules.alias      modules.ccwmap  modules.dep.bin  modules.ieee1394map  modules.isapnpmap  modules.pcimap    modules.softdep  modules.symbols.bin
modules.alias.bin  modules.dep     modules.devname  modules.inputmap     modules.ofmap      modules.seriomap  modules.symbols  modules.usbmap

最佳答案

从 openvz 页面下载并安装 linux-headers 包(更多关于 http://openvz.org/Installation_on_Debian)

1) 添加源

cat << EOF > /etc/apt/sources.list.d/openvz-rhel6.list
deb http://download.openvz.org/debian wheezy main
# deb http://download.openvz.org/debian wheezy-test main
EOF

2) 安装

wget http://ftp.openvz.org/debian/archive.key
sudo apt-key add archive.key
sudo apt-get update
sudo apt-get install "linux-headers-$(uname -r)"

请记住,您只能从“主机”执行此操作,而不能从 OpenVZ VPS 内部执行此操作。这意味着只有主机允许将内核驱动程序加载到内核,并且会影响该主机上的所有容器。

关于为 openvz VM 编译内核模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22387116/

相关文章:

c++ - 可以从用户空间找到 mmap 页面的脏污度吗?

c - Makefile, "nothing to be done for all"错误

c - typedef struct 声明错误?

c - 尝试通过原始 IP 数据包发送数据时的 EMSGSIZE

php - file_get_contents() 返回缓存的页面

java - 如何从 JNI 的现有 c 项目 (concorde) 创建 .dylib

makefile - Waf(构建工具): PHONY targets like Make?

c - 宏连接中的有效预处理器标记

c - 在 C 中引发堆栈下溢

C 比较运算符优先级