Ubuntu 20.04.3 LTS(64bit) on Raspberry Pi 4 Model B(8GB) の環境構築

Raspbian OS 64bit がまだ正式版が出ていないけど、せっかくの Raspberry Pi 4 なので64bitを活用したいので Ubuntu にした。
また、Raspberry Pi Imager から Ubuntu 20.04.3 LTS(64bit) の Desktop 版を入れたかったが、リストになかったので仕方なくServer版をインストールする。

Raspberry Pi Imager の最新版ではWIFI設定やSSH設定もできるようになっていたが、どうやらRaspbian OSのみ対応っぽい。
他にも、OSイメージを書き込んだSDカードを手元のPCにマウントしてルートディレクトリに設定ファイルを置くとWIFI設定とかを事前にできるらしいけど、 Ubuntu でもできるのかは未確認。

幸いにもモニタ、マウス、キーボードはあるので、普通にUbuntu 20.04.3 LTS(64bit) Server イメージを書き込んだSDカードをラズパイ本体に刺して起動してから設定する。

以下、セットアップメモ

ネットワーク設定

cp /etc/netplan/50-cloud-init.yaml /etc/netplan/99-cloud-init.yaml 
vi /etc/netplan/99-cloud-init.yaml
network: 
    version: 2 
    ethernets: 
        eth0: 
            dhcp4: true 
            optional: true 
----ここから追記-----
    wifis: 
        wlan0: 
            access-points: 
                {YOUR SSID}: 
                    password: "PASSWORD" 
            dhcp4: false 
            addresses: 
                - 192.168.11.210/24
            gateway4: 192.168.11.1
            nameservers: 
                addresses: 
                    - 8.8.8.8

waysakuユーザーの追加とubuntuユーザーをsudo不可にする

sudo adduser waysaku
sudo gpasswd -a waysaku sudo
sudo gpasswd -d ubuntu sudo

sudo passwd ubuntu   # ubuntuユーザーはデフォルトユーザとして何かしらOSのデフォルト設定に絡んでるかもしれないので削除せずにパスワード変更だけしておく

パッケージ更新と各種パッケージのインストール

sudo apt update
sudo apt upgrade

sudo apt install net-tools vim git make gcc build-essential -y

vcgencmd をインストールしてCPU温度をmackerelで監視設定

$ wget -q -O - https://mackerel.io/file/script/setup-all-apt-v2.sh | MACKEREL_APIKEY='3KmhW92duiSagrgP3UZJQ2snPgmAJjud1dDwt4vEMxdK' sh

$ sudo apt install libraspberrypi-bin -y 
$ sudo vcgencmd measure_temp
temp=48.7'C

$ sudo touch /etc/mackerel-agent/temperature.sh
$ sudo vi /etc/mackerel-agent/temperature.sh
---
#!/bin/bash
SECONDS=$(date '+%s')
NAME='temperature.cpu'
VALUE=$(vcgencmd measure_temp | grep -o -e "[0-9]\+\.[0-9]\+")
echo -e "${NAME}\t${VALUE}\t${SECONDS}"
----
$ sudo chmod 755 /etc/mackerel-agent/temperature.sh


$ sudo vi /etc/mackerel-agent/mackerel-agent.conf
---
[plugin.metrics.temp]
command = "/etc/mackerel-agent/temperature.sh"
---


$ sudo systemctl restart mackerel-agent     # mackerelエージェント再起動
$ sudo journalctl -u mackerel-agent.service # ログ確認

Raspberry PiのGPIOを操作する gpio コマンドのインストール

apt で wiringgpi をインストールしても gpio コマンドはエラーになる

$ sudo apt install wiringpi
$ gpio readall
Oops - unable to determine board type... model: 17
$ sudo apt remove wiringpi

どうやらgpioコマンドである WiringPi は既に開発停止しているらしい(作者がもうやってられないので開発やめるわってことらしい)
なので、aptでの WiringPi は最新パッケージが古くてRaspberry Pi 4Bのモデルに対応していない。

それに対応するために個別にRaspberry Pi 4Bに対応したパッケージを公開してくれているが、32bit版しかない http://wiringpi.com/wiringpi-updated-to-2-52-for-the-raspberry-pi-4b/

$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
$ sudo dpkg -i wiringpi-latest.deb
dpkg: error processing archive wiringpi-latest.deb (--install):
 package architecture (armhf) does not match system (arm64)
Errors were encountered while processing:
 wiringpi-latest.deb

なのでソースからビルドして gpio コマンドをインストールする https://github.com/WiringPi/WiringPi/blob/master/INSTALL

$ mkdir ~/git
$ cd ~/git
$ git clone https://github.com/WiringPi/WiringPi.git
$ cd WiringPi/
$ ./build

$ sudo vim /etc/ld.so.conf
---
include /etc/ld.so.conf.d/*.conf
/usr/local/lib   <- 追加する
---
$ sudo ldconfig

$ sudo gpio readall
 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT5 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT5 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI | ALT0 | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO | ALT0 | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK | ALT0 | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | OUT  | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+

$ gpio -g mode 3 out # でGPIO3(BCM) が OUT(出力)に変更できる
$ gpio -g write 17 1 # でGPIO17(BCM) を信号を出力する(= 3.3Vの電圧を流す)

無事動いた。
※ ちなみに Ubuntu 21.04 でもビルドできました

コマンドラインからGPIOを操作する https://tomaberry.hatenablog.com/entry/2017/02/12/155910

raspi-configのインストール

Raspberry Pi OS と違い Ubuntu の場合は ラズパイ本体のデバイス設定をいろいろと変更できる便利コマンドである raspi-config がインストールされていないので手動でインストールする https://linuxtut.com/en/a252676a3ce6bd1410da/

今回は空冷FANの動きを制御するために raspi-config を使いたい

$ sudo echo "deb http://archive.raspberrypi.org/debian/ bullseye main" >> /etc/apt/sources.list
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 7FA3303E
$ sudo apt-get update
$ sudo apt-get install raspi-config
$ sudo mount /dev/mmcblk0p1 /boot

$ sudo raspi-config  # FANの制御の設定変更する
$ cat /boot/config.txt
[all]
dtoverlay=gpio-fan,gpiopin=17,temp=70000

Ubuntu Desktop環境のインストール

sudo apt install -y ubuntu-desktop

Desktop環境インストールしたらなぜかWIFI繋がらなくなった。
etc/netplan/99-cloud-init.yaml を削除してubuntu-desktopを入れたGUIからWIFI設定したら無事WIFIに繋がった。
よくわからん。