how I detect insertion of usb-attached storage in linux

1,059 views
Skip to first unread message

fwinans

unread,
Feb 10, 2012, 3:36:49 PM2/10/12
to Pick and MultiValue Databases
I used to just look in the ring buffer show by linux dmesg command to
see
what redhat linux's response was to usb 'flash drive' insertion was,
but it
seems doing /bin/ls -dgG --full-time /sys/block/sd*
and fdisk -l
{resolving ambiguity, those were lowercase L, not digit 'one'}
are better information sources, though dmesg is still nice for a
doublecheck.

remember fdisk -l will verify the media has been inserted but not
yet
had an eject command done on the /dev/sd? since then, but you
will
in most situations need to mount /dev/sdb1 /myfavoritepath
and later do a matching umount /dev/sdb1 or umount /
myfavoritepath
if you actually want to read/write or even see the device in a df -
h
[ to be painfully obvious, that example presumes you found usb as /
dev/sdb ]

The commands lsub -v {for serial number and such trivia}
and lspci -v may help in some obscure future projects, too.
changing dash vee to dash vee vee or even dash vee vee vee gives
even
more low-grade information 'ore' to sift through, but is probably not
useful.

mlabel from the mtools rpm package of linux lets you access
device label,
though just using 'properties' page of the drive icon over on a winbox
is simpler.

Erm, I mention this here because sometimes I use these to put file-
saves on,
though yes I usually use linux samba and windows print and file
sharing instead.
I only ever use linux to support d3, so wanted an entry on this in
google search
engine for when I forget all this out in the field doing service
calls...

George Gallen

unread,
Feb 10, 2012, 4:14:07 PM2/10/12
to MVDMBS LIST
Well...if automount is running...
 
You could have a phantom which runs say every 30 seconds or so, and
  does a "df" to find any newly added mount points
 
Don't always assume howver, that it will have the same device number
each time you plug it in, because it might not.
 
> Date: Fri, 10 Feb 2012 12:36:49 -0800
> Subject: [mvdbms] how I detect insertion of usb-attached storage in linux
> From: fwi...@sbcglobal.net
> To: mvd...@googlegroups.com
> --
> You received this message because you are subscribed to
> the "Pick and MultiValue Databases" group.
> To post, email to: mvd...@googlegroups.com
> To unsubscribe, email to: mvdbms+un...@googlegroups.com
> For more options, visit http://groups.google.com/group/mvdbms

fwinans

unread,
Feb 10, 2012, 5:47:36 PM2/10/12
to Pick and MultiValue Databases
fwinans wrote:
> dmesg
> /bin/ls   -dgG  --full-time  /sys/block/sd*
> fdisk   -l
> df  -h
> lspci -v
> mlabel from the mtools rpm package
> lsub -v

Oops!
I fat-fingered that, should be lsusb -v
not lsub -v

fwinans

unread,
Feb 11, 2012, 10:40:12 AM2/11/12
to mvd...@googlegroups.com
Erm, no.  I think automount / autofs are for demand-based mounting / umounting of filesystems, but inserted  usb devices get automatically mounted, in runlevel 5,  by the   gnome   X windows manager. 

Googling to  Gnome Automount Disable  seems to confirm this recollection.
Memo:  who   -r   for linux runlevel 
            cat /etc/redhat-release for "am I on rhel4 vs. rhel5.4 vs. etc ?"

George Gallen

unread,
Feb 11, 2012, 5:58:26 PM2/11/12
to mvd...@googlegroups.com
OK..good then you don’t have to worry about the USB stick not getting automatically mounted.
 
There is way, I forget offhand, that you can use in the fstab file to mount a usb device based
on it’s device number (similar to a MAC address for network cards), so that USB device can be
mounted to the same mount point each time.
 
As long as you have know about the USB stick ahead of time, and have it defined in the fstab
file, you could always setup a crapload of mountpoints (ie. /mnt/usbs/usb1 /mnt/usbs/usb2.....)
 
use the df command in a phantom to scan for any newly aquired usb mounts
 
What was you were trying to do again? I didn’t quite understand from your first post, rather
what were you trying to do after you detected the usb stick was inserted?

George Gallen

unread,
Feb 11, 2012, 6:09:17 PM2/11/12
to mvd...@googlegroups.com
 
That describes how to find the UUID (Unique USB ID) for a USB memory stick and
how to use that in the fstab file to automatically mount a specific usb device to
a specific mount point.
 
This isn’t just mount /sda1 to a mountpoint , but actually being able to identify
which USB device it is, say you had 5 USB drives and wanted them to mount
to five different unique mount points, regardless of which usb port you plugged
them into, or in what order you plugged them
 
George
 
Sent: Saturday, February 11, 2012 5:58 PM
Subject: Re: [mvdbms] how I detect insertion of usb-attached storage in linux
 

fwinans

unread,
Feb 12, 2012, 1:26:31 AM2/12/12
to Pick and MultiValue Databases
I don't use usb much, and can eyeball things ad hoc to find
devices I've just stuffed in, but once in a blue moon clients will
ambush me during an unrelated pick service call and give
me a pop quiz on how they can automatically detect usb
storage device insertion, and ask could I maybe spend 30
seconds or so to whip up some idiot-proof utility to help use
them. They absolutely insist on a quick answer, no research
on my part, and no billing to them about this off-the-cuff query.
So normally this is trivia I'd not bother keeping on file, but it just
so happens I recently saw all the relevent info at one time, and
have collected it here in one place after verifying it on three
servers I was visiting for other reasons. They were running
redhat linux rhel4, rhel5, and rhel5.4 so it seems widely useful.

Your given web page link was for a debian linux server, but yes,
the notes apply to redhat too, as it turns out.
{Stick in the usb device, say for example it becomes whole-device
name /dev/sdb and you use the first {and likely only} partition,
/dev/sdb1 then you'd do
blkid /dev/sdb1
For a specimen I have here, I get back, in part,
UUID="0C19-2368" from doing blkid /dev/sdb1
{Grrr, the lspci command is rubbish if you want to find UUID,
so instead of lspci use either
blkid /dev /sdb1
or /lib/udev/vol_id /dev/sdb1 }

lspci -v -v gave me, in part,

idVendor 0x13fe Kingston Technology Company Inc.
idProduct 0x1a00 512MB/1GB Flash Drive
bcdDevice 1.00
iManufacturer 1 Kingston
iProduct 2 DataTraveler 2.0
iSerial 3 5B7603927FAD

Whereas /lib/udev/vol_id /dev/sdb1 gave me
ID_FS_USAGE=filesystem
ID_FS_TYPE=vfat
ID_FS_VERSION=FAT16
ID_FS_UUID=0C19-2368
ID_FS_LABEL=KINGSTON
ID_FS_LABEL_SAFE=KINGSTON

{Those KINGSTON labels both changed to FRED when I set up
/etc/mtools.conf file to assign MSDOS "d:" drive to
/dev/sdb1 then did mlabel d:
and when prompted supplied new label of
fred }
<< mlabel is dangerous -- with the -s flag it displays
current label but if you forget the -s
it displays then __erases__ the label
then prompts you for new one!!! Did you write
the
old one down? Are you relying on the old
label? >>
{Partiton is type vfat -- for a linux efs or efs2 filesys
I'd
have used tune2fs -L fred /dev/sdb1 to change the
label}

You can craft your line item in /etc/fstab file
replacing the plebian /dev/sb1 as first word with
UUID=0C19-2368 {in the case of my specimen device}
{no, you cannot use either "uuid" or "0c19-2368" instead}

I'd suggest relying on the filesystem label, since end-users could be
trained to change that after buying new devices. They may prefer
having a box of spare 'monday night media' devices all with label MON
for mounting on /media/MON/ or whatever. They'd rotate some
offsite,
and the rest of the box is for future monday-night backups. Scribble
'MON' on each one, and Bob's your uncle.
This is a label on the partition filesystem, not on the device as a
whole. On a winbox, properties of _that_partition_ shows this
label.
So then in fstab I'd use a line in with first word of
LABEL=KINGSTON {or now, LABEL=FRED} for my sample device.

I'm not sure UUID is immutable, graven in the hardware at the
factory. The man page for mke2fs notes UUID is stored in
superblock
at start of optionally external journal for the filesystem being made.
There is a linux command called uuid_generate
So UUID lives in a partition, instead of referring to whole physical
device.
I'd guess lsusb command's iSerial is the hardware
factory serial number, although iSerial is useless for fstab
purposes.
Reply all
Reply to author
Forward
0 new messages