1 x86 内核
1.1 内核
git clone https://github.com/torvalds/linux.git
make menuconfig64-bit Kernel (CONFIG_64BIT=y)General Setup –> Configure standard kernel features (expert users) –> Enable support for printk (CONFIG_PRINTK=y)General Setup –> Initial RAM filesystem and RAM disk (initramfs/initrd) support (CONFIG_BLK_DEV_INITRD=y)General Setup –>kernel compression mode (Gzip) (CONFIG_RD_GZIP=y)Executable file formats –> Kernel support for ELF binaries (CONFIG_BINFMT_ELF=y)Executable file formats –> Kernel support for scripts starting with #! (CONFIG_BINFMT_SCRIPT=y)File systems –> Pseudo filesystems –> /proc file system support (CONFIG_PROC_FS=y)Device Driver –> Character devices –> Enable TTY (CONFIG_TTY=y)Device Driver –> Character devices –> Serial Drivers –> 8250/16550 and compatible serial support (CONFIG_SERIAL_8250=y)Device Driver –> Character devices –> Serial Drivers –> Console on 8250/16550 and compatible serial port (CONFIG_SERIAL_8250_CONSOLE=y)
生成内核 arch/x86/boot/bzImage,(Size ≈ 800K).
1.2 文件系统
git clone https://github.com/hacker-jie/kernel-utils.git
弹出选择,都选n,生成内核 ./kernel-utils/initramfs.cpio.gz,(Size ≈ 1.3M).
1.3 QEMU测试
qemu-system-x86_64 -kernel [PATH_TO_KERNEL]/arch/x86/boot/bzImage -initrd [PATH_TO_ROOTFS]/initramfs.cpio.gz -m 32M -nographic -append "init=/bin/sh console=ttyS0"
2 树莓派3 (64位)
2.1 内核
git clone https://github.com/raspberrypi/linux.git
make menuconfigCONFIG_BINFMT_ELFCONFIG_BLK_DEV_INITRDCONFIG_RD_GZIPCONFIG_PRINTKCONFIG_TTYCONFIG_PROC_FSCONFIG_DEVTMPFS (optional)CONFIG_DEVTMPFS_MOUNT (optional)CONFIG_ARCH_BCM2835CONFIG_MAILBOXCONFIG_BCM2835_MBOXCONFIG_RASPBERRYPI_FIRMWARECONFIG_SERIAL_AMBA_PL011CONFIG_SERIAL_AMBA_PL011_CONSOLE
生成内核 arch/arm64/boot/Image.
注:编译器用 aarch64-linux-gnu, (sudo apt install -y gcc-aarch64-linux-gnu).
2.2 文件系统
与前述方法一致。
2.3 QEMU测试
qemu-system-aarch64 -M raspi3b -kernel [PATH_TO_KERNEL]/arch/arm64/boot/Image -dtb [PATH_TO_DTB]/bcm2710-rpi-3-b.dtb -initrd [PATH_TO_ROOTFS]/initramfs.cpio.gz -m 1024M -nographic -append "init=/bin/sh console=ttyAMA0,115200"
2.4 tiny_raspi: 用buildroot为树莓派制作最小内核
使用buildroot默认配置raspberrypi3_64_defconfig直接编译的出的镜像文件大小有153M
使用tiny_raspi编译生成一个使用ARM文件系统的最小内核, 以RaspberryPi 3为例,使用buildroot的EXTERNAL机制来定制化。我们需要添加3个文件用以描述外部buildroot树:
2.4.1 编译
git clone https://github.com/calinyara/tiny_raspi.gitcd tiny_raspigit clone git://git.buildroot.net/buildrootmkdir buildmkdir buildroot_dlcd buildmake BR2_EXTERNAL=../tiny_raspi_buildroot/ O=$PWD -C ../buildroot/ tiny_raspberrypi3_64_defconfigmake -j4
编译完成后在build/images目录会生成:Image,rootfs.cpio.gz,bcm2710-rpi-3-b.dtb.
buildroot会自动编译生成rootfs文件系统
2.4.2 QEMU运行
qemu-system-aarch64 -M raspi3b -kernel ./images/Image -dtb ./images/bcm2710-rpi-3-b.dtb -initrd ./images/rootfs.cpio.gz -m 1024 -nographic -append "init=/bin/sh console=ttyAMA0,115200"