4

I am trying to debug a situation where the SSD on my hardware is not being detected by the right device driver. The device driver that should own the SSD's is a software RAID driver (megasr) that will automically configure 2 SSD's in mirroring mode. I am pretty sure that the megasr driver is not detecting/owning the SSD's, but I am unsure which driver actually detects them.

Is there a way in Linux that I can determine which device driver owns a particular disk inside the /dev directory.

So I would like to determine which driver owns the device /dev/sda, for example.

The OS is RHEL 6.x.

Many thanks.

Shyamal Pandya
  • 532
  • 3
  • 16

2 Answers2

7

You can see the kernel log

dmesg | grep sda
<...>
[    0.618438] sd 2:0:0:0: [sda] Attached SCSI disk
<...>

The log is talking about a scsi disk, and sd 2:0:0:0 is the device that generated that message. You can inspect the sysfs for more details:

cd /sys/block/
cd /sys/block/sda/

Here you can find the information about all your block devices. If you look into the directory of your specific device you should see its information.

Here the information about the scsi bus. There are two directories: drivers and devices.

cd /sys/bus/scsi
cd /sys/bus/scsi/devices
cd /sys/bus/scsi/drivers

Here there is the list of drivers on the scsi bus.

ls /sys/bus/scsi/drivers
 sd  sr

The log said sd 2:0:0:0

cd /sys/bus/scsi/drivers/sd/2:0:0:0

Here my device, so sd is my driver.

The disk is a block device, you should see the directory block

cd /sys/bus/scsi/drivers/sd/2:0:0:0/block/sda

Probably there is a program that do this automatically :)

If the wrong driver is handling your device you can unbind the device from that driver, and you can bind it to another one.

ls /sys/bus/scsi/drivers/sd
 2:0:0:0  bind  uevent  unbind

You can write the device identifier on the unbind file to detach that device from the driver.

echo -n "2:0:0:0" > unbind

Then you can attach your device to another driver

cd /sys/bus/scsi/drivers/<a-driver>
echo -n "2:0:0:0" > bind

Obviously (1), you cannot bind a scsi device to non scsi driver. Obviously (2), this is not the answer for your specific problem, but it is a way to retrieve the information that you need to resolve the problem

Federico
  • 3,782
  • 32
  • 46
1
  • Get the SCSI host id using SCSI_IOCTL_GET_BUS_NUMBER(0x5386) IOCTL.
  • The /sys/class/scsi_host/host<scsi host id>/proc_name file contains the device driver.

Script:

#!/usr/bin/python
# Copyright 2016 Gris Ge <cnfourt@gmail.com>
# Licensed to the public domain.

from array import array
from fcntl import ioctl
import sys
import os

SCSI_IOCTL_GET_BUS_NUMBER = 0x5386
SYSFS_SCSI_HOST_DRV_FORMAT = "/sys/class/scsi_host/host{HOST_ID}/proc_name"

def get_scsi_host_id(path):
    fd = open(path, "wb")
    buff = array('i', [0])
    ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, buff, 1)
    fd.close()
    return int(buff[0])

def main():
    scsi_host_id = get_scsi_host_id(sys.argv[1])
    os.system("cat %s" %
              SYSFS_SCSI_HOST_DRV_FORMAT.format(**{"HOST_ID":scsi_host_id}))

main()

Example:

[fge@Gris-Laptop source]$ sudo python find_driver_of_sdx.py /dev/sda
ahci
Gris Ge
  • 46
  • 3