[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Determing the device of a USB or Flash Drive

Posted on 2009-12-28
9
Medium Priority
?
372 Views
Last Modified: 2013-12-16
Assume I insert a USB or Flash drive, I'm looking to find a reliable and robust method to determine the device of the drive.

For example I could do

fdisk -l | grep MB

but that assumes I don't have any very small hard disk partitions and that USB/Flash drives are a certain size.

Looking to test on Ubuntu/Debian based systems.
0
Comment
Question by:Tintin
9 Comments
 
LVL 6

Expert Comment

by:ollfried
ID: 26135158
Take a look at the output  of dmesg.
0
 

Expert Comment

by:CJ7Heep
ID: 26135178
You could use df, there are some switches available with this as well, such as -h. This will show output in GB. Try "man df", you'll see all the available outputs.
0
 
LVL 41

Expert Comment

by:noci
ID: 26136609
most modern distro's use dbus for low level activity, you can write a dbus client that monitor device events and acts according ly (that's what kde/gnome etc do  for automounting devices).
More basic use udev events..

You can run a script on the insertion of a memory card and go from there (try to mount it), check for file system etc.

at least in my distro (gentoo) there is a scsi-devfs.h script to create old devfs names. That can also be used to do extra things..

You can make it dependant on the USB id ... if you need
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 41

Expert Comment

by:noci
ID: 26136615
If needed you can also fix the name to rename is to a completely different name.
0
 
LVL 48

Author Comment

by:Tintin
ID: 26136771
noci, thanks for the suggestion, but I'm looking for simple solutions.

One idea I had was to look at the devices from fdisk -l and if it isn't listed in the grub config, then it's probably a new device, eg: usb/flash.
0
 
LVL 41

Expert Comment

by:noci
ID: 26137072
That might become very messy if device mapper devices are available or loopback adapters.... or the loaded devices use lvm...

May be a wrapper around the udev -scsi script:

 #check a flag file:
 if (flag file is more recent then boottime) then starts a background process
 exec's the original udev -scsi script

Background script:
 sleep a short while to let things settle
 do your stuff

#have an init script that enables your wrapper in a phase >= 3  (touch a flag file)

That way you have the exact real name shortly after insertion...
it's fairly clean easy to install. (& repair after updates).
0
 
LVL 48

Author Comment

by:Tintin
ID: 26160054
noci, thanks for the info, but that solution is probably a little over the top for my needs.  I don't need something 100% bullet proof.  It's going to be running on a known hardware/software platform, so some safe assumptions can be made.
0
 
LVL 41

Expert Comment

by:noci
ID: 26167277
No problem, but the most important info (name) is passed to you this way.
it's reliable and it's robust.
the more kludgy way would be to monitor dmesg.../kernel log for scsi mentions.

0
 
LVL 41

Accepted Solution

by:
noci earned 2000 total points
ID: 26169893
Oh, the next one down in the chain could be hal ofcourse (present if any modern desktop is used).
 - http://www.freedesktop.org/wiki/Software/hal
 - http://www.environnement.ens.fr/perso/dumas/halevt.html

And you could involve dbus (also present when a modern desktop is used)
 - http://dbus.freedesktop.org/

but that would not make things easier...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Welcome back to our beginners guide of the popular Unix tool, cron. If you missed part one where we introduced this tool, the link is below. We left off learning how to build a simple script to schedule automatic back ups. Now, we’ll learn how to se…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

613 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question