后面编译的是 4.2.2 在 gcc4.8 上编译4.14.14 时报不支持堆栈保护 反正都差不多就先编译了 4.2.2
1.下载 linux 内核源码
2.解压放到 /usr/src
3.创建软连接 (不一定非得打文件放到 /usr/src 看个人我直接放桌面)
[email protected]:~# cd /usr/src
[email protected]:/usr/src# ls
linux-4.14.14 linux-headers-4.2.0-27-generic
linux-headers-4.2.0-27
[email protected]:/usr/src# ln -s linux-4.14.14 linux
[email protected]:/usr/src#
4.安装必要的工具
[email protected]:/usr/src# sudo apt-get install build-essential kernel-package libncurses5-dev fakeroot
sudo aptitude install libqt3-headers libqt3-mt-dev libqt3-compat-headers libqt3-mt
(如果找不到命令,请先sudo?apt-get install?aptitude)
5.内核修改
# cd /usr/include/
# rm -r asm linux scsi
# ln -s /usr/src/linux/include/asm-generic asm
# ln -s /usr/src/linux/include/linux linux
# ln -s /usr/src/linux/include/scsi scsi
[email protected]:/usr/src# cd /usr/include/
[email protected]:/usr/include# rm -r asm linux scsi
[email protected]:/usr/include# ln -s /usr/src/linux/include/asm-generic asm
[email protected]:/usr/include# ln -s /usr/src/linux/include/linux linux
[email protected]:/usr/include# ln -s /usr/src/linux/include/scsi scsi
[email protected]:/usr/include#
6.添加新的系统调用
找到系统调用号的表(下面的linux 是一个刚才设置的软连接 )
注意版本不同文件地方不同
linux-2.6.32.65 在文件:/usr/src/linux/arch/x86/kernel/syscall_table_32.S
linux-3.5.4 在文件:/usr/src/linux/arch/x86/syscalls/syscall_64.tbl
linux-4.14 在文件:/usr/src/linux/arch/x86/entry/syscalls/syscall_64.tbl
这里是 4.14.14
添加一个系统调用号 (548)
548 x32 hello sys_hello
声明系统调用服务例程
打开:include/linux/syscalls.h 也要注意版本 不同版本不同
添加
asmlinkage long sys_hello(const char __user *name);
定义系统调用服务例程
asmlinkage long sys_hello(const char __user *name)
{
char *name_kd;
long ret;
name_kd = strndup_user(name, PAGE_SIZE);
if (IS_ERR(name_kd))
{
ret = PTR_ERR(name);
goto error;
}
printk("Hello, %s!\n", name_kd);
ret = 0;
error:
return ret;
}
kernel/sys.c 只要不插函数 头包含中间 其它随便
开始编译内核:
cd /usr/src/linux
[email protected]:/usr/include# cd /usr/src/linux
[email protected]:/usr/src/linux# ls
arch Documentation ipc Makefile security
block drivers Kbuild mm sound
certs firmware Kconfig net tools
COPYING fs kernel README usr
CREDITS include lib samples virt
crypto init MAINTAINERS scripts
[email protected]:/usr/src/linux#
输入 make mrproper
输入 make menuconfig (根据需要选择相关配置)可能报错 找不到linux/limits.h
直接安装apt-get install linux-libc-dev:amd64
来自 :https://blog.csdn.net/u014655590/article/details/38854555
可能又报错 scripts/kconfig/lxdialog/dialog.h:32: fatal error: curses.h: No such file or directory
解决:
sudo apt-get install libncurses5-dev
出现
不想搞直接保存就好 来自:https://blog.csdn.net/mrzhouxiaofei/article/details/79140435
make 编译
开始编译
编译完后
[email protected]:~/Desktop/linux-4.2.2# ls
arch crypto fs Kbuild MAINTAINERS modules.order REPORTING-BUGS signing_key.priv tools vmlinux-gdb.py
block Documentation include Kconfig Makefile Module.symvers samples signing_key.x509 usr vmlinux.o
COPYING drivers init kernel mm net scripts sound virt x509.genkey
CREDITS firmware ipc lib modules.builtin README security System.map vmlinux
[email protected]:~/Desktop/linux-4.2.2#
安装:
make modules_install install
可能报错:要安装
sudo apt install libssl-dev
scripts/sign-file.c:25:30: fatal error: openssl/opensslv.h: No such file or directory
#include <openssl/opensslv.h>
^
compilation terminated.
scripts/extract-cert.c:21:25: fatal error: openssl/bio.h: No such file or directory
#include <openssl/bio.h>
^
compilation terminated.
make[1]: *** [scripts/extract-cert] Error 1
make[1]: *** Waiting for unfinished jobs....
CC arch/x86/kernel/asm-offsets.s
make[1]: *** [scripts/sign-file] Error 1
HOSTLD scripts/mod/modpost
make: *** [scripts] Error 2
make: *** Waiting for unfinished jobs....
CHK include/generated/asm-offsets.h
UPD include/generated/asm-offsets.h
CALL scripts/checksyscalls.sh
[email protected]:~/Desktop/linux-4.14.14# sudo apt install libssl-dev
修改 grub 文件
gedit /etc/default/grub
注释掉:
#GRUB_HIDDEN_TIMEOUT=0
更新
sudo update-grub2
[email protected]:~/Desktop/linux-4.2.2# gedit /etc/default/grub
[email protected]:~/Desktop/linux-4.2.2# sudo update-grub2
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.2.2
Found initrd image: /boot/initrd.img-4.2.2
Found linux image: /boot/vmlinuz-4.2.0-27-generic
Found initrd image: /boot/initrd.img-4.2.0-27-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
done
[email protected]:~/Desktop/linux-4.2.2#
更换内核:
参考:
https://www.jianshu.com/p/b2d5fa8af581
https://www.jianshu.com/p/a6c33bf39067
原文地址:http://blog.51cto.com/haidragon/2172000