Debian buster/sid でdmesgを一般ユーザで実行

※この方法だと全ユーザで dmesg が実行できてしまうので sudo を使ってグループを制限する次の方法のほうがいいかもしれない.


Debian buster arm64 環境を作りました( Raspberry Pi 3 Model B の USB起動で Debian buster arm64 を動かす – matoken’s meme )が,dmesg でエラーとなります.sudo dmesg は動く.

$ dmesg 
dmesg: read kernel buffer failed: Operation not permitted
$ LC_MESSAGES=ja_JP.UTF-8 dmesg 
dmesg: read kernel buffer failed: 許可されていない操作です

そういえば以前 Debian sid amd64 でも起こった現象です.
Debian では Linux 4.8.0 から仕様が変わってroot 以外ではdmesg が読めないようになっています.log file の読める adm group のユーザも駄目.
これは kernelパラメータの kernel.dmesg_restrict で設定されていて以下のようなコマンドで確認できます.

$ sudo sysctl -a|grep dmesg
kernel.dmesg_restrict = 1
$ cat /proc/sys/kernel/dmesg_restrict
1

これを 1 から 0 に変更することで以前と同じ動作となります.sysctl コマンドや,

$ sudo sysctl -w kernel.dmesg_restrict=0

/proc/sys/kernel/dmesg_restrict を直接書き換えることで一時的に変更できます.

echo 0 | sudo tee /proc/sys/kernel/dmesg_restrict

動きました.

$ id | grep -o adm
$ echo hello | sudo tee /dev/kmsg 
hello
[  562.152729] hello
$ dmesg -H | tail
[  +0.019933] brcmfmac mmc1:0001:1: firmware: failed to load brcm/brcmfmac43430-sdio.clm_blob (-2)
[  +0.020182] firmware_class: See https://wiki.debian.org/Firmware for information about missing firmware
[  +0.020886] brcmfmac mmc1:0001:1: Direct firmware load for brcm/brcmfmac43430-sdio.clm_blob failed with error -2
[  +0.021916] brcmfmac: brcmf_c_process_clm_blob: no clm_blob available (err=-2), device may have limited channels available
[  +0.026818] brcmfmac: brcmf_c_preinit_dcmds: Firmware: BCM43430/1 wl0: Oct 23 2017 03:55:53 version 7.45.98.38 (r674442 CY) FWID 01-e58d219f
[  +0.077925] systemd-udevd[376]: link_config: autonegotiation is unset or enabled, the speed and duplex are not writable.
[  +0.537137] EXT4-fs (sda2): re-mounted. Opts: (null)
[  +2.001639] smsc95xx 1-1.1:1.0 eth0: hardware isn't capable of remote wakeup
[  +1.563625] smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1
[Mar 2 05:58] hello

再起動すると元に戻ってしまうので,/etc/sysctl.d/local.conf などに設定を書いてくと次回起動時以降も反映されます.

$ sudo install -m 644 -o root -g root /dev/null /etc/sysctl.d/local.conf
$ echo 'kernel.dmesg_restrict = 0' | sudo tee -a /etc/sysctl.d/local.conf
kernel.dmesg_restrict = 0

試した環境

$ dmesg --version
dmesg from util-linux 2.33.1
$ dpkg-query -W util-linux linux-image-*
linux-image-4.19.0-2-arm64      4.19.16-1
linux-image-4.19.0-2-arm64-unsigned
linux-image-arm64       4.19+102
util-linux      2.33.1-0.1
$ lsb_release -dr
Description:    Debian GNU/Linux buster/sid
Release:        testing
$ uname -a
Linux rpi3 4.19.0-2-arm64 #1 SMP Debian 4.19.16-1 (2019-01-17) aarch64 GNU/Linux
$ cat /proc/device-tree/model ;echo
Raspberry Pi 3 Model B Rev 1.2

Raspberry Pi 3 Model B の USB起動で Debian buster arm64 を動かす

最近入手した(遅) RaspberryPi 3 model B で USB メモリ起動の Debian buster aarch64 を設定してみたときのメモです.

ジモティー経由で Raspberry Pi 3 model B を入手しました.Raspberry Pi 3 model B + USB Wi-Fi + USB Bluetooth + ケースのセットが2,500円 + 送料手数料 500円と安いです.普通に新品で買うと 8,000円近くすると思います.今この記事を書いている時点で未だ在庫はあるようなので興味のある人は買うといいかもしれません.(6個同梱可能なのでそれだと1個あたりが少し安くなるので近所の友達と買うといいかも)

IMG-20190208-184136IMG-20190208-185154IMG-20190208-185222IMG-20190213-212056

64bitになった Raspberry Pi は欲しいと思っていたので安く手に入って助かりました :)
(後継の Raspberry Pi 3 Model B+/A+ や,SoCが 3B と同じで無線機能のない 2B v1.2 とかも欲しいですね.)

Raspberry Pi 3 model B は 32bitだけでなく64bit も利用できるハードウェアです.せっかくなので 64bitで動かしたいけど今の時点では Raspbian は 32bitのみです.そこで Debian の arm64 を試すことにしました.

Raspberry Pi 3 用 Debian buster arm64 イメージの入手とUSBメモリへの書き込み

イメージは岩松さんの東京エリアDebian勉強会の資料を真似して作ろうと思っていたのですが,

DebianWiki の RaspberryPi3 の項目を見たら非公式だけどイメージがあったので今回はそれを利用しました.現在 testing の Buster のイメージが入手できます.

ストレージは通常 microSD を利用しますが,手持ちがないのと Raspberry Pi 3 B はUSB起動が可能なはずなのでUSBメモリを利用することにしました.USB 2.0接続の容量2GBのものを用意しました.

このような感じでイメージを入手してハッシュのチェックをした後USBメモリへ書き込みました.ここでは Linuxのddコマンドで書き込んでいますが,Etcherなどを利用すると色々な環境でGUIでの書き込みが可能です.
※イメージは最新のものを利用するようにしましょう.

$ wget -c https://people.debian.org/~gwolf/raspberrypi3/20190206/20190206-raspberry-pi-3-buster-PREVIEW.img.xz https://people.debian.org/~gwolf/raspberrypi3/20190206/20190206-raspberry-pi-3-buster-PREVIEW.img.xz.sha256
$ sha256sum -c ./20190206-raspberry-pi-3-buster-PREVIEW.img.xz.sha256
20190206-raspberry-pi-3-buster-PREVIEW.img.xz: 完了
$ xzcat 20190206-raspberry-pi-3-buster-PREVIEW.img.xz | pv | sudo dd of=/dev/sdb bs=1M oflag=dsync

USBから起動できるようOTPを設定する

注意:OTPの設定は1度しか出来ないし,元に戻すことも出来ないと思われます.起動時間が長くなるなどデメリットもあるので気をつけて設定してください.

このUSBメモリを Raspberry Pi に接続して電源を入れてみましたが起動しません.恐らく OTP(One Time Programmable)メモリの設定が USB起動に対応していないので以下のページを参考に設定します.(Raspberry Pi 3+ シリーズは標準でUSB起動可能なのでこの設定は不要なはずです.)

先ず Raspbian 2017-04-10 以降のイメージを書き込んだ microSD を用意して起動します.
( uart経由で繋ぎたい場合起動前に /boot/config.txt に enable_uart=1 を書いておきます. $ echo enable_uart=1 | tee -a /media/matoken/boot/config.txt )

OTP を確認するとやはり有効になっていませんでした.

pi@raspberrypi:~$ vcgencmd otp_dump | grep ^17:
17:1020000a

以下のように /boot/config.txtprogram_usb_boot_mode=1 を追記して再起動すると反映されます.

$ echo program_usb_boot_mode=1 | sudo tee -a /boot/config.txt
program_usb_boot_mode=1
pi@raspberrypi:~$ sudo reboot

17 が 3020000a に変わればOK

pi@raspberrypi:~$ vcgencmd otp_dump | grep ^17:
17:3020000a

詳細は以下を参照してください

17 – bootmode register

Bit 1: sets the oscillator frequency to 19.2MHz
Bit 3: enables pull ups on the SDIO pins
Bit 19: enables GPIO bootmode
Bit 20: sets the bank to check for GPIO bootmode
Bit 21: enables booting from SD card
Bit 22: sets the bank to boot from
Bit 28: enables USB device booting
Bit 29: enables USB host booting (ethernet and mass storage)

設定前が,1020000a -> 0b010000001000000000000000001010 で設定後が,0x3020000a -> 0b110000001000000000000000001010 てことで,Bit 29 が 0 から 1 になってネットワーク起動とUSB起動を試すようにります.この状態で microSD 起動を試すとこれも大丈夫でした.(優先度は microSD > USB,Networkは未確認)

USBメモリからの起動

この状態で shutdown し,microSD を取り出して Debian の導入した USB メモリを接続して起動で起動しました.(USB起動の場合なかなか起動が始まらず焦ったけど30秒ほど待つとUARTにログが出はじめる.HDMIの出力は未確認)

login は root:raspberry
ちゃんと 64bit です :)

root@rpi3:~# uname -a
Linux rpi3 4.19.0-2-arm64 #1 SMP Debian 4.19.16-1 (2019-01-17) aarch64 GNU/Linux

細々とした設定(ほぼ余録)

パッケージの更新

root@rpi3:~# apt update && apt upgrade

一般ユーザの追加

root@rpi3:~# useradd -m matoken
root@rpi3:~# passwd matoken
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@rpi3:~# chsh matoken -s /bin/bash

OpenSSH の設定変更

root@rpi3:~# diff -u /etc/ssh/sshd_config.old /etc/ssh/sshd_config
--- /etc/ssh/sshd_config.old    2019-02-06 15:15:46.940904823 +0000
+++ /etc/ssh/sshd_config        2019-02-16 13:17:17.076873278 +0000
@@ -29,7 +29,7 @@
 # Authentication:

 #LoginGraceTime 2m
-PermitRootLogin yes
+PermitRootLogin prohibit-password
 #StrictModes yes
 #MaxAuthTries 6
 #MaxSessions 10
@@ -53,7 +53,7 @@
 #IgnoreRhosts yes

 # To disable tunneled clear text passwords, change to no here!
-#PasswordAuthentication yes
+PasswordAuthentication no
 #PermitEmptyPasswords no

 # Change to yes to enable challenge-response passwords (beware issues with
@@ -119,3 +119,5 @@
 #      AllowTcpForwarding no
 #      PermitTTY no
 #      ForceCommand cvs server
+
+AllowGroups sshusers

sshusers グループを作成して ssh login 出来るユーザを登録

root@rpi3:~# sudo groupadd sshusers
root@rpi3:~# sudo usermod -a -G sshusers matoken

短いmodulus を削除する

root@rpi3:~# awk '$5>=3071' /etc/ssh/moduli | tee /etc/ssh/moduli.tmp
root@rpi3:~# wc -l /etc/ssh/moduli /etc/ssh/moduli.tmp
    427 /etc/ssh/moduli
    368 /etc/ssh/moduli.tmp
    795 total
root@rpi3:~# sudo mv /etc/ssh/moduli.tmp /etc/ssh/moduli

hostpcからssh公開鍵をcpしておく

$ ssh-copy-id -i ~/.ssh/id_ed25519_pi.pub matoken@rpi3

ssh鍵ペアを作っておく

root@rpi3:~# sudo -u matoken ssh-keygen -t ed25519

sshd の再起動

root@rpi3:~# systemctl restart ssh

ssh login時に通知を飛ばすようにしておく

root@rpi3:~# vi /etc/ssh/sshrc
root@rpi3:~# chmod +x /etc/ssh/sshrc
root@rpi3:~# cat /etc/ssh/sshrc
#!/bin/sh

/home/matoken/bin/telegram.sh sshrc:${USER}@`hostname`:${SSH_CONNECTION}
/home/matoken/bin/sms.sh sshrc:${USER}@`hostname`:${SSH_CONNECTION}

ssh でパスワードログインできず,鍵認証でログインが出来る&通知が飛ぶのを確認

root@rpi3:~# ssh root@localhost -o PasswordAuthentication=yes
root@rpi3:~# ssh matoken@localhost -o PasswordAuthentication=yes
root@rpi3:~# ssh matoken@localhost -i ~/.ssh/id_ed25519

自分のネットワークに合わせて /etc/iptables/rules.v4, /etc/iptables/rules.v6 を変更.

root@rpi3:~# grep -v ^# /etc/iptables/rules.v4
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -s 127.0.0.0/8 -m comment --comment "RFC3330 loopback" -j ACCEPT
-A INPUT -s 192.168.1.0/24 -m comment --comment "RFC1918 reserved" -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m comment --comment SSH -j REJECT --reject-with icmp-port-unreachable
COMMIT
root@rpi3:~# grep -v ^# /etc/iptables/rules.v6
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -s ::1/128 -m comment --comment "RFC3513 loopback" -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -m comment --comment SSH -j REJECT --reject-with icmp6-port-unreachable
COMMIT

sudo を導入して設定

root@rpi3:~# apt install sudo

sudo groupにユーザを登録

root@rpi3:~# usermod -a -G sudo matoken

sudo が利用できるのを確認

matoken@rpi3:~$ sudo grep ^root: /etc/shadow
sudo: unable to resolve host rpi3: Name or service not known
root:!..VyaTFxP8kT6:17933:0:99999:7:::

rootユーザをロック

$ sudo passwd -l root 
sudo: unable to resolve host rpi3: Name or service not known
passwd: password expiry information changed.
matoken@rpi3:~$ sudo passwd -S root
sudo: unable to resolve host rpi3: Name or service not known
root L 02/06/2019 0 99999 7 -1

sudo時などに「sudo: unable to resolve host rpi3: Name or service not known」と言われるので hosts の localhost に rpi3 を追加

matoken@rpi3:~$ sudo vi /etc/hosts
127.0.0.1 localhost rpi3

ここで再起動すると rootfs が見つからないと言われ起動しない.

(initramfs) Gave up waiting for root file system device. Common problems:
- Boot args (cat /proc/cmdline)
- Check rootdelay= (did the system wait long enough?)
- Missing modules (cat /proc/modules; ls /dev)
ALERT! /dev/mmcblk0p2 does not exist. Dropping to a shell!


BusyBox v1.27.2 (Debian 1:1.27.2-3) built-in shell (ash)
Enter 'help' for a list of built-in commands.

cmdline では /dev/mmcblkp2 から起動しようとしているが, /dev/sda2 から起動したい

(initramfs) ls /dev
block tty12 tty47
bsg tty13 tty48
bus tty14 tty49
char tty15 tty5
console tty16 tty50
core tty17 tty51
cpu_dma_latency tty18 tty52
disk tty19 tty53
fb0 tty2 tty54
fd tty20 tty55
full tty21 tty56
gpiochip0 tty22 tty57
gpiochip1 tty23 tty58
input tty24 tty59
kmsg tty25 tty6
kvm tty26 tty60
mem tty27 tty61
memory_bandwidth tty28 tty62
network_latency tty29 tty63
network_throughput tty3 tty7
null tty30 tty8
port tty31 tty9
psaux tty32 ttyAMA0
ptmx tty33 ttyS0
pts tty34 ttyS1
random tty35 ttyS2
sda tty36 ttyS3
sda1 tty37 urandom
sda2 tty38 vcs
snapshot tty39 vcs1
stderr tty4 vcsa
stdin tty40 vcsa1
stdout tty41 vcsu
tty tty42 vcsu1
tty0 tty43 vga_arbiter
tty1 tty44 zero
tty10 tty45
tty11 tty46
(initramfs) cat /proc/cmdline
bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708_fb.fbswap=1 dma.dmachans=0x7f35 bcm2709.boardrev=0xa02082 bcm2709.serial=0xfaf144 bcm2709.uart_clock=48000000 smsc95xx.macaddr=B8:27:EB:FA:F1:44 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000 console=tty0 console=ttyS1,115200 root=/dev/mmcblk0p2 rw elevator=deadline fsck.repair=yes net.ifnames=0 cma=64M rootwait

/boot を mount して書き換えようと思ったけどうまく mount 出来ない?

(initramfs) mkdir /tmp/boot
(initramfs) mount /dev/sda1 /tmp/boot
mount: mounting /dev/sda1 on /tmp/boot failed: Invalid argument
(initramfs) ls -la /dev/sda1 /tmp/boot
brw-------    1 0        0           8,   1 Jan  1 00:00 /dev/sda1

/tmp/boot:
total 0
drwxr-xr-x    2 0        0                0 Jan  1 00:10 .
drwxr-xr-x    3 0        0                0 Jan  1 00:10 ..
(initramfs) mount -t vfat /dev/sda1 /tmp/boot
mount: mounting /dev/sda1 on /tmp/boot failed: No such device
(initramfs) mkdir /tmp/root
(initramfs) modprobe ext4
(initramfs) mount /dev/sda2 /tmp/root
[ 2144.515323] EXT4-fs (sda2): mounted filesystem with ordered data mode. Opts: (null)
(initramfs) find /tmp/root -name "vfat.ko"
/tmp/root/usr/lib/modules/4.19.0-2-arm64/kernel/fs/fat/vfat.ko
(initramfs) modprobe -v /tmp/root/usr/lib/modules/4.19.0-2-arm64/kernel/fs/fat/vfat.ko
(initramfs) lsmod|grep fat
(initramfs) mount -t vfat /dev/sda1 /tmp/boot
mount: mounting /dev/sda1 on /tmp/boot failed: No such device
(initramfs) lsmod|grep fat

一旦電源を切って /boot/cmdline.txt の root=/dev/mmcblk0p2 を root=/dev/sda2 に書き換えて起動するようになった.(LABEL指定に変えたほうがいいかな)

$ cat /boot/firmware/cmdline.txt 
console=tty0 console=ttyS1,115200 root=/dev/sda2 rw elevator=deadline fsck.repair=yes net.ifnames=0 cma=64M rootwait

raspi3-firmware あたりのアップデートが掛かると上書きされるよう.

systemd から sysvinit に変更(RAMが開くかと思ったけど数MB程度だった)

$ sudo apt install sysvinit-core sysvinit-utils

LANGを設定
en_US.UTF-8 UTF-8, ja_JP.UTF-8 UTF-8 を有効にして,デフォルトロケールを ja_JP.UTF-8 にしたりした.

$ sudo dpkg-reconfigure locales
$ echo 'export LANG="ja_JP.UTF-8"
export LC_TIME="en_US.UTF-8"' | tee -a ~/.profile
$ source ~/.profile
$ locale
LANG=ja_JP.UTF-8
LANGUAGE=
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME=en_US.UTF-8
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER="ja_JP.UTF-8"
LC_NAME="ja_JP.UTF-8"
LC_ADDRESS="ja_JP.UTF-8"
LC_TELEPHONE="ja_JP.UTF-8"
LC_MEASUREMENT="ja_JP.UTF-8"
LC_IDENTIFICATION="ja_JP.UTF-8"
LC_ALL=

Raspberry Pi には rtc がなくて再起動などで時計が初期化されてしまうので fake-hwclock を導入(電源断時などの時間はカウントされないので時間がずれるが時計が巻き戻らない)

$ sudo apt install fake-hwclock

ntpdate を導入して起動時の時計合わせをするようにする(systemdのままだとntpの面倒を見てくれるので必要ない)

$ sudo apt install ntpdate
$ sudo vi /etc/boot.d/ntpdate
$ cat /etc/boot.d/ntpdate
#!/bin/sh

ntpdate 1.jp.pool.ntp.org
$ sudo chmod +x /etc/boot.d/ntpdate

ntpdate iptables に合わせてこっちのほうがいいかも?(有線接続後に時計合わせする)

$ cat /etc/network/interfaces.d/eth0 
auto eth0

# TODO: switch back to iptables-persistent once it re-enters testing
iface eth0 inet dhcp
        pre-up iptables-restore < /etc/iptables/rules.v4
        pre-up ip6tables-restore < /etc/iptables/rules.v6
        up ntpdate 1.jp.pool.ntp.org

uartでsetial接続するために /etc/inittab に以下を設定(systemdだと設定済みなので不要)

T1:23:respawn:/sbin/getty -L ttyS1 115200 vt100

msmtpを導入してメールを飛ばせるように

$ sudo apt install msmtp
$ sudo vi /etc/msmtprc
$ grep -v ^# /etc/msmtprc|uniq
defaults
syslog on
aliases /etc/aliases

tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
auth on
syslog LOG_MAIL

account gmail
host smtp.gmail.com
port 587
from matohara@gmail.com
user matohara
password throjfeekdewxeib

account default : gmail
$ sudo vi /etc/aliases
$ grep -v ^# /etc/aliases
mailer-daemon:hoge@example.com
postmaster:hoge@example.com
nobody:hoge@example.com
hostmaster:hoge@example.com
usenet:hoge@example.com
news:hoge@example.com
webmaster:hoge@example.com
www:hoge@example.com
ftp:hoge@example.com
abuse:hoge@example.com
noc:hoge@example.com
security:hoge@example.com
root: hoge@example.com
default: hoge@example.com
$ echo 'hello' | msmtp matoken@ya.ru
$ sudo apt install msmtp-mta
$ echo 'hello' | /usr/sbin/sendmail matoken@ya.ru

無線を無効にするために /boot/firmware/config.txt に以下を追記……再起動しても反映されない?要確認

dtoverlay=pi3-disable-wifi
dtoverlay=pi3-disable-bt

apticron を導入して更新をメールで通知する

$ sudo apt install apticron
$ sudo cp -p /usr/lib/apticron/apticron.conf /etc/apticron/
$ grep ^EMAIL= /etc/apticron/apticron.conf #必要なら宛先を書き換えたり
EMAIL="root"

deborphan でみなしごパッケージを通知するようにする

$ sudo apt install deborphan
$ sudo crontab -e
$ sudo crontab -l | grep deborphan | grep -v ^#
7 4 * * *       deborphan && printf "\nsudo apt --autoremove purge $( deborpha)'

てことでとりあえず動くようになったけど昔もらったノベルティのUSBメモリなのでこのまま運用すると多分すぐ壊れてしまうと思います.
最近秋葉原でセールになっている様な安い SSD とか欲しいですね.

こちらは起動終了のUARTのメッセージ

asciicast

Raspberry Pi の USB-OTG ethernet の MAC address を model B の ethernet と同じ規則で設定する

Raspberry Pi model A/A+/Zero/ZeroW/ZeroHW/Compute Module/Compute module 3ではUSB-OTGが利用できます.(ZeroHW/Compute Module/Compute module 3は自分は未確認)

USB-OTGでUSBをEthernetにしてHostPCからアクセスするようにするとUSBケーブル1本で電源と併用できて便利です.

このときMACアドレスはモジュール読み込み毎に自動生成されます.
以下の例では,接続先(PC等)から見えるMACが HOST MAC 6a:b3:b1:5e:22:89 で,Raspberry Pi の中でのMACが MAC ae:3a:c7:8e:50:38 になっています.

$ sudo modprobe g_ether
$ dmesg | tail -15
[   88.517568] using random self ethernet address
[   88.517590] using random host ethernet address
[   88.519210] usb0: HOST MAC 6a:b3:b1:5e:22:89
[   88.520195] usb0: MAC ae:3a:c7:8e:50:38
[   88.520396] using random self ethernet address
[   88.520412] using random host ethernet address
[   88.520534] g_ether gadget: Ethernet Gadget, version: Memorial Day 2008
[   88.520546] g_ether gadget: g_ether ready
[   88.520580] dwc2 20980000.usb: bound driver g_ether
[   88.866651] IPv6: ADDRCONF(NETDEV_UP): usb0: link is not ready
[   94.048758] dwc2 20980000.usb: new device is high-speed
[   94.128837] dwc2 20980000.usb: new device is high-speed
[   94.197523] dwc2 20980000.usb: new address 9
[   96.057234] g_ether gadget: high-speed config #1: CDC Ethernet (ECM)
[   96.095719] IPv6: ADDRCONF(NETDEV_CHANGE): usb0: link becomes ready
$ /sbin/ifconfig usb0
usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.42.0.212  netmask 255.255.255.0  broadcast 10.42.0.255
        inet6 fe80::5855:ab0c:6628:557c  prefixlen 64  scopeid 0x20<link>
        ether ae:3a:c7:8e:50:38  txqueuelen 1000  (Ethernet)
        RX packets 208  bytes 17243 (16.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 67  bytes 7696 (7.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

これは毎回変わるのでふと気づくとホストPCのネットワークの設定が沢山になっていたり…….

$ nmcli connection show | grep ethernet | wc -l
25

MAC addressを固定するにはモジュール読み込み時に指定してあげればOKです.

$ sudo rmmod g_ether
$ sudo modprobe g_ether host_addr=de:ad:fe:ef:00:01 dev_addr=de:ad:fe:ef:00:00
$ dmesg | egrep usb0:.*MAC | tail -2
[ 1067.248443] usb0: HOST MAC de:ad:fe:ef:00:01
[ 1067.248958] usb0: MAC de:ad:fe:ef:00:00

永続化するには,g_etherモジュールを読んでいるところで指定してあげます.

/boot/cmdline.txt の場合 rootwait の前に以下を追加
modules-load=dwc2,g_cdc g_ether.host_addr=de:ad:fe:ef:00:01 g_ether.dev_addr=de:ad:fe:ef:00:00
/etc/modules の場合
g_ether g_ether.host_addr=de:ad:fe:ef:00:01 g_ether.dev_addr=de:ad:fe:ef:00:00
Note

※g_cdcの場合はこんな感じで

g_cdc g_cdc.host_addr=de:ad:fe:ef:00:01 g_cdc.dev_addr=de:ad:fe:ef:00:00

/etc/rc.local の場合 exit より前の行に以下を追加(※model Aの場合 cmdline.txt, modules 共に module は読み込まれるけど動作はうまく行かないことが多いので rc.local が良さそう)
modprobe g_ether host_addr=de:ad:fe:ef:00:01 dev_addr=de:ad:fe:ef:00:00

このときのMAC addressは g_ether が自動生成したものを使うのが無難だとおもうのですが,ランダムとかでいんだろうか?という不安があります.

ベンダーIDに使われていない領域だったら多分問題ないですが,これから埋まることもありそうです.(IPアドレスのプライベートIPアドレスのような領域があるのかもしれ無いけど未確認)

Raspberry Pi model B シリーズのNICには Raspberry Pi Foundation のベンダーコードが使われています.

nmap のMAC addressベンダー情報のファイルから確認
$ grep -i raspberry /usr/share/nmap/nmap-mac-prefixes
B827EB Raspberry Pi Foundation

Raspberry Pi の smsc95xx の MAC address の生成は,上6桁はベンダーIDの b8:27:eb を,下6桁はボードのserialの下6桁を割り当てているようです.

MAC address generation
To have a predetermined MAC address, a given SMSC LAN9512 must be attached to an EEPROM that contains the MAC address. But on the Raspberry Pi Model B, this EEPROM is not present; therefore, this driver must assign a MAC address itself. We do this by generating a MAC address from the board’s serial number. This guarantees that a given Raspberry Pi will always have the same MAC address and that two Raspberry Pis are extremely unlikely to be assigned the same MAC address.

手元の Raspberry Pi 2 Model B を確認すると確かにそうなっているようです.

$ ifconfig | grep ether
		ether b8:27:eb:ff:56:0a  txqueuelen 1000  (Ethernet)
$ grep Serial /proc/cpuinfo
Serial          : 0000000094ff560a
$ sed -n "s/^Serial.*:.*\(..\)\(..\)\(..\)$/b8:27:eb:::/p" /proc/cpuinfo
b8:27:eb:ff:56:0a
$ cat /proc/device-tree/model ;echo
Raspberry Pi 2 Model B Rev 1.1
Note

16進数6桁は 0xFFFFFF → 16777215です,Raspberry Pi は2018年3月時点で190万台出荷しているようなので2周目に入っています.低い確率でしょうが同じMAC addressが割り当てられる可能性が…….

we’ve sold 19 million Raspberry Pis in total

内蔵ネットワークに自分でMAC addressを指定したい場合はこんな感じでいけます.

/boot/cmdline.txt に以下を設定
smsc95xx.macaddr=b8:27:eb:00:00:00
/etc/network/interfaces でも設定できるはずだがRaspbianでは未確認
hwaddress ether b8:27:eb:00:00:00

CPU Serialを詐称する手も?

ということで,Raspberry Pi model A / Zero でもこの MAC address が利用できそうな感じです.外向けの HOST MAC をこのアドレスにしてみます.

$ sed -n "s/^Serial.*:.*\(..\)\(..\)\(..\)$/b8:27:eb:::/p" /proc/cpuinfo | tee ~/macaddress
b8:27:eb:d8:63:18
$ sudo vi /boot/cmdline.txt
$ sudo reboot
  :
  :
$ dmesg | grep usb0
[    4.937660] usb0: HOST MAC b8:27:eb:d8:63:18
[    4.937845] usb0: MAC 86:d7:fe:57:f2:d5

ホストPCでもちゃんと指定したものになっています.

$ /sbin/ifconfig enxb827ebd86318 | grep ether
			 ether b8:27:eb:d8:63:18  txqueuelen 1000  (Ethernet)

とりあえずこれで使ってみようと思います.

環境
$ lsb_release -d
Description:    Raspbian GNU/Linux 9.4 (stretch)
$ uname -m
armv6l
$ cat /proc/device-tree/model ;echo
Raspberry Pi Model A Rev 2

Raspbian stretch 2018-10-09

Raspberry Pi の Debian ベースのディストリビューションの最新版の Raspbian 2018-10-09 がリリースされたようです.

curl -sS -o – http://downloads.raspberrypi.org/raspbian/release_notes.txt | perl -e ‘$c=0;while(<STDIN>){if$c!=0 ) && ( $=~/^\ /{print $}elsif($c==0){print $_}else{exit} $c++}’
2018-10-09:
  * In startup wizard, assign keyboard to country as per Debian installer recommendations
  * In startup wizard, add option to use US keyboard in preference to country-specific option
  * In startup wizard, show IP address on first page
  * In startup wizard, check for existing wifi network connection and show it if there is one
  * In startup wizard, install language support packages for LibreOffice and other applications
  * In startup wizard, improve operation with keyboard only and no mouse
  * Password change in Raspberry Pi Configuration and startup wizard now works properly if passwords contain shell characters
  * Battery indicator plugin modified to cope with Pi-top hardware monitor crashing
  * Networking plugin hides wifi password characters by default
  * In Scratch 2 GPIO plugin, set pin from dropdown list rather than free text
  * In Scratch 2 SenseHAT plugin, swap x and y axis values for LED array
  * Include latest Adobe Flash player (31.0.0.108)
  * Include latest RealVNC Server (6.3.1)
  * Include libav-tools
  * Include ssh-import-id
  * Removed Mathematica
  * Merge in latest third-party code for Bluetooth ALSA interface
  * Add ability to prevent software update changing configuration files, by creating ~/.config/.lock file
  * Various other small bug fixes, tweaks and changes to text
  * Make dhcpcd work with 3G devices
  * Add hw acceleration to ffmpeg
  * Improved WiFi-BT coexistence parameters
  * Run fake-hwclock before systemd-fsck-root
  * Raspberry Pi PoE HAT support
  * Linux kernel 4.14.71
  * Raspberry Pi firmware 5b49caa17e91d0e64024380119ad739bb201c674

個人的には 「 Add hw acceleration to ffmpeg」 が嬉しいです.自分でbuildしなくてもよくなってそうです.

$ dpkg-query -W ffmpeg
ffmpeg  7:3.2.10-1~deb9u1+rpt2
$ ffmpeg 2>&1 | grep -o -E '\--enable-omx-rpi|\--enable-mmal'
--enable-omx-rpi
--enable-mmal

とりあえずダウンロードしておきます.

$ # ダウンロードURL確認
$ LANG=C wget --max-redirect=0 https://downloads.raspberrypi.org/raspbian_lite_latest 2>&1 | sed -En 's/Location: ([^ ]*).*$//p'
https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-10-11/2018-10-09-raspbian-stretch-lite.zip
$ # ファイル確認
$ w3m -dump https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-10-11/
Index of /raspbian_lite/images/raspbian_lite-2018-10-11

   [ICO]                        Name                        Last    Size Description
                                                          modified
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
[PARENTDIR] Parent Directory                                           -
[ ]         2018-10-09-raspbian-stretch-lite.info        2018-10-09  54K
                                                              13:34
[ ]         2018-10-09-raspbian-stretch-lite.zip         2018-10-09 351M
                                                              13:34
[ ]         2018-10-09-raspbian-stretch-lite.zip.sha1    2018-10-11   79
                                                              11:38
[ ]         2018-10-09-raspbian-stretch-lite.zip.sha256  2018-10-11  103
                                                              11:38
[ ]         2018-10-09-raspbian-stretch-lite.zip.sig     2018-10-10  473
                                                              13:37
[ ]         2018-10-09-raspbian-stretch-lite.zip.torrent 2018-10-11  28K
                                                              11:38
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


Apache/2.4.10 (Debian) Server at downloads.raspberrypi.org Port 80
$ # bitttorrent と 署名ファイルダウンロード
$ wget https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-10-11/2018-10-09-raspbian-stretch-lite.zip.torrent https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2018-10-11/2018-10-09-raspbian-stretch-lite.zip.sig
$ # Transmission で ダウンロード
$ transmission-cli ./2018-10-09-raspbian-stretch-lite.zip.torrent
   :
$ # 署名確認
$ gpg --verify 2018-10-09-raspbian-stretch-lite.zip.sig

Raspberry Pi でHTML5版「艦これ」を試すも失敗

先日「艦これ」がHTML5になったと聞いて試してみました.確かにadobe Flashの導入されていないLinux端末でも問題なく動作します.

kankore html5

Linuxも言及されていますね.
ということはarmやなにやでも動作するはずってことでまずはRaspberry Pi 2 B で試してみました.結果から言うと失敗しました.

環境

試した環境はこんな感じです.

  • board : Raspberry Pi 2 B

  • OS : RASPBIAN STRETCH WITH DESKTOP 2018-06-27

  • SoC : BCM2837

  • CPU : ARMv7

  • RAM : 1GB

  • WebBrowser : Chromium 65.0.3325.181-0+rpt4

RASPBIAN STRETCH WITH DESKTOP 2018-06-27 を導入して,system を apt update && apt upgrade した状態です.あとは既定値のままで,ウェブブラウザは Chromium,操作はVNC経由で行いました.
解像度が大分上がっています.とりあえず F11 でフルスクリーン表示にして, Ctrl + – で画面を縮小してみました.
ログインしてスクリーンが黒く塗りつぶされるまで数分,「Weigh Anchor! Download Now…」の表示のプログレス画面ですごく時間がかかります.20分以上経ってからエラー画面(所謂猫った状態)になります.
3回試して毎回プログレスバーが8割ほどのところでコケます.
システムの状態を見ていると,黒い画面の頃からずっとCPUが100%で張り付いたままです.RAMには余裕があります.恐らくゲームデータをローカルにコピーして展開などの処理でCPUを使い切ってなかなか処理が終わらずタイ雨アウトしているのではないかと思います.
CPUがボトルネックのようなのでクロックをオーバークロックしてみましたが体感速度は変わらずやはりエラーとなります.2回試しても同じように失敗して駄目でした.

ということでRaspberry Pi 2 B で艦これは今のところ無理そうです.しばらく待って艦これ自体のチューニングが進んだり,CPUの強化されている Raspberry Pi 2 B v1.2(BCM2837)以降なら動くかもしれません.持っている人にはぜひ試して結果を教えてほしいです.

#ちなみに試すのに一回あたり30分ほどかかっているので動いたとしてもあまり現実的ではないかもしれません…….どうしてもやりたいなら母艦の画面を転送したほうが良さそう.

192.168.2.211 (raspberrypi) - VNC Viewer_045範囲を選択_048192.168.2.211 (raspberrypi) - VNC Viewer_046

SoC

cpu clock

RAM

Pi

BCM2836

4 x 900Mhz

1GB

2B

BCM2837

4 x 900MHz

1GB

2B v1.2

BCM2837

4 x 1.2GHz

1GB

3B

BCM2837B0

4 x 1.4GHz

1GB

3B+

Raspbianの初期設定ウィザード( first-boot configuration )を試す

先日Raspberry Pi向けのOSのRaspbian 2018-06-27がリリースされました.

2018-06-27:
  * New first-boot configuration wizard added
  * Recommended Software installer added
  * Bluej, Greenfoot, NodeRED, Claws Mail, VNC Viewer removed from image - can now be installed from Recommended Applications
  * Qpdfview PDF viewer installed instead of Xpdf
  * Version 65.0 of Chromium browser included, with latest Flash player
  * Volume up / down keys now change by 5% increments and affect currently-selected output device rather than internal device only
  * Network plugin now remembers previously-entered WiFi network passwords when prompting for reconnection
  * Serial port and serial console can now be switched separately in Raspberry Pi Configuration
  * Lxkeymap keyboard language setting application removed - replaced with dialog within Raspberry Pi Configuration
  * Wifi country and keyboard language setting dialogs in Raspberry Pi Configuration now callable from other applications
  * New version of Piboto font included to render with correct weight under some rogue applications
  * Reconnection to Bluetooth audio devices on reboot improved
  * Disable click-to-rename behaviour in file manager if single-click selection enabled
  * Appearance Settings dialog makes config changes to some Qt files to match selected theme
  * MIME file type associations improved
  * Multiple desktop management options removed from mouse middle-click menu
  * Menu shortcuts to Raspberry Pi website amended
  * Python 2 IDLE menu link removed
  * Sample Magpi PDF installed in /home/pi/MagPi
  * Various minor tweaks, bug fixes and appearance changes
  * Bluetooth updates
    - Firmware with Bluetooth 4.2 features
    - SCO profile suppot added via bthelper.service
  * Linux kernel 4.14.50+
  * Raspberry Pi firmware 748fb17992426bb29d99224b93cb962fefbdc833

* New first-boot configuration wizard added が気になります.初回起動時にセットアップウィザードが走るようになったようです.気になるので試してみました.

先ずはOSイメージを入手.
以下のページから RASPBIAN STRETCH WITH DESKTOP の方を入手します.

適当な方法でイメージをSD cardに書き込みます.

HDMIモニタに接続,若しくはVNC経由(今回は後者)で起動すると「Welcome to Raspberry Pi」というウィンドウが表示されます.

Welcome to Raspberry Pi_001

まずは国関連の設定.以下は日本でに言語環境で利用する場合です.

Country: Japan
Language : Japanise
Timezone: Tokyo

Welcome to Raspberry Pi_003

次はpi アカウントのパスワードの設定です.初期状態ではraspberryですが,ここのままだとsshやVNCなどのリモート接続を有効にすると誰でも入れてしまい乗っ取られてしまうのでぜひ変更しましょう.(piアカウントを無効にして別のユーザを作るのもいいですね)

Welcome to Raspberry Pi_005

次にWi-Fiの設定です.今回試した Raspberry Pi は Raspberry Pi 2 B でWi-Fiは付いていませんがUSBのWi-Fiドングルを刺していたらこの画面になりました.付けていなくてもRaspberry Pi 3 B / Raspberry Pi 3 B+ / Raspberry Pi Zero W でも設定できると思います.

Welcome to Raspberry Pi_008

次は Update です.しばらく時間がかかります.

Welcome to Raspberry Pi_009

Updateが終了すると「System is up to date」と表示されます.

Welcome to Raspberry Pi_014

これでおしまい.「Reboot」ボタンで設定が反映されます.

Welcome to Raspberry Pi_015

もしまたこのウィザードを実行したい場合はpiwizコマンドで実行できます.

$ sudo -i piwiz

もし,以前からのRaspbianで実行したい場合はpiwizパッケージを導入することで実行できるようになります.

$ sudo apt update
$ sudo apt install piwiz

raspi-configrc_guiの簡易版みたいな感じですね.
画面は英語ですが,言語ファイルがあるので日本語化もできそうです.しかし,日本語化が反映されるのは設定後?となるとあまり意味はないですかね…….

microSDスロットの固定機構の壊れたRaspberry Pi 2 Bのバネを取り外して普通に使えるようにする

SANY0011

Raspberry Pi 2 Bを使っているのですが,以前からmicroSDソケットの固定機構が壊れてしまっています.セロハンテープで留めて更にケースに入れて動きにくくしているのですがたまにずれるようで起動中にストレージを見失ってしまうことがあります.(そして数回に一回は要fsckに)

ちょっと面倒なので対処したいなと思いながら放置していたのですが,また今日も起こったので調べてみることに.多分microSDを押し返すピンかバネを無くしてしまえたら解決しそうな気がします.でも内部が見えないのでどこにピンやバネがあるのかよくわからないです.

SANY0015

きっと先駆者が居るだろうと検索するといくつかそれらしいページを見つけました.

ソケットを交換するアプローチ

ソケットの中のスプリングを取り出してしまうアプローチ

後者のスプリングを取り出すのが簡単そうだし,最悪失敗しても前者のソケットを交換すればいいかなと試してみました.スプリングの場所が分かったのですが現物を見てもよくわからないです.光を当てながら少し斜めから見るとバネが見えました.バネの横の部分を一旦開けて中を確認する人も居るようです.

SANY0019

ここで安全ピンのピン先でこじるようにと思いましたがちょっと安全ピンが大きいせいかソケットが膨らむのでジャンパワイヤのオスを使いました.

ソケットのスプリングの上側からこじるとプラスチックのラッチが外れました.一旦ラッチを取り出して更にその奥をこじるとスプリングの反応がありました.スプリングを内側に倒してから引っ張り上げるようにして取り外せました.

SANY0020

この状態でmicroSDを差し込むとmicroSDは押し返されなくなりました!これで最近のRaspberry Pi Zero(多分3B/3B+も)と同じような使い勝手になったと思います.

SANY0022

今回はRaspberry Pi 2 Bでしたが,Raspberry Pi B+/Raspbery Pi A+もロック機構が壊れたら同様の機構なので同じように解決できると思います.

今だと最新のRaspberry Pi 3 B+を買う人が多いでしょうが.

Devuan ascii armhf(Raspberry Pi)で時計合わせ

Raspberry PiにはRTCが付いていません.なのでシステム停止すると時間情報が飛んでしまいます.fake-hwclockが設定されていたら前回停止時(に近い)時間がファイルに保存されていてその時間に設定されますが,停止時の時間はカウントされないので時間がずれます.ということでネットワークが有効な場合ntpを参照して時計合わせをしたいです.
最近のDebianやRaspbianはsystemdなのでtimesyncd.confでNTP serverを指定してtimedatectlコマンドで有効にする感じだと思うのですが(既定値でそうなっている),Devuanは脱systemdなディストリビューションなのでこの方法は使えません.

$ sudo vi /etc/systemd/timesyncd.conf
$ sudo timedatectl set-ntp true`

詳細はman timesyncd.conf, man timedatectl

ということでntpdateを入れてみました.

$ sudo apt install ntpdate
$ sudo ntpdate 192.168.1.102
$ grep -A1 -B2 ntp /etc/rc.local
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
  /usr/sbin/ntpdate 192.168.1.102
fi

長時間動かすならcrontabで定期的に設定したほうがいいかも?(若しくは何らかのntpd)

$ sudo -u root crontab -l|tail -2
@reboot /usr/sbin/ntpdate 192.168.1.102
3 3 * * *       /usr/sbin/ntpdate 192.168.1.102

追記)
このシステムはRaspbian stretchをDevuan asciiにdist-upgradeしたシステムなのでntpの設定がないだけかもしれません.Devuan asciiをクリーンインストールすれば必要ない設定かも(未確認)

Raspbian stretch イメージを Devuan ascii にしてみる

DevuanというDebianからのフォークディストリビューションがあります.

これは脱SystemdなLinuxディストリビューションで,Raspberry Pi等のarmhfにも対応しています.そしてDebianからDevuanに移行することが出来ます.(Debian jessie -> Devuan jessie)
DebianベースのRaspbianからも同様に移行できるというmailを見かけたので試してみます.

SD cardでやったほうが手順は少ないのですが,遅いし寿命も縮むだろうしってことでイメージサイズを拡大したRaspbianを元にchroot + QEMU User EmulatorでArm以外のhostででやってみました.

サイズを拡大したRaspbianイメージをマウント

以下の手順で用意したイメージを元にします.

raspbian-stretch-lite_latest.imgが元のイメージで,今回devuan_ascii.imgとしてコピーしたものを使います.

$ cp ./raspbian-stretch-lite_latest.img ./devuan_ascii.img
$ sudo kpartx -av ./devuan_ascii.img
add map loop0p1 (253:4): 0 85045 linear 7:0 8192
add map loop0p2 (253:5): 0 8129 linear 7:0 63
$ sudo mount /dev/mapper/loop0p2 /mnt
$ sudo mount /dev/mapper/loop0p1 /mnt/boot

QEMU User Emulatorが使えるようにqemu-user-staticをcp

これでQEMU User Emulatorが透過的に利用できます.

$ sudo cp -p /usr/bin/qemu-arm-static /mnt/usr/bin/

chroot用に/devと/procをbind mountしてchroot

$ sudo mount --bind /dev /mnt/dev
$ sudo mount --bind /dev/pts /mnt/dev/pts
$ sudo mount --bind /proc /mnt/proc
$ sudo chroot /mnt /bin/bash

Raspbian stretchを最新の状態にする

# apt update
# apt upgrade -y

Devuanリポジトリの鍵をインポート

Devuanリポジトリの鍵をインポートする.そのために必要なdirmngrpkgも導入.

# apt install dirmngr
# apt-key adv --recv-keys 94532124541922FB
Executing: /tmp/apt-key-gpghome.zqL0gcSbes/gpg.1.sh --recv-keys 94532124541922FB
gpg: key 94532124541922FB: public key "Devuan Repository (Primary Devuan signing key) <repository@devuan.org>" imported
gpg: Total number processed: 1
gpg:               imported: 1

source.listをDevuanのものに書き換え

Devuanのstableはjessieですが,元のRaspbianがstretchなのでそれに対応するAsciiを指定しています.
Devuan jessieを使いたい場合は,Raspbian jessieを元にするといいと思います.

Devuan Debian
Jessie(stable) Jessie(oldstable)
Ascii(development) Stretch(stable)
Beowulf(-) Buster(testing)
Ceres(unstable) Sid(unstable)
# vi /etc/apt/sources.list
# cat /etc/apt/sources.list|grep -v ^#
deb http://mirrordirector.raspbian.org/raspbian/ stretch rpi
deb http://auto.mirror.devuan.org/merged ascii main contrib non-free
deb http://auto.mirror.devuan.org/merged ascii-updates  main contrib non-free
deb http://auto.mirror.devuan.org/merged ascii-security main contrib non-free

Devuanのパッケージにupgrade

# apt update && apt upgrade
  :
274 upgraded, 7 newly installed, 0 to remove and 3 not upgraded.
Need to get 115 MB of archives.
After this operation, 25.0 MB disk space will be freed.
Do you want to continue? [Y/n] Y
  :
# apt dist-upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  bluez bluez-firmware cgmanager libcgmanager0 systemd-shim
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  libpam-systemd mountall pi-bluetooth plymouth raspberrypi-sys-mods systemd systemd-sysv
The following NEW packages will be installed:
  eudev libeudev1 sysvinit-core
The following packages will be upgraded:
  init libudev1 udev
3 upgraded, 3 newly installed, 7 to remove and 0 not upgraded.
Need to get 1162 kB of archives.
After this operation, 9431 kB disk space will be freed.
Do you want to continue? [Y/n] Y
  :

chrootから抜けてイメージアンマウント

#念の為kpartx -dの前にfsckしておくといいかも?

# exit
$ sudo umount /mnt/proc
$ sudo umount /mnt/dev/pts
$ sudo umount /mnt/dev
$ sudo umount /mnt/boot
$ sudo umount /mnt
$ sudo kpartx -d /dev/mapper/loop0p1
$ sudo kpartx -d /dev/mapper/loop0p2
$ sync

SD cardにイメージを書き込んでRaspberry Piで起動

普通にddとかEtcherとかでSD cardに書き込みます.

$ time sudo dcfldd if=./devuan_ascii.img of=/dev/sdd;sync;sync
128000 blocks (4000Mb) written.
128000+0 records in
128000+0 records out

real    7m13.207s
user    0m0.823s
sys     0m9.211s

後はRaspberry Piで起動すればOK.今回はRaspberry Pi 2 Bで試しました.

Systemdとか居ません.

RAMの利用状況はこんな感じ

$ free
              total        used        free      shared  buff/cache   available
Mem:         949584       21916      879120         188       48548      879976
Swap:        102396           0      102396

しかしneofetchとかではRaspbianのままですね.該当pkgのreinstallとかで更新できそうな気がしますが未検証.

IMG_20180123_001641

イメージ操作めんどくさいのでdebootstrapの方が楽かもしれない?

いやそれなら公式イメージのほうがいいか

Raspbianイメージを任意の広さに拡大する

Raspbian はSD cardに書き込んでRaspberry Piで起動するとraspi-configから簡単にパーティションサイズを拡大できます.しかし,パッケージのアップデートや導入に時間がかかるのでSD cardに書き込む前にこの辺りの処理は済ませておきたいです. chrootとQEMU User Emulatorを使えば出来るのですが,今度はDisk fullに……. ということでraspbianイメージを広げてみます.
※速度の速いストレージだと必要ない処理かもしれませんが,カスタマイズしたイメージを用意するとかにも使えます.
※ここではRaspbian Lite stretch 2017-11-29のイメージを元にしていますが,最終パーティションを広げるのであれば他の各種イメージでもほぼそのまま,途中のパーティションサイズを変更するのも応用でいけます.

今回は広げると行ってもオリジナルのファイルはそのままに,4GBほどのサイズのイメージを作ってそちらにデータをコピーしてそのイメージのサイズを広げることにします.

今回の環境は Debian sid amd64 環境です.Linux環境であればほぼ同じ手順で行けると思います.

4GBイメージを作成

4GBほどの容量のイメージを作成します.もっと大きい容量がいい人はcountを増やしましょう.
<追記>4GBにしてはちょっと大きかったかも.手持ちにカードの容量より少し小さいサイズにすると良いです.</追記>

$ dd if=/dev/zero of=./4gb.img bs=1M count=4000
$ ls -l ./4gb.img
-rw-r--r-- 1 mk mk 4194304000 Jan 19 21:28 ./4gb.img

losetupでデバイスマッピングをする

/dev/loop0に割り当てられた.

$ sudo losetup -f --show ./4gb.img
/dev/loop0

Raspbianイメージの用意とデバイスマッピング

/dev/loop1に割り当てられた.

$ unzip 2017-11-29-raspbian-stretch-lite.zip
$ sudo losetup -f --show ./2017-11-29-raspbian-stretch-lite.img 
/dev/loop1

Raspbianイメージを4GBのイメージにコピー

loop1(Raspbian)のイメージをloop0(4GB)にコピーしている.

$ sudo dd if=/dev/loop1 of=/dev/loop0 bs=1M
1772+0 records in
1772+0 records out
1858076672 bytes (1.9 GB, 1.7 GiB) copied, 161.929 s, 11.5 MB/s

Raspbianイメージを片付ける

$ sudo losetup -d /dev/loop1
$ rm ./2017-11-29-raspbian-stretch-lite.img

コピーしたイメージをfdiskで確認

Raspbianのパーティション情報が見えます.

$ sudo fdisk -l /dev/loop0 
Disk /dev/loop0: 3.9 GiB, 4194304000 bytes, 8192000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x37665771

Device       Boot Start     End Sectors  Size Id Type
/dev/loop0p1       8192   93236   85045 41.5M  c W95 FAT32 (LBA)
/dev/loop0p2      94208 3629055 3534848  1.7G 83 Linux

fdiskでパーテションサイズを変更する

ここはわかりづらいですが,以下のような操作をしています.

  • d コマンドで 2つ目のパーテションを削除
  • n コマンドで 2つ目のパーテションを最大容量で作り直し
    • この時 First sector を削除前のものと合わせること
  • p コマンドで確認
  • w コマンドで書き込み
    • w コマンドで書き込むまではファイルシステムは変更されないので間違ってもやり直しできます.
$ sudo fdisk /dev/loop0

Welcome to fdisk (util-linux 2.30.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-8191999, default 2048): 94208
Last sector, +sectors or +size{K,M,G,T,P} (94208-8191999, default 8191999): 8191999

Created a new partition 2 of type 'Linux' and of size 3.9 GiB.  
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Disk /dev/loop0: 3.9 GiB, 4194304000 bytes, 8192000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x37665771

Device       Boot Start     End Sectors  Size Id Type
/dev/loop0p1       8192   93236   85045 41.5M  c W95 FAT32 (LBA)
/dev/loop0p2      94208 8191999 8097792  3.9G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 無効な引数です                                                                         

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

fdiskに慣れていない場合はgparted等でGUIで操作したほうが直感的で解りやすいかもしれません(未検証) -> gksudo gparted /dev/loop0
20180121_07:01:45-6347

kpartxでパーティションをマッピングする

上で使ったlosetupでも出来るのですが,オフセットを自分で計算して指定する必要がある

losetupを使う場合はfdiskのpコマンドの対象パーティーションの Start * Sector sizeをlosetupの-oに指定します.今回の例だと2つ目のパーティーションは 94208 * 512 になります.(未検証)

$ losetup -f –show -o `expr 94208 \* 512` /dev/loop0

ので今回はそのあたりを自動で行ってくれるkpartxを利用します.大抵は標準では入っていないので以下のエントリを参考に導入してから実行してください.

1つ目のパーティションが /dev/mapper/loop0p1 2つ目のパーティションが /dev/mapper/loop0p2 として設定されました.

$ sudo kpartx -av /dev/loop0
add map loop0p1 (253:4): 0 85045 linear 7:0 8192
add map loop0p2 (253:5): 0 8129 linear 7:0 63

ファイルシステムの修正

ファイルシステムが壊れているのでfsckで修正します.しかし,cleanと言われてしまうので,

$ sudo fsck -n /dev/mapper/loop0p2
fsck from util-linux 2.30.2
e2fsck 1.43.8 (1-Jan-2018)
rootfs: clean, 39394/110656 files, 256435/441856 blocks

一回resize2fsを叩いて怒られてから

$ sudo resize2fs /dev/mapper/loop0p2
resize2fs 1.43.8 (1-Jan-2018)
Please run 'e2fsck -f /dev/mapper/loop0p2' first.

fsckを実行し直します.

$ sudo e2fsck -f /dev/mapper/loop0p2
e2fsck 1.43.8 (1-Jan-2018)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
rootfs: 39394/110656 files (0.1% non-contiguous), 256435/441856 blocks

パーティーションリサイズ

fsckが通ったら今度こそリサイズです.これで最大サイズに変更されます.

$ sudo resize2fs /dev/mapper/loop0p2
resize2fs 1.43.8 (1-Jan-2018)
Resizing the filesystem on /dev/mapper/loop0p2 to 1012224 (4k) blocks.

mountしてみる

広くなっているのがわかります :)

$ sudo mount -o ro /dev/mapper/loop0p2 /mnt
$ df /mnt
ファイルシス        1K-ブロック   使用  使用可 使用% マウント位置                                                              
/dev/mapper/loop0p2     3954560 966004 2769736   26% /mnt

後片付け

アンマウントしてloopデバイスも解除します.

$ sudo umount /mnt
$ sudo kpartx -d /dev/mapper/loop0p?
$ sudo losetup -d /dev/loop0 

QEMU User EmulatorでArm以外でapt upgradeする

この辺はまた別のエントリで書くつもりですがこんな感じでupgradeしたりしています.

$ sudo mount /dev/mapper/loop0p2 /mnt
$ sudo mount /dev/mapper/loop0p1 /mnt/boot
$ sudo mount --bind /dev /mnt/dev
$ sudo mount --bind /dev/pts /mnt/dev/pts
$ sudo mount --bind /proc /mnt/proc
$ sudo cp -p /usr/bin/qemu-arm-static /mnt/usr/bin/
$ sudo chroot /mnt /bin/bash
# apt update && apt upgrade -y

このイメージはオリジナルのRaspbianイメージと同じようにSD cardに書き込んで利用できます.