Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if a block device is a local disk or a removable usb disk

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!

like image 339
Andrea Avatar asked Oct 21 '25 08:10

Andrea


1 Answers

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
like image 135
Marcus Avatar answered Oct 23 '25 20:10

Marcus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!