Yocto简介

@

背景

Yocto 项目(YP)是一个开源协作项目,它帮助开发者创建基于 Linux 的自定义系统,且不受硬件架构的限制。
该项目提供了一套灵活的工具集,以及一个供全球嵌入式开发者共享技术、软件栈、配置和最佳实践的平台。这些资源可用于为嵌入式设备和物联网设备,或任何需要定制化 Linux 操作系统的场景,构建量身定制的 Linux 镜像。

简单说,Yocto提供一个统一的框架工具,能够让我们定制自己的Linux 镜像。

第一个镜像

参考:Yocto Project Quick Build

安装依赖

sudo apt install build-essential chrpath cpio debianutils diffstat file gawk gcc git iputils-ping libacl1 locales python3 python3-git python3-jinja2 python3-pexpect python3-pip python3-subunit socat texinfo unzip wget xz-utils zstd

下载工具

git clone https://git.openembedded.org/bitbake

配置构建环境

./bitbake/bin/bitbake-setup init

会让你选择项目配置:

Available configurations:
1. poky-whinlatter	Poky - The Yocto Project testing distribution configurations and hardware test platforms, release 5.3 'whinlatter' (supported until 2026-05-31)
2. oe-nodistro-whinlatter	OpenEmbedded - 'nodistro' basic configuration, release 5.3 'whinlatter' (supported until 2026-05-31)
3. poky-master	Poky - The Yocto Project testing distribution configurations and hardware test platforms
4. oe-nodistro-master	OpenEmbedded - 'nodistro' basic configuration

Please select one of the above configurations by its number: 3

Available bitbake configurations:
1. pokyPoky - The Yocto Project testing distribution
2. poky-with-sstate	Poky - The Yocto Project testing distribution with internet sstate acceleration. Use with caution as it requires a completely robust local network with sufficient bandwidth.

Please select one of the above bitbake configurations by its number: 1

目标机器:

Please select one of the above bitbake configurations by its number: 1

Target machines:
1. machine/qemux86-64
2. machine/qemuarm64
3. machine/qemuriscv64
4. machine/genericarm64
5. machine/genericx86-64

Please select one of the above options by its number: 1

版本:

Distribution configuration variants:
1. distro/poky
2. distro/poky-altcfg
3. distro/poky-tiny

Please select one of the above options by its number: 1

构建目录:

Enter setup directory name [poky-master-poky-distro_poky-machine_qemux86-64]:
Initializing a setup directory in
    /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64
Continue? (y/N): y

随后开始对目录进行配置:

Fetching layer/tool repositories into /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/layers
    bitbake
    openembedded-core
    meta-yocto
    yocto-docs

Setting up bitbake configuration in
    /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build

This bitbake configuration provides:
    Poky - The Yocto Project testing distribution

Usage instructions and additional information are in
     /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build/README

To run builds, source the environment using
    . /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build/init-build-env

Run 'bitbake-config-build enable-fragment <fragment-name>' to enable additional fragments or replace built-in ones (e.g. machine/<name> or distro/<name> to change MACHINE or DISTRO).

The bitbake configuration files (local.conf, bblayers.conf and more) can be found in
    /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build/conf

配置目录结构:

ls -lh bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/
total 12K
drwxrwxr-x 5 marvin marvin 4.0K Jan  3 13:50 build
drwxrwxr-x 3 marvin marvin 4.0K Jan  3 13:50 config
drwxrwxr-x 7 marvin marvin 4.0K Jan  3 13:50 layers

名为 build 的 BitBake 构建目录。构建完成后,该目录将包含构建过程中生成的所有文件。
此目录下还包含一个 README 文件,用于描述当前配置并提供一些操作指引。
构建 Poky 参考发行版所需的层文件,存放在 layers 目录中。
还有一个 config 目录,用于存放当前构建环境所使用的配置信息。

开始构建

配置环境

cd bitbake-builds
source poky-master-poky-distro_poky-machine_qemux86-64/build/init-build-env

查看配置

bitbake-config-build list-fragments
NOTE: Starting bitbake server...
Available built-in fragments:
machine/...	Sets MACHINE = ...
distro/...	Sets DISTRO = ...

Enabled built-in fragments:
distro/poky	Sets DISTRO = "poky"
machine/qemux86-64	Sets MACHINE = "qemux86-64"

Available fragments in core layer located in /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/layers/openembedded-core/meta:

Unused fragments:
core/yocto-autobuilder/autobuilder-resource-constraintsResource constraint variables used on the Yocto Projects autobuilder
core/yocto-autobuilder/autobuilder	Default variables used on the Yocto Projects autobuilder builds
core/yocto-autobuilder/multilib-mips64-n32	Enable a MIPS64 triarch multilib
core/yocto-autobuilder/multilib-x86-lib32	Enable an x86 lib32 multilib
core/yocto-autobuilder/multilib-x86-lib64	Enable an x86 lib64 multilib
core/yocto/root-login-with-empty-password	Log in as root without password on serial console and over ssh (use with caution).
core/yocto/sstate-mirror-cdn	Use prebuilt sstate artifacts for standard Yocto build configurations.

为了可以使用root登陆:

bitbake-config-build enable-fragment core/yocto/root-login-with-empty-password

开始构建

// with gui
bitbake core-image-sato
// minimal
// bitbake core-image-minimal

这里选择带GUI界面的版本,也可以用minimal的。
第一次过程比较慢,考虑到它需要从外网拉取包,同时也需要为其预留足够的磁盘空间(这个看项目,几十到上百G都有可能)。

如果使用Yocto做产品开发的话,公司内部会搭建专门的缓存服务器,这样每次拉取就很快了。

启动镜像

可以使用qemu启动

runqemu core-image-sato snapshot

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
后台看看qemu参数:

/home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build/tmp/work/x86_64-linux/qemu-helper-native/1.0/recipe-sysroot-native/usr/bin/qemu-system-x86_64 \
-device virtio-net-pci,netdev=net0,mac=52:54:00:12:34:02 \
-netdev tap,id=net0,ifname=tap0,script=no,downscript=no \
-object rng-random,filename=/dev/urandom,id=rng0 \
-device virtio-rng-pci,rng=rng0 \
-drive file=/home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build/tmp/deploy/images/qemux86-64/core-image-sato-qemux86-64.rootfs-20260103121451.ext4,if=virtio,format=raw \
-usb -device usb-tablet \
-usb -device usb-kbd \
-cpu Skylake-Client \
-machine q35,i8042=off -smp 4 -m 512 \
-snapshot \
-serial mon:vc \
-serial null \
-device virtio-vga \
-display sdl,show-cursor=on \
-kernel /home/marvin/workspace/Yocto/bitbake-builds/poky-master-poky-distro_poky-machine_qemux86-64/build/tmp/deploy/images/qemux86-64/bzImage \
-append root=/dev/vda rw  ip=192.168.7.2::192.168.7.1:255.255.255.0::eth0:off:8.8.8.8 net.ifnames=0 oprofile.timer=1 tsc=reliable no_timer_check rcupdate.rcu_expedited=1 swiotlb=0

参考

The Yocto Project
Yocto项目简介:嵌入式Linux开发的强大工具
Yocto 1:Ubuntu下开发环境搭建
Welcome to the Yocto Project Documentation
Yocto项目介绍及入门 -- 嵌入师工程师必备利器
Yocto Project Quick Build
Yocto嵌入式Linux大杀器-合集

posted @ 2026-01-03 14:20  main_c  阅读(10)  评论(0)    收藏  举报  来源