首先 ,打开veritas501的博客,然后 一步步来
编译内核
- 下载kernel源代码:https://www.kernel.org/
-
安装些必要的依赖
sudo apt-get update sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc
- 解压后进目录
make menuconfig
ps:可能会有需要flex和bison的错误,apt-get install即可
啥都不改,save即可
进入kernel hacking 勾选以下项目 Kernel debugging Compile-time checks and compiler options —> Compile the kernel with debug info和Compile the kernel with frame pointers KGDB 然后保存退出
ps :
make menuconfig
记得终端窗口留的大一些。。。不然会有提示。。。 -
make bzImage Setup is 17244 bytes (padded to 17408 bytes). System is 7666 kB CRC 5c77cbfe Kernel: arch/x86/boot/bzImage is ready (#1)
似乎还行,才花7min
添加个简单的syscall
来来来,helloworld来了
在源码根目录创建个helloworld
目录
$ pwd
/home/test/test_kernel/linux-xxxxxxx/helloworld
$ tree
.
├── helloworld.c
└── Makefile
$ cat helloworld.c
#include
asmlinkage long sys_helloworld(void){
printk("{==kernel==} hello world\n");
return 0;
}
$ cat Makefile
obj-y=helloworld.o
然后进源码根目录下的Makefile
,添加helloworld/
然后进include/linux/syscalls.h
,添加函数原型
在arch/x86/entry/syscalls/syscall_32.tbl
和arch/x86/entry/syscalls/syscall_64.tbl
里添加系统调用号
之后编译kernel
make bzImage
就能在./arch/x86/boot/
里得到bzImage
编译busybox
照例,官网下载https://busybox.net/
解压进目录
make menuconfig
Settings内选上Build static binary (no shared libs)
make install
编译完后会出现一个_install
目录,之后
$ cd _install
$ mkdir proc
$ mkdir sys
$ touch init
$ chmod +x init
$ cat init
#!/bin/sh
echo "{==DBG==} INIT SCRIPT"
mkdir /tmp
mount -t proc none /proc
mount -t sysfs none /sys
mount -t debugfs none /sys/kernel/debug
mount -t tmpfs none /tmp
# insmod /xxx.ko # load ko
mdev -s # We need this to find /dev/sda later
echo -e "{==DBG==} Boot took $(cut -d' ' -f1 /proc/uptime) seconds"
setsid /bin/cttyhack setuidgid 1000 /bin/sh #normal user
# exec /bin/sh #root
//对了对了,写个测试用例放到_install里吧
$ touch test.c
$ cat test.c
//gcc test.c -static -o test
#include
int main(void){
syscall(1337);
return 0;
}
$ gcc test.c -static -o test
//之后可以写一个脚本来打包rootfs.img
$ cd ../../
$ touch makeimg
$ chmod +x makeimg
$ cat makeimg
#!/bin/sh
echo "Generate rootfs.img"
cd busybox-x.xx.x/_install
find . | cpio -o --format=newc > ../../rootfs.img
$ ls
busybox-x.xx.x linux-x.x.x qemu-x.xx.x makeimg
$ ./makeimg
$ ls
busybox-x.xx.x linux-x.x.x qemu-x.xx.x makeimg rootfs.img
安装qumu
我的话,之前直接sudo apt-get install qemu
似乎不行,所以就源码编译了
wget https://download.qemu.org/qemu-4.1.0.tar.xz
tar xvJf qemu-4.1.0.tar.xz
cd qemu-4.1.0
./configure
make
make install
qemu启动
让我们从veritas501那拷贝一个脚本吧owo
$ ls
busybox-x.xx.x linux-x.x.x qemu-x.xx.x makeimg rootfs.img
$ touch runqemu
$ chmod +x runqemu
$ cat runqemu
#!/bin/sh
qemu-system-x86_64 \
-m 64M \
-kernel /home/test/test_kernel/linux-x.x.x/arch/x86/boot/bzImage \
-initrd /home/test/test_kernel/rootfs.img \
-append "root=/dev/ram rw console=ttyS0 oops=panic panic=1 kalsr" \
-netdev user,id=t0, -device e1000,netdev=t0,id=nic0 \
-nographic \
-monitor /dev/null \
-smp cores=2,threads=1 \
-enable-kvm \
-cpu kvm64,+smep \
# -gdb tcp::1234 \
# -S
之后就启动脚本
$ ./runqemu
完成!完结!撒花!
参考链接
以及,感谢MiGo和徐大哥Aris的指导OWO
以下是还可能出现的问题
因为懒,所以就瞎写了
pkg-config not found
sudo apt-get install pkg-config
glib-2.40 gthread-2.0 is required to compile QEMU
先apt-cache search all|grep glib查找apt软件里面glib的名字,找到glib名字为 libglib2.0-dev
然后再apt install libglib2.0-dev
ERROR: pixman >= 0.21.8 not present.
Please install the pixman devel package.
解决方法:
先apt-cache search pixman找名字,再apt install libpixman-1-dev
记得虚拟机开启CPU虚拟化
VNC server running on 127.0.0.1:5900
sudo apt-get install libsdl1.2-dev
sudo apt-get install gcc libsdl1.2-dev zlib1g-dev libasound2-dev pkg-config libgnutls-dev pciutils-dev
sudo apt-get install libsdl2-dev
sudo apt-get install libsdl2-2.0
sudo apt install libelf-dev
似乎还有个sdl的问题。。。网页关了。。。没记下来。。。自行搜索吧。。。