详细介绍了如何在 Ubuntu 20.04 主机上,利用 Docker 技术搭建适用于 NXP i.MX6ULL 平台的 Yocto 开发环境。内容涵盖 Docker 的安装与配置、基于 Ubuntu 22.04 构建 Yocto 开发容器、主机与容器的数据持久化挂载、国内镜像加速优化,以及如何获取和配置 NXP 官方 Yocto 源码、初始化和构建 i.MX6ULL 镜像的全过程。
1. ubuntu
本项目使用的ubuntu 主机是 20.04.
2. docker
卸载旧版本
bash
sudo apt remove -y docker docker-engine docker.io containerd runc
安装 Docker Engine 并验证
bash
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 验证安装
bash
sudo docker --version
sudo docker run hello-world
免 sudo 使用 Docker
bash
sudo usermod -aG docker $USER
newgrp docker
2.1 构建 Ubuntu 22.04 Yocto 开发容器
以下所有操作在主机上执行。
创建工作目录
bash
mkdir -p ~/yocto-imx6ull-docker && cd ~/yocto-imx6ull-docker
编写 Dockerfile
创建 Dockerfile,内容如下。核心要点:
• 基础镜像 ubuntu:22.04(通过 1.6 配置的国内镜像拉取)
• 替换 apt 为阿里云国内源
• 安装 Yocto 官方要求的完整依赖包
• 创建非 root 用户 yoctouser(Yocto 官方强烈建议不要用 root 构建)
dockerfile
FROM docker.m.daocloud.io/library/ubuntu:22.04
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
# 替换 apt 源为阿里云国内源
RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list && \
sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
RUN apt-get update && apt-get install -y \
build-essential chrpath cpio curl diffstat file gawk gcc-multilib git \
libncurses-dev libssl-dev libstdc++6 libxml-parser-perl locales lz4 make \
openssh-client vim wget gcc\
python3 python3-cryptography python3-git python3-pip \
python3-pyelftools python3-setuptools qemu-user-static socat \
u-boot-tools unzip wget xz-utils zstd \
&& apt-get clean && rm -rf /var/lib/apt/lists/* \
&& locale-gen en_US.UTF-8
RUN pip3 install --upgrade pip && pip3 install pyyaml ply
# 创建 yoctouser 用户
RUN useradd -m -s /bin/bash yoctouser
# 安装 repo 工具
RUN mkdir -p /home/yoctouser/bin && \
wget -O /home/yoctouser/bin/repo https://storage.googleapis.com/git-repo-downloads/repo && \
chmod a+x /home/yoctouser/bin/repo && \
chown -R yoctouser:yoctouser /home/yoctouser/bin && \
echo 'export PATH="${HOME}/bin:${PATH}"' >> /home/yoctouser/.bashrc
# 创建 .ssh 目录并设置权限,为运行时挂载 SSH 密钥做准备
RUN mkdir -p /home/yoctouser/.ssh && \
chmod 700 /home/yoctouser/.ssh && \
chown -R yoctouser:yoctouser /home/yoctouser/.ssh
USER yoctouser
WORKDIR /home/yoctouser
CMD ["echo", "Welcome to the Yocto Development Environment! You are logged in as 'yoctouser'."]
构建 Docker 镜像
bash
cd ~/yocto-imx6ull-docker
docker build -t yocto-imx6ull:22.04 .
由于已配置 Docker Hub 国内镜像,拉取 ubuntu:22.04 基础镜像时会自动走国内加速。
启动容器并进入 Yocto 构建环境
- 创建主机目录用于持久化缓存
Yocto 构建会产生大量下载文件和编译缓存,建议挂载到主机,避免容器删除后丢失。
bash
mkdir -p ~/yocto-build/{downloads,sstate-cache,build}
# 启动容器
docker run -it --rm \
--name yocto-imx6ull \
-v ~/yocto-build:/home/yoctouser/yocto-build \
-v ~/.ssh:/home/yoctouser/.ssh:ro \
-v /etc/localtime:/etc/localtime:ro \
--dns 114.114.114.114 \
yocto-imx6ull:22.04 \
/bin/bash
进入容器后,后续命令均在容器内执行。
验证 repo 已可用
bash
repo --version
配置 git 信息(首次需要)
bash
git config --global user.email "your@email.com"
git config --global user.name "Your Name"
3 yocto
1.3.1 获取 Yocto 源码
i.MX6ULL 属于 NXP 处理器,最成熟的 Yocto 方案是 NXP 官方 i.MX Yocto Release(使用 repo 工具管理多个 Git 仓库),或者手动拉取 poky + meta-freescale。
此方案使用 NXP 官方 manifest,包含针对 i.MX6ULL 优化过的内核、U-Boot 和驱动。
Bash
# 以下repo 安装指令已经包含在 dockerfile里,无需再执行
# 安装 repo 工具
mkdir -p ~/bin
# curl -s https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
wget -O ~/bin/repo https://storage.googleapis.com/git-repo-downloads/repo
chmod a+x ~/bin/repo
export PATH=~/bin:$PATH
# 创建工作目录 -
mkdir -p ~/yocto-build/imx-yocto && cd ~/yocto-build/imx-yocto
# 初始化 NXP manifest(以 imx-linux-scarthgap / 6.6 内核为例,适配 i.MX6ULL)
# init(用官方地址,不要用任何代理)
repo init -u https://github.com/nxp-imx/imx-manifest.git \
-b imx-linux-scarthgap \
-m imx-6.6.52-2.2.0.xml
# 同步代码(耗时较长,约 5~20GB)
repo sync -j$(nproc)
3.2 配置 Yocto 构建(容器内)
初始化构建环境
bash
cd ~/yocto-build/imx-yocto
# 接受 EULA 并初始化构建目录
EULA=1 DISTRO=fsl-imx-wayland MACHINE=imx6ull14x14evk source imx-setup-release.sh -b build
# 若提示 DISTRO fsl-imx-wayland not found,可尝试 DISTRO=fsl-imx-fb 或不指定 DISTRO,使用默认 poky 发行版。
配置 local.conf(国内加速核心)
- 编辑 ~/yocto-build/imx-yocto/build/conf/local.conf,在文件末尾追加以下内容:
bitbake
# ========== 国内网络优化配置 ==========
# 1. 下载目录和 SState 缓存共享(挂载的主机目录)
DL_DIR ?= "${HOME}/yocto-build/downloads"
SSTATE_DIR ?= "${HOME}/yocto-build/sstate-cache"
# 2. 启用 SState 镜像(如果公司有内部缓存服务器可替换)
SSTATE_MIRRORS ?= "\
file://.* http://sstate.yoctoproject.org/PATH;downloadfilename=PATH \n \
file://.* http://mirrors.ustc.edu.cn/yocto/sstate-cache/PATH;downloadfilename=PATH \n \
"
# 3. 配置源码下载代理(PREMIRRORS)
PREMIRRORS:prepend = "\
git://.*/.* https://ghproxy.com/https://github.com/ \n \
https://.*/.* https://ghproxy.com/https://github.com/ \n \
http://.*/.* https://ghproxy.com/https://github.com/ \n \
"
# 4. 并行编译加速(根据容器分配的 CPU 核心调整)
BB_NUMBER_THREADS ?= "8"
PARALLEL_MAKE ?= "-j8"
# 5. QEMU 相关:确保生成 ext4 镜像以便 runqemu 使用
IMAGE_FSTYPES:append = " ext4 wic.qcow2"
SSTATE_MIRRORS 能极大加速二次构建,BitBake 会优先查找预编译缓存
检查 bblayers.conf
确保 build/conf/bblayers.conf 包含以下层(NXP 官方脚本通常自动配置):
bitbake
BBLAYERS ?= " \
${BSPDIR}/sources/poky/meta \
${BSPDIR}/sources/poky/meta-poky \
${BSPDIR}/sources/meta-openembedded/meta-oe \
${BSPDIR}/sources/meta-openembedded/meta-multimedia \
${BSPDIR}/sources/meta-openembedded/meta-python \
${BSPDIR}/sources/meta-freescale \
${BSPDIR}/sources/meta-freescale-3rdparty \
${BSPDIR}/sources/meta-freescale-distro \
${BSPDIR}/sources/meta-imx/meta-bsp \
${BSPDIR}/sources/meta-imx/meta-sdk \
"
3.3 构建镜像
构建 i.MX6ULL 最小系统(用于实际硬件)
bash
cd ~/yocto-build/imx-yocto/build
bitbake core-image-minimal
首次构建耗时较长(3~8 小时,视网络和 CPU 而定),后续修改后重编译会快很多。
如果编译中断后重新进入:
bash
# 重新进入容器
docker run -it --rm \
--security-opt seccomp=unconfined \
--name yocto-imx6ull \
-v ~/yocto-build:/home/yoctouser/yocto-build \
-v ~/.ssh:/home/yoctouser/.ssh:ro \
-v /etc/localtime:/etc/localtime:ro \
--dns 114.114.114.114 \
yocto-imx6ull:22.04 /bin/bash
# 设置环境变量
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# 1. 配置 Git 走 SSH(解决 GnuTLS 问题)
git config --global url."git@github.com:".insteadOf "https://github.com/"
# 2. 启动 ssh-agent 并加载密钥
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
# 3. 验证 GitHub 连通性
ssh -T git@github.com
# 4. 回到构建目录,重新初始化环境
cd ~/yocto-build/imx-yocto
DISTRO=fsl-imx-xwayland MACHINE=imx6ull14x14evk source imx-setup-release.sh -b build
# 5. 用 tmux 挂后台跑,防止终端断开
tmux new -s yocto
bitbake core-image-minimal
# 然后按 Ctrl+B,再按 D,退出 tmux 回到普通终端
# 构建会在后台继续
构建产物位于:
- 镜像:~/yocto-build/imx-yocto/build/tmp/deploy/images/imx6ull14x14evk/
- 包含:core-image-minimal-imx6ull14x14evk.wic、zImage、imx6ull-14x14-evk.dtb 等
完整目录结构参考
构建完成后,主机上的目录结构大致如下:
bash
~/yocto-build/
├── imx-yocto/ # NXP Yocto 源码(容器内挂载)
│ ├── sources/ # poky, meta-freescale, meta-imx...
│ ├── build/ # imx6ull14x14evk 构建目录
├── downloads/ # 共享下载缓存(源码包)
└── sstate-cache/ # 共享编译缓存(加速二次构建)
通过以上步骤,可以在 Ubuntu 20.04 主机上,借助 Ubuntu 22.04 Docker 容器和国内镜像源,完整搭建出 i.MX6ULL 的 Yocto 构建环境.