10

Linux Kernel 编译踩坑

 3 years ago
source link: https://o0xmuhe.github.io/2019/10/24/Linux-Kernel-%E7%BC%96%E8%AF%91%E8%B8%A9%E5%9D%91/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
a year ago环境配置踩坑5 minutes lesen (Über 703 Worte)

Linux Kernel 编译踩坑

env config

ubuntu 18.04 amd64

gcc-8

works on ubuntu 16.04 and 18.04

1
2
3
4
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-8 g++-8
gcc-8 --version

clang

1
2
3
4
5
6
7
8
9
R=348261
svn co -r $R http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm
(cd tools && svn co -r $R http://llvm.org/svn/llvm-project/cfe/trunk clang)
(cd projects && svn co -r $R http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt)
mkdir llvm_cmake_build && cd llvm_cmake_build
cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON ../
make -j64 clang
export KMSAN_CLANG_PATH=`pwd`/bin/clang

文档里-j64真的奢华…

qemu kvm

这部分推荐手动编译最新版本/较新版本,直接apt安装的版本有点老,后面可能有影响。

基本没啥坑,就是耗时耗力 :(

build kernel

mainline

1
2
3
make menuconfig
cp path/to/config .config
make bzImage -j2

linux-next

一样的编译过程,next只是代码比较激进的版本

1
2
3
make menuconfig
cp path/to/config .config
make bzImage -j2

kmsan

跟着readme走就行了

遇到一个坑点,

1
2
3
export KMSAN_CLANG_PATH=`pwd`/bin/clang
...
make CC=$KMSAN_CLANG_PATH -j64 -k 2>&1 | tee build.log

直接这么编译有问题,试了几次都不行,提示说编译器不支持xxxx这类问题。
最后我尝试了:

1
export CC=/path/to/clang

之后直接编译就没问题了

1
make bzImage -j2

这个版本我后面弄了个更新版本的clang也可以直接编译过。

ktsan

编译没啥坑,就是注意.config文件别乱改。

1
2
3
4
5
6
7
git clone https://github.com/google/ktsan.git
cd ktsan/
make defconfig
make kvmconfig
scripts/config -e KTSAN -e SLAB -d SLUB -e DEBUG_INFO
yes '' | make oldconfig
make -j2 LOCALVERSION=-tsan

run kernel

创建文件系统

1
2
3
4
5
6
7
8
9
IMG=qemu-image.img
DIR=mount-point.dir
qemu-img create $IMG 10g
mkfs.ext2 $IMG
mkdir $DIR
sudo mount -o loop $IMG $DIR
sudo debootstrap --arch amd64 jessie $DIR
sudo umount $DIR
rmdir $DIR

这样直接创建不行,需要在取消挂载之前设置好新文件系统里的用户名密码。

1
sudo chroot mount-point.dir /bin/bash

进去之后,直接修改root密码就行了

1
# passwd root

其实还能做其他的设置,但是只是为了验证加载的内核是否正常,只需要登录进去就ok了。

基本上都能用这个命令起来

1
2
3
4
5
6
7
sudo qemu-system-x86_64 \
-enable-kvm \
-m 2G -smp 2 \
-hda qemu-image.img \
-kernel linux-5.4-rc4/arch/x86/boot/bzImage \
-append "debug root=/dev/sda console=ttyS0" \
-nographic

除了ktsan

1
2
3
4
5
6
7
qemu-system-x86_64 \
-drive file=qemu-image.img,index=0 \
-m 24G -smp 4 \
-net user,hostfwd=tcp::10022-:22 -net nic \
-nographic \
-kernel arch/x86/boot/bzImage -append "console=ttyS0 root=/dev/sda rw debug earlyprintk=serial slub_debug=QUZ"\
-enable-kvm -cpu host

我用它wiki里的命令无法成功启动,各种panic,或者就是文件系统挂载出问题,我换了我原本的ext2的img之后多试几次,就能正常工作了。

我这里给的是24G内存。

reference

ktsan
kmsan
linux-kernel


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK