Raspberry Pi の OSイメージを書き込む Raspberry Pi Imager v1.6 の新機能で書き込み時にカスタマイズ

Raspberry Pi で利用するストレージへのOSイメージ書き込みツールのRaspberry Pi Imagerの最新版のv1.6がリリースされたようです.

自分は出た当初少し試しただけで他のツールに比べて嬉しいことがあまりないと感じたので使っていなかったのですが,このv1.6ではイメージのカスタマイズ機能が入ったようなので試してみました.

Raspberry Pi Imagerの導入

$ sudo apt install rpi-imager

Raspberry Pi OSであればパッケージが存在するのでこれを導入すればokです.

Linux/Windows/macOSはGitHubのreleaseページ 🏛️から入手できます.
若しくは,Raspberry Pi downloadsページ 🏛️にもあります.こちらだとGutHubとファイル名が違いますがamd64の.debをdiffしてみると同じもので署名ファイルもあります..AppImageもありますが試してみると最新ではないようでした.

$ wget https://github.com/raspberrypi/rpi-imager/releases/download/v1.6/rpi-imager_1.6_amd64.deb (1)
$ wget https://downloads.raspberrypi.org/imager/imager_1.6_amd64.deb https://downloads.raspberrypi.org/imager/imager_1.6_amd64.deb.sig (2)
$ diff ./imager_1.6_amd64.deb ./rpi-imager_1.6_amd64.deb (3)
$ gpg2 --verify ./imager_1.6_amd64.deb.sig (4)
gpg: assuming signed data in './imager_1.6_amd64.deb'
gpg: Signature made Tue 16 Mar 2021 08:56:51 PM JST
gpg:                using RSA key 54C3DD610D9D1B4AF82A37758738CD6B956F460C
gpg: Good signature from "Raspberry Pi Downloads Signing Key" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 54C3 DD61 0D9D 1B4A F82A  3775 8738 CD6B 956F 460C
$ gpg2 --verify ./imager_1.6_amd64.deb.sig ./rpi-imager_1.6_amd64.deb
gpg: Signature made Tue 16 Mar 2021 08:56:51 PM JST
gpg:                using RSA key 54C3DD610D9D1B4AF82A37758738CD6B956F460C
gpg: Good signature from "Raspberry Pi Downloads Signing Key" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 54C3 DD61 0D9D 1B4A F82A  3775 8738 CD6B 956F 460C
$ sudo apt install ././imager_1.6_amd64.deb (5)
  1. GitHubからダウンロード
  2. downloads.raspberrypi.orgからダウンロード
  3. ファイル名は違うけど同じ内容なのを確認
  4. 署名確認
  5. インストール

Debian sid amd64環境ではAppImageが古いので.debを導入しました.

Imagerの起動と書き込み設定

Raspberry Pi ロゴのアイコンの「Imager」や, rpi-imager を実行すると起動します.

ここで,「ctrl + shift + x」を押すことで今回のカスタマイズ用の拡張オプション画面が表示されます.

rpi imager01
rpi imager02
rpi imager03
rpi imager04

今回はhostnameの修正,SSHの有効化,ssh公開鍵の登録,Wi-Fi 設定,タイムゾーンの設定を行いました.

「SAVE」して「RASPBERRY PI OS LITE (32-BIT)」を書き込んでみました.

書き込みが終了しても1つ目のパーティション(/boot)がマウントされたままでした.これはバグかな?(1回しか試していないので勘違いかもしれない)

$ mount | grep /dev/mmcblk0p1 (1)
/dev/mmcblk0p1 on /media/matoken/boot type vfat (rw,nosuid,nodev,relatime,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro,uhelper=udisks2)
$ sudo umount /dev/mmcblk0p1 (2)
  1. 1つ目のパーティションがマウントされたまま
  2. アンマウントする

カスタマイズ内容確認

書き込まれたメディアの中を見ると /boot/firstrun.sh が作成されていました.Raspberry Pi Imagerは直接設定を書き換えるわけではなく,OSイメージを書き込んだあと子のファイルを作って,Raspberry Pi OSの初回起動時にこのscriptを実行して設定を反映していくようです.

/boot/firstrun.sh
#!/bin/bash

set +e

CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
echo raspberrypi-custom >/etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\traspberrypi-custom/g" /etc/hosts
FIRSTUSER=`getent passwd 1000 | cut -d: -f1`
FIRSTUSERHOME=`getent passwd 1000 | cut -d: -f6`
install -o "$FIRSTUSER" -m 700 -d "$FIRSTUSERHOME/.ssh"
install -o "$FIRSTUSER" -m 600 <(echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPgwY9aZPxN/YoBBzd7TOcCk7EuGO0E9PuUjCHPtTuHP mk@x220") "$FIRSTUSERHOME/.ssh/authorized_keys"
echo 'PasswordAuthentication no' >>/etc/ssh/sshd_config
systemctl enable ssh
cat >/etc/wpa_supplicant/wpa_supplicant.conf <<WPAEOF
country=JP
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
ap_scan=1

update_config=1
network={
	ssid="matoken"
	psk="○○○○○○○○○○○○"
}

WPAEOF
chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
rfkill unblock wifi
for filename in /var/lib/systemd/rfkill/*:wlan ; do
  echo 0 > $filename
done
rm -f /etc/xdg/autostart/piwiz.desktop
rm -f /etc/localtime
echo "Asia/Tokyo" >/etc/timezone
dpkg-reconfigure -f noninteractive tzdata
cat >/etc/default/keyboard <<KBEOF
XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""
KBEOF
dpkg-reconfigure -f noninteractive keyboard-configuration
rm -f /boot/firstrun.sh
sed -i 's| systemd.run.*||g' /boot/cmdline.txt
exit 0

ちょっと雑かなと感じるところもありますが便利な感じです.
( /etc/wpa_supplicant/wpa_supplicant.confwpa_passphrase コマンドを通してpskにしておきたいとか)

自分はsd card書き込み前にPCでマウントしてパッケージを最新にしたりインストールしたりといったことをしているので,sd card書き込みをしなくてもこのファイルを書き出してくれると嬉しいかなと思いました.

アンインストール

$ sudo apt purge rpi-imager
$ rm -r ~/.config/Raspberry\ Pi/Imager.conf ~/.cache/Raspberry\ Pi/Imager

パッケージを削除して,設定ファイルとキャッシュを削除しました.

環境

$ dpkg-query -W rpi-imager
rpi-imager      1.6
$ lsb_release -dr
Description:    Debian GNU/Linux bullseye/sid
Release:        unstable
$ uname -m
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)