我用udev枚举scsi_generic设备以检索/dev/sg*文件名,这样我就可以向每个设备发送一个查询。
当我在那里的时候,我还能得到/dev/sd*文件名( scsi_device)吗?
如果没有,我如何获得scsi_device的scsi_generic路径?
这是我的udev代码:
IntPtr udev = IntPtr.Zero;
IntPtr enumerate = IntPtr.Zero;
try
{
udev = Libudev.udev_new();
if (udev == IntPtr.Zero)
throw new UdevException("Failed to create udev");
enumerate = Libudev.udev_enumerate_new(udev);
if (enumerate == IntPtr.Zero)
throw new UdevException("Failed to enumerate udev");
Libudev.udev_enumerate_add_match_subsystem(enumerate, "scsi_generic");
Libudev.udev_enumerate_scan_devices(enumerate);
for (IntPtr listEntry = Libudev.udev_enumerate_get_list_entry(enumerate);
listEntry != IntPtr.Zero;
listEntry = Libudev.udev_list_entry_get_next(listEntry))
{
// Get the filename of the /sys entry for the device
string deviceSysPath = Libudev.udev_list_entry_get_name(listEntry);
IntPtr device = Libudev.udev_device_new_from_syspath(udev, deviceSysPath);
if (device == IntPtr.Zero)
continue;
// Get the /dev path of the device (/dev/sg*)
string deviceDevPath = Libudev.udev_device_get_devnode(device);
// Send Inquiry
// <snip>
Libudev.udev_device_unref(device);
}
}
catch (DllNotFoundException ex)
{
throw new DllNotFoundException("libudev is either not installed, or ldconfig has mapped it in an unexpected way. See source README file for information.", ex);
}
finally
{
if (enumerate != IntPtr.Zero)
Libudev.udev_enumerate_unref(enumerate);
if (udev != IntPtr.Zero)
Libudev.udev_unref(udev);
}发布于 2014-06-30 15:32:14
我通过执行两个枚举来解决这个问题。首先通过枚举scsi_generic设备,然后通过block设备。
我创建了一个哈希表(Dictionary< string, string >)来将通用设备的sysname映射到它的devnode。
在枚举scsi_generic设备时,我得到父设备(udev_device_get_parent),然后获取sysname (udev_device_get_sysname)并将其添加到映射中。对于SCSI磁盘,您可以得到类似于“9:0:0:0:0”的内容。
在枚举block设备时,我也会这样做:获取父级和sysname。如果哈希表包含相同的键,则已找到与块设备匹配的通用设备。
发布于 2014-06-26 18:01:17
可以使用查询命令检索设备序列号,然后使用序列号在/dev/disk/by-id/中查找关联的块设备。
$ sudo sg_inq --page=sn /dev/sg0
VPD INQUIRY: Unit serial number page
Unit serial number: 110427E3834563H46N0N
$ basename $(readlink /dev/disk/by-id/scsi-SATA_*110427E3834563H46N0N)
sdahttps://stackoverflow.com/questions/24436808
复制相似问题