裁剪Linux内核,用qemu进行调试

1. 内核编译

  1. 进入到下载好的Linux内核文件中,将配置文件中的选项清空

    make allnoconfig
    
  2. make menuconfig 进入配置文件菜单选项,将以下选择勾选

    64-bit kernel ---> yes
    General setup ---> Initial RAM filesystem and RAM disk (initramfs/initrd) support ---> yes
    General setup ---> Configure standard kernel features ---> Enable support for printk ---> yes
    Executable file formats / Emulations ---> Kernel support for ELF binaries ---> yes
    Executable file formats / Emulations ---> Kernel support for scripts starting with #! ---> yes
    Device Drivers ---> Generic Driver Options ---> Maintain a devtmpfs filesystem to mount at /dev ---> yes
    Device Drivers ---> Generic Driver Options ---> Automount devtmpfs at /dev, after the kernel mounted the rootfs ---> yes
    Device Drivers ---> Character devices ---> Enable TTY ---> yes
    Device Drivers ---> Character devices ---> Serial drivers ---> 8250/16550 and compatible serial support ---> yes
    Device Drivers ---> Character devices ---> Serial drivers ---> Console on 8250/16550 and compatible serial port ---> yes
    File systems ---> Pseudo filesystems ---> /proc file system support ---> yes
    File systems ---> Pseudo filesystems ---> sysfs file system support ---> yes
    

裁剪Linux内核,用qemu进行调试_第1张图片

  1. 编译内核,差不多只有1M

    sudo make -j8
    

    裁剪Linux内核,用qemu进行调试_第2张图片

2. 使用busybox构建根文件系统

  1. 进入busybox文件夹,使用默认的配置文件

    make defconfig
    
  2. make menuconfig 编辑配置文件,一定开启静态编译

    Busybox Settings ---> Build Options ---> Build BusyBox as a static binary (no shared libs) ---> yes
    
  3. 编译busybox

    time make -j 8
    
  4. 安装busybox, install之后出现_install 文件夹

    make install
    
  5. cd _install 创建一些文件夹以及文件

    • mkdir -p proc sys dev etc etc/init.d lib tmp
      
    • ln -sf linuxrc init
      
    • cat > etc/init.d/rcS <<EOF
      #!/bin/sh
      mount -t proc none /proc
      mount -t sysfs none /sys
      /sbin/mdev -s
      ifconfig lo up
      EOF
      
    • chmod +x etc/init.d/rcS
      
    • cat > etc/inittab <<EOF
      # /etc/inittab
      ::sysinit:/etc/init.d/rcS
      ::askfirst:-/bin/sh
      ::ctrlaltdel:/sbin/reboot
      ::shutdown:/bin/umount -a -r
      EOF
      
  6. 使用cpio生成根文件系统

    find . -print0 | cpio --null -ov --format=newc   | gzip -9 > ../initramfs.cpio.gz
    
  7. busybox文件夹中,解压刚才生成的initramfs.cpio.gzinitramfs.cpio

3. 使用qemu启动编译好的内核

  • qemu-system-x86_64 -kernel ~/Desktop/linux-4.14/arch/x86/boot/bzImage -initrd initramfs.cpio 
    

    裁剪Linux内核,用qemu进行调试_第3张图片

你可能感兴趣的:(Linux内核)