If you could go with UDisks2 DBus interface the answer would be quite simple :-) First of all, it seems that UDisks2 (and probably UDisks1, but I haven't it installed) handles only Mass Storage devices connected via USB, completely ignoring USB-connected MTP-devices and other kinds of USB devices such as printers and mice. Have a look:
Right now I have 6 USB devices connected:
alex@galene ~ $ /usr/sbin/lsusb | grep -v 'root hub' Bus 001 Device 004: ID 058f:6362 Alcor Micro Corp. Flash Card Reader/Writer Bus 001 Device 003: ID 09da:000a A4 Tech Co., Ltd Optical Mouse Opto 510D Bus 008 Device 090: ID 04e8:6860 Samsung Electronics Co., Ltd GT-I9100 Phone [Galaxy S II], GT-P7500 [Galaxy Tab 10.1] Bus 008 Device 075: ID 04b8:0007 Seiko Epson Corp. Printer Bus 009 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive Bus 009 Device 088: ID 1058:1010 Western Digital Technologies, Inc. Elements External HDD
058f:6362 is an "all-in one" card reader w/ a SD-card currently plugged in. 058f:6387 and 1058:1010 are real USB Mass Storage devs, a flash and an external HDD respectively. 04e8:6860 is a Samsung Galaxy Note1 (an MTP-device) and the rest are the mouse and the printer.
Also I have an internal SATA drive.
Here's the current udisksctl status
output (as you may see Galaxy Note is missing):
MODEL REVISION SERIAL DEVICE -------------------------------------------------------------------------- Hitachi HDT725040VLA360 V5COA7EA VFM301R3196R5F sda Generic Flash Disk 8.07 8D0147DF sdb WDC WD1200BEVS-22RST0 04.01G04 WD-WXE108V72690 sdc Generic USB CF Reader 1.01 058F312D81B sde Generic USB SM Reader 1.02 058F312D81B sdf Generic USB MS Reader 1.03 058F312D81B sdg Generic USB SD Reader 1.00 058F312D81B sdd
To distinguish pluggable USB Mass storage devices from stationary drives one may check 'ConnectedBus' and ''Removable' properties. Please note that there's another property 'MediaRemovable' which corresponds to the physical media of the device. Here's the script which selects removable USB Mass Storage devices and prints their 'MediaRemovable' property
alex@galene ~ $ cat <<EOF | python - > import dbus > bus = dbus.SystemBus() > ud_manager_obj = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2') > om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager') > for k,v in om.GetManagedObjects().iteritems(): > drive_info = v.get('org.freedesktop.UDisks2.Drive', {}) > if drive_info.get('ConnectionBus') == 'usb' and drive_info.get('Removable'): > print("Device Path: %s, MediaRemovable: %s" % (k, drive_info['MediaRemovable'])) > block_devices = [''.join(chr(c) for c in device[dbus.String(u'org.freedesktop.UDisks2.Block')][dbus.String(u'PreferredDevice')] if chr(c) != '\x00' ) for device in om.GetManagerObjects().values() if dbus.String(u'org.freedesktop.UDisks2.Block') in device] > EOF Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_SM_Reader_058F312D81B, MediaRemovable: 1 Device Path: /org/freedesktop/UDisks2/drives/Generic_Flash_Disk_8D0147DF, MediaRemovable: 1 Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_SD_Reader_058F312D81B, MediaRemovable: 1 Device Path: /org/freedesktop/UDisks2/drives/WDC_WD1200BEVS_22RST0_WD_WXE108V72690, MediaRemovable: 0 Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_CF_Reader_058F312D81B, MediaRemovable: 1 Device Path: /org/freedesktop/UDisks2/drives/Generic_USB_MS_Reader_058F312D81B, MediaRemovable: 1
Basically it solves the problem you're trying to solve. Rumors say that there're non-removable USB-connected storage devices but I haven't seen such.