Method to determine if a device is a usb device (RHEL5 / RHEL6)
Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Method to determine if a device is a usb device (RHEL5 / RHEL6)
Hi,
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".
Background
---------
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 2.6.27.5-117.fc10.i686.
I have a method which seems to work for RHEL5, and is as follows, using /dev/sdb to illustrate:
Upon inspection of the target, I see the path element "usb1" and so can infer that it is on a usb bus. But I prefer to be more determinstic (strings can change I suppose), so I continue with step (b).
b) I list all "usb bus" type symlinks on the system:
If at least one of the absolute symlink paths just above matches the first part of the absolute path from (a), the I conclude that the device (/dev/sdb) must be on a usb bus.
When I try to apply the same algorithm in Fedora 10 (RHEL6), things are a little different in step (a).
Note the considerably shorter target of the symlink. I can't infer anything about USB from this path, and cannot compare it against the symlinks found in /sys/bus/usb.
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").
My Questions
-----------
- 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.
Sample:
Code:
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]
bash-3.1#
Thanks,
Sasha
Last edited by GrapefruiTgirl; 08-15-2009 at 12: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:
Code:
NOTES
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
defined.
DESCRIPTION
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:
Code:
bash-3.1# lsscsi -v | grep sda | grep -o USB
USB
bash-3.1#
Last edited by GrapefruiTgirl; 08-15-2009 at 07:49 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.