惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - Aricc

如何在已有内网网络中增加项目专用域名解析服务器(DNS) Windows使用命令行查看cpu的温度 屌丝公司:设置服务器的时区、时间及时间同步 从QNAP威联通NAS的Container Station中的容器中复制文件到本地Window电脑 sourcetree外部差异/合并工具设置 树莓派开通VPN服务端时IPTable设置 - Aricc 动态调用WebService的代理类 为Owin项目增加WebApi 如何一步一步新建一个Owin项目 WebApi Owin OAuth Sql Server查看死锁及堵塞脚本 路由表脚本 配电管家 数据绑定时(<%#Eval)单引号双引号嵌套问题 About SSDT BI 在Win8中用批处理创建Oracle数据库时报“Unable to open file” 收藏网址 SQL SERVER 2005 CLR存储过程调用WCF,64位操作系统。 TX-2B/RX-2B射频发送/接收电路
树莓派插入U盘自动拷贝系统日志到U盘或通过U盘升级程序
Aricc · 2019-08-10 · via 博客园 - Aricc

注意,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盘测试,发现并未成功的拷贝日志。

通过如下命令查看日志

看到有如下错误提示:

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