Raspbian

安裝後在 USB 中創建文件夾

  • May 20, 2019

安裝後,我正在嘗試在 USB 中創建一個文件夾。我正在使用以下 udev 規則**ID_FS_UUID_ENC** (64AC6F22AC6EEE4C)作為參數發送,並start.sh在此路徑中創建子文件夾 -/media/pi/64AC6F22AC6EEE4C

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_UUID_ENC}!="", ENV{dir_name}="%E{ID_FS_UUID_ENC}"  
#ENV{ID_FS_UUID_ENC}=="", ENV{dir_name}=""  
# Global mount options  
ACTION=="add", DRIVERS=="usb-storage", RUN+="/usr/bin/setsid /home/pi/raspberry-stilas/start.sh %E{ID_FS_UUID_ENC}"
ACTION=="add", DRIVERS=="usb-storage", RUN+="/bin/sh -c 'echo == >> /home/pi/raspberry-stilas/usb-storage-env.log; env >> /home/pi/raspberry-stilas/usb-storage-env.log'"
# Exit  
LABEL="media_by_label_auto_mount_end"

現在的問題是當我插入 USB 時start.sh會創建一個新文件夾並重命名傳遞的文件夾。因此創建了一個新文件夾64AC6F22AC6EEE4C並將 USB 標籤重命名為64AC6F22AC6EEE4C1. 誰能告訴我我做錯了什麼?是不是沒有安裝 USB 並且我正在嘗試創建文件夾?

更新

我檢查了系統日誌和我的自定義日誌,它看起來設備在我的腳本執行後正在安裝。有什麼我可以做的只有在安裝後才能執行。

Custom Log - Stilas Python program run at 2019-05-18 11:33:01 PM
Syslog - May 18 23:33:02 raspberrypi udisksd[725]: Mounted /dev/sda1 at /media/pi/64AC6F22AC6EEE4C1 on behalf of uid 1000

添加後的日誌sleep 5

May 19 01:33:40 raspberrypi kernel: [  944.893519] usb 1-1.2: USB disconnect, device number 8
May 19 01:33:42 raspberrypi kernel: [  947.749507] usb 1-1.2: new high-speed USB device number 9 using dwc_otg
May 19 01:33:43 raspberrypi kernel: [  947.880955] usb 1-1.2: New USB device found, idVendor=0781, idProduct=558a
May 19 01:33:43 raspberrypi kernel: [  947.880971] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
May 19 01:33:43 raspberrypi kernel: [  947.880980] usb 1-1.2: Product: Ultra
May 19 01:33:43 raspberrypi kernel: [  947.880988] usb 1-1.2: Manufacturer: SanDisk
May 19 01:33:43 raspberrypi kernel: [  947.880997] usb 1-1.2: SerialNumber: 4C530001270213117013
May 19 01:33:43 raspberrypi kernel: [  947.881806] usb-storage 1-1.2:1.0: USB Mass Storage device detected
May 19 01:33:43 raspberrypi kernel: [  947.882227] scsi host1: usb-storage 1-1.2:1.0
May 19 01:33:43 raspberrypi mtp-probe: checking bus 1, device 9: "/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2"
May 19 01:33:43 raspberrypi mtp-probe: bus: 1, device: 9 was not an MTP device
May 19 01:33:44 raspberrypi kernel: [  948.890623] scsi 1:0:0:0: Direct-Access     SanDisk  Ultra            1.00 PQ: 0 ANSI: 6
May 19 01:33:44 raspberrypi kernel: [  948.891609] sd 1:0:0:0: Attached scsi generic sg1 type 0
May 19 01:33:44 raspberrypi kernel: [  948.891730] sd 1:0:0:0: [sdb] 120127488 512-byte logical blocks: (61.5 GB/57.3 GiB)
May 19 01:33:44 raspberrypi kernel: [  948.892813] sd 1:0:0:0: [sdb] Write Protect is off
May 19 01:33:44 raspberrypi kernel: [  948.892828] sd 1:0:0:0: [sdb] Mode Sense: 43 00 00 00
May 19 01:33:44 raspberrypi kernel: [  948.893398] sd 1:0:0:0: [sdb] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
May 19 01:33:44 raspberrypi kernel: [  948.902832]  sdb: sdb1
May 19 01:33:44 raspberrypi kernel: [  948.905690] sd 1:0:0:0: [sdb] Attached SCSI removable disk

Start.sh

#!/bin/sh
sleep 5
echo "Device Mounted on - `date +"%Y-%m-%d %r."` Path - /media/pi/{$1}" env >> /home/pi/raspberry-stilas/mount.log
if [ "$1" != "" ]; then
 echo "Stilas Python program run at `date +"%Y-%m-%d %r."` Path - /media/pi/{$1}" env >> /home/pi/raspberry-stilas/mount.log
 python3 /home/pi/raspberry-stilas/stilas/stilas.py "/media/pi/$1"
fi

我建議 fork 的回答將不再適用於較新的 udev 系統:

RUN
...
Starting daemons or other long running processes is not appropriate for
udev; the forked processes, detached or not, will be unconditionally killed
after the event handling has finished.

您可以使用一個通用的 systemd 服務,它需要您的mount,設備已安裝,您使用:sudo systemctl list-units -t mount為您的設備找到 systemd 安裝,寫下它的名稱。

然後,您創建一個名為/etc/systemd/system/your.service `

[Unit] 
Description=My mount script trigger 
Requires=<whatever the command above returned>.mount 
After=<whatever the command above returned>.mount 

[Service] 
ExecStart=/home/pi/start.sh 

[Install] 
WantedBy=<whatever the command above returned>.mount 

優點:易於設置

缺點:僅適用於具有特定名稱的 USB 設備。您必須自己在腳本中計算出 UUID。

假設/home/pi/start.sh有一個 shebang(#!/bin/sh在第一行有),sleep 5在第 2 行,並且執行位設置(chmod +x /home/pi/start.sh)。

使用setsid,它會分叉,這意味著腳本執行是在後台執行的。上面的睡眠是為了讓您的系統有時間掛載 USB 設備。Udev 在掛載設備之前不會等待腳本完成,而是setsid會立即返回並且設備應該會快速掛載。

ACTION=="add", DRIVERS=="usb-storage", RUN+="/usr/bin/setsid /home/pi/start.sh %E{ID_FS_UUID_ENC}"

編輯:udev 等待所有孩子,因此該過程必須創建自己的組。

確保/home/pi/start.sh具有以下標頭:

#!/bin/sh
pgid_from_pid() {
   local pid=$1
   ps -o pgid= "$pid" 2>/dev/null | egrep -o "[0-9]+"
}

pid="$$"
if [ "$pid" != "$(pgid_from_pid $pid)" ]; then
   exec setsid "$(readlink -f "$0")" "$@"
fi
sleep 5

創建一個 init.sh 腳本:

#!/bin/sh
/home/pi/start.sh "$@"

我們結合以下 udev 規則:

ACTION=="add", DRIVERS=="usb-storage", RUN+="/bin/sh /home/pi/init.sh %E{ID_FS_UUID_ENC}"

引用自:https://unix.stackexchange.com/questions/519670