setodaNote

忘れる用のメモ書き for Cybersecurity

Raspberry Pi 4 で CentOS 7 を動かす

Raspberry Pi でも CentOS が動くと聞いたので試してみました。 手順については多くのブログで紹介されているのでそれを参考にしていますが、手間取ったところもあったのでそれを含めて記載しています。

Raspberry Pi 用の CentOS 7 イメージを SD カードに準備する

以下から Raspberry Pi 用の CentOS 7 イメージをダウンロードします。*1

Index of /pgreco/images/forrpi4v2

圧縮されているので 7zip などを用いて展開し「.raw」ファイルにしておきます。

このイメージファイルを SD カードに書き込みます。

他のブログで紹介されていた DD for Windows というツールを利用しました。 β版も公開されていましたが、うまく動いてくれなかったので「Ver.0.9.9.8」をダウンロードするのがよいと思います。

DD for Windows - Tech Info

そのまま起動するとうまく SD カードを認識しない場合があるので、 起動前に実行ファイル(DDWin.exe)を右クリックし、メニューのプロパティから [互換性]タブ > [互換モード] > [互換モードでプログラムを実行する] にチェックを入れて「Windows XP(Service Pack 3)」と設定しておきます。

DDWin.exe プロパティ(互換性モード)

DDWin.exe を起動して SD カードが読み込まれていれば OK です。

DDWin.exe 実行時のイメージ

「ファイル選択」ボタンを押し、先程ダウンロードして展開した raw データ「CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-exp4-1810-sda.raw」を選びます。

選択し終わったら「<< 書込 <<」ボタンを押します。2回程度ファイルサイズなどに関する警告が出ますので、確認をして進めます。

書き込みが終了したら事前準備は完了です。次からは Raspberry Pi 上での操作です。

Raspberry Pi の CentOS 7 を起動する

初期状態では WiFi を認識しないので LAN ケーブルを挿してから電源を投入します。 IP アドレスは DHCP で割り当てられるようにしておけば OK です。

接続するのに必要なので Raspberry Pi に割り当てられた IP アドレスは適当な方法で把握しておきます。 (私の場合は接続機器が多くなかったので、同じネットワークの IP アドレス帯で可能性のあるものを順に ping や SSH を打ち込み確認しました。)

SSH で接続する

Raspberry Pi 起動させたら SSH で接続します。

初期状態では以下の通りパスワードが設定されています。

  • Username: root
  • Password: centos

うまく接続できればプロンプトが帰ってきます。

>ssh 192.168.1.3 -l root

The authenticity of host '192.168.10.3 (192.168.1.3)' can't be established.
ECDSA key fingerprint is SHA256:zVTKJY2mREyRdmxxxxxxxxxx+xxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.3' (ECDSA) to the list of known hosts.
root@192.168.1.3's password:
[root@localhost ~]#

パーティションを拡張する

初期状態ではルートディレクトリの最大容量が 2GB 程度になっており、SD カードの容量を使い切っていません。

[root@localhost ~]# df -h

ファイルシス   サイズ  使用  残り 使用% マウント位置
/dev/root        1.4G  886M  461M   66% /
devtmpfs         1.9G     0  1.9G    0% /dev
tmpfs            2.0G     0  2.0G    0% /dev/shm
tmpfs            2.0G   17M  1.9G    1% /run
tmpfs            2.0G     0  2.0G    0% /sys/fs/cgroup
/dev/mmcblk0p1   286M   37M  250M   13% /boot
tmpfs            391M     0  391M    0% /run/user/0

以下のようにコマンドを入力してパーティションを拡張します。

[root@localhost ~]# /usr/bin/rootfs-expand

/dev/mmcblk0p3 /dev/mmcblk0 3
Extending partition 3 to max size ....
CHANGED: partition=3 start=1593344 old: size=2930688 end=4524032 new: size=123142111 end=124735455
Resizing ext4 filesystem ...
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mmcblk0p3 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 8
The filesystem on /dev/mmcblk0p3 is now 15392763 blocks long.

Done.

時刻設定を日本時間に変更する

ntp コマンドをインストールします。

[root@localhost ~]# yum install -y ntp

以下の通りファイルを編集して問い合わせ先を国内の NTP サーバに変更しておきます。

[root@localhost ~]#  vi /etc/ntp.conf 

##server 0.centos.pool.ntp.org
##server 1.centos.pool.ntp.org
##server 2.centos.pool.ntp.org
pool ntp.nict.jp
pool ntp.jst.mfeed.ad.jp
pool s2csntp.miz.nao.ac.jp
[root@localhost ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
cp: overwrite '/etc/localtime'? y
[root@localhost ~]# date

WiFi を有効にする

初期状態では無線ネットワーク(wlan0) が有効になっていません。

[root@localhost ~]# nmcli d
DEVICE  TYPE      STATE      CONNECTION
eth0    ethernet  connected  Wired connection 1
lo      loopback  unmanaged  --

WiFi を利用できるように無線ネットワークを有効化します。 以下のようにしてファームウェアを更新します。

[root@localhost ~]# yum -y install git
[root@localhost ~]# git clone https://github.com/RPi-Distro/firmware-nonfree.git
[root@localhost ~]# mv /lib/firmware/brcm{,.bak}
[root@localhost ~]# cp -R firmware-nonfree/brcm /lib/firmware/brcm
[root@localhost ~]# reboot

再起動後に wlan0 が認識されていれば OK です。

[root@localhost ~]# nmcli d
DEVICE         TYPE      STATE         CONNECTION
eth0           ethernet  connected     Wired connection 1
wlan0          wifi      disconnected  --
p2p-dev-wlan0  wifi-p2p  disconnected  --

wlan0 - nmcli d

あとは WiFi のアクセスポイントに接続するだけです。

以下のコマンドでアクセスポイントの一覧を確認します。

[root@localhost ~]# nmcli d wifi

IN-USE  SSID        MODE   CHAN  RATE        SIGNAL  BARS  SECURITY
        SSID001     Infra  1     130 Mbit/s  92      ▂▄▆█  WPA2
        SSID002     Infra  40    540 Mbit/s  82      ▂▄▆█  WPA2
        SSID003     Infra  2     130 Mbit/s  75      ▂▄▆_  WPA2
        SSID004     Infra  44    270 Mbit/s  64      ▂▄▆_  WPA2
        SSID005     Infra  11    130 Mbit/s  45      ▂▄__  WPA2
        SSID006     Infra  10    540 Mbit/s  42      ▂▄__  WPA2
        SSID007     Infra  10    270 Mbit/s  40      ▂▄__  WPA1 WPA2
        SSID008     Infra  4     270 Mbit/s  39      ▂▄__  WPA1 WPA2
        SSID009     Infra  6     405 Mbit/s  39      ▂▄__  WPA1 WPA2
        SSID010     Infra  36    405 Mbit/s  19      ▂___  WPA2

以下のコマンドで指定のアクセスポイントに接続します。

nmcli --ask dev wifi connect <SSID>

successfully と表示されていれば接続成功です。

[root@localhost ~]# nmcli --ask dev wifi connect <SSID>
Password: ••••••••••••••••••••••••
Device 'wlan0' successfully activated with 'xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx'.
[root@localhost ~]# nmcli d
DEVICE         TYPE      STATE         CONNECTION
eth0           ethernet  connected     Wired connection 1
wlan0          wifi      connected     <SSID>
p2p-dev-wlan0  wifi-p2p  disconnected  --

もし、パスワードはあっているはずなのにログインできない事象に遭遇した場合は付録に解決方法を記載していますのでご参照ください。

パッケージをアップデートする

最後に全体をアップデートしておきます。

[root@localhost ~]# yum -y update

アップデートの際に GPG key インポートが3回程度発生します。 問題なければ y で進めます。

 
 

以上

参考文献

付録

本当は Raspberry Pi 上に EXIST を構築したかった

Raspberry Pi に CentOS を入れたのは EXIST を動かしたかったからなのですが、 各種パッケージが通常のそれとは異なるようで、うまく自動インストールが走らなかったので中断しました。

無理に CentOS にせず、Raspberry Pi 用のメジャーな OS で動かす方法を探す方が 情報源が沢山あってうまく行きそうな気もします。

エラー「Error: Connection activation failed: (7) Secrets were required, but not provided.」

パスワードはあっているはずなのにログインできない事象に遭遇しました。 検索すると色々と解決策が出てきますが、多くはうまくいかず、最終的に以下のとおり情報を一度削除してから設定するという方法で解決しました。

[root@localhost ~]# nmcli con delete <SSID>
Connection '<SSID>' (yyyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyy) successfully deleted.

[root@localhost ~]# nmcli --ask dev wifi connect <SSID>
Password: ••••••••••••••••••••••••
Device 'wlan0' successfully activated with 'xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxx'.

CPUの温度を取得する

[root@localhost ~]# cat /sys/class/thermal/thermal_zone0/temp
50147 # -> 50.147℃
[root@localhost ~]# vcgencmd measure_temp
temp=53.0'C

Raspberry Pi のファームウェアアップデートは推奨されない

ちなみに実行すると以下のような感じです。

[root@localhost ~]# rpi-update
 *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
 *** Performing self-update
 *** Relaunching after update
 *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
 *** We're running for the first time
 *** Backing up files (this will take a few minutes)
 *** Backing up firmware
 *** Backing up modules 4.19.61-v7l.1.el7
A newer bootloader EEPROM version is available.
On Debian, try: sudo apt update; sudo apt install rpi-eeprom
then reboot to install the new bootloader
##############################################################
WARNING: 'rpi-update' updates to pre-releases of the linux
kernel tree and Videocore firmware.

'rpi-update' should only be used if there is a specific
reason to do so - for example, a request by a Raspberry Pi
engineer.

DO NOT use 'rpi-update' as part of a regular update process.

###############################################################
Would you like to proceed? (y/N)
 *** Downloading specific firmware revision (this will take a few minutes)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   168    0   168    0     0    183      0 --:--:-- --:--:-- --:--:--   183
100  115M  100  115M    0     0  1848k      0  0:01:03  0:01:03 --:--:-- 1570k
 *** Updating firmware
 *** Updating kernel modules
 *** depmod 4.19.86+
 *** depmod 4.19.86-v7+
 *** Updating VideoCore libraries
 *** Using HardFP libraries
 *** Updating SDK
 *** Running ldconfig
 *** Storing current firmware revision
 *** Deleting downloaded files
 *** Syncing changes to disk
 *** If no errors appeared, your firmware was successfully updated to b9ecbe8d0e3177afed08c54fc938938100a0b73f
 *** A reboot is needed to activate the new firmware

Raspberry Pi と一緒に購入したもの

  • USB 充電器 ACアダプター:エレコム(EC-AC03BK)
    https://www.amazon.co.jp/dp/B07Z5976XX

    • 「各ポート2.4A、2ポート合計4.8A」という表記を確認して、もし1台増えても対応できそうということで選びました。他にもある有名メーカーにしようか迷いましたが、最終的には安全そうかなとこちらにしました。  
       
  • SD カード:microSDXC 64GB SanDisk(B073XLMDC7)
    https://www.amazon.co.jp//dp/B073XK1DNH

    • それなりに書き込み速度があるほうがいいかなと選びました。特に問題なく使えています。  
       
  • USB Type C ケーブル:Rampow 【1m/黒】 (JPRAMPOWT01)
    https://www.amazon.co.jp/dp/B0791FBQ83 (2020/08/31 追記:削除されていました)

    • 同メーカーで他の種類のケーブルを購入したことがあり丈夫で使いやすかったので選びました。特に問題なく使えています。なお、購入するとかわいいお礼のカードが封入されてきます。  
       
  • ヒートシンク:Raspberry Pi 4b Heatsink (B07ZTQKGMW)
    https://www.amazon.co.jp/dp/B07ZTQKGMW

    • 静音な方がいいなとファン付きケースを避けていたので、ヒートシンクだけを選びました。あまり深く考えずに買ったらケースを買うのとあまり変わらない値段になってしまいました。
### 参考:初回 yum update 後の CPU 温度 ##

[root@localhost ~]# vcgencmd measure_temp
temp=54.0'C

###(その後のアイドル状態) ##

[root@localhost ~]# vcgencmd measure_temp
temp=53.0'C
[root@localhost ~]# vcgencmd measure_temp
temp=54.0'C
[root@localhost ~]# vcgencmd measure_temp
temp=53.0'C
[root@localhost ~]# vcgencmd measure_temp
temp=52.0'C

更新履歴

  • 2019/12/05 新規作成
  • 2020/08/31 いくつかのページが閉鎖されていたのを反映
  • 2023/01/10 一部記載を修正

*1:これより更新日時の新しいファイル「CentOS-Userland-7-armv7hl-generic-Minimal-exp-rpi-1908-sda.raw.xz」も存在していたので試しましたが、Raspberry Pi が起動すらしませんでした。