我喜欢Fabby的方法,但是最好教人们这个坏习惯(他们并不是一直在受控机器上工作)。这是类似于OSX功能,如所描述这里。
您可以使用它来知道它是否可以安全删除,这之间有一个小的区别:
通过监视udev事件注意到:
udevadm monitor -u --environment
安全移除
UDEV [8292.380554] change /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/host16/target16:0:0/16:0:0:0/block/sdb (block)
ACTION=change
DEVLINKS=/dev/disk/by-id/usb-ADATA_USB_Flash_Drive_8d90ec535e6663-0:0 /dev/disk/by-path/pci-0000:00:14.0-usb-0:2:1.0-scsi-0:0:0:0
DEVNAME=/dev/sdb
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/host16/target16:0:0/16:0:0:0/block/sdb
DEVTYPE=disk
DISK_MEDIA_CHANGE=1
ID_BUS=usb
ID_INSTANCE=0:0
ID_MODEL=USB_Flash_Drive
ID_MODEL_ENC=USB\x20Flash\x20Drive\x20
ID_MODEL_ID=c96a
ID_PATH=pci-0000:00:14.0-usb-0:2:1.0-scsi-0:0:0:0
ID_PATH_TAG=pci-0000_00_14_0-usb-0_2_1_0-scsi-0_0_0_0
ID_REVISION=0.00
ID_SERIAL=ADATA_USB_Flash_Drive_8d90ec535e6663-0:0
ID_SERIAL_SHORT=8d90ec535e6663
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=ADATA
ID_VENDOR_ENC=ADATA\x20\x20\x20
ID_VENDOR_ID=125f
MAJOR=8
MINOR=16
SEQNUM=2989
SUBSYSTEM=block
TAGS=:systemd:
USEC_INITIALIZED=554873
不安全删除
UDEV [8391.320280] remove /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host17/target17:0:0/17:0:0:0/block/sdb (block)
ACTION=remove
DEVLINKS=/dev/disk/by-id/usb-ADATA_USB_Flash_Drive_8d90ec535e6663-0:0 /dev/disk/by-path/pci-0000:00:14.0-usb-0:3:1.0-scsi-0:0:0:0
DEVNAME=/dev/sdb
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host17/target17:0:0/17:0:0:0/block/sdb
DEVTYPE=disk
ID_BUS=usb
ID_INSTANCE=0:0
ID_MODEL=USB_Flash_Drive
ID_MODEL_ENC=USB\x20Flash\x20Drive\x20
ID_MODEL_ID=c96a
ID_PART_TABLE_TYPE=dos
ID_PATH=pci-0000:00:14.0-usb-0:3:1.0-scsi-0:0:0:0
ID_PATH_TAG=pci-0000_00_14_0-usb-0_3_1_0-scsi-0_0_0_0
ID_REVISION=0.00
ID_SERIAL=ADATA_USB_Flash_Drive_8d90ec535e6663-0:0
ID_SERIAL_SHORT=8d90ec535e6663
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=ADATA
ID_VENDOR_ENC=ADATA\x20\x20\x20
ID_VENDOR_ID=125f
MAJOR=8
MINOR=16
SEQNUM=3022
SUBSYSTEM=block
TAGS=:systemd:
USEC_INITIALIZED=436355
创建udev规则(更改useranme
)
$ sudo nano /etc/udev/rules.d/90-unsafe-remove-notify.rules
ACTION=="remove", KERNEL=="sd?", ENV{ID_PART_TABLE_TYPE}!="", RUN+="/usr/bin/sudo -u username DISPLAY=:0 notify-send 'Unsafe Remove' '<b><i>Your long message</b></i>' -i /usr/share/icons/gnome/48x48/emotes/face-worried.png -t 10000"
重新加载规则
sudo udevadm control --reload-rules
另一种方法,您可以使用连接到udisks
DBUS 的脚本(python)。它具有您需要的有关分区安装/卸载,磁盘插入/拔出的所有必需信息...
参考/来源: Gentoo Wiki:Udisks-USB_Thumb_Drive_Example
df -a | grep 'sd[b-z]'
命令监视USB的安装/卸载时间。udev规则可以将删除日期发送到某种日志。如果脚本给出的删除时间与udev规则的删除时间匹配(至少按分钟计算,可能会有几秒钟的延迟),则不会挂载usb。