![内核、udev 和 DBus 究竟如何挂载外部设备?](https://linux22.com/image/951693/%E5%86%85%E6%A0%B8%E3%80%81udev%20%E5%92%8C%20DBus%20%E7%A9%B6%E7%AB%9F%E5%A6%82%E4%BD%95%E6%8C%82%E8%BD%BD%E5%A4%96%E9%83%A8%E8%AE%BE%E5%A4%87%EF%BC%9F.png)
我试图了解 SD 卡在 Ubuntu 中卸载/弹出的具体工作原理,以便调试此问题的上下文: https://bugs.launchpad.net/ubuntu/+source/gvfs/+bug/1980080
看了此主题获取基本信息:
kernel/linux → sysfs → udev → udisks2 → GVolumeMonitor → desktop-manager (usually, same as the file manager).
udisks2 is the first daemon to provide an API through DBUS
现在我关心的是,当通过 Gnome 按下弹出按钮或eject /dev/mmcblk0
从命令行启动命令时,在正常工作的系统上实际上应该发生什么。在我的情况下,无论分区是否已安装,eject
命令都会失败并显示错误代码。然后包括以下几行:1
/dev/mmcblk0p1
strace eject /dev/mmcblk0
openat(AT_FDCWD, "/dev/mmcblk0", O_RDWR|O_EXCL|O_NONBLOCK) = 3
ioctl(3, CDROM_LOCKDOOR, 0) = -1 EINVAL (Invalid argument)
ioctl(3, SG_GET_VERSION_NUM, 0x7ffd53f9b2e8) = -1 EINVAL (Invalid argument)
ioctl(3, FDEJECT, 0x55874d4359d0) = -1 EINVAL (Invalid argument)
ioctl(3, MTIOCTOP, 0x7ffd53f9b320) = -1 EINVAL (Invalid argument)
write(2, "eject: ", 7eject: ) = 7
write(2, "unable to eject", 15unable to eject) = 15
想知道这是否意味着只有那些可以通过物理和程序弹出的设备(如 CDROM 驱动器)的退出代码才为 0。以及其他用户空间程序(如不同的 Gnome 服务)如何解释此输出。
udevadm monitor
显示以下几行:
已插入卡
KERNEL[49421.254375] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
KERNEL[49421.255242] add /devices/virtual/bdi/179:0 (bdi)
UDEV [49421.256281] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
UDEV [49421.256354] add /devices/virtual/bdi/179:0 (bdi)
KERNEL[49421.264022] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
KERNEL[49421.264062] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
KERNEL[49421.264093] bind /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
UDEV [49421.321281] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
UDEV [49421.409280] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
UDEV [49421.413271] bind /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
Gnome 中的弹出按钮:
KERNEL[49428.212348] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
KERNEL[49428.213105] change /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
KERNEL[49428.213189] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
UDEV [49428.215677] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
UDEV [49428.242018] change /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
UDEV [49428.310305] add /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
在上面我最关心的是第三行内核重新添加设备的原因。
卡被物理移除:
KERNEL[49432.237855] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
KERNEL[49432.238271] remove /devices/virtual/bdi/179:0 (bdi)
KERNEL[49432.238678] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
UDEV [49432.241920] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
UDEV [49432.242568] remove /devices/virtual/bdi/179:0 (bdi)
UDEV [49432.245293] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
KERNEL[49432.269966] unbind /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
KERNEL[49432.270065] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
UDEV [49432.271890] unbind /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)
UDEV [49432.273292] remove /devices/pci0000:00/0000:00:1d.0/0000:72:00.0/rtsx_pci_sdmmc.0/mmc_host/mmc0/mmc0:aaaa (mmc)