如何禁用日志文件中记录闪存驱动器的功能?

如何禁用日志文件中记录闪存驱动器的功能?

如果我插入闪存驱动器 (pendrive),它会记录在 dmesg 和 /var/log* 文件中。我该如何禁用此功能?我不想禁用日志记录,我只想禁用有关 USB 闪存驱动器的日志:

dmesg
...
[83400.640015] usb 2-2: new high speed USB device using ehci_hcd and address 4
[83400.777225] usb 2-2: configuration #1 chosen from 1 choice
[83401.386977] Initializing USB Mass Storage driver...
[83401.387192] scsi4 : SCSI emulation for USB Mass Storage devices
[83401.387282] usbcore: registered new interface driver usb-storage
[83401.387284] USB Mass Storage support registered.
[83401.388725] usb-storage: device found at 4
[83401.388728] usb-storage: waiting for device to settle before scanning
[83406.388491] usb-storage: device scan complete
[83406.389480] scsi 4:0:0:0: Direct-Access     Corsair  UFD              1100 PQ: 0 ANSI: 0 CCS
[83406.389923] sd 4:0:0:0: Attached scsi generic sg2 type 0
[83406.392468] sd 4:0:0:0: [sdb] 7864320 512-byte logical blocks: (4.02 GB/3.75 GiB)
[83406.393211] sd 4:0:0:0: [sdb] Write Protect is off
[83406.393214] sd 4:0:0:0: [sdb] Mode Sense: 43 00 00 00
[83406.393216] sd 4:0:0:0: [sdb] Assuming drive cache: write through
[83406.395834] sd 4:0:0:0: [sdb] Assuming drive cache: write through
[83406.395838]  sdb: sdb1
[83406.458088] sd 4:0:0:0: [sdb] Assuming drive cache: write through
[83406.458093] sd 4:0:0:0: [sdb] Attached SCSI removable disk
...

或者只是:如何禁用给定 USB 端口的记录?

答案1

以 root 用户身份每分钟运行此操作以清除给定端口的日志:

#!/bin/bash

tmpfile="$(mktemp)"

sync; sync; sync
fgrep -iIR "] usb 2-3: " /var/log/* 2>/dev/null | fgrep -i " and address " | awk '{ print $NF }' | sort -u | while read i; do 
    egrep -iIR "] usb-storage: waiting|] usb-storage: device |usb-storage: device found at $i| scsi$i : SCSI emulation for|] scsi $i:|] sd $i:" /var/log/* 2>/dev/null
    grep -iIR "] sd $i:0:0:0: \[*.*\] Write P" /var/log/* 2>/dev/null | sed 's/\[//g; s/\]//g' | awk '{print $9}' | sort -u | while read ONEDEV; do grep -iIR "$ONEDEV: " /var/log/* 2>/dev/null; done
    fgrep -iIR "] usb 2-3: " /var/log/* 2>/dev/null
    done > "$tmpfile"

    while read ONELINE; do 
        FILENAME="`echo "$ONELINE" | cut -d : -f 1`"
        STRING="`echo "$ONELINE" | sed 's/:/\n/' | fgrep -v "/var/log" | sed 's/\[/\\\[/g' | sed 's/\]/\\\\]/g' | sed 's/\//\\\\\\//g'`"
        echo "$FILENAME XXX $STRING"
        sed -i "/$STRING/d" "$FILENAME"
    done < "$tmpfile"
    rm "$tmpfile"

dmesg -c > /dev/null

相关内容