我有一个 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/