LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

Buildroot使用记录

 关键词:rootfs、BR2_EXTERNAL等等。

 记录buildroot使用各种方法,以及解决的问题。

1 定制文件系统方法

1.1 根文件系统覆盖(BR2_ROOTFS_OVERLAY)

将BR2_ROOTFS_OVERLAY指向的目录覆盖到output/target根文件系统。还可以通过都好间隔,指定多个目录。

配置方式:

System configuration
    ->Root filesystem overlay directories

1.2 post-build脚本处理(BR2_ROOTFS_POST_BUILD_SCRIPT)

post-build脚本在所有buildroot编译结束之后,文件系统打包之前被调用。可以通过此脚本删除或者修改目标文件系统内容。

如果post-build脚本需要传入参数,可以通过BR2_ROOTFS_POST_SCRIPT_ARGS指定。

对其配置如下:

System configuration
    ->Custom scripts to run before creating filesystem images
    ->Extra arguments passed to custom scripts

在脚本中可能需要使用如下环境变量:

BR2_CONFIG: the path to the Buildroot .config file
CONFIG_DIR: the directory containing the .config file, and therefore the top-level Buildroot Makefile to use (which is correct for both in-tree and out-of-tree builds)
HOST_DIR, STAGING_DIR, TARGET_DIR: see Section 18.6.2, “generic-package reference”
BUILD_DIR: the directory where packages are extracted and built
BINARIES_DIR: the place where all binary files (aka images) are stored
BASE_DIR: the base output directory

1.3 rootfs skeleton

根文件系统是从一个skeleton开始的,然后逐渐将package编译结果安装到output/target中。

buildroot提供一个默认skeleton,以及增加自定义skeleton的方式。

System configuration
    ->Root FS skeleton
        ->default target skeleton
        ->custom target skeleton(BR2_ROOTFS_SKELETON_CUSTOM BR2_ROOTFS_SKELETON_CUSTOM_PATH )

1.4 创建设备以及设置文件权限和所属

1.4.1 BR2_ROOTFS_DEVICE_TABLE

BR2_ROOTFS_DEVICE_TABLE中定义了待执行的设备文件权限列表,多个列表可以通过逗号间隔:

System configuration
  ->/dev management
    ->Dynamic using devtmpfs + mdev(BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_MDEV)
->Path to the permission tables(BR2_ROOTFS_DEVICE_TABLE)

BR2_ROOTFS_DEVICE_TABLE的配置文件会被makedevs调用:

# See package/makedevs/README for details
#
# This device table is used to assign proper ownership and permissions
# on various files. It doesn't create any device file, as it is used
# in both static device configurations (where /dev/ is static) and in
# dynamic configurations (where devtmpfs, mdev or udev are used).
#
# <name>                <type>    <mode>    <uid>    <gid>    <major>    <minor>    <start>    <inc>    <count>
/dev                    d    755    0    0    -    -    -    -    -
/tmp                    d    1777    0    0    -    -    -    -    -
/etc                    d    755    0    0    -    -    -    -    -
/root                    d    700    0    0    -    -    -    -    -
/var/www                d    755    33    33    -    -    -    -    -
/etc/shadow                f    600    0    0    -    -    -    -    -
/etc/passwd                f    644    0    0    -    -    -    -    -
/etc/network/if-up.d            d    755    0    0    -    -    -    -    -
/etc/network/if-pre-up.d        d    755    0    0    -    -    -    -    -
/etc/network/if-down.d            d    755    0    0    -    -    -    -    -
/etc/network/if-post-down.d        d    755    0    0    -    -    -    -    -
/dev/null                c    666    0    0    1    3    -    -    -

在fs/common.mk中rootfs-common编译子命令将ROOTFS_DEVICE_TABLES内容写到ROOTFS_FULL_DEVICES_TABLE中:

rootfs-common: $(ROOTFS_COMMON_DEPENDENCIES) target-finalize
    @$(call MESSAGE,"Generating root filesystems common tables")
    rm -rf $(FS_DIR)
    mkdir -p $(FS_DIR)

    $(call PRINTF,$(PACKAGES_USERS)) >> $(ROOTFS_FULL_USERS_TABLE)
ifneq ($(ROOTFS_USERS_TABLES),)
    cat $(ROOTFS_USERS_TABLES) >> $(ROOTFS_FULL_USERS_TABLE)
endif

    $(call PRINTF,$(PACKAGES_PERMISSIONS_TABLE)) > $(ROOTFS_FULL_DEVICES_TABLE)
ifneq ($(ROOTFS_DEVICE_TABLES),)
    cat $(ROOTFS_DEVICE_TABLES) >> $(ROOTFS_FULL_DEVICES_TABLE)
endif
ifeq ($(BR2_ROOTFS_DEVICE_CREATION_STATIC),y)
    $(call PRINTF,$(PACKAGES_DEVICES_TABLE)) >> $(ROOTFS_FULL_DEVICES_TABLE)
endif

1.4.2 makedevs

makedevs使用方法:

makedevs: [-d device_table] rootdir

Creates a batch of special files as specified in a device table.
Device table entries take the form of:
name type mode user group major minor start increment count

Where name is the file name,  type can be one of:
      f       A regular file
      d       Directory
      r       Directory recursively
      c       Character special device file
      b       Block special device file
      p       Fifo (named pipe)
uid is the user id for the target file, gid is the group id for the target file.
The rest of the entries (major, minor, etc) apply to to device special files.
A
'-' may be used for blank entries.

1.4.3 fakeroot调用makedevs

在fakeroot脚本中调用makedevs生成根文件系统:

/xxx/output/host/bin/makedevs -d /xxx/output/build/buildroot-fs/full_devices_table.txt /xxx/output/build/buildroot-fs/cpio/target

1.5 创建用户

BR2_ROOTFS_USERS_TABLES 配置用户列表,makeusers命令进行创建。配置方法:

System configuration
    ->Path to the users tables

1.6 post-image脚本

post-image脚本由BR2_ROOTFS_POST_IMAGE_SCRIPT 指定,在镜像生成之后执行。

配置方法:

System configuration
    ->Custom scripts to run after creating filesystem images

1.7 文件系统类型选择

进入make menuconfig->Filesystem images配置文件系统类型:

2 BR2_EXTERNAL

在Makefile中增加BR2_EXTERNAL定义,buildroot则会使用manu-folder中的配置:

export BR2_EXTERNAL=manu-folder

manu-folder中必须包含如下三个文件:

external.desc
external.mk
Config.in

external.desc中填写name和desc两个值:

name: xxx
desc: xxx board config

external.mk中定义了external的mk文件,可以通过include包括各package:

include $(sort $(wildcard $(BR2_EXTERNAL_XXX_PATH)/package/*/*.mk))

Config.in中提供了宏定义和选择。可以通过source将子目录Config.in包含:

source "package1/Config.in"
Source "package2/Config.in"

在make menuconfig的External options中进行配置选择。

除了以上三个文件,还可能包括如下目录:

board--存放不同board特有配置。
configs--存放defconfig。
package--存放新增package配置。

3 一些优化

3.1 fstab挂在debugfs

修改package/skeleton-init-sysv/skeleton/etc/fstab文件:

# <file system>    <mount pt>    <type>    <options>    <dump>    <pass>
/dev/root    /        ext2    rw,noauto    0    1
proc        /proc        proc    defaults    0    0
devpts        /dev/pts    devpts    defaults,gid=5,mode=620,ptmxmode=0666    0    0
tmpfs        /dev/shm    tmpfs    mode=0777    0    0
tmpfs        /tmp        tmpfs    mode=1777    0    0
tmpfs        /run        tmpfs    mode=0755,nosuid,nodev    0    0
sysfs        /sys        sysfs    defaults    0    0
debugfs        /sys/kernel/debug    debugfs    defaults    0    0

3.2 以root用户直接登录

通过menuconfig设置getty附加参数:

System configuration
    ->Run a getty (login prompt) after boot
        ->other options to pass to getty

 

设置参数为“-n -l /etc/autologin”。

再inittab中启动console的命令就会变成:

console::respawn:/sbin/getty -L -n -l /etc/autologin console 0 vt100 # GENERIC_SERIAL

autologin中是一个脚本:

#!/bin/sh
/bin/login -f root

 

编译问题解决

问题1 Incorrect selection of kernel headers

>> toolchain-external-custom  Patching
>>> toolchain-external-custom  Configuring
Incorrect selection of kernel headers: expected 5.4.x, got 4.20.x
make[1]: *** [package/pkg-generic.mk:283: /xxx/output/build/toolchain-external-custom/.stamp_configured] Error 1
make: *** [Makefile:84: _all] Error 2

解决方法:

Toolchain->External toolchain kernel headers series->4.20.x

或者修改工具链的libc/usr/include/linux/version.h文件:

#define LINUX_VERSION_CODE 267277
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))

比如所需要的版本为5.4.31,那么对应的16进制应为0x5041f,改成如下:

#define LINUX_VERSION_CODE 328735
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))

 

posted on 2023-04-23 23:59  ArnoldLu  阅读(864)  评论(0编辑  收藏  举报

导航