みつきんのメモ

組み込みエンジニアです。Interface誌で「Yocto Projectではじめる 組み込みLinux開発入門」連載中

Yocto Project Kirkstone(4.0) do_fetch以外でのネットワークアクセスについて

はじめに

GW前にYoctoProject 4.0(kirkstone)がリリースされた。

変更点の詳細はRelease 4.0 (kirkstone)を参照。

使ってみようと思っていたら、ツイッターで下記のような情報が流れてきた。

Release 4.0 (kirkstone)に下記のような記述を見つけた。

Network access from tasks is now disabled by default on kernels which support this feature (on most recent distros such as CentOS 8 and Debian 11 onwards). This means that tasks accessing the network need to be marked as such with the network flag.

関係ありそうなのでちょっと実験をしてみる。

Kirkstoneをビルド

作業場所は~/work/yocto/kirkstoneとする。

$ mkdir -p ~/work/yocto/kirkstone && cd ~/work/yocto/kirkstone

ソースの取得

下記のコマンドでpokyのビルド環境をダウンロードする。

$ git clone -b kirkstone git://git.yoctoproject.org/poky.git

観葉変数の設定

$ source poky/oe-init-build-env

ここまでで実験の準備は整った。

cmakeのexternal_projectの実験

タスクの中からネットワークを参照するとエラーになるかどうかを確認する。

テスト用レイヤの作成

$ bitbake-layers create-layer meta-test
$ bitbake-layers add-layer ./meta-test

レシピの作成

作成されたレイヤにデフォルトで含まれているrecipes-example/example/example_0.1.bbを使用する。

ソースファイルを配置するディレクトリを作成し、CMakeLists.txtを作成する。

$ mkdir ./build/meta-test/recipes-example/example/files
$ vim ./build/meta-test/recipes-example/example/files/CMakeLists.txt

CMakeLists.txtの内容は下記のようにする。

cmake_minimum_required(VERSION 3.16)
project(example)

set(helloworld_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/helloworld")
set(helloworld_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/helloworld")
set(helloworld_CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${helloworld_INSTALL_DIR})

include(ExternalProject)
ExternalProject_Add(
  helloworld
  PREFIX ${helloworld_PREFIX}
  GIT_REPOSITORY https://github.com/mickey-happygolucky/helloworld.git
  GIT_TAG main
  INSTALL_DIR ${helloworld_INSTALL_DIR}
  CMAKE_ARGS ${helloworld_CMAKE_ARGS}
)

これはExternal Projectとしてgithubからhelloworldを取得するようになっている。

example_0.1.bbを編集する。

$ vim ./build/meta-test/recipes-example/example/example_0.1.bb

内容は下記。

SUMMARY = "bitbake-layers recipe"
DESCRIPTION = "Recipe created by bitbake-layers"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"

inherit cmake

SRC_URI = "file://CMakeLists.txt"

S = "${WORKDIR}"

do_install() {
    install -d ${D}/${bindir}
    install ${B}/helloworld/bin/helloworld ${D}/${bindir}
}

exampleのビルド

exampleをビルドしてみる。

$ bitbake example

Ubuntu20.04 + CROPSではエラーにならない

Ubuntu 20.04のCROPS環境では、全く問題なくビルドできてしまった。

debian 11ではエラー

下記のような記述があったので、VMdebian 11 の環境を作成して試してみた。(すごい時間がかかった。)

Network access from tasks is now disabled by default on kernels which support this feature (on most recent distros such as CentOS 8 and Debian 11 onwards).

するとexampleのレシピが、helloworldをクローンするタイミングでエラーが発生した。

| Cloning into 'helloworld'...
| fatal: unable to access 'https://github.com/mickey-happygolucky/helloworld.git/': Could not resolve host: github.com
| Cloning into 'helloworld'...
| fatal: unable to access 'https://github.com/mickey-happygolucky/helloworld.git/': Could not resolve host: github.com
| Cloning into 'helloworld'...
| fatal: unable to access 'https://github.com/mickey-happygolucky/helloworld.git/': Could not resolve host: github.com
| -- Had to git clone more than once:
|           3 times.
| CMake Error at /home/mickey/work/yocto/kirkstone/build/tmp/work/core2-64-poky-linux/example/0.1-r0/build/helloworld/tmp/helloworld-gitclone.cmake:31 (message):
|   Failed to clone repository:
|   'https://github.com/mickey-happygolucky/helloworld.git'
(...snip...)
Summary: 1 task failed:
  /home/mickey/work/yocto/kirkstone/build/meta-test/recipes-example/example/example_0.1.bb:do_compile

たぶんこれが件の現象と同じものだと思う。このケースではdo_compileタスクがエラーを起こしているので、下記をexample_0.1.bbに追加する。

do_compile[network] = "1"

これでビルドがとおるようになった。

まとめ

  • kirkstoneでは、fetch以外のタスクがネットワークにアクセスするとエラーがで発生するようになった。
  • ただしホストOSがUbuntu 20.04の場合では発生せず、debian 11やCentOS8など新し目のディストロだと発生するようだ。
  • 基本的にはdo_fetch以外のタスクがネットワークにアクセスしては行けない。
  • その他のタスクでネットワークアクセスしたい場合はタスクにnetworkフラグを設定することで許容される。

※この記事の発端となったツイートの方はまだ解決していないようなので、まだ調査の余地はあるかも。