Linux下的SCSI设备都是按照设备的发现顺序命名的,对于磁盘盘符名称就是sda,sdb,sdc等等。存储设备一般都配有很多块磁盘,盘符名称和槽位号没有对应关系,当磁盘插入,拔出时对应的盘符可能会变化。这给使用和运维带来很多不便。
对于SATA磁盘可通过修改sd_mod模块中的sd_probe(drivers/scsi/sd.c)来完成“磁盘槽位和盘符”的绑定。
/**
*sd_probe - called during driver initialization and whenever a
*new scsi device is attached to the system. It is called once
*for each scsi device (not just disks) present.
*@dev: pointer to device object
*
*Returns 0 if successful (or not interested in this scsi device
*(e.g. scanner)); 1 when there is an error.
*
*Note: this function is invoked from the scsi mid-level.
*This function sets up the mapping between a given
* (found in sdp) and new device name
*(e.g. /dev/sda). More precisely it is the block device major
*and minor number that is chosen here.
*
*Assume sd_attach is not re-entrant (for time being)
*Also think about sd_attach() and sd_remove() running coincidentally.
**/
static int sd_probe(struct device *dev)
{
struct scsi_device *sdp = to_scsi_device(dev);
struct scsi_disk *sdkp;
struct gendisk *gd;
u32 index;
int error;
error = -ENODEV;
if (sdp->type != TYPE_DISK && sdp->type != TYPE_MOD && sdp->type != TYPE_RBC)
goto out;
SCSI_LOG_HLQUEUE(3, sdev_printk(KERN_INFO, sdp,
"sd_attach\n"));
error = -ENOMEM;
sdkp = kzalloc(sizeof(*sdkp), GFP_KERNEL);
if (!sdkp)
goto out;
gd = alloc_disk(SD_MINORS);
if (!gd)
goto out_free;
do {
if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
goto out_put;
spin_lock(&sd_index_lock);
error = ida_get_new(&sd_index_ida, &index);
spin_unlock(&sd_index_lock);
} while (error == -EAGAIN);
if (error)
goto out_put;
if (index >= SD_MAX_DISKS) {
error = -ENODEV;
sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name space exhausted.\n");
goto out_free_index;
}
error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
if (error)
goto out_free_index;
......
}
具体负责给gendisk分配磁盘名称的就是 sd_format_disk_name,只需修改这个函数即可。为了完成槽位和盘符绑定我们需要一个对应表,这张表可以根据系统中的一些不变量,如磁盘的SCSI host号来确定对应关系。
如下是笔者根据scsi host(针对某款特定主板)确定盘符对应关系的Patch:
error = sd_format_disk_name("sd", sdp->host->host_no, gd->disk_name, DISK_NAME_LEN);
static int sd_format_disk_name(char *prefix, int host_no, char *buf, int buflen)
{
char *begin = buf + strlen(prefix);
*begin = 'a' + host_no;
*(begin + 1) = '\0';
memcpy(buf, prefix, strlen(prefix));
return 0;
}
作者:sage meng
出处:http://blog.csdn.net/lkkey80/article/details/50263083