4

There's any way to detect if a block device, like /dev/sda or /dev/sdc, is related to a local disk (scsi or sata I mean) or to a removable USB disk?

I'm writing a shell script that have to detect ONLY local disks block devices, excluding any removable disks.

Thanks!

Andrea
  • 87
  • 2
  • 9

3 Answers3

3

You can use udev, the Linux device manager.

Querying for each block device will show several informations about it, including the bus, which you can use in order to discern if the device is a removable USB one.

This is the script:

for device in /sys/block/sd*; do
  device_info="$(udevadm info --query=property --path=$device)"

  device_name=$(echo "$device_info" | perl -ne 'print "$1" if /^DEVNAME=(.*)/')
  device_bus=$(echo "$device_info" | perl -ne 'print "$1" if /^ID_BUS=(.*)/')

  echo "Device $device_name bus: $device_bus"
done

and this is a sample result:

Device /dev/sda bus: ata
Device /dev/sdb bus: ata
Device /dev/sdc bus: usb
Marcus
  • 5,104
  • 2
  • 28
  • 24
2

Use lshw:

lshw -class disk -class storage

and look for the bus info string.

ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
0

I wasn't satisfied with either answer (the first one gave 'ata' for some external disks and 'usb' only with thumb drives), so I came up with this:

for dev in $(lsblk -ndo name)
do
    udevadm info --query=property --path=/sys/block/$dev | 
        sed -n "s|^DEVPATH=|$dev/|p" |
        cut -d/ -f1,6 | tr / :
done
AimoE
  • 1
  • 1