当前位置: 首页 > article >正文

Yocto - 使用Yocto开发嵌入式Linux系统_03 基于Poky制作第一个系统

Chapter 2: Baking Our First Poky-Based System
让我们动手实践吧!在本章中,我们将了解 Poky 工作流程中涉及的基本概念。我们将介绍下载、配置和准备 Poky 构建环境的步骤,并烘焙出可用的东西。这里涉及的步骤在测试和开发中很常见。它们将为我们提供一些使用 Poky 的经验,并让我们领略到它的能力。
Let’s get our hands dirty! In this chapter, we will understand the basic concepts involved in the Poky workflow. We will cover the steps to download, configure, and prepare the Poky build environment and bake something usable. The steps covered here are common for testing and development. They will give us some experience using Poky and a taste of its capabilities.
1, Preparing the build host system
本节介绍如何准备 Windows 和 Linux 分发版主机系统。虽然我们将介绍 Windows 的步骤,但重点是使用 Linux 分发版主机系统。
This section describes how to prepare Windows and Linux distribution host systems. Although we will describe the Windows steps, we will focus on using a Linux distribution host system.
Tip/提示
使用 macOS 作为主机系统是可能的。不过,这需要使用 CROss PlatformS (CROPS) 框架,该框架利用 Docker,允许使用包括 macOS 在内的外来操作系统。更多信息,请参阅《Yocto 项目开发任务手册》(https://docs.yoctoproject.org/4.0.4/dev-manual/start.html#setting-up-to-use-cross-platforms-crops)中的 “设置使用 CROss PlatformS (CROPS)” 部分。
The use of macOS as a host system is possible. Still, it involves using the CROss PlatformS (CROPS) framework, which leverages Docker, allowing the use of foreign operating systems, including macOS. For more information, you can refer to the Setting Up to Use CROss PlatformS (CROPS) section from the Yocto Project Development Tasks Manual (https://docs.yoctoproject.org/4.0.4/dev-manual/start.html#setting-up-to-use-cross-platforms-crops).
接下来,我们将提供启动构建主机系统准备工作所需的信息。
Next, we will provide the necessary information to start the build host system preparation.
Using Windows Subsystem for Linux (WSLv2)
如果你是 Windows 用户,可以在 Windows 上安装 Linux 分发版。WSLv2 仅适用于大于 18917 的 Windows 10+ 版本。WSLv2 允许使用 Yocto 项目进行开发。你可以从 Microsoft Store 安装 Linux 发行版。
You can set up a Linux distribution on Windows if you are a Windows user. WSLv2 is only available for Windows 10+ builds greater than 18917. WSLv2 allows development using the Yocto Project. You can install the Linux distribution from the Microsoft Store.
请参阅《Yocto 项目开发任务手册》( Yocto Project Development Tasks Manual — The Yocto Project ® 4.0.4 documentation) 中的 “设置使用 Windows Linux 子系统 ”部分 ( 2 Setting Up to Use the Yocto Project — The Yocto Project ® 4.0.4 documentation)。一旦设置好 WSLv2,就可以像在本地 Linux 机器上运行一样执行接下来的章节。
Please refer to the Setting Up to Use Windows Subsystem For Linux session ( 2 Setting Up to Use the Yocto Project — The Yocto Project ® 4.0.4 documentation ) from the Yocto Project Development Tasks Manual ( Yocto Project Development Tasks Manual — The Yocto Project ® 4.0.4 documentation  ). Once you have WSLv2 set up, you can follow the next sections as if you were running on a native Linux machine.
Preparing a Linux-based system
设置主机系统所需的过程取决于我们使用的 Linux 发行版。Poky 有一套支持的 Linux 发行版。假设我们是嵌入式 Linux 开发的新手。在这种情况下,建议使用其中一个受支持的 Linux 发行版,以免浪费时间调试与主机系统支持相关的问题。
The process needed to set up our host system depends on the Linux distribution we use. Poky has a set of supported Linux distributions. Let’s suppose we are new to embedded Linux development. In that case, it is advisable to use one of the supported Linux distributions to avoid wasting time debugging issues related to the host system support.
如果你使用的是下列发行版之一的当前版本,你就可以在你的机器上开始使用 Yocto 项目了:
If you use the current release of one of the following distributions, you should be good to start using the Yocto Project on your machine:
* Ubuntu
* Fedora
* CentOS
* AlmaLinux
* Debian
* OpenSUSE Leap
要确认你的版本是否受支持,建议你在线查看 “构建主机所需软件包 ”部分的官方文档 ( 1 System Requirements — The Yocto Project ® 4.0.4 documentation)。
To confirm whether your version is supported, it is advisable to check the official documentation online in the Required Packages for the Build Host section ( 1 System Requirements — The Yocto Project ® 4.0.4 documentation ).
如果你喜欢的发行版不在上述列表中,并不意味着不能在该发行版上使用 Poky。你的主机开发系统必须满足 Git、tar、Python 和 GCC 的特定版本要求。你的 Linux 发行版应该提供这些基础工具的兼容版本。不过,你的主机开发系统有可能不满足所有这些要求。在这种情况下,你可以安装包含这些工具的 buildtools 压缩包来解决这个问题,详见所需 Git、tar、Python 和 GCC 版本 ( 1 System Requirements — The Yocto Project ® 4.0.4 documentation)。
If your preferred distribution is not in the preceding list, it doesn’t mean it is not possible to use Poky on it. Your host development system must meet some specific versions for Git, tar, Python, and GCC. Your Linux distributions should provide compatible versions of those base tools. However, there is a chance that your host development system does not meet all these requirements. In that case, you can resolve this by installing a buildtools tarball that contains these tools, as detailed in Required Git, tar, Python, and GCC Versions (https://docs.yoctoproject.org/4.0.4/ref-manual/system-requirements.html#required-git-tar-python-and-gcc-versions).
我们必须在主机系统上安装一些软件包。本书提供了 Debian 和 Fedora 这两个我们首选的发行版的安装说明,我们接下来将讨论这两个发行版。其他支持的发行版的软件包可以在《Yocto 项目参考手册》( 1 System Requirements — The Yocto Project ® 4.0.4 documentation )中找到。
We must install a few packages on the host system. This book provides instructions for Debian and Fedora, our preferred distributions, which we will look at next. The set of packages for other supported distributions can be found in the Yocto Project Reference Manual ( 1 System Requirements — The Yocto Project ® 4.0.4 documentation ).
Debian-based distribution
要为无头主机系统安装必要的软件包,请运行以下命令:
To install the necessary packages for a headless host system, run the following command:
$ sudo apt install gawk wget git diffstat unzip texinfo gcc build-essential chrpath socat cpio python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev pylint3 xterm python3-subunit mesa-common-dev zstd liblz4-tool
Fedora
To install the needed packages for a headless host system, run the following command:
$ sudo dnf install gawk make wget tar bzip2 gzip python3 unzip perl patch diffutils diffstat git cpp gcc gcc-c++ glibc-devel texinfo chrpath ccache perl-Data-Dumper perl-Text-ParseWords perl-Thread-Queue perl-bignum socat python3-pexpect findutils which file cpio python python3-pip xz python3-GitPython python3-jinja2 SDL-devel xterm rpcgen mesa-libGL-devel perl-FindBin perl-File-Compare perl-File-Copy perl-locale zstd lz4
在Ubuntu(Debian-based distribution)上安装软件包时,上面的pylint3提示安装有问题,于是安装的是pylint。
2, Downloading the Poky source code
在开发主机系统上安装好所需软件包后,我们就可以使用 Git 下载当前 LTS 版本(本文撰写时)的 Poky 源代码,命令如下:
After we have installed the required packages on our development host system, we can download the current LTS version (at the time of writing) of Poky source code using Git, with the following command:
$ git clone https://git.yoctoproject.org/poky -b kirkstone
因为直接下载实在太慢,于是使用了github的mirror:
$ git clone https://github.com/yoctoproject/poky.git -b kirkstone
Tip 提示
Learn more about Git at https://git-scm.com.
下载过程完成后,我们应该在 poky 目录中找到以下内容:
After the download process is complete, we should have the following contents inside the poky directory:
Figure 2.1 – The content of the poky directory after downloading
Note
本章及后续章节中介绍的示例和代码以 Yocto Project 4.0 版本(代号 Kirkstone)为参考。
The examples and code presented in this and subsequent chapters use the Yocto Project 4.0 release (codenamed Kirkstone) as a reference.
3, Preparing the build environment
在 poky 目录下有一个名为 oe-init-build-env 的脚本,用于设置构建环境。但首先,该脚本必须按以下步骤运行(而不是执行):
Inside the poky directory exists a script named oe-init-build-env, which sets up the building environment. But first, the script must be run-sourced (not executed) as follows:
$ source oe-init-build-env [build-directory]
这里,[build-directory] 是一个可选参数,用于指定配置环境的目录名称。如果为空,则默认为 build。build-directory 参数是我们执行构建的位置。
Here, [build-directory] is an optional parameter for the name of the directory where the environment is configured. If it is empty, it defaults to build. The [build-directory] parameter is the place where we perform the builds.
"source oe-init-build-env build"的输出会显示一些重要配置,如文件位置、一些项目 URL 和一些常用目标,如可用图像。下图显示了一个输出示例:
The output from source oe-init-build-env build displays some important configurations such as the file location, some project URLs, and some common targets, such as available images. The following figure shows an output example:
Figure 2.2 – Output of the source oe-init-build-env build command
使用不同的构建目录非常方便。我们可以在不影响其他构建的情况下,并行或试验性地构建不同的项目。
It is very convenient to use different build directories. We can work on separate projects in parallel or experimental setups without affecting our other builds.
Note
在本书中,我们将使用 build 作为联编目录。当我们需要指向联编目录内的文件时,我们将采用相同的约定,例如 build/conf/local.conf。
Throughout the book, we will use build as the build directory. When we need to point to a file inside the build directory, we will adopt the same convention – for example, build/conf/local.conf.
4, Knowing the local.conf file
当我们初始化构建环境时,它会创建一个名为 build/conf/local.conf 的文件。这个配置文件功能强大,几乎可以配置构建过程的方方面面。我们可以设置目标机器,可以设置自定义跨平台工具链的主机架构,优化选项以最大限度缩短构建时间,等等。build/conf/local.conf 文件中的注释是极好的文档,也是可能的变量及其默认值的参考。我们可能需要更改的最小变量集如下:
When we initialize a build environment, it creates a file called build/conf/local.conf. This config file is powerful, since it can configure almost every aspect of the build process. We can set the target machine and the toolchain host architecture to be used for a custom cross-toolchain, optimize options for maximum build time reduction, and so on. The comments inside the build/conf/local.conf file are excellent documentation and a reference of the possible variables and their defaults. The minimal set of variables that we probably want to change from the default is the following:
MACHINE ??= "qemux86-64"
通过 MACHINE 变量,我们可以确定要构建的目标机器。在撰写本文时,Poky 的参考 BSP 支持以下机器:
The MACHINE variable is where we determine the target machine we wish to build. At the time of writing, Poky supports the following machines in its reference BSP:
* beaglebone-yocto: This is BeagleBone, which is the reference platform for 32-bit ARM
* genericx86: This is generic support for 32-bit x86-based machines
* genericx86-64: This is generic support for 64-bit x86-based machines
* edgerouter: This is EdgeRouter Lite, which is the reference platform for 64-bit MIPS
这些机器由一个名为 meta-yocto-bsp 的层提供。除了这些机器外,OpenEmbedded Core 在 meta 目录中还提供了对以下快速仿真(QEMU)机器的支持:
The machines are made available by a layer called meta-yocto-bsp. Besides these machines, OpenEmbedded Core, inside the meta directory, also provides support for the following Quick Emulation (QEMU) machines:
* qemuarm: This is the QEMU ARMv7 emulation
* qemuarmv5: This is the QEMU ARMv5 emulation
* qemuarm64: This is the QEMU ARMv8 emulation
* qemumips: This is the QEMU MIPS emulation
* qemumips64: This is the QEMU MIPS64 emulation
* qemuppc: This is the QEMU PowerPC emulation
* qemuppc64: This is the QEMU PowerPC 64 emulation
* qemux86-64: This is the QEMU x86-64 emulation
* qemux86: This is the QEMU x86 emulation
* qemuriscv32: This is the QEMU RISC-V 32 emulation
* qemuriscv64: This is the QEMU RISC-V 64 emulation
一些供应商提供的额外 BSP 层可支持其他机器。使用额外 BSP 层的过程见第 11 章 “探索外部层”。
Extra BSP layers available from several vendors provide support for other machines. The process of using an extra BSP layer is shown in Chapter 11, Exploring External Layers.
Note
local.conf 文件是覆盖整个 Yocto 项目工具中若干全局默认配置的便捷方法。从本质上讲,我们可以更改或设置任何变量,例如在映像文件中添加额外的软件包。更改 build/conf/local.conf 文件非常方便,但源代码管理系统通常不会跟踪此目录中的临时更改。
build/conf/local.conf 文件可以设置多个变量。值得花些时间阅读生成的文件注释,以大致了解可以设置哪些变量。
The local.conf file is a convenient way to override several global default configurations throughout the Yocto Project’s tools. Essentially, we can change or set any variable – for example, adding additional packages to an image file. Changing the build/conf/local.conf file is convenient; however, the source code management system usually does not track temporary changes in this directory.
The build/conf/local.conf file can set several variables. It is worth taking some time and reading through the file comments that are generated to get a general idea of what variables can be set.
5,Building a target image
Poky 提供了几种预先设计好的映像配方,我们可以用它们来构建二进制映像。我们可以在 poky 目录下运行以下命令,查看可用图像列表:
Poky provides several predesigned image recipes we can use to build our binary image. We can check the list of available images by running the following command from the poky directory:
$ ls meta*/recipes*/*images/*.bb
所有配方提供的映像都是一组已解压和配置的软件包,生成的文件系统可用于硬件或受支持的 QEMU 机器之一。
All the recipes provide images that are a set of unpacked and configured packages, generating a filesystem that we can use with hardware or one of the supported QEMU machines.
接下来,我们可以看到最常用的镜像列表:
Next, we can see the list of most commonly used images:
* core-image-minimal: 这是一个允许设备启动的小型镜像。它便于内核和引导加载器的测试和开发。
* core-image-base: 该图像仅用于控制台,为目标设备提供基本硬件支持。
* core-image-weston: 该镜像提供 Wayland 协议库和参考 Weston 合成器。
* core-image-x11:这是一个带有终端的基本 X11 镜像。
* core-image-sato:这是一个支持 Sato 的镜像,并为使用 X11 的移动设备提供移动环境。它提供终端、编辑器、文件管理器、媒体播放器等应用程序。
* core-image-full-cmdline: 纯控制台镜像,安装了功能更全面的 Linux 系统功能。
* core-image-minimal: This is a small image allowing a device to boot. It is handy for kernel and bootloader tests and development.
* core-image-base: This console-only image provides basic hardware support for the target device.
* core-image-weston: This image provides the Wayland protocol libraries and the reference Weston compositor.
* core-image-x11: This is a basic X11 image with a terminal.
* core-image-sato: This is an image with Sato support and a mobile environment for mobile devices that use X11. It provides applications such as a terminal, editor, file manager, media player, and so on.
* core-image-full-cmdline: A console-only image with more full-featured Linux system functionality installed.
社区还提供其他参考镜像。一些镜像支持实时、initramfs 和 MTD(闪存工具)等功能。最好查看源代码或《Yocto 项目参考手册》( Yocto Project Reference Manual — The Yocto Project ® 4.0.4 documentation),了解完整的最新列表。
There are other reference images available from the community. Several images support features, such as Real Time, initramfs, and MTD (flash tools). It is good to check the source code or the Yocto Project Reference Manual ( Yocto Project Reference Manual — The Yocto Project ® 4.0.4 documentation  ) for the complete and updated list.
为目标构建镜像的过程非常简单。但首先,在使用 BitBake 之前,我们需要使用 source oe-init-build-env [build-directory] 来设置构建环境。要构建镜像,我们可以使用下面命令中的模板:
The process of building an image for a target is straightforward. But first, we need to set up the build environment using  source oe-init-build-env [build-directory] before using BitBake. To build the image, we can use the template in the following command:
$ bitbake <recipe name>
Figure 2.3 – How to build a recipe using BitBake
Note
在下面的示例中,我们将使用 MACHINE = “qemux86-64”。你可以在 build/conf/local.conf 中进行相应设置。
We will use MACHINE = "qemux86-64" in the following examples. You can set it in build/conf/local.conf accordingly.
例如,要构建 core-image-full-cmdline,请运行以下命令:
For example, to build core-image-full-cmdline, run the following command:
$ bitbake core-image-full-cmdline
构建过程根据网速、主机性能会需要不同的时长,但第一次构建总是会很慢的,一般少说也要一小时。根据国内的网络情况,一些个别的package的do_fetch任务可能就需要很长时间。
比如下面这个:
WARNING: linux-yocto-5.15.166+gitAUTOINC+567f0adb9d_a78a104311-r0 do_fetch: Failed to fetch URL git://git.yoctoproject.org/linux-yocto.git;name=machine;branch=v5.15/standard/base;, attempting MIRRORS if available
这个包下载速度7KB/s,我足足下了一晚上。就像前面从yocto网站下载poky会比较慢一样,这里也建议把下载地址从 git.yoctoproject.org改成镜像 GitHub - yoctoproject/poky: Mirror of https://git.yoctoproject.org/poky/.,修改的bb文件是:/meta/recipes-kernel/linux/linux-yocto_5.15.bb。
Poky 的结构如下图所示:
The Poky build looks like the following figure:
[ Figure 2.4 – The result of bitbake core-image-full-cmdline ]
6, Running images in QEMU
我们可以使用硬件仿真来加快开发过程,因为它可以在不涉及任何实际硬件的情况下进行测试运行。幸运的是,大多数项目只有很小一部分依赖于硬件。
We can use hardware emulation to speed up the development process, as it enables a test run without involving any actual hardware. Fortunately, most projects have only a tiny portion that is hardware-dependent.
QEMU 是一款免费的开源软件包,可执行硬件虚拟化。基于 QEMU 的机器可以在没有真实硬件的情况下进行测试和开发。目前支持 ARMv5、ARMv7、ARMv8、MIPS、MIPS64、PowerPC、PowerPC 64、RISC-V 32、RISC-V 64、x86 和 x86-64 仿真。我们将在后面章节《通过仿真加速产品开发 - QEMU》中详细介绍 QEMU软件的使用。
QEMU is a free, open source software package that performs hardware virtualization. QEMU-based machines allow testing and development without real hardware. ARMv5, ARMv7, ARMv8, MIPS, MIPS64, PowerPC, PowerPC 64, RISC-V 32, RISC-V 64, x86, and x86-64 emulations are currently supported. We will go into more detail about QEMU usage in sw, Speeding Up Product Development through Emulation – QEMU.
OpenEmbedded Core 提供了 runqemu 脚本工具,该工具是一个包装器,可使 QEMU 的使用更加简便。运行脚本工具的方法如下:
OpenEmbedded Core provides the runqemu script tool, which is a wrapper to make use of QEMU easier. The way to run the script tool is as follows:
$ runqemu <machine> <zimage> <filesystem>
这里,<machine> 这里是 qemux86-64 的机器/架构,或任何其他受支持的机器。此外,<zimage> 是内核的路径(例如,bzImage-qemux86-64.bin)。最后,<filesystem> 是 ext4 映像(例如 filesystem-qemux86-64.ext4)或 NFS 目录的路径。前面调用 runqemu <zimage> 和 <filesystem> 时的所有参数都是可选的。只需运行 runqemu 即可在设置了构建环境的 shell 中启动映像,因为它会自动获取构建环境时的默认设置。
Here, <machine> is the machine/architecture to be used as qemux86-64, or any other supported machine. Also, <zimage> is the path to a kernel (for example, bzImage-qemux86-64.bin). Finally, <filesystem> is the path to an ext4 image (for example, filesystem-qemux86-64.ext4) or an NFS directory. All parameters in the preceding call to runqemu <zimage> and <filesystem> are optional. Just running runqemu is sufficient to launch the image in the shell where the build environment is set, as it will automatically pick up the default settings from building the environment.
因此,举例来说,如果我们运行 runqemu qemux86-64 core-image-full-cmdline,就会看到与下面截图类似的内容:
So, for example, if we run runqemu qemux86-64 core-image-full-cmdline, we can see something similar to that shown in the following screenshot:
[ Figure 2.5 – The QEMU screen during the Linux kernel boot ]
启动 Linux 后,会出现登录提示,如图 2.6 所示:
After finishing booting Linux, you will see a login prompt, as shown in Figure 2.6:
[ Figure 2.6 – The QEMU screen during user login ]
我们可以使用空密码登录 root 账户。即使在 QEMU 中执行,系统也会像普通机器一样运行。在真实硬件中部署映像的过程各不相同,取决于所使用的存储类型、引导加载程序等。不过,生成镜像的过程是相同的。我们将在第 15 章 “启动我们的定制嵌入式 Linux ”中探讨如何在真实硬件中构建和运行映像。
We can log in to the root account using an empty password. The system behaves as a regular machine, even when executed inside the QEMU. The process to deploy an image in real hardware varies, depending on the type of storage used, the bootloader, and so on. However, the process of generating the image is the same. We explore how to build and run an image in real hardware in Chapter 15, Booting Our Custom Embedded Linux.
7, Summary
在本章中,我们学习了设置 Poky 和构建第一个映像所需的步骤。然后,我们使用 runqemu 运行了该镜像,这让我们对可用功能有了一个很好的概览。在下一章中,我们将介绍 BitBake 的人性化界面 Toaster。我们将用它来构建镜像并进一步定制。
In this chapter, we learned the steps needed to set up Poky and get our first image built. Then, we ran that image using runqemu, which gave us a good overview of the available capabilities. In the next chapter, we will introduce Toaster, a human-friendly interface for BitBake. We will use it to build an image and customize it further.

http://www.kler.cn/a/317283.html

相关文章:

  • GISBox VS ArcGIS:分别适用于大型和小型项目的两款GIS软件
  • SOLIDWORKS代理商鑫辰信息科技
  • Systemd: disable和mask的区别
  • AI绘画经验(stable-diffusion)
  • 深入理解 Vue v-model 原理与应用
  • 大数据面试题--kafka夺命连环问(后10问)
  • vue.js 展示一个树形结构的数据视图,并禁用其中默认选中的节点
  • java并发之并发理论
  • 【自动驾驶】基于车辆几何模型的横向控制算法 | Pure Pursuit 纯跟踪算法详解与编程实现
  • 同一网络下两台电脑IP一样吗?探究局域网内的IP分配机制
  • 释放TK49N65W5 MOSFET的潜力
  • 镭射限高防外破预警装置-线路防外破可视化监控,安全尽在掌握中
  • C++继承(上)
  • 数据结构 - 概述及其术语
  • AI教你学Python 第18天 : 线性数据结构
  • 【LeetCode:1014. 最佳观光组合 + 思维题】
  • 【linux】基础IO(上)
  • 使用 PHPstudy 建立ThinkPHP8 本地集成环境
  • SM2协同签名算法中随机数K的随机性对算法安全的影响
  • (八)使用Postman工具调用WebAPI
  • 花园管理系统
  • 论文阅读与分析:Few-Shot Graph Learning for Molecular Property Prediction
  • 服务器操作系统【sar 命令】
  • MongoDB的备份和恢复命令
  • macos macport软件包管理工具 sudo port install xxx 安装的软件的路径 与 brew install xxx 软件安装路径总结
  • 【android10】【binder】【3.向servicemanager注册服务】