用户工具

站点工具


02-工程实践:树莓派:首页

树莓派

参考资料

选购

必备配件

最好一次性买齐,套餐不包含或者型号不对时可以联系卖家补差价选合适配件。

  • 外壳
  • micro hdmi ,系统无法启动时连接显示器查找故障原因
  • micro sd ,买32G,32G和16G只差几块钱(16G太小了,装了 texlive-full 之后就剩5G了)
  • 读卡器
  • 网线
  • 电源

安装

烧录

假设sd卡为 /dev/sdb

snippet.bash
dd bs=4M if=2020-02-13-raspbian-buster.img of=/dev/sdb status=progress conv=fsync

U盘启动

必备操作

是否是64位

查看系统信息

dmidecode 用不了,用 inxi

inxi/stable 3.0.32-1-1 all
  full featured system information script

ubuntu 19.10

参考:

步骤:

  • 从官网下载最新64位镜像,烧录到sd卡,什么也不需要改,会自动生成一个有线网的配置
  • 网线插路由器,自动获取IP
  • 升级软件和固件
  • 设置 crda ,REGDOMAIN=CN,否则搜不到5Ghz wifi,如下所示
root@ubuntu:~# cat /etc/default/crda 
# Set REGDOMAIN to a ISO/IEC 3166-1 alpha2 country code so that iw(8) may set
# the initial regulatory domain setting for IEEE 802.11 devices which operate
# on this system.
#
# Governments assert the right to regulate usage of radio spectrum within
# their respective territories so make sure you select a ISO/IEC 3166-1 alpha2
# country code suitable for your location or you may infringe on local
# legislature. See `/usr/share/zoneinfo/zone.tab' for a table of timezone
# descriptions containing ISO/IEC 3166-1 alpha2 country codes.

REGDOMAIN=CN
  • 重启机器
  • /etc/netplan 中设置网络,参考配置:
snippet.yaml
network:
    wifis:
        wlan0:
            dhcp4: true
            access-points:
                "ssid":
                    password: "password"
    ethernets:
        eth0:
            dhcp4: false
            addresses:
              - 10.0.0.2/24
            optional: true
    version: 2
  • 执行 netplan --debug apply 应用配置,会自动启动 netplan.wpa@wlan0.service
  • 从路由器拔出网线,插到电脑上
  • 一切正常的话就,树莓派和电脑直连的网络以及 wifi 网络都能通

编译 raspberrypi-bin

不响应arp

临时解决方案,加定时任务每分钟ping一次局域网上的ip,其他机器就能获取到树莓派的mac地址了

snippet.bash
#!/bin/bash
for id in `nmap -sPn 192.168.1.0/27 |grep report |awk '{print $NF}' |tr -d '()'`;do ping -W 1 -c 1 -f $id;done

发现是用了有线网络之后才会出问题。目前通过网线和电脑直连。在PC端禁用对应网络适配器之后无此问题

复现

  • 将手机断开wifi
  • 手机连接wifi
  • 访问树莓派IP上的web服务,失败
  • 树莓派上 ping 手机ip
  • 再次访问树莓派,成功

路由器抓包可以看到手机arp请求问树莓派的mac,但是树莓派没有回应

可能是网卡驱动bug,参见:

驱动信息

driver: brcmfmac
version: 7.45.18
firmware-version: 01-6a2c8ad4
expansion-rom-version: 
bus-info: mmc1:0001:1
supports-statistics: no
supports-test: no
supports-eeprom-access: no
supports-register-dump: no
supports-priv-flags: no

升级驱动参考

固件升级

参考:

snippet.bash
sudo rpi-update
sudo rpi-eeprom-update -a

USB问题

表现为 移动硬盘 盘符经常变动,sda1, sdb1, sdc1,sdd1 等之间漂移。dmesg日志有 大量 usb usb2-port3: over-current change #231

尝试升级固件是否能解决

破案:是爱国者优盘坏掉了。。插windows电脑上都没法格式化

更新:应该是电源的问题。换了小米5V3A的手机电源适配器,没有 Under-voltage detected 的报错了。参考此文:https://scribles.net/lightning-bolt-under-voltage-warning-on-raspberry-pi/。优盘可能是因为电源才损坏的。Under-voltage may cause SD card corruption and/or other problems.

磁盘挂载

对于故障率高的盘或者纯数据盘,用 systemd 挂载,防止影响系统启动。

应避免使用 ntfs 文件系统,在linux上 ntfs-3g 速度较低,建议使用 ext4 或者 xfs,速度大约比ntfs快2倍

nas.mount

[Unit]
Description=Auto mount nas
DefaultDependencies=no
ConditionPathExists=/nas
After=sysinit.target

[Mount]
What=/dev/sdb1
Where=/nas
Type=ntfs

[Install]
WantedBy=multi-user.target

data.mount

[Unit]
Description=Auto mount data
DefaultDependencies=no
ConditionPathExists=/data
After=sysinit.target

[Mount]
What=/dev/sda1
Where=/data
Type=btrfs

[Install]
WantedBy=multi-user.target

systemd 等待磁盘挂载:https://www.renyiwei.com/archives/1828.html

docker , samba, cloud-torrent, aria2 等需要数据盘的都需要设置

[Unit]
Requires=nas.mount data.mount
After=nas.mount data.mount

防止盘符改变:http://www.initdba.com/?p=116

blkid 获取 UUID,systemd 配置中 What=UUID=...

smartmontools

查看移动硬盘信息

smartctl -a /dev/sdb
查看硬盘的详细信息
关键字段说明:
	Raw_Read_Error_Rate:          底层数据读取错误率
	Rotation Rate:                硬盘转数信息,固态硬盘显示Solid State Device
	User Capacity:                硬盘容量
	Start_Stop_Count:             硬盘加电次数
	Power_On_Hours:               硬盘累计使用时长(小时)
	Power-Off_Retract_Count:      非正常关闭硬盘的次数(突然关闭,或者强制热插拔硬盘的次数)
	Temperature_Celsius:          硬盘温度
	Seek_Error_Rate:              寻道错误率

磁盘测速

  • /data 是优盘
  • /root 是TF卡
  • /nas 是移动硬盘

可见移动硬盘最快。安装 iTop 时,如果 MySQL 数据存储在 优盘 上,则更新数据库时总是 504 超时,使用 mysqld --initialize --user=mysql --datadir /nas/mysql 安装到 移动硬盘 之后就好了。

root@ubuntu:/data# dd if=/dev/zero of=/data/test.db bs=8k count=50000 conv=fdatasync
50000+0 records in
50000+0 records out
409600000 bytes (410 MB, 391 MiB) copied, 39.2218 s, 10.4 MB/s
root@ubuntu:/data# 
root@ubuntu:/data# dd if=/dev/zero of=/nas/test.db bs=8k count=50000 conv=fdatasync
50000+0 records in
50000+0 records out
409600000 bytes (410 MB, 391 MiB) copied, 10.0974 s, 40.6 MB/s
root@ubuntu:/data# 
root@ubuntu:/data# dd if=/dev/zero of=/root/test.db bs=8k count=50000 conv=fdatasync
50000+0 records in
50000+0 records out
409600000 bytes (410 MB, 391 MiB) copied, 35.1674 s, 11.6 MB/s

网线直连电脑

编辑 /etc/dhcpcd.conf

interface eth0
static ip_address=10.0.0.2/24
nogateway

win10 设置同网段IP,比如 10.0.0.3,连不上时可尝试 禁用该网络适配器然后在启用节能乙太网路 导致的问题,详情见 https://www.annhe.net/article-4504.html

ubuntu上使用usb网卡

带宽测试

安装 iperf

snippet.bash
# raspbian 服务端
sudo apt install iperf
iperf -s
 
# windows 客户端,使用msys2
pacman -S iperf
iperf -c raspbian-ip

丢包问题

更新,问题一般出现在电脑重启后,和树莓派建立连接出了问题,考虑用以下方法测试:

  • 检查 /var/log/messages 确定 eth0 是否最近到 link up 状态
  • nmap -sn pc_ip 检查pc是否在线

然后根据检查结果重启 eth0

snippet.bash
#!/bin/bash
# 加入计划任务 2分钟执行一次,1分钟太短
 
DT=`date +%Y-%m-%d`
TIME=`date +%H:%M:%S`
LOG="/tmp/check-eth0-$DT.log"
IP="10.0.0.3"
 
# /var/log/message
MESSAGE="genet eth0: Link is Up"
 
# 网卡启动时间
LINKUP=`grep "$MESSAGE" /var/log/messages |tail -n 1 |awk '{print $1,$2,$3}'`
LINKUP_TIMESTAMP=`date -d "$LINKUP" +%s`
NOW_TIMESTAMP=`date +%s`
 
((DIFFER=$NOW_TIMESTAMP-$LINKUP_TIMESTAMP))
 
 
if [ $DIFFER -gt 300 ];then
	echo "$TIME eth0 up more than 5 minutes. ignore" |tee -a $LOG
else
	for id in `seq 1 5`;do
		nmap -sPn $IP
	done |grep "0 hosts up"
 
	if [ $? -eq 0 ];then
		ifconfig eth0 down
		ifconfig eth0 up
		echo "$TIME found problem. restart eth0..." |tee -a $LOG
	else
		echo "$TIME link ok" |tee -a $LOG
	fi
fi

参考:https://security.stackexchange.com/questions/36198/how-to-find-live-hosts-on-my-network

参考:

树莓派上执行 ifconfig eth0 down , ifconfig eth0 up ,重启 eth0 之后就恢复了。。windows上可能也需要重启网络适配器

无线管理

参考:https://linux.cn/article-4015-1.html

snippet.bash
# 扫描
sudo iw dev wlan0 scan |grep SSID
# 查看速率
iwconfig wlan0

编辑 /etc/wpasupplicant/wpa_supplicant.conf

snippet.json
network={
    ssid="[网络 ssid]"
    psk="[密码]"
    priority=1
}

sudo wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf

Wifi不稳定

关闭电源管理,参考:

rc.local 中添加

iwconfig wlan0 power off

也可能是路由器的问题,重启下路由器试试

关闭LED指示灯

参考:https://zhuanlan.zhihu.com/p/104483389

snippet.bash
echo 0 | sudo tee  /sys/class/leds/led0/brightness
echo none | sudo tee  /sys/class/leds/led0/trigger
echo none | sudo tee  /sys/class/leds/led1/trigger
echo 0 | sudo tee /sys/class/leds/led1/brightness

开机生效,加入 /etc/rc.local

关闭网卡led

windows 休眠,绿灯黄灯都亮

windows 执行以下操作后:

  • 网卡配置,高级里,关闭 wake on lan 相关项目
  • 关机(非休眠)

结果:黄灯灭,绿灯依然亮

windows 执行以下操作后:

  • 网卡配置,高级里,关闭 wake on lan 相关项目
  • 休眠

todo

Docker

snippet.bash
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
 
# add apt source
deb https://mirrors.aliyun.com/docker-ce/linux/raspbian buster stable

构建多架构Docker镜像

golang交叉编译

snippet.bash
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build

Samba

参考: https://shumeipai.nxez.com/2013/08/24/install-nas-on-raspberrypi.html

添加用户

snippet.bash
useradd user
smbpasswd -a user

DLNA

BT下载

aria2 命令

  • 查看 torrent 内容 aria2c -S file.torrent
  • 选择下载文件 aria2c --select-files=<index> file.torrent index格式参考: 2,3-5

transmission设置,参考:

通过web界面修改的重启daemon之后会被覆盖,因此因在配置文件中修改,然后reload daemon systemctl reload transmission-daemon

当需要从samba访问管理transmission下载的文件时,需要设置umask为2,即让transmission以664 权限创建新文件,参考:https://www.cnblogs.com/wish123/p/7073114.html

运行效果:

监控

无线打印

HP打印机,安装驱动:apt install hplip

参考:

通过web远程管理cups,需要修改 /etc/cups/cupsd.conf 配置

Listen 0.0.0.0:631
# access all hostname
ServerAlias *

# allow access from remote
<Location />
  Order allow,deny
  Allow 192.168.3.0/24
</Location>

# Restrict access to the admin pages...
<Location /admin>
  Order allow,deny
  Allow 192.168.3.0/24
</Location>

# Restrict access to configuration files...
<Location /admin/conf>
  AuthType Default
  Require user @SYSTEM
  Order allow,deny
  Allow 192.168.3.0/24
</Location>

# Restrict access to log files...
<Location /admin/log>
  AuthType Default
  Require user @SYSTEM
  Order allow,deny
  Allow 192.168.3.0/24
</Location>

将某个用户加入 lpadmin

sudo usermod -a -G lpadmin pi

windows添加打印机

选 按名称添加,填写打印机地址,例如:http://192.168.2.10:631/printers/HP_DeskJet_1110_series,不要带https

参考: https://blog.csdn.net/windsnow1/article/details/88059375

hp打印机修复

用惠普 print and scan doctor 检测问题。我遇到的问题打印机无响应,一直显示设备忙,通过移除无用的彩色墨盒解决了。

风扇

查询风扇转速

暂无解决方案

根据温度启停风扇

  • 三极管方案
  • 相关产品,淘宝搜 树莓派 温控风扇

根据温度调整转速

温度

  • 长期跑transmission
  • 有风扇温度在42左右
  • 拔下风扇55左右
  • 执行apt install texlive-full 峰值63度
  • 执行 stress -c 8 --timeout 180s,峰值 67度
  • 开启风扇,执行 stress -c 8 --timeout 180s,峰值 54度

功率

暂无方案

  • 整机(含风扇)功率

编译pandoc

放弃在树莓派上编译,pandoc 编译成功但用时很久,pandoc-citeproc 总是 out of memory,无法完成编译。电脑装个虚拟机,编译快很多,20多分钟就完成了stack安装,pandoc编译过程。考虑研究交叉编译。参考资料: https://www.jianshu.com/p/ee7ba9a188d0

apt 安装的 pandoc 版本太旧,编译参考:

make -jcabal install -j 提升速度

cabal install pandoc 失败

[133 of 161] Compiling Text.Pandoc.Readers.LaTeX
ghc: internal error: getMBlock: mmap: Invalid argument
    (GHC version 8.4.4 for arm_unknown_linux)
    Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug
cabal: Leaving directory '/tmp/cabal-tmp-8306/pandoc-2.9.2'

编译 ghc-8.6.5 失败

/tmp/ghc20796_0/ghc_8.s: Assembler messages:

/tmp/ghc20796_0/ghc_8.s:32:0: error:
     Error: selected processor does not support `movw r7,:lower16:stg_enter_info' in ARM mode
   |
32 |         movw    r7, :lower16:stg_enter_info
   | ^

/tmp/ghc20796_0/ghc_8.s:33:0: error:
     Error: selected processor does not support `movt r7,:upper16:stg_enter_info' in ARM mode
   |
33 |         movt    r7, :upper16:stg_enter_info
   | ^

/tmp/ghc20796_0/ghc_8.s:231:0: error:
     Error: selected processor does not support `movw r2,#32512' in ARM mode
    |
231 |         movw    r2, #32512
    | ^

/tmp/ghc20796_0/ghc_8.s:233:0: error:
     Error: selected processor does not support `movt r2,#640' in ARM mode
    |
233 |         movt    r2, #640
    | ^
`gcc' failed in phase `Assembler'. (Exit code: 1)
make[1]: *** [rts/ghc.mk:315: rts/dist/build/StgStartup.o] Error 1
make: *** [Makefile:127: all] Error 2

使用stack安装

参考:

4GB内存 + 1GB swap 的树莓派4b,stack install 时会出现 out of memory. 考虑在低配机器上设置 -j1 而非 -j4。参考:

修改 /etc/dphys-swapfile 配置 CONF_SWAPFILE=/data/var/swap 来自定义 swap 文件位置,可以增加swap大小,节省 sd 卡系统盘空间。

下载最新 stack,不要用 apt 安装的老版本,如果有,先卸载

snippet.bash
curl -sSL https://get.haskellstack.org/ > stack.sh

编辑 stack.sh, 将 dltofile 函数改用 aria2c 加速下载

snippet.bash
dl_to_file() {
    aria2c -x 16 "$1" --out="$2" --dir=/
}

为提高速度,手动下载 ghc

axel -n 20 https://github.com/commercialhaskell/ghc/releases/download/ghc-8.6.3-release/ghc-8.6.3-armv7-deb8-linux.tar.xz
mv ghc-8.6.3* /root/.stack/programs/arm-linux/ghc-8.6.3.tar.xz
# 可能需要设置代理,内存不够用时设置 -j1
http_proxy=xxx https_proxy=xxx stack setup --compiler=ghc-8.6.3 -v -j4
http_proxy=xxx https_proxy=xxx stack install --compiler=ghc-8.6.3 -v -j4
# 静态编译
stack install --compiler=ghc-8.6.3 -v -j1 --flag pandoc-citeproc:static

其他用途

  • 24小时开机,可做备份机,定时拉取服务器上的备份
02-工程实践/树莓派/首页.txt · 最后更改: 2021/02/09 11:57 由 annhe