LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 08-15-2009, 12:00 PM   #1
mholden64
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
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:

a) I grab the target of the following symlink:

>ls -l /sys/block/sdb/device
lrwxrwxrwx 1 root root 0 Aug 15 10:32 /sys/block/sdb/device -> ../../devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-1/1-1:1.0/host1/target1:0:0/1:0:0:0

and resolve it to it's absolute path:

/sys/devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-1/1-1:1.0/host1/target1:0:0/1:0:0:0

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:

>ls -l /sys/bus/usb/devices
total 0
lrwxrwxrwx 1 root root 0 Aug 15 10:18 1-0:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-0:1.0
lrwxrwxrwx 1 root root 0 Aug 15 10:35 1-1 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-1
lrwxrwxrwx 1 root root 0 Aug 15 10:35 1-1:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-1/1-1:1.0
lrwxrwxrwx 1 root root 0 Aug 15 10:18 2-0:1.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-0:1.0
lrwxrwxrwx 1 root root 0 Aug 15 10:18 usb1 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1
lrwxrwxrwx 1 root root 0 Aug 15 10:18 usb2 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2

and resolve them to their absolute paths:

/sys/devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-0:1.0
/sys/devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-1
/sys/devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1/1-1/1-1:1.0
/sys/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-0:1.0
/sys/devices/pci0000:00/0000:00:11.0/0000:02:04.0/usb1
/sys/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2

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).

>ls -l /sys/block/sdb/device
lrwxrwxrwx 1 root root 0 2009-08-10 20:49 /sys/block/sdb/device -> ../../../4:0:0:0

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.

I then tried the following:

>cd /sys/block/sdb
>ls -l device
lrwxrwxrwx 1 root root 0 2009-08-10 21:23 device -> ../../../4:0:0:0
>cd ../../../4:0:0:0
>pwd
/sys/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0

The above path is the result I want and will make the algorithm as I described for RHEL5 work in Fedora 10 (RHEL6) as well.

Making it a bit more "productized" for inclusion into a script:

>ls -l /sys/block/sdb/device
lrwxrwxrwx 1 root root 0 2009-08-10 21:23 /sys/block/sdb/device -> ../../../4:0:0:0

>pushd . > /dev/null; cd /sys/block/sdb/../../../4:0:0:0; pwd; popd > /dev/null
/sys/devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host4/target4:0:0/4:0:0:0

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)?


Thanks,
Mark
 
Old 08-15-2009, 12:28 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
Old 08-15-2009, 07:31 PM   #3
mholden64
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Original Poster
Rep: Reputation: 0
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?

Thanks,
Mark
 
Old 08-15-2009, 07:44 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
Old 08-15-2009, 07:45 PM   #5
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
dupe

Last edited by GrapefruiTgirl; 08-15-2009 at 07:46 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB drive not working properly, read only device? (USB key storage device) Arodef Linux - Hardware 14 01-01-2010 07:32 AM
Using udev to tell Ubuntu to use the proper device on a multiple USB device Jeff91 Linux - Hardware 20 08-20-2009 12:10 PM
Determine usb flash memory stick scsi device ? Vilius Linux - Hardware 1 01-29-2009 12:24 AM
How to force kernel to use a USB 2.0-compatible device (ehci_hcd) as a USB 1.1 device eze Linux - Hardware 0 05-16-2006 05:24 AM
determine type of device for USB devices lyar1031 Linux - Newbie 2 07-29-2004 08:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 07:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration