Share
Embed
Yocto projectを用いたCustom Linux Image のつくりかた/How to build Custom Linux Image with Yocto project
Yocto projectを用いたCustom Linux imageの作り方について解説します。
Yocto projectの全体像からパッケージの追加の方法までをなるべくわかりやすくご紹介します。
Toshifumi NISHINAGA
March 08, 2019
Transcript
124 slides
-
© 2019 NTT Communications Yocto projectを用いた Custom Linux Image のつくりかた
Toshifumi NISHINAGA 1 -
© 2019 NTT Communications $ who • Name: Toshifumi NISHINAGA
• Activities: ◦ Google Summer of Code 2016(Linux Foundation) ▪ https://summerofcode.withgoogle.com/archive/2016/projects/6617849892175872/ ▪ Porting Linux to ARM Cortex-M7 microcontroller. ◦ U-Boot contributor(not active recently…) • link: ◦ https://github.com/tnishinaga ◦ https://speakerdeck.com/tnishinaga 2 -
© 2019 NTT Communications 今のお仕事の目的 • Kamueeを配布するための Kamuee Image Builderを更改したい
• Cutting Edge側の希望 ◦ VyOSのようなISOインストーラーがほしい ◦ ソフトウェアバージョンの冪等性確保 ◦ ビルド時間短縮 ◦ ライセンス管理の簡略化 ◦ version切り替え及びUpdateが容易な仕組み 3 Yocto Custom Linux Installer -
© 2019 NTT Communications 目次 1. Custom Linux ImageとYocto project
2. Handson: Let's build linux image with yocto project 3. Yocto project overview 4. Handson: Add new package to reference image 5. Appendix 4 -
© 2019 NTT Communications なぜカスタムLinuxイメージを作るのか • Debian等のディストリをベースに改造したほうが便利 • custom linux
imageが必要になる場面 ◦ ストレージ容量が極端に少ない ◦ 余計なソフトウェアを入れたくない ◦ 入っているソフトウェアのライセンスをすべて把握したい ◦ ソフトウェアバージョンの冪等性を確保したい 6 -
© 2019 NTT Communications Custom Linux Imageを作るソフトウェア • Yocto project
◦ https://www.yoctoproject.org ◦ 流行ってる方。将来性があるかを確かめるためにも今回開発に利用 ◦ Buildrootに比べると最初のビルドからちょっと大変 • Buildroot ◦ https://buildroot.org ◦ Linux kernelのmenuconfigの感覚でCustom Linux Imageが簡単に作れる。 ◦ Yoctoに比べると最初のビルドは簡単 7 Ref: [Deciding between Buildroot & Yocto](https://lwn.net/Articles/682540/) -
© 2019 NTT Communications Yocto projectとは • Custom Linux Imageを作るため
の巨大なプロジェクト • ビルドシステム、標準ディストリ ビューションなどを内包 https://www.yoctoproject.org/ 8 -
© 2019 NTT Communications Simple Image: Yocto project Tux: ©
Larry Ewing, Simon Budig and Anja Gerwinski http://www.home.unix-ag.org/simon/penguin/README 9 Yocto project Build Linux Kernel + Rootfs -
© 2019 NTT Communications Yocto features(pickup) • Binary Reproducibility ◦
アプリケーションバージョンの冪等性を保てる (ビルド番号とcommit idがあれば) • License Manifest ◦ イメージに含まれるソフトウェアのライセンスはすべて把握可能 ◦ 拒否したいライセンスの除外も可 • Supports Partial Builds ◦ 2度目以降はキャッシュが使えて高速ビルドができる。ただし 1度目は遅い。 • その他 ◦ Webビルドの仕組みがある(2019/02前半にバグが直って使えるようになった) ◦ Cross Toolchain(SDK)の作成も一緒にできる 10 Ref: https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#gs-features -
© 2019 NTT Communications Tutorial: Let's build linux image with
yocto project 11 -
© 2019 NTT Communications Tutorial: Let's build linux image with
yocto • Contents ◦ build machine requirements ◦ build machine setup ◦ build ◦ run on QEMU 12 -
© 2019 NTT Communications build machine requirements(my experience) • CPU
◦ more than 4 core(high performance as possible as you can) • Memory ◦ more than 1GB x (cpu cores) • Storage ◦ more than 120GB(minimal) • High speed Internet • OS ◦ Ubuntu 16.04 LTS 13 -
© 2019 NTT Communications Build machine setup • Install packages
◦ sudo apt-get install \ gawk wget git-core diffstat unzip texinfo gcc-multilib \ build-essential chrpath socat cpio python python3 python3-pip \ python3-pexpect xz-utils debianutils iputils-ping \ libsdl1.2-dev xterm xsltproc docbook-utils fop dblatex xmlto \ python-git • Reference ◦ https://www.yoctoproject.org/docs/2.6/ref-manual/ref-manual.html#requir ed-packages-for-the-build-host 14 Ref: https://www.yoctoproject.org/docs/2.6/brief-yoctoprojectqs/brief-yoctoprojectqs.html -
© 2019 NTT Communications Build machine setup • Clone poky
repository ◦ git clone git://git.yoctoproject.org/poky ~/poky • Switch to newest stable branch(thud:2.6) ◦ cd ~/poky ◦ git checkout -b thud origin/thud 15 Ref: https://www.yoctoproject.org/docs/2.6/brief-yoctoprojectqs/brief-yoctoprojectqs.html -
© 2019 NTT Communications Build • load yocto toolchains ◦
cd ~/poky ◦ source oe-init-build-env ▪ *** this script creates build directory(~/poky/build) and cd to it *** ▪ *** it loads yocto build system tools(bitbake, devtool, etc...) *** • build reference image ◦ bitbake core-image-minimal • Have a coffee break(more than 1 hour) ◦ ☕☕☕ 16 -
© 2019 NTT Communications Run on QEMU • runqemu qemux86
core-image-minimal nographic 17 -
© 2019 NTT Communications Yocto Challenges • Steep Learning Curve
• Understanding What Changes You Need to Make For Your Design Requires Some Research ◦ チュートリアル以降の難易度がゲキムズ ◦ 「同じことをするために複数の方法があるので迷う」と公式にあるが、それどこ ろか1つもわからないときがある ◦ わからない→わかる→なんもわからんの繰り返し ◦ ようこそ組み込み開発の世界へ。 21 Ref: https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#gs-challenges -
© 2019 NTT Communications • Linuxとその周辺に関する深い知識 ◦ bootloaderからlogin shellが出るまでに何がどう動くかの知識 ◦
Linuxのディレクトリ構造の知識 ◦ package作成手順の知識 • アプリケーションのビルド経験 ◦ 問題切り分け能力 ▪ Yoctoの特殊環境が原因? ただのバグ? CPUアーキテクチャ固有の問題 ? ◦ 経験からくるエスパー力 ▪ 情報のないエラーログからも10の原因を想定し試して解決する • エラーに絶対負けないという根気 Yocto projectを使うのに必要な能力 22 -
© 2019 NTT Communications • Yocto projectの全体像 ◦ recipe, config,
layer, bitbakeの関係 ◦ ソースコードの概要 • 何をしたいときに何をすれば良い? • イメージの変更方法 ◦ パッケージの追加方法(recipe作成) ◦ Kernel moduleの追加(kernel config) ◦ CPUアーキテクチャの設定方法(Machine BSP Config) わからなかったこと 24 -
© 2019 NTT Communications 今後の目次 • Yoctoの全体像と用語を知る ◦ Overview ◦
レイヤシステム ◦ build system ◦ ソースコードの概要 • 何をしたいときに何をすれば良い? • Handson: パッケージを追加してみる 25 -
© 2019 NTT Communications 今後の目次 • Yoctoの全体像と用語を知る ◦ Overview ◦
レイヤシステム ◦ build system ◦ ソースコードの概要 • 何をしたいときに何をすれば良い? • Handson: パッケージを追加してみる 26 -
© 2019 NTT Communications Yocto project overview • Yocto projectを構成する要素を分解する
• 出てくる用語に迷わないようにする ◦ open-embedded(oe) ◦ poky ◦ version(develop name) ◦ bitbake ◦ layer ◦ recipe ◦ etc... 28 Ref: https://www.yoctoproject.org/docs/2.5.2/mega-manual/mega-manual.html#what-is-the-yocto-project -
© 2019 NTT Communications Image from : https://www.yoctoproject.org/docs/2.5.2/meg a-manual/figures/key-dev-elements.png Yocto
project overview • Yocto is not distribution name ◦ Yocto is project name ◦ It contains build-tools, distribution, etc… • Poky ◦ Reference Distribution • OpenEmbedded ◦ Build framework ◦ It provide build system(bitbake, recipe, etc...) 29 -
© 2019 NTT Communications Simple Image: Yocto project 30 Yocto
project Poky OpenEmbedded Recipe sets build system Poky based distribution (Linux Kernel + rootfs) Tux: © Larry Ewing, Simon Budig and Anja Gerwinski http://www.home.unix-ag.org/simon/penguin/README -
© 2019 NTT Communications Yocto project version • Here is
version number and codename ◦ 2.6 : thud (latest at 2019/02/01) ◦ 2.5 : sumo ◦ 2.4 : rocko • Yocto using both name ◦ https://git.yoctoproject.org/cgit/cgit.cgi/poky/refs/ ◦ when checkout 2.6 branch(it has update since released) ▪ git checkout -b thud origin/thud ◦ when checkout 2.6 release ▪ git checkout tags/yocto-2.6 -b yocto-2.6 31 -
© 2019 NTT Communications Build System Overview(simple version) 32 bitbake
Metadata Configurations Poky based distribution (Linux Kernel + rootfs) Tux: © Larry Ewing, Simon Budig and Anja Gerwinski http://www.home.unix-ag.org/simon/penguin/README -
© 2019 NTT Communications Build System Concepts • Metadata(recipe +
patch) • Configurations ◦ User configuration ◦ Machine BSP Configuration ◦ Policy Configuration • Layer • bitbake 33 https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#openembedded-build-system- build-concepts -
© 2019 NTT Communications Recipe • パッケージ作成の素 • ビルド手順(tasks)等が書いてある •
拡張子は.bbと.bbappendの2種類ある ◦ .bb : オリジナルのレシピファイル ◦ .bbappend : オリジナルに変更を加える 34 -
© 2019 NTT Communications • イメージに簡易な変更を加える設定集 • 拡張子は.conf • 大まかに以下の3つがある
◦ User config(ここではUser configを紹介) ◦ Machine BSP config ◦ Policy Configuration Configurations 35 https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#metadata-machine-configuration-and-poli cy-configuration -
© 2019 NTT Communications User configuration • ユーザーの希望する設定をビルドシステムに 伝えるもの •
build/conf以下にファイルがある • 詳細は次ページ 36 https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#user-configuration -
© 2019 NTT Communications User configuration files 37 • conf/bblayers.conf
◦ レイヤの登録 ◦ レイヤの順序関係もここで設定 • conf/local.conf ◦ ターゲットマシンの設定や出力イメージタイプの設定な どができる ◦ 追加パッケージ設定もここで行える ◦ 軽微な変更はとりあえずここに書けばOK https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#user-configuration -
© 2019 NTT Communications Layer(meta-XXX) • レシピやconfigを集めたもの • イメージはこのレイヤーを重ねて作っ ていく
• 下のレイヤでappendレシピを書くと 上のレイヤのレシピを加筆修正でき る • レイヤの順序はconf/bblayers.conf に定義されている Distribution layer1 layer3 layer2 38 append append -
© 2019 NTT Communications bitbake • イメージをビルドするシステム • Recipeとconfigを参考にソースの取得、ビ ルド、イメージ生成、SDK生成等を行う
• bitbakeコマンドで使える 39 https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#bitbake-dev-environment -
© 2019 NTT Communications これまでの用語まとめ 40 用語 概要 Yocto プロジェクト名
OpenEmbedded(OE) ビルドシステム Poky ディストリビューション名 thud 開発名。Versionでは2.6 bitbake ビルドシステムのコマンド recipe パッケージビルド方法を書いたもの layer パッケージやconfigをまとめたもの -
© 2019 NTT Communications Yocto project source code • Poky
repository ◦ https://git.yoctoproject.org/git/poky ◦ Yocto projectのシステムが全て入ったリポジトリ • 今後のためPokyディレクトリ以下の構造を簡単にご説明 42 -
© 2019 NTT Communications tree -L 1 poky 44 bitbakeコマンドなど
ビルドシステムのファイルが 入っている -
© 2019 NTT Communications tree -L 1 poky 45 ビルド用ディレクトリ
ビルド済みイメージや user configなどが入る -
© 2019 NTT Communications tree -L 1 poky 47 環境設定用ファイル
最初にsourceコマンドで読み 込む -
© 2019 NTT Communications tree -L 1 poky 48 開発を便利にするスクリプトた
ちがあるところ wicやrunqemuはここにある -
© 2019 NTT Communications その他知ると便利なディレクトリ • build/tmp/deploy/images/[machine] ◦ ビルド後のイメージが入っているディレクトリ ◦
.ext4やbzImageなど • build/tmp/deploy/licenses ◦ packageのライセンスが集められているディレクトリ 49 -
© 2019 NTT Communications 今後の目次 • Yoctoの全体像と用語を知る ◦ Overview ◦
レイヤシステム ◦ build system ◦ ソースコードの概要 • 何をしたいときに何をすれば良い? • Handson: パッケージを追加してみる 50 -
© 2019 NTT Communications 何をしたいときに何をすれば良いかのまとめ 52 やりたいこと 方法 パッケージの追加したい recipeの追加、新規作成
既存パッケージの編集したい recipeの加筆修正 既存パッケージのconfigを変更したい local.conf修正など Linux Kernel configを変更したい recipeの加筆修正 ISOイメージからインストールしたい local.conf修正、recipeの加筆修正 Target CPUを変更したい Machine BSP configの作成 local.confの修正 -
© 2019 NTT Communications 今後の目次 • Yoctoの全体像と用語を知る ◦ Overview ◦
レイヤシステム ◦ build system ◦ ソースコードの概要 • 何をしたいときに何をすれば良い? • Handson: パッケージを追加してみる 53 -
© 2019 NTT Communications • Add cowsay command with devtool
◦ cowsay: 牛が話すジョークアプリ ◦ devtool: レシピの追加等をサポートする ヘルパーツール ◦ https://github.com/schacon/cowsay Handson: Add new package to reference image 55 -
© 2019 NTT Communications • レシピの書き方 ◦ https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#new-recipe-writing-a- new-recipe ◦
https://www.slideshare.net/iwamatsu/ss-31662659 • devtoolの使い方 ◦ https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#using-devtool-in-your- sdk-workflow ◦ https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#ref-devtool-reference ◦ https://kumadasu.com/2017/05/14/devtool%E3%81%AE%E4%BD%BF%E3%81%84%E6%96 %B9-1%EF%BC%9A%E7%92%B0%E5%A2%83%E6%BA%96%E5%82%99%E3%81%A8% E3%83%AC%E3%82%B7%E3%83%94%E4%BD%9C%E6%88%90%E3%81%AE%E6%B5 %81%E3%82%8C/ Reference: devtool, recipe 56 -
© 2019 NTT Communications devtoolを用いたレシピ作成手順 1. レシピの雛形作成 2. レシピ編集 3.
ビルド 4. テスト 5. 2. から 4.の繰り返し 6. layerへのレシピ追加 7. ビルドテスト 57 -
© 2019 NTT Communications 雛形作成 • レシピの雛形作成 ◦ devtool [options]
[package name] [source url] ◦ example: devtool add cowsay https://github.com/schacon/cowsay • options ◦ add : recipeの新規作成(.bb) ◦ modify: 既存recipeの修正(.bbappend) ◦ upgrade: recipeのバージョンUP 58 -
© 2019 NTT Communications Tips: ビルド手順の指定(tasks) • recipeはbuild tasks(do_***)を記述するもの •
基本はdo_configure, do_compile, do_installの3つ • known build system(make, cmake)は省略可(inherit) ◦ ただし変更がある場合はtasksの記述が必要 • 参考 ◦ https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html# normal-recipe-build-tasks 70 -
© 2019 NTT Communications cowsay_git.bb diff 72 • ./install.sh PREFIX_PATHでインストールできる
• インストール先は ${D}/${prefix} -
© 2019 NTT Communications Tips: ${prefix}などの変数定義 • ${D}の他にも変数がある ◦ ${prefix}
: /usr ◦ ${bindir} : ${prefix}/bin ◦ ${datadir} : ${prefix}/share ◦ … • 詳細な定義は以下を参照 ◦ http://git.yoctoproject.org/cgit.cgi/poky/plain/meta/conf/bitbake.conf?h=bli nky 73 -
© 2019 NTT Communications log.do_configure • recipeにはパッケージに含めるファイル一覧記述が必要 • FILESで設定する。設定内容と実態が異なるとエラー。 ◦
https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#var-FILES 78 -
© 2019 NTT Communications • パッケージを実際に動くか確かめてみる • devtool deploy-target [package]
[email protected] ◦ runqemuで動かしたイメージにパッケージを転送するコマンド ◦ runqemuで動かすイメージにSSH serverが必要 deploy 80 -
© 2019 NTT Communications Tips: imageへのSSHサーバー追加 • conf/local.confに変更を加えて再ビルド ◦ EXTRA_IMAGE_FEATURESにssh-server-opensshを追加
◦ 再ビルドコマンドは bitbake core-image-minimal 81 -
© 2019 NTT Communications deploy • Terminal 1でqemuを起動 ◦ runqemu
nographic core-image-minimal # enter sudo password • Terminal 2でパッケージを転送 ◦ devtool deploy-target cowsay [email protected] -c ◦ -c: no ssh-hostkey check 82 -
© 2019 NTT Communications Fix runtime package dependency problems 88
• 正しい手順 ◦ 依存情報をレシピに記載する(RDEPENDS) ◦ RDEPENDS_${PN} += "perl perl-modules" とレシピに書いておけばビルド時に勝手に依存関係が解決 • 今回の手順 ◦ devtoolはruntime依存パッケージインストールができない ▪ 知らないだけ…? ◦ local.confに書いて最初から入れる方法を取る -
© 2019 NTT Communications Tips: DEPENDS と RDEPENDS • DEPENDS
◦ build time depend packageを追加する変数 ◦ 参考 ▪ https://www.yoctoproject.org/docs/2.6/mega-manual/mega-manual.html#var-DEPENDS • RDEPENDS ◦ runtime depend packageを追加する変数 ◦ ここに書いたパッケージは依存で勝手にインストールされる ▪ (はずなのだけど私の環境ではうまく動いてない時がある ) ◦ RDEPENDS_${PN} += "perl perl-modules" を書いておく 89 -
© 2019 NTT Communications Add package to image 90 •
conf/local.conf に以下を追記 ◦ CORE_IMAGE_EXTRA_INSTALL += "perl perl-modules" • rebuild ◦ bitbake core-image-minimal -
© 2019 NTT Communications shebang修正 • workspace/sources/cow say/cowsay を修正 ◦
envコマンドでperlを参照 ◦ cowsのパスを固定 92 -
© 2019 NTT Communications Add new layer • poky/build/meta-joke layerを作る
◦ bitbake-layers create-layer "$BUILDDIR/meta-joke" • poky/build/meta-joke layerをlayerとして追加する ◦ bitbake-layers add-layer "$BUILDDIR/meta-joke" 95 -
© 2019 NTT Communications Layerへのレシピ登録 • devtool finish cowsay meta-joke
◦ devtoolで作ったrecipe(cowsay)をlayer(meta-joke)に登録する • sourceを編集した場合 ◦ workspace/sourceで変更点をgit commitする ◦ untracked fileなどが残ってるとdevtool finishがエラーになる ◦ in-source buildなコードはこのあたりで辛くなる ◦ 絶対にout-of-source buildしよう!! 96 -
© 2019 NTT Communications Add package to image 97 •
CORE_IMAGE_EXTRA_INSTALLにパッケージを書く • conf/local.conf に以下を追記 ◦ CORE_IMAGE_EXTRA_INSTALL += "cowsay" • rebuild ◦ bitbake core-image-minimal -
© 2019 NTT Communications • Yocto project makes that easy(:thinking_face:)
to create custom linux image. まとめ 100 -
© 2019 NTT Communications Certificate You are master of Yocto
because you finished this handson. Welcome to embedded world!! 101 -
© 2019 NTT Communications Yoctoの学び方 • エラー内容でググる • やりたいことの単語でググる •
既存のレシピを読んで書き方を覚える • ビルドシステムのコードを読んで理解する 102 -
© 2019 NTT Communications • ターゲットマシンを定義するconfig ◦ local.confのMACHINE変数で選択してるやつ • なぜ作るか
◦ 既存のMACHINEで希望が満たせないときに作る ◦ 例:Core iシリーズ以降でないと動かないプログラムを使いたい • 書く場所 ◦ meta-hoge/conf/machine/[machine-name].conf Machine BSP configとは 106 -
© 2019 NTT Communications おすすめの書き方 • すでに書かれたconfigを参考に作る ◦ meta-intel ◦
https://git.yoctoproject.org/cgit.cgi/meta-intel/tree/conf/machine/intel-corei 7-64.conf?h=thud • 107 -
© 2019 NTT Communications Machine BSP Configに書くこと • 実機で動かす向けの設定 ◦
DEFAULTTUNE ◦ MACHINE_FEATURES ◦ KERNEL_FEATURES • qemu向けの設定(runqemu動かすのに必要) ◦ virtioの追加 ◦ QB_***変数の追加 108 -
© 2019 NTT Communications 実機で動かす向け設定 • アーキテクチャやCPUを設定する • 既存の.incファイルは以下にある ◦
poky/meta/conf/machine/ • 例: core-iシリーズ(nehalem)以降が欲しい時 ◦ DEFAULTTUNE ?= "corei7-64" ◦ require conf/machine/include/tune-corei7.inc ◦ require conf/machine/include/genericx86-common.inc 109 -
© 2019 NTT Communications qemu向けの設定 • runqemuを動かすための設定がいる ◦ virtioドライバ追加 ◦
kernel optionの追記 ◦ その他 • QB_***の設定を行っていく 110 -
© 2019 NTT Communications Installerの実態 • initrd-scriptsパッケージにある a. meta/recipes-core/initrdscripts •
インストールスクリプト a. meta/recipes-core/initrdscripts/files/init-install*.sh • 起動の流れ a. grub b. linux kernel c. sysinit/systemd d. init-install*.sh 122 -
© 2019 NTT Communications init-install*.shの動作 • インストール先ディスク選択 • パーティション作成 •
fstab作成 ◦ init-install*.sh内記述から生成。外部ファイルは参照しない • grubインストール • grubエントリ作成 ◦ こちらもinit-install*.sh内記述から生成 123 -
© 2019 NTT Communications 注意点 • fstabとgrubエントリはinit-install*.shから作られる ◦ 他のレシピで設定した内容は無視される ◦
インストール後のFS等を設定したいならinit-install*.shを設定する 124