林光伟 原创作品转载请注明出处  《Linux内核分析》MOOC课程http://mooc.study.163.com/course/USTC-1000029000 

 一、准备实验环境

  1、下载内核源码

wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.9.tar.xz

  2、解压内核源码

tar -xvf linux-3.18.9.tar.xz

  3、配制内核,并编译

make i386_defconfig
make -j4

       4、下载init进程源码

git clone https://github.com/mengning/menu

  5、编译init源码

cd menu
gcc -o init linktable.c menu.c test.c -m32 -static -lpthread

  于是就看到了init这个程序,实际上就是一个普通程序,可以当前系统上直接运行看看

    6、创建根文件系统镜像

cd ..
mkdir rootfs
cd rootfs
cp ../menu/init ./
find | cpio -o -Hnewc | gzip -9 > ../rootfs.img

  于是就生成了简易的rootfs.img根文件系统镜像

      7、测试运行该linux系统

cd ..
qemu-system-i386 -kernel linux-3.18.9/arch/x86/boot/bzImage -initrd rootfs.img

     可以看到系统运行起来了,试试输入help命令

 

二、调试代码

       1、为了使编译出来的linux内核能够被调试,需要添加debug选项

make menuconfig
选上
Kernel hacking  --->
          Compile-time checks and compiler options  --->
               [*] Compile the kernel with debug info
make
                

       可以按照说明采用gdb来调试,但这样不是很直观,所以我喜欢用eclieps+gdb来调试,这也可以为后面的课程做铺垫。

 

posted on 2015-03-22 22:29  林德伟  阅读(80)  评论(0)    收藏  举报