ubuntu20.04 搭建kernel调试环境第二篇--制作rootfs

rootfs是linux的根文件系统,就是我们看到的 /目录及其中一些重要的子目录,比如/etc ,/lib,/sbin等等,在linux中,一切都是文件,一切都是从/目录开始的,没有rootfs,linux就没法运行。

可以通过buildroot制作rootfs(比较方便,建议用这种方式),也可以通过busybox制作rootfs。

一、buildroot制作rootfs

root@linux:~#wget https://buildroot.org/downloads/buildroot-2020.02.8.tar.gz

root@linux:~#tar zxvf  buildroot-2020.02.8.tar.gz && cd buildroot-2020.02.8

root@linux:~#export ARCH=x86_64

root@linux:~#export FORCE_UNSAFE_CONFIGURE=1

root@linux:~#make menuconfig

Toolchain  --->
    # 编译一些package会用到,比如f2fstools
    [*] Enable WCHAR support

System configuration  --->
     # 系统启动后,密码为空,回车登录shell
     [*] Enable root login with password
          ( )    Root password
      # 设置eth0接口为DHCP,如果不设置,qemu启动kernel镜像后,需要手动配置网络
     (eth0) Network interface to configure through DHCP

Target packages  --->
    Filesystem and flash utilities  --->
        # 格式化f2fs用到
        [*] f2fs-tools

Filesystem images  ---> 
     [ ] ext2/3/4 root filesystem
     # 设置rootfs的文件系统类型
     [*] f2fs root filesystem

root@linux:~#make

编译时间较长,等编译完成后,在output/images目录中生成rootfs.f2fs文件。将该文件拷贝到内核源码目录即可,如下:

root@linux:/home/gsf/linux-5.10.3# ls| grep f2fs

rootfs.f2fs

注意点:

1)修改buildroot-2020.02.8/package目录下的package,重新编译前,需要删除output/build/目录中对应的package,否则编译出来的rootfs.f2fs不会包含改动。

二、busybox制作rootfs

版本:busybox-1.33.0。

编译_install目录

root@linux:/home/gsf/source-code/busybox-1.33.0# export ARCH=arm
root@linux:/home/gsf/source-code/busybox-1.33.0# export CROSS_COMPILE=arm-linux-guneabi-
root@linux:/home/gsf/source-code/busybox-1.33.0# make menuconfig

Settings --> Build Options -->  [*] Build static binary (no shared libs)。若没有编译成静态库,需要将宿主机/bin、/sbin目录中的动态库文件拷贝到_install目录中。为了简化,编译成静态库即可。

root@linux:/home/gsf/source-code/busybox-1.33.0# make install

编译完成后,会在当前目录中生成一个_install目录。将其拷贝到linux源码根目录。

制作rootfs用到的文件

1) 将busybox的提供的example中的etc目录拷贝到_install中,,并稍作修改。

  root@linux:/home/gsf/debug/kernel/linux-5.10.3# cp ../busybox-1.33.0/examples/bootfloppy/etc ./_install/ -R

修改1:./_install/etc/inittab

     tty2::askfirst:-/bin/sh   --------->   ::askfirst:-/bin/sh

 

修改2:./_install/etc/fstab  增加下面3行

    sysfs /sys sysfs defaults 0 0

    tmpfs /dev tmpfs defaults 0 0

    tmpfs /tmp tmpfs defaults 0 0

 

修改3:./_install/etc/ini.d增加2行,注意顺序,改成下面的样子

#! /bin/sh

mkdir /proc  /sys  /tmp

/bin/mount -a

mdev -s

2) 在_install目录中创建基本目录

    root@linux:/home/gsf/debug/kernel/linux-5.10.3/_install# mkdir dev mnt

3)在dev目录创建设备节点:

    mknod console c 5 1
    mknod null c 1 1

编译内核

kernel源码linux 5.10.3。

交叉工具链安装参考“ubuntu20.04 搭建kernel调试环境第一篇--安装系统”。

1)在arch/架构/configs目录中有*_defconfig文件,在内核根目录执行make *_defconfig,比如arm平台:

root@linux:/home/gsf/debug/kernel/linux-5.10.3-x86# export ARCH=arm
root@linux:/home/gsf/debug/kernel/linux-5.10.3-x86# make vexpress_defconfig

2)make menuconfig

General setup --> Initial RAM filesystem and RAM disk (initramfs/initrd) support --> Initramfs source file(s)填入: _install

Kernel hacking --> Generic Kernel Debugging Instruments --> Debug Filesystem选上,也可以根据需要添加其他的debug项

3)编译vmlinux

root@linux:/home/gsf/debug/kernel/linux-5.10.3-x86# export CROSS_COMPILE=arm-linux-gnueabihf-

root@linux:/home/gsf/debug/kernel/linux-5.10.3# make bzImage -j4

三、运行自己编译的kernel

gsf@linux:~/debug/kernel/linux-5.10.3$ qemu-system-arm -nographic -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage -append "rdinit=/linuxrc console=ttyAMA0 loglevel=8" -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb 

注:如果用root用户执行上面命令出现Failed to initialize PA contextXDG_RUNTIME_DIR错误,换成普通用户试试。

--nographic:关闭图形调试,将打印信息输出到串口。

-M  machine type,可通过qemu-system-arm -machine help查询支持哪些machine。

-m 内存大小

-kernel   要运行的额内核image

-append   kernel运行时的命令行参数

-dtb   设备树文件

远程调试时,会用到-S -s选项。

-S: 冻结CPU直至接受到c命令,用于远程gdb调试。

-s:是 -gdb tcp::1234缩写,表示在TCP 1234端口打开一个gdb服务器,用于远程gdb调试。

console=ttyAMA0,用于输出终端信息,终端名称取决于驱动。vexpress-a9平台终端名称是ttyAMA0。

posted @ 2021-08-23 15:36  geshifei  阅读(131)  评论(0编辑  收藏  举报  来源