Yocto で RaspberryPi3 OS を3種類の方法で build する

RaspberryPi3 の OS を Yocto でビルドします。
2017年夏時点で何種類かの OS がビルド出来るのでまとめます。
＃他にも知ってるよという方は教えて頂ければ幸いです。

# 動機・この記事の重視した点

動機は以下を満たす RaspberryPi3 用の OS を作りたかったためです。
試行錯誤し、結果何種類か作ったのでまとめて記事にします。

* （Raspbian ではなく Yocto でビルドし）カスタマイズ出来ること（ライブラリを追加したかったり、 gcc の version を決め打ちしたかったり。この辺の「Yocto の local.conf のいじり方」を別途記事にする需要ありますかねぇ？）
* GUI が存在すること
* Yocto の SDK が作成出来ること

# 用語

| 用語       |       意味 | 注    |
|:-----------------|:------------------|:------------------|
| SDK             | Yocto 的クロスビルド環境 | ラズパイ上で動作するアプリを Ubuntu PC でクロスビルドするときには必要。ホストビルド(ラズパイで動作するアプリをラズパイでビルド)するだけなら不要 |

# 種類

今回3種類ビルドします。
好きなものを使ってください。

### その1：CUI
```
$ bitbake rpi-basic-image
```

### その2：GUI。X11
```
$ bitbake core-image-sato
```

### その3：GUI。Wayland/Weston(desktop-shell)
```
$ bitbake core-image-weston
```

# スクリーンショット

## rpi-basic-image (CUI)

![DSC_0014_edit.JPG](https://qiita-image-store.s3.amazonaws.com/0/35514/c67ae350-ec4f-e877-0005-8741f17809d5.jpeg)


## core-image-sato (GUI, X11)

![DSC_0085.JPG](https://qiita-image-store.s3.amazonaws.com/0/35514/b3fa100e-2e97-3db8-7aec-ef5f25edb69a.jpeg)


## core-image-weston (GUI, Wayland/Weston(desktop-shell))

![DSC_0001 (1).JPG](https://qiita-image-store.s3.amazonaws.com/0/35514/26966dde-7410-d389-cc76-b7d42115c740.jpeg)

# 環境

PC
OS: Ubuntu 14.04
CPU: Intel Core i7-4770 CPU @ 3.40GHz × 8 
RAM: 15.6 GB
Disk: SSD

RaspberryPi3




 


# ビルドと実行

## rpi-basic-image (CUI)

### ビルド

```
$ sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat libsdl1.2-dev xterm
$ git clone git://git.yoctoproject.org/poky
$ cd poky
$ git checkout pyro // 現時点の最新 stable。オリジナルの記事は morty ブランチを使用。
Branch pyro set up to track remote branch pyro from origin.
Switched to a new branch 'pyro'
$ git clone -b pyro git://git.openembedded.org/openembedded-core
$ git clone -b pyro git://git.openembedded.org/meta-openembedded
$ git clone -b pyro git://git.yoctoproject.org/meta-raspberrypi
$ ../bitbake/bin/bitbake-layers add-layer ../meta-yocto
$ ../bitbake/bin/bitbake-layers add-layer ../meta-raspberrypi
$ source oe-init-build-env

You had no conf/local.conf file. This configuration file has therefore been
created for you with some default values. You may wish to edit it to, for
example, select a different MACHINE (target hardware). See conf/local.conf
for more information as common configuration options are commented.

You had no conf/bblayers.conf file. This configuration file has therefore been
created for you with some default values. 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:
    http://yoctoproject.org/documentation

For more information about OpenEmbedded see their website:
    http://www.openembedded.org/

### Shell environment set up for builds. ###

You can now run 'bitbake <target>'

Common targets are:
    core-image-minimal
    core-image-sato
    meta-toolchain
    meta-ide-support

You can also run generated qemu images with a command like 'runqemu qemux86'
$ ../bitbake/bin/bitbake-layers add-layer ../meta-yocto
$ ../bitbake/bin/bitbake-layers add-layer ../meta-raspberrypi
$ cat conf/bblayers.conf 
# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
POKY_BBLAYERS_CONF_VERSION = "2" 

BBPATH = "${TOPDIR}" 
BBFILES ?= "" 

BBLAYERS ?= " \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto/poky/meta \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto/poky/meta-poky \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto/poky/meta-yocto-bsp \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto/poky/meta-yocto \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto/poky/meta-raspberrypi \
  " 
$ vi ./conf/local.conf
- MACHINE ??= "qemux86" 
+ MACHINE ??= "raspberrypi3" 
+ DL_DIR = "/opt2/Yocto/downloads/raspberrypi" 
+ SSTATE_DIR ?= "/opt2/Yocto/sstate/raspberrypi" 
$ time bitbake rpi-basic-image
WARNING: Host distribution "ubuntu-14.04" has not been validated with this version of the build system; you may possibly experience unexpected failures. It is recommended that you use a tested distribution.
Parsing recipes: 100% |###########################################################################################| Time: 0:00:22
Parsing of 851 .bb files complete (0 cached, 851 parsed). 1320 targets, 73 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION        = "1.34.0" 
BUILD_SYS         = "x86_64-linux" 
NATIVELSBSTRING   = "ubuntu-14.04" 
TARGET_SYS        = "arm-poky-linux-gnueabi" 
MACHINE           = "raspberrypi3" 
DISTRO            = "poky" 
DISTRO_VERSION    = "2.3.1" 
TUNE_FEATURES     = "arm armv7ve vfp thumb neon vfpv4 callconvention-hard cortexa7" 
TARGET_FPU        = "hard" 
meta              
meta-poky         
meta-yocto-bsp    
meta-yocto        = "pyro:5686f4e1fe5229705b8c7d35895aa03827796d13" 
meta-raspberrypi  = "pyro:8ba2d6fc80b31c87d25c87c863e2a77752b07c3c" 

NOTE: Fetching uninative binary shim from http://downloads.yoctoproject.org/releases/uninative/1.6/x86_64-nativesdk-libc.tar.bz2;sha256sum=2b4fffa308d9f19e0742a1a404ff42495fb50c165e5ca0458cedca157372691a
--2017-07-22 20:35:01--  http://downloads.yoctoproject.org/releases/uninative/1.6/x86_64-nativesdk-libc.tar.bz2
Resolving downloads.yoctoproject.org (downloads.yoctoproject.org)... 198.145.20.127
Connecting to downloads.yoctoproject.org (downloads.yoctoproject.org)|198.145.20.127|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2535308 (2.4M) [application/octet-stream]
Saving to: ‘/opt/Yocto/downloads/raspberrypi/uninative/2b4fffa308d9f19e0742a1a404ff42495fb50c165e5ca0458cedca157372691a/x86_64-nativesdk-libc.tar.bz2’

2017-07-22 20:35:03 (987 KB/s) - ‘/opt/Yocto/downloads/raspberrypi/uninative/2b4fffa308d9f19e0742a1a404ff42495fb50c165e5ca0458cedca157372691a/x86_64-nativesdk-libc.tar.bz2’ saved [2535308/2535308]

Initialising tasks: 100% |########################################################################################| Time: 0:00:03
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
WARNING: libpng-native-1.6.28-r0 do_fetch: Failed to fetch URL http://distfiles.gentoo.org/distfiles/libpng-1.6.28.tar.xz, attempting MIRRORS if available
NOTE: Tasks Summary: Attempted 2512 tasks of which 6 didn't need to be rerun and all succeeded.

Summary: There were 2 WARNING messages shown.
$
$ ls ./tmp/deploy/images/raspberrypi3/ | grep sdimg
rpi-basic-image-raspberrypi3-20170722131637.rootfs.rpi-sdimg
rpi-basic-image-raspberrypi3.rpi-sdimg
```

### microSD にコピー

```
$ mount
/dev/sdi1 on /media/nxdev/sdcard type ext2 (rw,nosuid,nodev,uhelper=udisks2)
$ umount /dev/sdi1
$ sudo dd if=./tmp/deploy/images/raspberrypi3/rpi-basic-image-raspberrypi3.rpi-sdimg of=/dev/sdi bs=4M
33+0 レコード入力
33+0 レコード出力
138412032 バイト (138 MB) コピーされました、 36.3876 秒、 3.8 MB/秒
```

※sdi がデバイス名だった場合。実際は各自の環境に合わせてください。

### 実機電源ON

root でログイン。
＃ startx で not found。X 起動出来ず。

![DSC_0014_edit.JPG](https://qiita-image-store.s3.amazonaws.com/0/35514/c67ae350-ec4f-e877-0005-8741f17809d5.jpeg)



### ディスク使用量

合計 8GB 必要でした。
${BUILDTOP}/poky = ビルドディレクトリ
/opt/Yocto/sstate/raspberrypi/ = sstate キャッシュディレクトリ(local.conf で指定)
/opt/Yocto/downloads/raspberrypi/ = ダウンロードディレクトリ(local.conf で指定)

```
${BUILDTOP}/poky$ du -sh ./
2.4G	./

$ du -sh /opt/Yocto/sstate/raspberrypi/
1.3G	/opt/Yocto/sstate/raspberrypi/

$ du -sh /opt/Yocto/downloads/raspberrypi/
4.3G	/opt/Yocto/downloads/raspberrypi/
```


## core-image-sato (GUI, X11)

### ビルド

```
$ sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat libsdl1.2-dev xterm
$ git clone git://git.yoctoproject.org/poky.git -b morty
$ cd poky
$ git clone git://git.yoctoproject.org/meta-raspberrypi -b morty
$ git clone https://github.com/mickey-happygolucky/meta-hdmi5inch-rpi.git -b morty
$ source poky/oe-init-build-env
$ bitbake-layers add-layer ../poky/meta-raspberrypi
$ bitbake-layers add-layer ../poky/meta-hdmi5inch-rpi

$ cat conf/bblayers.conf 
# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf
# changes incompatibly
POKY_BBLAYERS_CONF_VERSION = "2"

BBPATH = "${TOPDIR}"
BBFILES ?= ""

BBLAYERS ?= " \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto12_morty22_sato/poky/meta \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto12_morty22_sato/poky/meta-poky \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto12_morty22_sato/poky/meta-yocto-bsp \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto12_morty22_sato/poky/meta-raspberrypi \
  /media/nxdev/SanDiskSDD960G/Proj/EmbeddedFW/yocto12_morty22_sato/poky/meta-openembedded/meta-oe \
  "

$ vi conf/local  // 以下3行追加
+ MACHINE ?= "raspberrypi3"
+ DL_DIR = "/opt2/Yocto/downloads/raspberrypi"
+ SSTATE_DIR ?= "/opt2/Yocto/sstate/raspberrypi"

$ time bitbake core-image-sato  // OS Image ビルド
Parsing recipes: 100% |#################################################################################################################################| Time: 0:00:31
Parsing of 1505 .bb files complete (0 cached, 1505 parsed). 2077 targets, 132 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION        = "1.32.0" 
BUILD_SYS         = "x86_64-linux" 
NATIVELSBSTRING   = "Ubuntu-14.04" 
TARGET_SYS        = "arm-poky-linux-gnueabi" 
MACHINE           = "raspberrypi3" 
DISTRO            = "poky" 
DISTRO_VERSION    = "2.2.2" 
TUNE_FEATURES     = "arm armv7ve vfp thumb neon vfpv4 callconvention-hard cortexa7" 
TARGET_FPU        = "hard" 
meta              
meta-poky         
meta-yocto-bsp    = "morty:7d5822bf4cb2089687c3c9a908cf4a4ef4e9a53a" 
meta-raspberrypi  = "morty:380bf2ff445c2049bdea15f3bb36e8cb36540345" 
meta-oe           = "morty:fe5c83312de11e80b85680ef237f8acb04b4b26e" 
meta-nodejs       = "master:eec531e97a17bfd406f3bf76dee4057dcf5286a4" 

Initialising tasks: 100% |##############################################################################################################################| Time: 0:00:09
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
NOTE: Tasks Summary: Attempted 6234 tasks of which 3040 didn't need to be rerun and all succeeded.

real    41m51.710s
user    242m5.610s
sys    51m32.270s

$ time bitbake core-image-sato -c populate_sdk
Parsing recipes: 100% |#################################################################################################################################| Time: 0:00:22
Parsing of 1505 .bb files complete (0 cached, 1505 parsed). 2077 targets, 132 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION        = "1.32.0" 
BUILD_SYS         = "x86_64-linux" 
NATIVELSBSTRING   = "universal-4.9" 
TARGET_SYS        = "arm-poky-linux-gnueabi" 
MACHINE           = "raspberrypi3" 
DISTRO            = "poky" 
DISTRO_VERSION    = "2.2.2" 
TUNE_FEATURES     = "arm armv7ve vfp thumb neon vfpv4 callconvention-hard cortexa7" 
TARGET_FPU        = "hard" 
meta              
meta-poky         
meta-yocto-bsp    = "morty:7d5822bf4cb2089687c3c9a908cf4a4ef4e9a53a" 
meta-raspberrypi  = "morty:380bf2ff445c2049bdea15f3bb36e8cb36540345" 
meta-oe           = "morty:fe5c83312de11e80b85680ef237f8acb04b4b26e" 
meta-nodejs       = "master:eec531e97a17bfd406f3bf76dee4057dcf5286a4" 

Initialising tasks: 100% |##############################################################################################################################| Time: 0:00:08
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
NOTE: Tasks Summary: Attempted 5518 tasks of which 5314 didn't need to be rerun and all succeeded.

real    14m31.534s
user    58m56.546s
sys    5m52.275s
$
```


### microSD にコピー

```
$ mount
/dev/sdi1 on /media/nxdev/sdcard type ext2 (rw,nosuid,nodev,uhelper=udisks2)
$ umount /dev/sdi1
$ sudo dd if=./tmp/deploy/images/raspberrypi3/core-image-sato-raspberrypi3.rpi-sdimg of=/dev/sdi bs=4M
```

※sdi がデバイス名だった場合。実際は各自の環境に合わせてください。



### 実機電源ON

![DSC_0085.JPG](https://qiita-image-store.s3.amazonaws.com/0/35514/b3fa100e-2e97-3db8-7aec-ef5f25edb69a.jpeg)


### ディスク使用量

合計約 52.5GB 必要でした。

`${BUILDTOP}/poky` = ビルドディレクトリ
`/opt/Yocto/sstate/raspberrypi/` = sstate キャッシュディレクトリ(local.conf で指定)
`/opt/Yocto/downloads/raspberrypi/` = ダウンロードディレクトリ(local.conf で指定)

```
${BUILDTOP}/poky$ du -sh ./
28G	./

$ du -sh /opt/Yocto/sstate/raspberrypi/
18G	/opt/Yocto/sstate/raspberrypi/

$ du -sh /opt/Yocto/downloads/raspberrypi/
6.5G	/opt/Yocto/downloads/raspberrypi/
```



## core-image-weston (GUI, Wayland/Weston(desktop-shell))

### ビルド

```
$ sudo apt-get install gawk wget git-core diffstat unzip texinfo gcc-multilib build-essential chrpath socat libsdl1.2-dev xterm
$ git clone git://git.yoctoproject.org/poky.git -b morty
$ cd poky
$ git clone git://git.yoctoproject.org/meta-raspberrypi -b morty
$ git clone git://git.openembedded.org/meta-openembedded -b morty
$ git clone https://github.com/imyller/meta-nodejs.git 
$ source oe-init-build-env
$ bitbake-layers add-layer ../meta-raspberrypi
$ bitbake-layers add-layer ../meta-openembedded/meta-oe

$ vi ./conf/local.conf
- MACHINE ??= "qemux86" 
+ MACHINE ?= "raspberrypi3" 
+ DISTRO_FEATURES_append = " wayland opengl" 
+ DISTRO_FEATURES_remove = " x11" 
+ MACHINE_FEATURES += "vc4graphics" 
+ DL_DIR = "/opt/Yocto/downloads/raspberrypi" 
+ SSTATE_DIR ?= "/opt/Yocto/sstate/raspberrypi" 
+ CONNECTIVITY_CHECK_URIS = "" 

$ time bitbake core-image-weston
$ time bitbake core-image-weston -c populate_sdk
```


### microSD にコピー

```
$ sudo dd if=tmp/deploy/images/raspberrypi3/core-image-weston-raspberrypi3.rpi-sdimg of=/dev/sdi bs=100M
```
※sdi がデバイス名だった場合。実際は各自の環境に合わせてください。

### ディスク使用量

合計約 37.5GB 必要でした。
`${BUILDTOP}/poky` = ビルドディレクトリ
`/opt/Yocto/sstate/raspberrypi/` = sstate キャッシュディレクトリ(local.conf で指定)
`/opt/Yocto/downloads/raspberrypi/` = ダウンロードディレクトリ(local.conf で指定)


```
${BUILDTOP}/poky$ du -sh ./
13G	./

$ du -sh /opt/Yocto/sstate/raspberrypi/
18G	/opt/Yocto/sstate/raspberrypi/

$ du -sh /opt/Yocto/downloads/raspberrypi/
6.5G	/opt/Yocto/downloads/raspberrypi/
```

## まとめ

まとめます。

| OS image 名       | 一言特徴                            | SDK 作成可否 |
|:------------------|:-----------------------------------|:------------|
| rpi-basic-image   | CUI                                | 可 |
| core-image-sato   | GUI, X11                           | 可 |
| core-image-weston | GUI, Wayland/Weston(desktop-shell) | 可 |


## その他

ビルドコマンド

```
$ vi ./conf/local.conf
- MACHINE ??= "qemux86" 
+ MACHINE ??= "raspberrypi3" 
+ DL_DIR = "/opt/Yocto/downloads/raspberrypi" 
+ SSTATE_DIR ?= "/opt/Yocto/sstate/raspberrypi" 
```

の `DL_DIR` , `SSTATE_DIR`  は Yocto ビルドの高速化設定です。
詳しくは [Yocto build 時間を10時間から10分に高速化した話](http://qiita.com/naohikowatanabe/items/868eefed28cefec20ff8)
にまとめているので良かったら参考にどうぞです。


# 参考

### Yocto 全体
[YoctoProject Release Activity](https://wiki.yoctoproject.org/wiki/Releases)

### rpi-basic-image (CUI)
[Yocto ProjectによるRaspberry Pi用のカスタムLinux作成](https://www.usagi1975.com/24mar172100/)
[raspberrypi3でyocto](http://mickey-happygolucky.hatenablog.com/entry/2016/03/23/002406)

### core-image-sato (GUI, X11)
[meta-raspberrypi mortyでcore-image-satoが起動できない時の対処 その2](http://mickey-happygolucky.hatenablog.com/entry/2017/02/08/013946)

### core-image-weston (GUI, Wayland/Weston(desktop-shell))
[ラズベリーパイ3 pyroでweston](http://mickey-happygolucky.hatenablog.com/entry/2017/05/31/011009)
[raspberrypi2 yoctoでweston(wayland)](http://mickey-happygolucky.hatenablog.com/entry/2015/03/01/160138)