Detecting USB drives via sysfs

Post there if you plan to contribute to this SystemCd
stsquad
Posts: 4
Joined: 17 Jun 2010, 14:08

Detecting USB drives via sysfs

Postby stsquad » 08 Jul 2010, 10:21

Hi,

The usb_inst.sh script doesn't detect my USB key. It fails because it checks the sysfs device node /sys/block/sdh/removable which reports it as not removable.

I don't think this flag means what the script thinks it means. In the kernel tree this maps to the flag GENHD_FL_REMOVABLE which isn't set for USB devices AFAICT. The documentation of the meaning is a little sparse but from the MMC code:

* As discussed on lkml, GENHD_FL_REMOVABLE should:
*
* - be set for removable media with permanent block devices
* - be unset for removable block devices with permanent media
*
* Since MMC block devices clearly fall under the second
* case, we do not set GENHD_FL_REMOVABLE. Userspace
* should use the block device creation/destruction hotplug
* messages to tell when the card is present.

USB keys also fall under the second category of being block devices that are destroyed on removal (unlike the card readers on my machine which exist even when no media is in them).

I'm looking around to see if there is another check that can be performed but the current check is IMHO broken.

Regards,

Alex

stsquad
Posts: 4
Joined: 17 Jun 2010, 14:08

Re: Detecting USB drives via sysfs

Postby stsquad » 08 Jul 2010, 11:52

OK, the following (ugly) patch seems to work for me:

Code: Select all

--- /mnt/iso/usb_inst.sh   2010-06-28 20:36:16.000000000 +0100
+++ /export/imgs/usb_inst.sh   2010-07-08 12:50:32.000000000 +0100
@@ -216,10 +216,11 @@
 {
    curdev="$1"
    
-   remfile="/sys/block/${curdev}/removable"
+   devlink=`readlink /sys/block/${curdev}/device`
    vendor="$(cat /sys/block/${curdev}/device/vendor 2>/dev/null)"
    model="$(cat /sys/block/${curdev}/device/model 2>/dev/null)"
-   if [ -f "${remfile}" ] && cat ${remfile} 2>/dev/null | grep -qF '1' \
+
+   if [ -L "/sys/block/${curdev}/device" ] && echo ${devlink} | grep -qF 'usb' \
       && cat /sys/block/${curdev}/device/uevent 2>/dev/null | grep -qF 'DRIVER=sd'
    then
       return 0
@@ -406,7 +407,7 @@
             mbsizeofdev="$((secsizeofdev/2048))"
             sizemsg=" and size=${mbsizeofdev}MB"
          fi
-         echo "Device [${devname}] detected as [${vendor} ${model}] is removable${sizemsg}"
+         echo "Device [${devname}] detected as [${vendor} ${model}] ${sizemsg}"
          if is_dev_mounted "${devname}"
          then
             echo "* Device [${devname}] is mounted: cannot use it"
@@ -423,12 +424,12 @@
    done
    if [ ${devsallcnt} -eq 0 ]
    then
-      echo "No valid USB/Removable device has been detected on your system"
+      echo "No valid USB device has been detected on your system"
       return 1   
    fi
    if [ ${devsokcnt} -eq 0 ]
    then
-      echo "All valid USB/Removable devices are currently mounted, unmount these devices first"
+      echo "All valid USB devices are currently mounted, unmount these devices first"
       return 1
    fi
 
@@ -513,7 +514,7 @@
             mbsizeofdev="$((secsizeofdev/2048))"
             sizemsg=" and size=${mbsizeofdev}MB"
          fi   
-         echo "Device [${devname}] detected as [${vendor} ${model}] is removable${sizemsg}"
+         echo "Device [${devname}] detected as [${vendor} ${model}] ${sizemsg}"
          if is_dev_mounted "${devname}"
          then
             echo "Device [${devname}] is mounted"

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Re: Detecting USB drives via sysfs

Postby admin » 08 Jul 2010, 18:02

I could be happy if we can improve that, but the assumption based on the link that contains "usb" is just wrong

# readlink /sys/block/sde/device
../../../10:0:0:0

# cat /sys/block/sde/removable
1

stsquad
Posts: 4
Joined: 17 Jun 2010, 14:08

Re: Detecting USB drives via sysfs

Postby stsquad » 09 Jul 2010, 14:42

What sort of USB stick is that device? Is it using the standard usb mass storage driver or some special variant?

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Re: Detecting USB drives via sysfs

Postby admin » 28 Jul 2010, 11:43

It's just a standard 8GB USB stick: (ByteStor 8GB)

Code: Select all

usb 1-7: new high speed USB device using ehci_hcd and address 4
usb 1-7: New USB device found, idVendor=1516, idProduct=1213
usb 1-7: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-7: Product: DISK 2.0
usb 1-7: Manufacturer: USB
usb 1-7: SerialNumber: 001731583EF7A9B142CF7AC8
scsi5 : usb-storage 1-7:1.0
scsi 5:0:0:0: Direct-Access     USB      DISK 2.0         1.00 PQ: 0 ANSI: 2
sd 5:0:0:0: Attached scsi generic sg6 type 0
sd 5:0:0:0: [sdf] 16035840 512-byte logical blocks: (8.21 GB/7.64 GiB)
sd 5:0:0:0: [sdf] Write Protect is off
sd 5:0:0:0: [sdf] Mode Sense: 23 00 00 00
sd 5:0:0:0: [sdf] Assuming drive cache: write through
sd 5:0:0:0: [sdf] Assuming drive cache: write through

stsquad
Posts: 4
Joined: 17 Jun 2010, 14:08

Re: Detecting USB drives via sysfs

Postby stsquad » 19 Aug 2010, 12:51

Perhaps querying udev would acceptable?

Code: Select all

>udevinfo --query=all --name=/dev/sdh
P: /block/sdh
N: sdh
S: disk/by-id/usb-USB_Flash_Disk_AA09021200031594-0:0
S: disk/by-path/pci-0000:00:1a.7-usb-0:1.3:1.0-scsi-0:0:0:0
E: DEVTYPE=disk
E: ID_VENDOR=USB
E: ID_MODEL=Flash_Disk
E: ID_REVISION=1100
E: ID_SERIAL=USB_Flash_Disk_AA09021200031594-0:0
E: ID_SERIAL_SHORT=AA09021200031594
E: ID_TYPE=disk
E: ID_INSTANCE=0:0
E: ID_BUS=usb
E: ID_PATH=pci-0000:00:1a.7-usb-0:1.3:1.0-scsi-0:0:0:0


Return to “Contributions”

Who is online

Users browsing this forum: No registered users and 0 guests