Buildroot的简单使用--使用buildroot创建嵌入式系统和交叉编译工具链
Buildroot 是什么
Buildroot是Linux平台上一个构建嵌入式Linux系统的框架。它可以让你从toolchain,boot,kernel到文件系统及各类应用(如qt,gstream,busybox)一站式make,省去了你编译工具和内核版本的不匹配,glibc库不对啦,每次编译都要去改arch cross_compile等N多烦恼.你也不用在去瞎找各种tarball下载了,它都给你提供了官方的下载途径.总之就是省心省力.具体可看官网说明.
安装
必须安装的工具包括:
which
sed
make (version 3.81 or any later)
binutils
build-essential (only for Debian based systems)
gcc (version 4.8 or any later)
g++ (version 4.8 or any later)
bash
patch
gzip
bzip2
perl (version 5.8.7 or any later)
tar
cpio
unzip
rsync
file (must be in /usr/bin/file)
bc
wget
为了使用它的图形界面,需要安装一些额外的库。我发主要使用menuconfig,需要libncurses5-dev 库
Ubuntu上:
~$ sudo apt install git build-essential wget cpio unzip rsync bc libncurses5-dev screen
工具基本上就安装全了。
简单使用 -- 创建一个qemu虚拟机
环境:ubuntu20 桌面版+buildroot-2020.02.4.tar.gz
在ubuntu16桌面版+ buildroot-2020.02.3.tar.gz,总是出现各种各样的错误。。换成20版本就搞定了。。
注意:
一定要把vpn环境配置好。否则make的时候几乎不能搞不定。
在虚拟机里make的时候,把内存调的大一点,否则经常出现页错误。
手册上建议buildroot的所有操作都是普通用户操作,不需要root权限。
在config目录下,有很多预先定义好的配置,就是xxx_defconfig文件。例如qemu_mips32r6_malta_defconfig ,在 buildroot根目录下
make qemu_mips32r2el_malta_defconfig
就导入了一个适合qume上运行的mips的配置。Once executed, this command will generate the file .config, which contains all the packages, kernel, toolchain and properties needed for our image.
make list-defconfigs 可以查看所有的defconfig文件。
导入之后,再调用menuconfig界面进行微调。很多时候不需要怎么微调了。
$ make menuconfig
$ make menuconfig 跳出配置界面。注意这个界面的退出是 Press <Esc><Esc> to exit,不然找不到退出的位置。。。。
可配置以下的各个选项:
Target options --->选择目标板架构特性。
Build options --->配置编译选项。
Toolchain ---> 配置交叉工具链,使用buildroot自己编译制作工具链还是使用外部提供。
System configuration --->
Kernel ---> 配置内核版本
Target packages --->配置应用
Filesystem images --->
Bootloaders --->
Host utilities --->
Legacy config options --->
下图是qemu_mips32r6_malta_defconfig这个配置文件中 target options的配置
关于tool chain,需要注意的是的kernel headers这一项。这里制定编译toolchain时,用到的linux头文件的版本。
网上很多文章都说错了,不是上位机的uname看到的那一个。。。
编译C库的时候会去访问Linux kernel headers(*.h)文件,就是这些头文件定义了用户空间的程序与内核之间的交互接口(系统调用,数据结构等),重点是这个库接口是向后兼容,也就是说你不知道选哪个就选旧版的吧.选太新的是不能识别旧版的内核通信,但选旧版头文件仍可以和新版内核通信,详细说明如下:
Change the version of the Linux kernel headers used to build the toolchain. This item deserves a few explanations. In the process of building a cross-compilation toolchain, the C library is being built. This library provides the interface between userspace applications and the Linux kernel. In order to know how to "talk" to the Linux kernel, the C library needs to have access to the Linux kernel headers (i.e. the .h files from the kernel), which define the interface between userspace and the kernel (system calls, data structures, etc.). Since this interface is backward compatible, the version of the Linux kernel headers used to build your toolchain do not need to match exactly the version of the Linux kernel you intend to run on your embedded system. They only need to have a version equal or older to the version of the Linux kernel you intend to run. If you use kernel headers that are more recent than the Linux kernel you run on your embedded system, then the C library might be using interfaces that are not provided by your Linux kernel. 做过交叉编译工具的都知道,就是要尽量和要编译的内核的版本一致,或者低一些版本的。
这里选择 same as kernel being built,Custom kernel headers series条目就会自动出现并且和当前kernel版本一致。(如果是直接 make menuconfig ,没有选用某个defconfig,也就没有same as kernel being built这个了)
关于C library, uClibc是专为嵌入式简化的C库,小巧精简,但不兼容glibc,是独立的实现的. 这个选项也很重要,需要注意目标系统上是哪个c库。
Once everything is configured, the configuration tool generates a .config file that contains the entire configuration. This file will be read by the top-level Makefile.
开始make,要等挺长时间的。
$ make
Note: Don’t use the -j parameter of make here, buildroot will figure that out on its own instead.
Make完成之后,Buildroot output is stored in a single directory, output/. This directory contains several subdirectories:
images/ 生成的内核和根文件系统.
build/ 构建过程中的所有的组件,各种程序,一个文件夹就是一个程序.
host/ contains both the tools built for the host, and the sysroot of the target toolchain. The former is an installation of tools compiled for the host that are needed for the proper execution of Buildroot, including the cross-compilation toolchain. The latter is a hierarchy similar to a root filesystem hierarchy. It contains the headers and libraries of all user-space packages that provide and install libraries used by other packages. However, this directory is not intended to be the root filesystem for the target: it contains a lot of development files, unstripped binaries and libraries that make it far too big for an embedded system. These development files are used to compile libraries and applications for the target that depend on other libraries.
交叉编译工具链默认存放于 output/host/. 把 output/host/bin/添加到 PATH 中就可以方便的使用了。也可以 make sdk 把相关工具打包。
staging/ is a symlink to the target toolchain sysroot inside host/, which exists for backwards compatibility.
target/ which contains almost the complete root filesystem for the target: everything needed is present except the device files in /dev/ (Buildroot can’t create them because Buildroot doesn’t run as root and doesn’t want to run as root). Also, it doesn’t have the correct permissions (e.g. setuid for the busybox binary). Therefore, this directory should not be used on your target. Instead, you should use one of the images built in the images/ directory. If you need an extracted image of the root filesystem for booting over NFS, then use the tarball image generated in images/ and extract it as root. Compared to staging/, target/ contains only the files and libraries needed to run the selected target applications: the development files (headers, etc.) are not present, the binaries are stripped.
然后就可以在qemu里启动linux了。启动的命令可以在 /board/qemu/mips32r2el-malt/readme.txt文件中找到。这里的命令是:
qemu-system-mipsel -M malta -kernel output/images/vmlinux -serial stdio -drive file=output/images/rootfs.ext2,format=raw -append "rootwait root=/dev/hda" -net nic,model=pcnet -net user
虚拟机正常运行:
参考:
手册 https://buildroot.org/downloads/manual/manual.html
https://blog.conan.io/2019/08/27/Creating-small-Linux-images-with-Buildroot.html
How to create small VMs with buildroot:http://people.redhat.com/~thuth/blog/general/2019/01/28/buildroot.html
buildroot使用介绍 https://www.bbsmax.com/A/D854LrA65E/ (推荐)
https://blog.csdn.net/mxgsgtc/article/details/53047562
https://www.thirtythreeforty.net/posts/2020/01/mastering-embedded-linux-part-3-buildroot/ 系列文章,推荐