View on GitHub

富乎 · 地问


avatar
辗转探寻为富乎?《天问》无解向地问!

<<< 返回主页

嵌入式根文件系统构建实录

1、构建工具的考量

1.1 Yocto

1.2 BusyBox

1.3 Buildroot

2、Buildroot构建根文件系统过程

2.1 环境及组件版本

2.2 操作步骤简述(理想情况下)

$ mkdir -p ~/src
$ tar -jxvf buildroot-2019.02.6.tar.bz2 -C ~/src/
$ cd ~/src/buildroot-2019.02.6/
$ make menuconfig # 详见附录的参考配置;结果保存在:.config
$ make busybox-menuconfig # 详见附录的参考配置;结果保存在:output/build/busybox-1.29.3/.config
$ time make # 结果保存在output/images目录

2.3 实际排雷详情(仅供参考)

  1. 屏蔽有问题的源码包的补丁:

     $ for i in fakeroot m4
     do
         ls package/${i}/*.patch | while read j
         do
             mv ${j} ${j}.unused && echo "因更换不同版本的源码包而屏蔽原补丁:${j}"
         done
     done
    
  2. 打开package/fakeroot/fakeroot.mk,修改fakeroot源码包版本号, 解决_STAT_VER undeclared报错:

     #FAKEROOT_VERSION = 1.20.2
     FAKEROOT_VERSION = 1.31
     #FAKEROOT_SOURCE = fakeroot_$(FAKEROOT_VERSION).orig.tar.bz2
     FAKEROOT_SOURCE = fakeroot_$(FAKEROOT_VERSION).orig.tar.gz
     #FAKEROOT_SITE = http://snapshot.debian.org/archive/debian/20141005T221953Z/pool/main/f/fakeroot
     FAKEROOT_SITE = https://snapshot.debian.org/archive/debian/20230315T145325Z/pool/main/f/fakeroot
    
  3. 打开package/fakeroot/fakeroot.hash,修改源码包的散列码(列与列之间最好以水平制表符隔开):

     # From http://snapshot.debian.org/package/fakeroot/1.20.2-1/
     # From http://snapshot.debian.org/package/fakeroot/1.31-1/
     #sha1	367040df07043edb630942b21939e493f3fad888	fakeroot_1.20.2.orig.tar.bz2
     sha1	84f38c917f330ec1776d88650566eb805f03a590	fakeroot_1.31.orig.tar.gz
     # Calculated based on the hash above
     #sha256	7c0a164d19db3efa9e802e0fc7cdfeff70ec6d26cdbdc4338c9c2823c5ea230c	fakeroot_1.20.2.orig.tar.bz2
     sha256	63886d41e11c56c7170b9d9331cca086421b350d257338ef14daad98f77e202f	fakeroot_1.31.orig.tar.gz
    
  4. 打开package/m4/m4.mk,修改m4源码包版本号,解决SIGSTKSZ报错:

     #M4_VERSION = 1.4.18
     M4_VERSION = 1.4.19
    
  5. 打开package/m4/m4.hash,修改源码包的散列码(列与列之间最好以水平制表符隔开):

     # Locally calculated after checking pgp signature
     #sha256	f2c1e86ca0a404ff281631bdc8377638992744b175afb806e25871a24a934e07	m4-1.4.18.tar.xz
     sha256	63aede5c6d33b6d9b13511cd0be2cac046f2e70fd0a07aa9573a04a82783af96	m4-1.4.19.tar.xz
     # License files, locally calculated
     #sha256	8ceb4b9ee5adedde47b31e975c1d90c73ad27b6b165a1dcd80c7c545eb65b903	COPYING
     sha256	3972dc9744f6499f0f9b2dbf76696f2ae7ad8af9b23dde66d6af86c9dfb36986	COPYING
    
  6. 在编译cmake时,若报“‘numeric_limits’ is not a member of ‘std’”的错, 则打开output/build/host-cmake-3.8.2/Source/cmServerProtocol.cxx,添加以下头文件:

     #include &lt;limits&gt;
    

2.4 添加自定义目录及文件(实际做产品的刚需)

  1. 执行make menuconfig,然后进行以下配置:
     System configuration  --->
         (overlay) Root filesystem overlay directories
    
  2. 创建自定义目录及文件:

     $ mkdir overlay # 必需:创建上一步骤指定的目录,也可以是buildroot以外的目录,但上一步骤就必须使用绝对路径
     $ mkdir overlay/root # 以下均是测试示例,根据实际情况而定,可创建多个目录及文件,通常是开机启动脚本和Shell配置
     $ echo haha > overlay/root/.bashrc
    

    更多细节详见官方文档。 若链接失效,可点击此处 查看备份文档(9.5. Customizing the generated target filesystem)。

2.5 添加U-Boot命令以方便调试

=> setenv root_path /home/foo/src/buildroot-2019.02.6/output/target
=> setenv nfsargs 'setenv bootargs "console=${console},${baudrate} root=/dev/nfs rw nfsroot=${serverip}:${root_path},v3,tcp ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}::${device}"'
=> setenv nfsboot 'run nfsargs; run debugboot;'
=> setenv bootcmd 'run nfsboot'

其中,${console}${baudrate}${device}一般已经预先定义好, debugboot的定义则详见此说明

此外,为防止某些厂商在移植U-Boot时画蛇添足地为某些场景添加保存环境变量的操作, 最好将bootargs内容备份一下,即:

=> setenv bootargs-bk 'bootargs原先的内容'

最后,不要忘记保存:

=> saveenv

3、注意事项

4、附录:menuconfig配置参考

4.1 Buildroot主要配置项

Target options  --->
    Target Architecture (ARM (little endian))  --->
    Target Binary Format (ELF)  --->
    Target Architecture Variant (cortex-A7)  --->
    Target ABI (EABIhf)  --->
    Floating point strategy (NEON/VFPv4)  --->
    ARM instruction set (ARM)  --->

Toolchain  --->
        Toolchain type (External toolchain)  --->
        *** Toolchain External Options ***
        Toolchain (Custom toolchain)  --->
        Toolchain origin (Pre-installed toolchain)  --->
    (/home/foo/bin/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux) Toolchain path
    ($(ARCH)-linux-gnueabihf) Toolchain prefix
        External toolchain gcc version (4.9.x)  --->
        External toolchain kernel headers series (3.1.x)  --->
        External toolchain C library (glibc/eglibc)  --->
    [*] Toolchain has SSP support?
    [*] Toolchain has RPC support?
    [*] Toolchain has C++ support?
    [ ] Toolchain has Fortran support?
    ()  Extra toolchain libraries to be copied to target
    [*] Copy gdb server to the Target
        *** Toolchain Generic Options ***
    [ ] Copy gconv libraries
    [*] Enable MMU support
    ()  Target Optimizations
    ()  Target linker options
    [ ] Register toolchain within Eclipse Buildroot plug-in

System configuration  --->
        Root FS skeleton (default target skeleton)  --->
    (iMX6ULL) System hostname
    (Welcome to iMX6ULL) System banner
        Passwords encoding (sha-256)  --->
        Init system (BusyBox)  --->
        /dev management (Dynamic using devtmpfs + eudev)  --->
    (system/device_table.txt) Path to the permission tables
    [ ] support extended attributes in device tables
    [ ] Use symlinks to /usr for /bin, /sbin and /lib
    [*] Enable root login with password
    (123456) Root password
        /bin/sh (busybox' default shell)  --->
    [*] Run a getty (login prompt) after boot  --->
    [ ] remount root filesystem read-write during boot
    ()  Network interface to configure through DHCP
    (/bin:/sbin:/usr/bin:/usr/sbin) Set the system's default PATH
    [*] Purge unwanted locales
    (C en_US zh_CN) Locales to keep
    ()  Generate locale data
    [ ] Enable Native Language Support (NLS)
    [*] Install timezone info
    (default) timezone list
    (Etc/UTC) default local time

Kernel  --->
    [ ] Linux Kernel

Target packages  --->
    -*- BusyBox
    [ ]   Show packages that are also provided by busybox
        Debugging, profiling and benchmark  --->
            [*] gdb
            [*]   full debugger
            [*]   TUI support
            [*] kexec
            [*]   zlib support
            [*] strace
            [*] valgrind
            [*]   < Sub-items of valgrind ... >
        Filesystem and flash utilities  --->
            [*] mtd, jffs2 and ubi/ubifs tools
                  *** MTD tools selection ***
            [*]   flashcp
            [*]   flash_erase
            [*]   flash_lock
            [*]   flash_unlock
            [*]   nanddump
            [*]   nandwrite
            [*]   ubiattach
            [*]   ubicrc32
            [*]   ubidetach
            [*]   ubiformat
            [*]   ubimkvol
            [*]   ubinfo
            [*]   ubinize
            [*]   ubirename
            [*]   ubirmvol
            [*]   ubirsvol
            [*]   ubiupdatevol
            [*]   ubiblock
        Networking applications  --->
            [*] can-utils
            [*] iftop
            [*] ifupdown scripts
            [*] iperf
            [*] nload
            [*] openssh
            [*] tcpdump
        Shell and utilities  --->
            [*] file
            [*] logrotate

Filesystem images  --->
    [*] ubi image containing an ubifs root filesystem
    (0x20000) physical eraseblock size
    (2048) sub-page size
    [ ]   Use custom config file
    ()    Additional ubinize options
    -*- ubifs root filesystem
    (0x1f000) logical eraseblock size
    (0x800) minimum I/O unit size
    (2048) maximum logical eraseblock count
          ubifs runtime compression (lzo)  --->
          Compression method (no compression)  --->
    ()    Additional mkfs.ubifs options

Bootloaders  --->
    [ ] afboot-stm32
    [ ] Barebox
    [ ] grub2
    [ ] mxs-bootlets
    [ ] s500-bootloader
    [ ] ts4800-mbrboot
    [ ] U-Boot

# 其余配置项可自行选取,或保持默认

4.2 BusyBox主要参数

Settings  --->
    [*] Enable compatibility for full-blown desktop systems
    --- Build Options
    [ ] Build static binary (no shared libs)
    [ ]   Build position independent executable
    --- Library Tuning
    [*] Command line editing
    (1024) Maximum length of input
    [*]   vi-style line editing commands
    (1000) History size
    [ ]   History saving
    [*]   Reverse history search
    [*] Support Unicode
    [*]   Check $LC_ALL, $LC_CTYPE and $LANG environment variables
    (63)  Character code to substitute unprintable characters with
    (0)   Range of supported Unicode characters
    [ ]   Allow zero-width Unicode characters on output
    [*]   Allow wide Unicode characters on output

Coreutils  --->
    [*] 几乎可以全选

Linux Module Utilities  --->
    [ ] Simplified modutils

Linux System Utilities  --->
    [ ] mdev (16 kb)

# 其余配置项可自行选取,或保持默认