树莓派插入U盘自动拷贝系统日志到U盘或通过U盘升级程序

注意,U盘用Fat32格式,NTFS格式的话,需要在Linux另外安装相应驱动。

可通过udev实现如题的功能。


在/etc/udev/rules.d/目录下新建规则文件98-logcopy.rules 

内容如下:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"

# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}" RUN+="/bin/cp -r /opt/trash/logs /media/%E{dir_name}"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

# Exit
LABEL="media_by_label_auto_mount_end"

 可根据实际情况,修改挂载后的处理脚本。

保存文件并重启,插入U盘测试,发现并未成功的拷贝日志。

 

通过如下命令查看日志

systemctl status udev

看到有如下错误提示:

pi@raspberrypi:~ $ systemctl status udev
● systemd-udevd.service - udev Kernel Device Manager
   Loaded: loaded (/lib/systemd/system/systemd-udevd.service; static; vendor preset: enabled)
   Active: active (running) since Sat 2019-08-10 16:31:27 CST; 6h ago
     Docs: man:systemd-udevd.service(8)
           man:udev(7)
 Main PID: 128 (systemd-udevd)
   Status: "Processing with 16 children at max"
   CGroup: /system.slice/systemd-udevd.service
           └─128 /lib/systemd/systemd-udevd

Aug 10 21:27:15 raspberrypi mtp-probe[8780]: checking bus 1, device 8: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.1/1-1.1.2"
Aug 10 21:27:17 raspberrypi systemd-udevd[8785]: Process '/root/mount_manager/mount_manager add' failed with exit code 1.
Aug 10 21:27:30 raspberrypi mtp-probe[8822]: checking bus 1, device 9: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3"
Aug 10 21:27:49 raspberrypi systemd-udevd[8871]: Process '/root/mount_manager/mount_manager remove' failed with exit code 1.
Aug 10 22:34:07 raspberrypi systemd-udevd[10802]: Process '/bin/umount -l /media/usbhd-sda1' failed with exit code 32.
Aug 10 22:34:07 raspberrypi systemd-udevd[10802]: Process '/bin/rmdir /media/usbhd-sda1' failed with exit code 1.
Aug 10 22:34:43 raspberrypi mtp-probe[10815]: checking bus 1, device 10: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.1/1-1.1.2"
Aug 10 22:34:45 raspberrypi systemd-udevd[10819]: Process '/bin/mkdir -p /media/usbhd-sda1' failed with exit code 1.
Aug 10 22:34:45 raspberrypi systemd-udevd[10819]: Process '/bin/mount -o relatime,utf8,gid=100,umask=002 /dev/sda1 /media/usbhd-sda1' failed with exit code 32.
Aug 10 22:34:45 raspberrypi systemd-udevd[10819]: Process '/bin/cp /opt/trash/logs /media/usbhd-sda1' failed with exit code 1.

  经过Google查询,原来树莓派的系统有点Bug。关于此问题的详细信息 看这里

解决方法:

sudo nano /lib/systemd/system/systemd-udevd.service

打开上面的文件,将后7行注释掉,就可以了。否则udev规则虽会触发执行,但会执行失败。

修改后的文件内容如下:

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=udev Kernel Device Manager
Documentation=man:systemd-udevd.service(8) man:udev(7)
DefaultDependencies=no
Wants=systemd-udevd-control.socket systemd-udevd-kernel.socket
After=systemd-udevd-control.socket systemd-udevd-kernel.socket systemd-sysusers.service
Before=sysinit.target
ConditionPathIsReadWrite=/sys

[Service]
Type=notify
OOMScoreAdjust=-1000
Sockets=systemd-udevd-control.socket systemd-udevd-kernel.socket
Restart=always
RestartSec=0
ExecStart=/lib/systemd/systemd-udevd
#KillMode=mixed
#WatchdogSec=3min
#TasksMax=infinity
#MountFlags=slave
#MemoryDenyWriteExecute=yes
#RestrictRealtime=yes
#RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6

 再次保存,重启。

将U盘插入树莓派,已经成功复制了日志文件。

 

现在也还有不完善的地方,比如,设备如果有多套,往U盘拷贝日志的路径是一样的,那么永远只能拿到最后插的那台设备的日志。所以应该能在Copy日志的时候用与设备相关的名称做为目录对日志文件进行管理。

按如上设想修改后,就可以用一个U盘从现场取N个设备的日志了。

我又想到,也可以反方向的,像安卓手机卡刷一样,用U盘升级系统。

 ===================================================================================================

2019-08-11更新 实现上面末尾提到的两个功能

由板载程序在/opt/trash/boxid.txt位置创建一个保存设备编号的文件。板载程序通过登录应答消息从服务端得到设备编号,并写入到上述文件。

再把规则文件修改一下,挂载完U盘后执行一个Shell脚本,修改后内容如下:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"

# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}" RUN+="/opt/trash/usb %E{dir_name}"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

# Exit
LABEL="media_by_label_auto_mount_end"

  在/opt/trash目录下创建一个shell脚本,名称为usb

shell脚本内容如下:

#!/bin/bash

boxid=$(cat /opt/trash/boxid.txt)
curdt=$(date +%Y%m%d%H%M%S)

sudo mkdir -p /media/$1/boxlogs/${boxid}/${curdt}
sudo cp -r /opt/trash/logs/* /media/$1/boxlogs/${boxid}/${curdt}

sudo cp /media/$1/boxupdate/* /opt/trash

以上脚本先从/opt/trash/boxid.txt文件取得保存的设备编号,然后又取了当前时间,按“设备号/时间”的形式创建了目录,然后再把日志拷贝到这个目录里。

最后一句是从U盘指定目录拷贝内容到板载程序目录,实现了用U盘更新板载程序的功能。

这样,功能就完美了。

 

 

 

附几个命令:

udevadm monitor

sudo udevadm control --reload-rules

  

 

posted @ 2019-08-10 23:48  Aricc  阅读(1218)  评论(0编辑  收藏  举报