Method to determine if a device is a usb device (RHEL5 / RHEL6)
Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Method to determine if a device is a usb device (RHEL5 / RHEL6)
I have a couple of questions I was hoping somebody could help me with. This needs some background context and is a little lengthy, so I broke it into two headings below--"Background" and "My Questions".
I support RHEL-based customized linux system installers (and runtime) at my work. I have a requirement where, given a device name such as "/dev/sdb", I need to determine where or not it is a USB device (attached to a USB bus). I need to be able to do this without the use of udev, since I need to support this during installation time (where udev is not running).
I need to do this in RHEL5, but also need to make sure it works for RHEL6 for when it becomes available in the future. I have been told by Redhat that RHEL6 will be based on Fedora 10/11. Note that I'm currently testing this on RHEL5 kernel 2.6.18-128.1.6.el5 and the Fedora 10 kernel 22.214.171.124-117.fc10.i686.
I have a method which seems to work for RHEL5, and is as follows, using /dev/sdb to illustrate:
So, it would seem that there is something magical about the target of the /sys/block/sdb/device symlink. If you do straight "ls -l" on it, you get an abbreviated relative path; if you follow the actual link and do a "pwd" you get the full device path. The way I see it, I have to use the method describe just above (following the link instead of just using "ls -l").
- What is this piece of magic that is happening in RHEL6 with the symlink?
- Is my method of following the symlink (i.e., "cd" then "pwd") a valid one? Am I missing something? I would like one algorithm that I would like to implement right now and have it work for both RHEL5 and RHEL6 (I don't want to revisit this in the future). The approach described above for Fedora 10 does work in RHEL5 as well and so it would be the basis of the algorithm.
- Is my overall algorithm for determining if a device on the usb bus valid? (ie., comparing links from /sys/block/<dev>/device and /sys/bus/usb)?
Have you tried the `lsscsi` command, provided of course that you (can) have it available at the point at which you need it?
At very least, I believe lsscsi does not depend on udev. It queries sysfs as you have.
I haven't yet fully analyzed what you've posted above, but if `lsscsi` does not (or isn't available) give you what you need, please post again.
bash-3.1# lsscsi -v
[0:0:0:0] cd/dvd PIONEER DVD-RW DVR-212D 1.24 /dev/sr0
dir: /sys/bus/scsi/devices/0:0:0:0 [/sys/devices/pci0000:00/0000:00:0e.0/host0/target0:0:0/0:0:0:0]
[4:0:0:0] disk Generic USB SD Reader 1.00 /dev/sda
dir: /sys/bus/scsi/devices/4:0:0:0 [/sys/devices/pci0000:00/0000:00:0b.1/usb1/1-7/1-7:1.0/host4/target4:0:0/4:0:0:0]
[4:0:0:1] disk Generic USB CF Reader 1.01 /dev/sdb
dir: /sys/bus/scsi/devices/4:0:0:1 [/sys/devices/pci0000:00/0000:00:0b.1/usb1/1-7/1-7:1.0/host4/target4:0:0/4:0:0:1]
[4:0:0:2] disk Generic USB SM Reader 1.02 /dev/sdc
dir: /sys/bus/scsi/devices/4:0:0:2 [/sys/devices/pci0000:00/0000:00:0b.1/usb1/1-7/1-7:1.0/host4/target4:0:0/4:0:0:2]
[4:0:0:3] disk Generic USB MS Reader 1.03 /dev/sdd
dir: /sys/bus/scsi/devices/4:0:0:3 [/sys/devices/pci0000:00/0000:00:0b.1/usb1/1-7/1-7:1.0/host4/target4:0:0/4:0:0:3]
Last edited by GrapefruiTgirl; 08-15-2009 at 01:33 PM.
Reason: changed lsusb to lsscsi.
Thanks for the reply...I just checked both of my test machines and neither seem to have the lsscsi command. From looking at the output in your post, I'm not quite sure what to depend on. I know that these commands often read data from the device and include it in the output. I tend to not want to rely on that data, but more on what the kernel itself provides. Do you know if this data is kernel-provided or read from the device?
The information is kernel provided, and is derived from sysfs.
From the man page:
Information for this command is derived from the sysfs file system whose mount point is
found by examining the contents of /proc/mounts . SCSI (pseudo) devices that have been
detected by the SCSI mid level will be listed even if the required upper level drivers (i.e.
sd, sr, st, osst or ch) have not been loaded. If the appropriate upper level driver has not
been loaded then the device file name will appear as '-' rather than something like
'/dev/st0'. Note that some devices (e.g. scanners and medium changers) do not have a primary
upper level driver and can only be accessed via a scsi generic (sg) device name.
lsscsi version 0.21 or later is required to correctly display SCSI devices in linux kernel
2.6.26 (and possibly later) when the CONFIG_SYSFS_DEPRECATED_V2 kernel option is not
Uses information in sysfs (linux kernel series 2.6 and later) to list scsi devices (or
hosts) currently attached to the system. Options can be used to control the amount and form
of information provided for each device.
You could do something like:
bash-3.1# lsscsi -v | grep sda | grep -o USB
Last edited by GrapefruiTgirl; 08-15-2009 at 08:49 PM.