This doesn't need root privileges
(but many of these commands use and depend on bashisms,
so they will not work in all POSIX-compliant shells):
There is 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 really 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 )) && 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 )) && echo ${GENHD_FLAGS[i]}
done
REMOVABLE
MEDIA_CHANGE_NOTIFY
UP
SUPPRESS_PARTITION_INFO
At all, for only knowing if flag removable
is set:
grep REMOVABL /usr/src/linux-headers-3.2.0-4-common/include/linux/genhd.h
#define GENHD_FL_REMOVABLE 1
so
for disk in sd{a,b,c,d,e,f,g,h} ; do
(( $(< /sys/block/$disk/capability ) & 1 )) && echo $disk is removable
done
works by testing whether the capability
value
(which is 52 in my sda
example, above)
has the 1
bit set (i.e., whether it is an odd number).
But Linux renders all flags in /sys
, so asking for /sys/block/sdX/removable
is a lot simpler! ;-)
So a USB key could be removable
, but as there are lots of removable devices, I would prefer to ensure that the size of the medium is greater than 0 (like an unloaded CD-ROM tray, for sample)
and that the device is not in use: In watching that sdX/trace/enable
is not binded:
Nota: All this is well tested on bash v4.2+.
Under bash, you could use this very quick and efficient way:
for disk in /sys/block/* ; do
[ -f "$disk/removable" ] && [ $(<"$disk/removable") -gt 0 ] &&
[ -f "$disk/size" ] && [ $(<"$disk/size") -gt 0 ] &&
[ -f "$disk/trace/enable" ] && [ -z "$(<"$disk/trace/enable")" ] &&
echo "${disk##*/} $(($(<"$disk/size")/1953125))G $(<"$disk/device/model")"
done
On my system, there are 4 USB keys, but one of them (sde
) is already mounted, so the previous command output:
sdd 8G Trans-It Drive
sdf 7G Storage Media
sdg 4G silicon-power
My script:
There is a little bash function I wrote to install upgraded Debian Live.
#!/bin/bash
txtsize() {
local _c=$1 _i=0 _a=(b K M G T P)
while [ ${#_c} -gt 3 ] ; do
((_i++))
_c=$((_c>>10))
done
_c=000$(( ( $1*1000 ) >> ( 10*_i ) ))
((_i+=${3:-0}))
printf -v ${2:-REPLY} "%.2f%s" ${_c:0:${#_c}-3}.${_c:${#_c}-3} ${_a[_i]}
}
# The first part only renders human readable size. The function begins there.
chooseFreeUsbKey() {
local _lUdisk _lUsize _lUdialog=dialog # whiptail # gdialog
local -A _lUdevices
unset ${1:-REPLY}
for _lUdisk in /sys/block/*; do
[ -f $_lUdisk/removable ] && [ $(<$_lUdisk/removable) -gt 0 ] &&
[ -f $_lUdisk/size ] && [ $(<$_lUdisk/size) -gt 0 ] &&
txtsize $(<$_lUdisk/size)*512 _lUsize &&
[ -f $_lUdisk/trace/enable ] && [ -z "$(<$_lUdisk/trace/enable)" ] &&
_lUdevices[${_lUdisk##*/}]="$_lUsize $(<$_lUdisk/device/model)"
done
case ${#_lUdevices[@]} in
0 ) ;; # echo Sorry no key found. ;;
1 ) IFS=§ read -a ${1:-REPLY} \
<<< "${!_lUdevices[@]}§${_lUdevices[@]%% *}§${_lUdevices[@]#* }";;
* ) declare -a menu
for _lUdisk in ${!_lUdevices[@]}; do
menu+=($_lUdisk "${_lUdevices[$_lUdisk]}")
done
_lUdisk=$($_lUdialog --menu "Choose a USB stick" \
$((LINES-3)) $((COLUMNS-3)) $((LINES-8)) \
"${menu[@]}" 2>&1 >/dev/tty)
IFS=§ read -a ${1:-REPLY} \
<<< "$_lUdisk§${_lUdevices[$_lUdisk]%% *}§${_lUdevices[$_lUdisk]#* }"
esac
}
This assigns the answer, as an array, to the variable given as the first argument or to variable $REPLY
:
chooseFreeUsbKey stick
echo "$stick"
sdf
echo "${stick[1]}"
7.26G
echo "${stick[2]}"
Storage Media
(The last field may contain spaces.)