1.1、欢迎!
本文将指导您完成使用 Yocto 项目构建典型镜像的过程。还介绍了如何配置编译一个特定硬件。您将使用 Yocto 项目构建一个名为 Poky 的嵌入式操作系统。
注意
本文实例使用Ubuntu 20.04.6 LTS物理机系统编译,大于此版的Ubuntu系统和其他的Linux发行版最新系统,应该也可以工作。
您也可以在 Windows 10使用 Windows Subsystem For Linux (WSL2),或更高版本、Windows Server 2019 或更高版本上设置构建主机
1.2、兼容的 Linux 发行版
- 至少 90 Gbytes 的可用磁盘空间,更多的空间有助于运行多个构建并通过重用构建工件提高性能。
- 至少 8 Gbytes 的 RAM,强烈建议使用具有尽可能多 RAM 和 CPU 核心的现代构建主机,以最大化构建性能。
- 运行受支持的 Linux 发行版(即 Fedora、openSUSE、CentOS、Debian 或 Ubuntu 的最新版本)。
- Git 1.8.3.1 或更高版本
- tar 1.28 或更高版本
- Python 3.8.0 或更高版本。
- gcc 8.0 或更高版本。
- GNU make 4.0 或更高版本
1.3、构建主机软件包
首先必须在主机上安装一些基本的主机软件包依赖包。
$ sudo apt install build-essential chrpath cpio debianutils diffstat file gawk gcc git iputils-ping libacl1 liblz4-tool locales python3 python3-git python3-jinja2 python3-pexpect python3-pip python3-subunit socat texinfo unzip wget xz-utils zstd
1.4、使用 Git 命令下载Poky工程
依赖工具下载完后,需要在主机上获取 Poky 仓库的副本。使用以下命令克隆 Poky 仓库。
[poky]$git clone git://git.yoctoproject.org/poky
Cloning into 'poky'...
remote: Enumerating objects: 678107, done.
remote: Counting objects: 100% (394/394), done.
remote: Compressing objects: 100% (207/207), done.
remote: Total 678107 (delta 249), reused 283 (delta 186), pack-reused 677713
Receiving objects: 100% (678107/678107), 213.65 MiB | 1.69 MiB/s, done.
Resolving deltas: 100% (492684/492684), done.
跳转到Releases - Yocto Project页面,选择一个稳定和长期支持的发布版本。
进入poky目录,并查看现有分支。选择一个发布版本(例如styhead)
[poky]$cd poky/
[poky]$git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/bernard
remotes/origin/blinky
remotes/origin/clyde
remotes/origin/daisy
remotes/origin/danny
remotes/origin/denzil
remotes/origin/dizzy
remotes/origin/dora
remotes/origin/dunfell
remotes/origin/dylan
remotes/origin/edison
remotes/origin/elroy
remotes/origin/fido
remotes/origin/gatesgarth
remotes/origin/green
remotes/origin/hardknott
remotes/origin/honister
remotes/origin/jethro
remotes/origin/kirkstone
remotes/origin/krogoth
remotes/origin/langdale
remotes/origin/laverne
remotes/origin/master
remotes/origin/master-next
remotes/origin/mickledore
remotes/origin/morty
remotes/origin/nanbield
remotes/origin/pinky
remotes/origin/purple
remotes/origin/pyro
remotes/origin/rocko
remotes/origin/scarthgap
remotes/origin/styhead
remotes/origin/sumo
remotes/origin/thud
remotes/origin/warrior
remotes/origin/zeus
检出 styhead 分支作为一个本地分支:
[poky]$git checkout -t origin/styhead -b my-styhead
Branch 'my-styhead' set up to track remote branch 'styhead' from 'origin'.
Switched to a new branch 'my-styhead'
上述 Git 检出命令创建了一个名为 my-styhead 的本地分支。该分支中的文件与 styhead 发布分支中的文件完全匹配。
可以定期在同一个目录中输入以下命令以使本地文件与发布分支保持同步:
$ git pull
1.5、构建镜像
使用以下步骤构建镜像。构建过程从源代码创建整个 Linux 发行版,包括工具链。
注意 如果主机有防火墙且未设置代理,您可能会在获取源代码的过程中遇到问题(fetcher failures or Git failures)。 如果您不知道代理如何设置,请查看本地网络并获取该信息。
1.5.1、初始化编译环境
在 poky 目录中,运行 oe-init-build-env 环境设置脚本,初始化主机上的Yocto 项目的编译环境。
[poky]$source poky/oe-init-build-env
You had no conf/local.conf file. This configuration file has therefore been
created for you from /home/heyanbo/workqueue/poky/poky/meta-poky/conf/templates/default/local.conf.sample
You may wish to edit it to, for example, select a different MACHINE (target
hardware).
You had no conf/bblayers.conf file. This configuration file has therefore been
created for you from /home/heyanbo/workqueue/poky/poky/meta-poky/conf/templates/default/bblayers.conf.sample
To add additional metadata layers into your configuration please add entries
to conf/bblayers.conf.
The Yocto Project has extensive documentation about OE including a reference
manual which can be found at:
https://docs.yoctoproject.org
For more information about OpenEmbedded see the website:
https://www.openembedded.org/
This is the default build configuration for the Poky reference distribution.
### Shell environment set up for builds. ###
You can now run 'bitbake <target>'
Common targets are:
core-image-minimal
core-image-full-cmdline
core-image-sato
core-image-weston
meta-toolchain
meta-ide-support
You can also run generated qemu images with a command like 'runqemu qemux86-64'.
Other commonly useful commands are:
- 'devtool' and 'recipetool' handle common recipe tasks
- 'bitbake-layers' handles common layer tasks
- 'oe-pkgdata-util' handles common target package tasks
执行完成以后,脚本创建了一个build目录,并自动进入build目录下,并将当前的目录设置为编译目录。稍后,当编译完成时,当前目录将包含编译过程中创建的所有文件。
[poky]$ls
build poky
[poky]$ls build/conf
bblayers.conf conf-notes.txt conf-summary.txt local.conf templateconf.cfg
1.5.2、检查本地配置文件
设置编译环境后,编译目录会生成一个 conf 子目录,在这个子目录中将提供一个名为local.conf 的本地配置文件。在此示例中,默认设置的编译生成目录为qemux86。使用的软件包管理器为 RPM 软件包管理器。
1.5.3、开始编译
使用以下命令编译生成一个系统目标,当然你也可以编译其他系统,编译的时间取决于系统性能和网络速度(首次编译需要下载大量源码)
$ bitbake core-image-sato
[build]$bitbake core-image-sato
Loading cache: 100% |################################################################################| Time: 0:00:00
Loaded 1867 entries from dependency cache.
NOTE: Resolving any missing task queue dependencies
Build Configuration:
BB_VERSION = "2.9.1"
BUILD_SYS = "x86_64-linux"
NATIVELSBSTRING = "universal"
TARGET_SYS = "x86_64-poky-linux"
MACHINE = "qemux86-64"
DISTRO = "poky"
DISTRO_VERSION = "5.1.3"
TUNE_FEATURES = "m64 core2"
TARGET_FPU = ""
meta
meta-poky
meta-yocto-bsp = "my-styhead:b2cf0d5cd0adb73708ce6035ce8611c0bc3860d5"
Sstate summary: Wanted 3850 Local 0 Mirrors 0 Missed 3850 Current 844 (0% match, 17% complete) | ETA: 0:00:00
Initialising tasks: 100% |###########################################################################| Time: 0:00:14
NOTE: Executing Tasks
NOTE: Tasks Summary: Attempted 9397 tasks of which 2622 didn't need to be rerun and all succeeded.
Summary: There were 25 WARNING messages.
1.5.4、使用 QEMU 仿真镜像
编译完成以后,Yocto里面包含QEMU模拟器,可以启动 QEMU仿真运行编译生成的固件
runqemu qemux86-64
1.5.5、退出 QEMU
通过点击关机图标或在调用 QEMU 的 QEMU 传输窗口中输入 Ctrl-C 来退出 QEMU。
1.6、添加新硬件支持
目前为止,我们只是快速速构建了一个仅适用于仿真的镜像。本节展示了如何添加新的硬件到Yocto工程项目。初始学习我们无法从零新建一个硬件层的支持,我们可以下载一个别人已经完成的硬件成。学习一下新的硬件的添加步骤。
通常,层包含Yocto项目应该如何编译的相关指令和配置。将相关的元数据隔离到特定的功能层中,有助于模块化开发,并使层元数据更容易重用。
注意
通常情况下,层名称以字符串“meta-”开头。
按照以下步骤添加新的硬件层:
1.6.1 、找一个可以使用的硬件层
首次添加,我们可以选择一个通用的硬件层,网上有许多硬件层可用。Yocto 项目源代码存储库中也有许多硬件层。此示例添加了 meta-altera 硬件层。
1.6.2、下载层代码
使用 Git 命令下载层副本到本地。将层副本放在之前创建的 Poky 仓库的顶层:
$ cd poky
$git clone https://github.com/kraj/meta-altera.git
Cloning into 'meta-altera'...
remote: Enumerating objects: 2153, done.
remote: Counting objects: 100% (166/166), done.
remote: Compressing objects: 100% (79/79), done.
remote: Total 2153 (delta 80), reused 134 (delta 74), pack-reused 1987 (from 1)
Receiving objects: 100% (2153/2153), 292.34 KiB | 956.00 KiB/s, done.
Resolving deltas: 100% (1078/1078), done.
现在,meta-altera硬件层已经插入主机上的 Poky 项目中,并包含支持 Altera 硬件所需的所有元数据,Altera 是 Intel 的子公司。
1.6.3、修改配置以构建特定机器
在buld/local.conf 文件中的 MACHINE 变量用来指定构建的机器。在此示例中,将 MACHINE 变量设置为 cyclone5。这些配置可以到如下链接下载:https://github.com/kraj/meta-altera/blob/master/conf/machine/cyclone5.confhttps://github.com/kraj/meta-altera/blob/master/conf/machine/cyclone5.conf.
1.6.4、添加专有层到层配置文件
在编译之前,必须将专用层(meta-altera)添加到build/conf下的bblayers.conf配置文件中。
我们使用bitbake-layers add-layer 命令将层添加到配置文件,bitbake命令的详细使用方法,后续会专门写一篇
$ cd poky/build
[build]$bitbake-layers add-layer ../poky/meta-altera
NOTE: Starting bitbake server...
ERROR: Layer meta-altera is not compatible with the core layer which only supports these series: styhead (layer is compatible with kirkstone honister mickledore)
ERROR: Parse failure with the specified layer added, exiting.
添加错误,报错的原因有说明,这是因为我们我们测试的分支是styhead,meta-altera不支持。我们需要切换为他支持的分支(kirkstone honister mickledore)
[poky]$git checkout -t origin/honister -b my-honister
Branch 'my-honister' set up to track remote branch 'honister' from 'origin'.
Switched to a new branch 'my-honister'
重新生成环境变量
source poky/oe-init-build-env
再次添加,添加成功
[build]$bitbake-layers add-layer ../poky/meta-altera
NOTE: Starting bitbake server...
vi conf/bblayers.conf
1 # POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
2 # changes incompatibly
3 POKY_BBLAYERS_CONF_VERSION = "2"
4
5 BBPATH = "${TOPDIR}"
6 BBFILES ?= ""
7
8 BBLAYERS ?= " \
9 /home/heyanbo/workqueue/poky/poky/meta \
10 /home/heyanbo/workqueue/poky/poky/meta-poky \
11 /home/heyanbo/workqueue/poky/poky/meta-yocto-bsp \
12 /home/heyanbo/workqueue/poky/poky/meta-altera \
这时已将meta-altera 层添加到Yocto 项目开发环境,并配置为构建cyclone5 机器。
注意
上述步骤仅用于演示目的。如果您尝试为cyclone5 机器构建镜像,您应该阅读 Altera 的README
1.7、创建自己的通用层
我们可以使用bitbake-layers create-layer 命令创建自己的通用层。这个命令工具会创建一个通用层命名子目录,里面包括包含一个layer.conf 配置文件,许可证文件和README文件, 一个recipes-example目录,recipes-example目录下包含example.bb 。
如下运行命令可以在poky 目录中创建一个名为 meta-mylayer 的通用层:
$ cd poky
$ bitbake-layers create-layer meta-mylayer
NOTE: Starting bitbake server...
Add your new layer with 'bitbake-layers add-layer meta-mylayer'
[poky]$tree meta-mylayer
meta-mylayer
├── conf
│ └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
└── example
└── example_0.1.bb
3 directories, 4 files