如何在 Ubuntu 上用 Yocto 创建你自己的嵌入式 Linux 发行版
发行版 嵌入式 Yocto poky
本文主要聚焦在如何使用 Yocto 在 Ubuntu 上创建一个最小化的 Linux 发行版。Yocto 项目在嵌入式 Linux 的世界非常著名，这是因为它用起来非常灵活、方便。Yocto 的目标是为嵌入式软硬件开发商创建自己的 Linux 发行版。本文我们将会创建一个可以运行在 QEMU 上的最小化的 Linux，并且在 QEMU 上实际运行。


![](/data/attachment/album/201703/05/193501cvqcovv9hqoch95c.jpg)


### 开发机的基本条件


* 最少 4-6 GB 内存
* 最新版的 Ubuntu 系统（本文使用了 16.04 LTS）
* 磁盘剩余空间至少 60-80 GB
* 在创建 Linux 发行版之前先安装下面的软件包
* 下载最新的 Yocto（Poky 是其最小开发环境）稳定分支



```
apt-get update

```


```
apt-get install wget git-core unzip make gcc g++ build-essential subversion sed autoconf automake texi2html texinfo coreutils diffstat python-pysqlite2 docbook-utils libsdl1.2-dev libxml-parser-perl libgl1-mesa-dev libglu1-mesa-dev xsltproc desktop-file-utils chrpath groff libtool xterm gawk fop

```

![Install prerequisites for Yocto](/data/attachment/album/201703/05/193519mtal096za6m0ct2w.png)


如下所示，开发环境要安装的软件包将近 1GB 大小。


![Install the development packages](/data/attachment/album/201703/05/193523yljhlkqtpq1u1uwj.png)


在这个教程中，系统上克隆的是 poky 的 `morty` 稳定分支。



```
 git clone -b morty git://git.yoctoproject.org/poky.git

```

![install poky](/data/attachment/album/201703/05/193526gek6slpwsac74zll.png)


进入 `poky` 目录，然后运行下面的命令为 Yocto 开发环境设置（设置/导出）一些环境变量。



```
source oe-init-build-env

```

如下所示，在运行了 open embedded (oe) 的构建环境脚本之后，终端里的路径会自动切换到 `build` 目录，以便进行之后行发行版的的配置和构建。


![Prepare OE build environment](/data/attachment/album/201703/05/193529yv70rvrt0ll85ts7.png)


上面的截屏展示了在 `conf` 目录下创建的文件 `local.conf`。这是 Yocto 用来设置目标机器细节和 SDK 的目标架构的配置文件。


如下所示，这里设置的目标机器是 `qemux86-64`。


![Set the target machine type](/data/attachment/album/201703/05/193532il9pq2f2cs4ssc9q.png)


如下面截图所示，在 `local.conf` 中取消下面参数的注释符号。



```
DL_DIR ?= "${TOPDIR}/downloads"

```

![Configure local.conf file](/data/attachment/album/201703/05/193535o8ukurh9mmjmm3lu.png)



```
SSTATE_DIR ?= "${TOPDIR}/sstate-cache"

```

![Set SSTATE_DIR](/data/attachment/album/201703/05/193537kcwa2xpwphhychj5.png)



```
TMPDIR ?= "${TOPDIR}/tmp"

```

![Set TMPDIR](/data/attachment/album/201703/05/193540eaf24lvnignvedvi.png)



```
PACKAGE_CLASSES ?= "package_rpm"
SDKMACHINE ?= "i686"

```

![Set PACKAGE_CLASSES and SDKMACHINE](/data/attachment/album/201703/05/193544ozq7rna4in5s3abo.png)


如下所示，在 `local.conf` 中为基于 Yocto 的 Linux 设置空密码和后续的一些参数。否则的话用户就不能登录进新的发行版。



```
EXTRA_IMAGE_FEATURES ?= "debug-tweaks"

```

![Set debug-tweaks option](/data/attachment/album/201703/05/193547qx5ke5k54rrbbqdh.png)


我们并不准备使用任何图形化工具来创建 Linux OS，比如 `toaster` （`hob` 已经不再支持了）。


### Yocto 编译构建过程


现在运行下面的 `bitbake` 工具命令开始为选定的目标机器下载和编译软件包。



```
bitbake core-image-minimal

```

![Start bitbake](/data/attachment/album/201703/05/193550frry7knu2vjnx612.png)


**非常重要的是要在普通 Linux 用户下运行上面的命令，而不是使用 root 用户**。如下面截图所示，当你在 root 用户下运行 bitbake 命令会产生下面所示的错误。


![Do not run bitbake as root](/data/attachment/album/201703/05/193554n5ddg15kg8nqjqzo.png)


再一次运行导出环境变量的脚本（`oe-init-build-env`），重新执行相同的命令来启动下载和编译过程。


![rerun commands](/data/attachment/album/201703/05/193556s82b86upkfv2bbf0.png)


如下所示，构建脚本组件的第一步工作是解析配置（`recipe`）。


![Parse the build recipes](/data/attachment/album/201703/05/193600oxam8ccs4fn78cs3.png)


下面的截图展示了构建脚本的解析过程。同时也显示了用来构建你的新的基于 yocto 的发行版的构建系统的细节。


![Building proceeds](/data/attachment/album/201703/05/193603g0tjp5rcxfxzlcp7.png)


在下载了 SDK 和必要的库之后，下一步工作是下载并编译软件包。如下截图展示了为构建新发行版而执行的任务。这一步将会执行 2-3 小时，因为首先要下载需要的软件包，然后还要为新的 Linux 发行版编译这些软件包。


![Compilation will take several hours](/data/attachment/album/201703/05/193606eswl5g0hyawft0ll.png)


下面的截图表明了任务列表执行完毕。


![Compilation](/data/attachment/album/201703/05/193608p9k3kf90fxdn2gh9.png)


为目标机器类型 `qemux86-64` 编译好的新镜像位于 `build/tmp/deploy/images/qemux86-64`：


![Build complete](/data/attachment/album/201703/05/193612zggxgg3xg3gb2gi3.png)


如下所示，上面的命令如果运行在 `Putty` 上会产生一个错误。


![command error in putty](/data/attachment/album/201703/05/193617bupjqhlvvsojh79j.png)


通过 `rdp` 在 Ubuntu 平台上再次运行上面的命令。


![Command works fine in rdp](/data/attachment/album/201703/05/193626ey06rzxyy79r06ky.png)


为运行新的基于 Yocto 的 Linux 发行版的 qemu 打开一个新屏幕。


![Open Quemu emulator](/data/attachment/album/201703/05/193634xz17kwl7csbsd990.png)


下面展示了新发行版的登录界面，同时也显示了使用的 yocto 项目的版本号。默认的用户名是 `root` ，密码为空。


![Linux distribution started](/data/attachment/album/201703/05/193640wyy3q3yel0d0mmbj.png)


最后使用 `root` 用户名和空密码登录新发行版。如下截图所示，在这个最小版本的 Linux 上运行了基本的命令（`data` 、 `ifconfig` 和 `uname`）。


![Test the Linux distribution](/data/attachment/album/201703/05/193647wg4fppxugjggjg7y.png)


本文的目标是理解使用 Yocto 创建新的 Linux 发行版的过程。




---


via: <https://www.howtoforge.com/tutorial/how-to-create-your-own-linux-distribution-with-yocto-on-ubuntu/>


作者：[Ahmad](https://www.howtoforge.com/tutorial/how-to-create-your-own-linux-distribution-with-yocto-on-ubuntu/) 译者：[Ezio](https://github.com/oska874) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to create your own Linux Distribution with Yocto on Ubuntu

In this article, our focus is the creation of a minimal Linux distribution using the Yocto project on the Ubuntu platform. The Yocto project is very famous in the embedded Linux world because of its flexibility and ease of use. The purpose of the Yocto project is to create a Linux distro for manufacturers of embedded hardware and software. A new minimal Linux distro will be created for qemu as the (qemu is a basic software emulator) target machine and we will run it in qemu.

## Prerequisites for the development machine

- At least 4 - 6 GB RAM.
- Recent Ubuntu OS (16.04 LTS in this case).
- At least 60-80 GB free space on the disk.
- Installation of following packages before creation of new Linux distro.
- Download latest Yocto (Poky which is minimal development environment) stable branch.

apt-get update

apt-get install wget git-core unzip make gcc g++ build-essential subversion sed autoconf automake texi2html texinfo coreutils diffstat python-pysqlite2 docbook-utils libsdl1.2-dev libxml-parser-perl libgl1-mesa-dev libglu1-mesa-dev xsltproc desktop-file-utils chrpath groff libtool xterm gawk fop

As shown below, almost 1 GB size is required to install required development packages.

In this tutorial, the "morty" stable release of poky is cloned on the system.

git clone -b morty git://git.yoctoproject.org/poky.git

Go inside the "poky" directory and run the following command to set/export some variables for yocto development.

source oe-init-build-env

As shown below, after running the open embedded (oe) build environment script, the path location in the terminal will be changed to a "build" directory for the further configuration and compilation of new distribution.

The above screenshot shows that the "local.conf" file is created inside the "conf" directory. This is the configuration file for yocto which specifies details of the target machine and SDK for desired architecture etc.

As shown below, setting target machine "qemux86-64".

Uncomment following parameters in "local.conf" file as shown in the screenshots.

DL_DIR ?= "${TOPDIR}/downloads"

SSTATE_DIR ?= "${TOPDIR}/sstate-cache"

TMPDIR ?= "${TOPDIR}/tmp"

PACKAGE_CLASSES ?= "package_rpm"

SDKMACHINE ?= "i686"

As shown below, set a blank password for the Yocto based Linux and include the following parameters in the local.conf file. Otherwise, the user will not be able to login in the new distro.

EXTRA_IMAGE_FEATURES ?= "debug-tweaks"

We are not using any GUI tool such as toaster (hob is no more supported) to create Linux OS.

## Yocto Compilation and Building Process

Now run the following command of the bitbake utility to start the download and compilation of packages for the selected target machine.

bitbake core-image-minimal

It is important to run the above command as a normal Linux user and not the root user. As shown in the following sscreenshot, an error is generated when you run the bitbake command as root user.

Again, run the export of environment variables script (oe-init-build-env) and re-run the same command to start the downloading and compilation process.

As shown below, the first step of build script utility is to parse the recipe.

The following screenshot shows the completion of the parsing step of the build script. It also shows the details of the build system on which the new yocto based distro will be generated.

After downloading the SDK and necessary libraries, the next step is to download and compile the packages. The following screenshot shows the task for the new distribution. This step will take 2-3 hours because first, it downloads the required packages and then compiles for the new Linux distribution.

The following screenshot shows the completion of the task list.

The compiled new images for the target machine type "qemux86-64" is inside the "build/tmp/deploy/images/qemux86-64" path as shown below.

As shown below, above command will produce an error if run in the Putty.

Above command is again run inside the terminal via rdp on Ubuntu platform.

Another screen is opened for the qemu emulator for new yocto based Linux distro.

The login screen of the new distro is shown below which also shows the reference version of the yocto project. The default username is root and a blank password.

Finally, login in the new distro with root username and an empty password. As shown in the following screenshot, basic commands (date,ifconfig and uname) are run in the minimal version of Linux.

The purpose of this article is to understand the procedure for the creation of new Linux distribution using yocto project.