LoginSignup
4

More than 3 years have passed since last update.

yocto(2.6 Thud)でRaspberry-pi Zero W - Step2 - 無線自動接続

Last updated at Posted at 2019-04-07

はじめに

前回にイメージビルドし起動まで確認できました。
今後のマイルストーンは以下のように考えています

  1. yoctoでraspberry pi w zeroのイメージを作成
  2. 起動時のwlan0自動接続のためレシピを追加 ← イマココ
  3. 起動時のsshd自動起動
  4. USB-LTEモジュールの接続とテスト
  5. GPSモジュールの接続とテスト
  6. soracom harvestで位置情報取得

今回は、自動接続のための設定を行います。

実施詳細

手順を順に示します。

レイヤの追加

新規でレシピを編集するため、レシピを追加します。

$ cd ~/poky-thud/oe-init-build-env ~/yocto_raspberrypi-zero
$ bitbake-layers create-layers meta-raspberrypi-append
$ bitbake-layers add-layer meta-raspberrypi-append

以上の動作でbuildディレクトリ(今回は~/yocto_raspberrypi-zero)にmeta-raspberrypi-appendが作成されます。
この際、conf/bblayers.confには自動で追加されます(以下はhomedirを置換したあとのものです)

$ git diff
index 43baa06..4d1d090 100644
--- a/conf/bblayers.conf
+++ b/conf/bblayers.conf
@@ -14,4 +14,6 @@ BBLAYERS ?= " \
   ${HOME}/poky-thud/meta-openembedded/meta-perl \
   ${HOME}/poky-thud/meta-openembedded/meta-python \
   ${HOME}/poky-thud/meta-raspberrypi \
+  ${HOME}/yocto_raspberrypi_zero/meta-raspberrypi-append \
   "
diff --git a/conf/local.conf b/conf/local.conf

/etc/network/interfaceの編集

/etc/network/interfaceの配置はrecipes-coreのinit-ifupdownにて行っているため、このレシピを編集します。
まずはdevtoolを用いてレシピ編集用のworkspaceを作成します。

$ cd ~/yocto_raspberrypi-zero
$ devtool status
$ ls -d ~/yocto_raspberrypi-zero/workspace/

devtool modifyにより既存のレシピを取得します

$ devtool modify init-ifupdown
$ tree -L
.
├── README
├── appends
│   └── init-ifupdown
├── conf
│   └── layer.conf
└── sources
    └── init-ifupdown

ソースファイルはsourcesに保管されているため、source/ifupdown/interfacesを前回通りに編集

$ cat workspace/source/ifupdown/interfaces
# The loopback interface
auto lo
iface lo inet loopback

# Wireless interfaces
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant.conf

これらのファイルはgit管理されているため、git diffで変更内容を確認できます。
また、git checkout .等で状態を戻すことも可能です。

変更内容に問題がなければdevtool finishでレシピレイヤに追加します

$ devtool finish meta-raspberrypi-append
$ tree ~/yocto_raspberrypi_zero/meta-raspberrypi-append/
├── COPYING.MIT
├── README
├── conf
│   └── layer.conf
└── recipes-core
    └── init-ifupdown
        ├── init-ifupdown
        │   └── interfaces
        └── init-ifupdown_%.bbappend

最後に、conf/local.confにてインストール対象としてinit-ifupdownを追加すれば良い

dada@dada-VirtualBox:~/yocto_raspberrypi_zero$ git diff
diff --git a/conf/bblayers.conf b/conf/bblayers.conf
index 648e3a0..089a362 100644
--- a/conf/local.conf
+++ b/conf/local.conf
@@ -31,7 +31,7 @@ PARALLEL_MAKE = "-j 12"

 CONF_VERSION = "1"
 #IMAGE_INSTALL_append = " linux-firmware-bcm43430 connman connman-client pulseaudio pulseaudio-server usbutils sudo wiringpi"
-IMAGE_INSTALL_append = " linux-firmware-rpidistro-bcm43430 usbutils sudo wiringpi wpa-supplicant"
+IMAGE_INSTALL_append = " linux-firmware-rpidistro-bcm43430 usbutils sudo wiringpi wpa-supplicant packagegroup-core-ssh-dropbear init-ifupdown"
 IMAGE_FEATURES_append = " ssh-server-dropbear"

/etc/wpa_supplicant.confの編集

基本的には操作は同じです。
wpa_supplicant.confはbb内のdo_installにてコピーされているため、
対象ファイルはoe-local-filesに配置されています。

$ cat workspace/sources/wpa-supplicant/oe-local-files/wpa_supplicant.conf-sane 
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
update_config=1

network={
    ssid="ssid"
    proto=WPA2
    key_mgmt=WPA-PSK
    pairwise=CCMP
    group=CCMP
    psk="pass"
}

ifup/ifdownのservice登録

接続設定自体は問題ありませんが、自動でifupされないためsystemdのサービス登録が必要になります。
今回は、ifupdownレシピを編集します。

sources/ifupdown/oe-local-files/network-wireless@.service
[Unit]
Description=Wireless network connectivity (%i)
Wants=network.target
Before=network.target
BindsTo=sys-subsystem-net-devices-%i.device
After=sys-subsystem-net-devices-%i.device
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/ifup %i
ExecStop=/sbin/ifdown %i
[Install]
WantedBy=multi-user.target
appends/ifupdown_0.8.16.bbappend
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"
FILESPATH_prepend := "/home/dada/yocto_raspberrypi_zero/workspace/sources/ifupdown/oe-local-files:"
# srctreebase: /home/dada/yocto_raspberrypi_zero/workspace/sources/ifupdown

inherit externalsrc

BBCLASSEXTEND
EXTERNALSRC_pn-ifupdown = "/home/dada/yocto_raspberrypi_zero/workspace/sources/ifupdown"
EXTERNALSRC_BUILD_pn-ifupdown = "/home/dada/yocto_raspberrypi_zero/workspace/sources/ifupdown"

SRC_URI += "file://network-wireless@.service"
SYSTEMD_SERVICE_${PN} += " network-wireless@wlan0.service"

do_install_append() {
    install -d ${D}/${systemd_system_unitdir}
    install -m 0644 ${WORKDIR}/network-wireless@.service ${D}/${systemd_system_unitdir}
    install -d ${D}/${sysconfdir}/systemd/system/multi-user.target.wants/
    ln -sf ${systemd_system_unitdir}/network-wireless@.service \
        ${D}/${sysconfdir}/systemd/system/multi-user.target.wants/network-wireless@wlan0.service
}

FILES_${PN} += "/lib/systemd/system"

REQUIRED_DISTRO_FEATURES= "systemd"
  • SRC_URIで追加したnetwork-wireless@.serviceを指定
  • SYSTEMD_SERVICE_${PN}でsystemdサービス追加
  • do_install_append()でdo_install()の後続としてserviceファイルのコピーを実行

編集後にdevtool finish ifupdown meta-raspberrypi-appendで適用

今回の成果

起動時にwlan0が有効になり、自動で接続可能となりました。
次回はsshd設定を行う予定です。

dada@dada-VirtualBox:~/yocto_raspberrypi_zero/meta-raspberrypi-append$ tree
.
├── COPYING.MIT
├── README
├── conf
│   └── layer.conf
├── recipes-connectivity
│   └── wpa-supplicant
│       ├── wpa-supplicant
│       │   └── wpa_supplicant.conf-sane
│       └── wpa-supplicant_%.bbappend
└── recipes-core
    ├── ifupdown
    │   ├── ifupdown
    │   │   └── network-wireless@.service
    │   └── ifupdown_%.bbappend
    └── init-ifupdown
        ├── init-ifupdown
        │   └── interfaces
        └── init-ifupdown_%.bbappend

9 directories, 9 files

参考サイト

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4