[英]Way to get SCSI disk names in Linux C++ application
In my Linux C++ application I want to get names of all SCSI disks which are present on the system. 在我的Linux C ++应用程序中,我想获取系统上存在的所有SCSI磁盘的名称。 eg /dev/sda, /dev/sdb, ... and so on. 例如/ dev / sda,/ dev / sdb等。
Currently I am getting it from the file /proc/scsi/sg/devices output using below code: 目前,我正在使用以下代码从文件/ proc / scsi / sg / devices输出中获取它:
host chan SCSI id lun type opens qdepth busy online 0 0 0 0 0 1 128 0 1 1 0 0 0 0 1 128 0 1 1 0 0 1 0 1 128 0 1 1 0 0 2 0 1 128 0 1
// If SCSI device Id is > 26 then the corresponding device name is like /dev/sdaa or /dev/sdab etc.
if (MAX_ENG_ALPHABETS <= scsiId)
{
// Device name order is: aa, ab, ..., az, ba, bb, ..., bz, ..., zy, zz.
deviceName.append(1, 'a'+ (char)(index / MAX_ENG_ALPHABETS) - 1);
deviceName.append(1, 'a'+ (char)(index % MAX_ENG_ALPHABETS));
}
// If SCSI device Id is < 26 then the corresponding device name is liek /dev/sda or /dev/sdb etc.
else
{
deviceName.append(1, 'a'+ index);
}
But the file /proc/scsi/sg/devices also contains the information about the disk which were previously present on the system. 但是文件/ proc / scsi / sg / devices也包含有关系统上先前存在的磁盘的信息。 eg If I detach the disk (LUN) /dev/sdc from the system the file /proc/scsi/sg/devices still contains info of /dev/sdc which is invalid. 例如,如果我从系统中分离了磁盘(LUN)/ dev / sdc,则文件/ proc / scsi / sg / devices仍包含无效的/ dev / sdc信息。
Tell me is there any different way to get the SCSI disk names? 告诉我是否有其他方法来获取SCSI磁盘名称? like a system call? 像系统调用一样?
Thanks 谢谢
You can simply read list of all files like /dev/sd*
(in C, you would need to use opendir
/ readdir
/ closedir
) and filter it by sdX
(where X is one or two letters). 您可以简单地读取所有文件的列表,例如/dev/sd*
(在C中,您将需要使用opendir
/ readdir
/ closedir
)并用sdX
过滤(其中X是一个或两个字母)。
Also, you can get list of all partitions by reading single file /proc/partitions
, and then filter 4th field by sdX
: 另外,您可以通过读取单个文件/proc/partitions
来获取所有分区的列表,然后通过sdX
过滤第四个字段:
$ cat /proc/partitions
major minor #blocks name
8 0 52428799 sda
8 1 265041 sda1
8 2 1 sda2
8 5 2096451 sda5
8 6 50066541 sda6
which would give you list of all physical disks together with their capacity (3rd field). 这将为您提供所有物理磁盘的列表及其容量(第三个字段)。
After get disk name list from /proc/scsi/sg/devices, you can verify the existence through code. 从/ proc / scsi / sg / devices获取磁盘名称列表后,可以通过代码验证是否存在。 For example, install sg3-utils, and use sg_inq to query whether the disk is active. 例如,安装sg3-utils,然后使用sg_inq查询磁盘是否处于活动状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.