10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

「Raspberry PiといえばRaspbian(Raspberry Pi OS)」

...だけど、FreeBSDも動くんだよ。

実際にRaspberry Pi 4で動かしてみた。

対応状況

サポートされているRaspberry Pi

モデル アーキテクチャ サポート
Raspberry Pi 1 armv6
Raspberry Pi 2 armv7
Raspberry Pi 3 aarch64
Raspberry Pi 4 aarch64
Raspberry Pi 5 aarch64 △(開発中)
Raspberry Pi Zero armv6
Raspberry Pi Zero 2 aarch64

aarch64(64bit)が推奨

ハードウェアサポート状況(Pi 4)

機能 サポート
USB
Ethernet
WiFi
Bluetooth
HDMI
GPIO
I2C/SPI
カメラ
ハードウェアビデオ

イメージのダウンロード

# Raspberry Pi 4用 (aarch64)
wget https://download.freebsd.org/releases/arm64/aarch64/ISO-IMAGES/14.2/FreeBSD-14.2-RELEASE-arm64-aarch64-RPI.img.xz

# 展開
xz -d FreeBSD-14.2-RELEASE-arm64-aarch64-RPI.img.xz

SDカードへの書き込み

Linuxの場合

# SDカードのデバイス名を確認
lsblk

# 書き込み(/dev/sdXは適宜変更)
sudo dd if=FreeBSD-14.2-RELEASE-arm64-aarch64-RPI.img of=/dev/sdX bs=4M status=progress conv=fsync

macOSの場合

# デバイス確認
diskutil list

# アンマウント
diskutil unmountDisk /dev/diskN

# 書き込み
sudo dd if=FreeBSD-14.2-RELEASE-arm64-aarch64-RPI.img of=/dev/rdiskN bs=4m

Windowsの場合

RufusbalenaEtcherを使用。

初回起動

接続

Raspberry Pi 4
├── HDMI → モニター
├── USB → キーボード
├── Ethernet → ルーター(DHCPで自動設定)
└── 電源(USB-C, 5V/3A)

ログイン

FreeBSD/arm64 (Amnesiac) (ttyu0)

login: root
Password: root

初期パスワードはroot

初期設定

パスワード変更

passwd

ホスト名設定

sysrc hostname="raspberrypi"

タイムゾーン

tzsetup
# Asia → Japan を選択

ユーザー追加

adduser
# Username: pi
# Login group [pi]:
# Invite pi into other groups? []: wheel
# Shell: /bin/sh
# ...

SSH有効化

sysrc sshd_enable="YES"
service sshd start

パーティション拡張

初期状態ではSDカードの一部しか使われていない。

# 現在のサイズ確認
df -h
# /dev/mmcsd0s2a    3.8G    1.2G    2.3G    34%    /

# パーティション拡張
gpart recover mmcsd0
gpart resize -i 2 mmcsd0
growfs /dev/mmcsd0s2a

# 確認
df -h
# /dev/mmcsd0s2a   29.8G    1.2G   26.2G     4%    /

WiFi設定

ファームウェアインストール

pkg install bwi-firmware-kmod

設定

# /etc/wpa_supplicant.conf
network={
    ssid="MyWiFi"
    psk="MyPassword"
}
# /etc/rc.conf
wlans_brcm0="wlan0"
ifconfig_wlan0="WPA DHCP"
service netif restart

接続確認

ifconfig wlan0
# wlan0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> ...
#         inet 192.168.1.150 netmask 0xffffff00 broadcast 192.168.1.255

GPIO制御

FreeBSDでもGPIOが使える。

GPIOの確認

gpioctl -l
# pin 00: 0       bcm0<IN>, name "ID_SD"
# pin 01: 0       bcm1<IN>, name "ID_SC"
# pin 02: 0       bcm2<IN>, name "SDA1"
# ...
# pin 17: 0       bcm17<IN>, name ""
# pin 18: 0       bcm18<IN>, name "PCM_CLK"
# ...

LEDを点滅させる

# GPIO17を出力に設定
gpioctl -c 17 OUT

# ON
gpioctl 17 1

# OFF
gpioctl 17 0

# 点滅スクリプト
while true; do
    gpioctl 17 1
    sleep 0.5
    gpioctl 17 0
    sleep 0.5
done

Cプログラムから制御

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/gpio.h>

int main(void)
{
    struct gpio_req req;
    int fd;

    fd = open("/dev/gpioc0", O_RDWR);
    if (fd < 0) {
        perror("open");
        return 1;
    }

    /* GPIO17を出力に設定 */
    req.gp_pin = 17;
    req.gp_flags = GPIO_PIN_OUTPUT;
    if (ioctl(fd, GPIOSETCONFIG, &req) < 0) {
        perror("GPIOSETCONFIG");
        return 1;
    }

    /* HIGH */
    req.gp_pin = 17;
    req.gp_value = 1;
    ioctl(fd, GPIOSET, &req);

    sleep(1);

    /* LOW */
    req.gp_value = 0;
    ioctl(fd, GPIOSET, &req);

    close(fd);
    return 0;
}
cc -o gpio_test gpio_test.c
./gpio_test

I2Cデバイス

I2Cの有効化

# /boot/loader.conf
iic_load="YES"
iicbus_load="YES"
bcm2835_bsc_load="YES"
reboot

デバイス検出

i2c -s
# Scanning I2C devices on /dev/iic0: 27 
# Found 1 device(s)

温湿度センサー(DHT20等)を読む

pkg install i2c-tools

# デバイスの存在確認
i2cdetect -y 0

パフォーマンス

CPU情報

sysctl hw.model
# hw.model: ARM Cortex-A72 r0p3

sysctl hw.ncpu
# hw.ncpu: 4

sysctl dev.cpu.0.freq
# dev.cpu.0.freq: 1500

温度監視

sysctl dev.cpu.0.temperature
# dev.cpu.0.temperature: 45.0C

定期監視スクリプト:

while true; do
    temp=$(sysctl -n dev.cpu.0.temperature)
    echo "$(date): $temp"
    sleep 10
done

ベンチマーク

# pkg インストール時間
time pkg install vim
# real    0m45.123s  # Pi 4の場合

# ディスクI/O
dd if=/dev/zero of=/tmp/test bs=1M count=100 conv=fsync
# 100+0 records in
# 100+0 records out
# 104857600 bytes transferred in 2.345 secs (44.7 MB/s)

jailでサービス隔離

Raspberry PiでもjailやZFSが使える。

# ezjailインストール
pkg install ezjail

# ベースシステム取得
ezjail-admin install

# jail作成
ezjail-admin create webserver 'lo0|127.0.1.1'

# 起動
ezjail-admin start webserver

# jail内に入る
jexec webserver /bin/sh

用途例

1. ホームサーバー

pkg install nginx php82 mariadb1011-server

2. NAS(ZFS)

# USBドライブをZFSで管理
zpool create tank /dev/da0
zfs create tank/share

3. ルーター/ファイアウォール

# IPフォワーディング
sysrc gateway_enable="YES"

# PFでNAT
echo 'nat on ue0 from 192.168.2.0/24 to any -> (ue0)' >> /etc/pf.conf
sysrc pf_enable="YES"
service pf start

4. 監視サーバー

pkg install munin-node
sysrc munin_node_enable="YES"

トラブルシューティング

起動しない

  • SDカードを再書き込み
  • 電源が弱い(5V/3A推奨)
  • HDMIが認識されない場合はシリアルコンソールを使う

シリアルコンソール

GPIO14 (TX) → USB-Serial RX
GPIO15 (RX) → USB-Serial TX
GND → GND
# 別PCから
screen /dev/ttyUSB0 115200

WiFiが繋がらない

# ドライバ確認
dmesg | grep -i brcm

# ファームウェア確認
ls /boot/firmware/

まとめ

Raspberry PiでFreeBSD:

  • Pi 4で安定動作
  • GPIO/I2C/SPIも使える
  • ZFS/jailも動く
  • サーバー用途に最適

「Linuxじゃなきゃ」って思ってた人、FreeBSDも試してみて

この記事が役に立ったら、いいね・ストックしてもらえると嬉しいです!

10
2
0

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
10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?