how to locate a flash drive / identify /dev device

Asked by edA-qa

I want a way that a bash script can identify a USB drive which I plugin for the purpose of mounting that drive for me. The drive in this case is encrypted, though I'm not sure it makes a big difference here (using cryptsetup with luks).

If I do "lsusb" I can see the drive I want:
  Bus 002 Device 004: ID 090c:1000 Feiya Technology Corp. Memory Bar

And if I do "dmesg" I can sort of see what is happening under the usb-storage entries. In there it eventually identifies a device "sdc1" in this case. I could start doing some parsing, or awking, of this file to determine what I want, but I was hoping there was an easier way.

NOTE: I realize newer versions of Ubuntu can automount encrypted drives, but that still won't be quite what I want. I need to give the mount point a specific name so that I can automatically do a sync to that device, and so that other scripts can always look in one location.

Is there anyway to figure out what device the drive is easily? Or is there any way to force the mount path for a device anytime it is automounted*?

(*Though in my case automounting of encrypted drives doesn't appear to be working)

Question information

English Edit question
Ubuntu Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Revision history for this message
Fred (frederic-lespez) said :

In Nautilus, you can specify the mount for a device.
Todo that :
Right-click on the desktop icon for your key. Go to Properties and then tab "Volume". Here under parameters you can specify a mount for that device.

You can also specify some mount options : May be you will be able to fix your automount problem with that.

Keep me posted.

Revision history for this message
edA-qa (eda-qa) said :

Clarification, I'm using KDE and if I choose Properties for a mounted path, or for a device, I don't actually get any mount options. Is there an equivalent options somwhere in KDE.

However, normally the flash usb drive appears on my desktop without a problem when it is VFAT formatted without encryption. When encrypted it doesn't show up automatically on the desktop, I need to do a manual "pmount".

Revision history for this message
Fred (frederic-lespez) said :

Sorry, I am not a KDE user so I won't be able to help any further :-(

pmount is no longer used in Ubuntu for automounting. Since Feisty, automounting is based on HAL.
Try googling "hal luks KDE".
One of the top result is this one for example :
Automounting LUKS encrypted volumes in KDE

May be you will find some leads.

Revision history for this message
edA-qa (eda-qa) said :

I suspect that those intructions would very much solve my mount problem, I'll try it out sometime.

Though, unless that allows me to specify the mount location for a particular device (which giving it a quick read it doesn't), then my original question still stands: how to identify the device associated with a USB stick.

Revision history for this message
edA-qa (eda-qa) said :

I cannot get KDE_LUKS package is compile -- after having to install all sorts of dev files for various packages, which forced the removal of some other vital packages... Not a quest for the weary.

So, I'll give up on that route.

Though it did force me to install the libhal-dev, and there is the hal-device-manager, so I suspect I could use that library to figure out which device it is attached to...

Revision history for this message
Fred (frederic-lespez) said :

I looked more closely to HAL.
You can do things like that :
$ hal-find-by-capability --capability volume

Now you have a list all the devices (in fact their HAL ID) with capability "Volume".
The first one is my USB key :
$ lshal -l -u /org/freedesktop/Hal/devices/volume_uuid_010B_3892
udi = '/org/freedesktop/Hal/devices/volume_uuid_010B_3892'
  block.device = '/dev/sdb1' (string)
  block.is_volume = true (bool)
  block.major = 8 (0x8) (int)
  block.minor = 17 (0x11) (int)
  block.storage_device = '/org/freedesktop/Hal/devices/storage_serial_Intuix_DiskOnKey_06B183504023299B_0_0' (string)
  info.capabilities = {'volume', 'block'} (string list)
  info.category = 'volume' (string)
  info.interfaces = {'org.freedesktop.Hal.Device.Volume'} (string list)
  info.parent = '/org/freedesktop/Hal/devices/storage_serial_Intuix_DiskOnKey_06B183504023299B_0_0' (string)
  info.product = 'CLE_1GO' (string)
  info.udi = '/org/freedesktop/Hal/devices/volume_uuid_010B_3892' (string)
  linux.hotplug_type = 3 (0x3) (int)
  linux.sysfs_path = '/sys/block/sdb/sdb1' (string)
  org.freedesktop.Hal.Device.Volume.method_argnames = {'mount_point fstype extra_options', 'extra_options', 'extra_options'} (string list)
  org.freedesktop.Hal.Device.Volume.method_execpaths = {'hal-storage-mount', 'hal-storage-unmount', 'hal-storage-eject'} (string list)
  org.freedesktop.Hal.Device.Volume.method_names = {'Mount', 'Unmount', 'Eject'} (string list)
  org.freedesktop.Hal.Device.Volume.method_signatures = {'ssas', 'as', 'as'} (string list)
  storage.model = '' (string)
  volume.block_size = 512 (0x200) (int)
  volume.fstype = 'vfat' (string)
  volume.fsusage = 'filesystem' (string)
  volume.fsversion = 'FAT16' (string)
  volume.ignore = false (bool)
  volume.is_disc = false (bool)
  volume.is_mounted = true (bool)
  volume.is_mounted_read_only = false (bool)
  volume.is_partition = true (bool)
  volume.label = 'CLE_1GO' (string)
  volume.linux.is_device_mapper = false (bool)
  volume.mount.valid_options = {'ro', 'sync', 'dirsync', 'noatime', 'nodiratime', 'noexec', 'quiet', 'remount', 'exec', 'utf8', 'shortname=', 'codepage=', 'iocharset=', 'umask=', 'dmask=', 'fmask=', 'uid=', 'flush', 'usefree'} (string list)
  volume.mount_point = '/media/CLE_1GO' (string)
  volume.num_blocks = 2007008 (0x1e9fe0) (int)
  volume.partition.flags = {'boot'} (string list)
  volume.partition.label = '' (string)
  volume.partition.media_size = 1027604480 (0x3d400000) (uint64)
  volume.partition.number = 1 (0x1) (int)
  volume.partition.scheme = 'mbr' (string)
  volume.partition.start = 16384 (0x4000) (uint64)
  volume.partition.type = '0x0e' (string)
  volume.partition.uuid = '' (string)
  volume.size = 1027588096 (0x3d3fc000) (uint64)
  volume.unmount.valid_options = {'lazy'} (string list)
  volume.uuid = '010B-3892' (string)

So with HAL you can know when you USB key is attached or not.

And it seems that you can use the command hal-set-property (or use hal policy files) to modify the mount point and specify mount options, but I don't know how exactly...

Good luck.

Revision history for this message
edA-qa (eda-qa) said :

volume.label looks like the good option, and then one can just do (in your case):

# hal-find-by-property --key volume.label --string CLE_1G0

# hal-get-property --udi /org/freedesktop/Hal/devices/volume_uuid_ab4c6518_b93c_413d_92a1_5006c9c5e254 --key block.device

The only problem is that LUKS doesn't appear to have volume labels -- they are always empty! :(
For now I'll resort to using UUID's and the volume.uuid property, though this is less than ideal since I work with multiple sticks (multiple backup copies). I'll just add them to the list.