This doesn't need root privileges:
There are a quick way to ask about a sdX:
grep -H . /sys/block/sda/{capability,uevent,removable,device,{model,type,vendor,uevent}}
/sys/block/sda/capability:52
/sys/block/sda/uevent:MAJOR=8
/sys/block/sda/uevent:MINOR=0
/sys/block/sda/uevent:DEVNAME=sda
/sys/block/sda/uevent:DEVTYPE=disk
/sys/block/sda/removable:0
/sys/block/sda/device/model:WDC WD360GD-00FN
/sys/block/sda/device/type:0
/sys/block/sda/device/vendor:ATA
/sys/block/sda/device/uevent:DEVTYPE=scsi_device
/sys/block/sda/device/uevent:DRIVER=sd
/sys/block/sda/device/uevent:MODALIAS=scsi:t-0x00
The realy interesting file is capability, On my Debian, I have a genhd.h file, so:
eval $(sed -ne '
s/#define.*GENHD_FL_\([A-Z0-9_]*\)[ \t]*\([0-9]*\) \?.*$/GENHD_FLAGS[\2]="\1"/p
' /usr/src/linux-headers-2.6.32-5-common-openvz/include/linux/genhd.h)
diskCapa=$(</sys/block/sda/capability)
for i in ${!GENHD_FLAGS[@]};do
[ $((diskCapa & i)) -ne 0 ] && echo ${GENHD_FLAGS[i]}
done
MEDIA_CHANGE_NOTIFY
UP
SUPPRESS_PARTITION_INFO
diskCapa=$(</sys/block/sdd/capability)
for i in ${!GENHD_FLAGS[@]};do
[ $((diskCapa & i)) -ne 0 ] && echo ${GENHD_FLAGS[i]}
done
REMOVABLE
MEDIA_CHANGE_NOTIFY
UP
SUPPRESS_PARTITION_INFO
At all, for only knowing if flag removable is set, ask for /sys/block/sdX/removable is a lot simplier! ;-)
So an USB key is removable, but as there are lot of removable device, I would prefer to ensure that size of medium is greater than 0.
My script:
I have 3 HD and 3 usb keys:
for dev in $(
grep -Hv '^0$' /sys/block/*/removable |
sed 's/removable:.*$/device\/uevent/' |
xargs grep -H '^DRIVER=sd' |
sed 's/device.uevent.*$/size/' |
xargs grep -Hv '^0$' |
cut -d / -f 4
) ;do
echo "$dev \"$(
sed -e 's/ *$//g' </sys/block/$dev/device/model
)\""
done
sdd "Storage Media"
sde "Freecom Databar"
sdf "silicon-power"
Nota: This is the very first part of a script I'm currently working on for installing a dual-boot live USB with ability of booting Debian or Ubuntu in persistent with a live-rw for Debian and a casper-rw for Ubuntu.
There is this very first part, which seem stable now and quiet, ( not as the rest of script with fdisk and mkfs).
#!/bin/bash
export USBKEYS=($(
grep -Hv ^0$ /sys/block/*/removable |
sed s/removable:.*$/device\\/uevent/ |
xargs grep -H ^DRIVER=sd |
sed s/device.uevent.*$/size/ |
xargs grep -Hv ^0$ |
cut -d / -f 4
))
export STICK
case ${#USBKEYS[@]} in
0 )
echo No USB Stick found
exit 0
;;
1 )
STICK=$USBKEYS
;;
* )
STICK=$(
bash -c "$(
echo -n dialog --menu \
\"Choose wich USB stick have to be installed\" 22 76 17;
for dev in ${USBKEYS[@]} ;do
echo -n \ $dev \"$(
sed -e s/\ *$//g </sys/block/$dev/device/model
)\" ;
done
)" 2>&1 >/dev/tty
)
;;
esac
[ "$STICK" ] || exit 0
echo $STICK...