Skip to Content.
Sympa Menu

baslinux - Re: [BL] USB devices

baslinux AT lists.ibiblio.org

Subject: Baslinux mailing list

List archive

Chronological Thread  
  • From: "David Moberg" <davidjmoberg AT gmail.com>
  • To: "Christof Lange" <cce.zizkov AT volny.cz>, baslinux AT lists.ibiblio.org
  • Subject: Re: [BL] USB devices
  • Date: Tue, 2 Jan 2007 14:52:20 -0800

Christof Lange wrote:

With USB storages devices it is a bit more complicated. When I
exchange them they show up at different addresses. When I tested
Sindi's and David's 2.4.31 kernels I realized that the first
deviced plugged in turns up at /dev/sda1 , the second one at
/dev/sdb1 , the third one at /dev/sdc1 and the fourth at /dev/sdd1 .
After that the next device can be mounted from /dev/sda1 again.

You are certainly not the first person to have problems with this
(standard) behavior.

I would like to write a script that uses one or more mount points,
but will be able to mount a new device with only one command, eg.
u: (and :u for unmounting). The script, however, must be able to
check where a device is. When I open the 'file' /dev/sda1 in
Midnight commander I get an error message if the device does not
exist and some contents if it exists. How can I check the contents
of these device files automatically? I tried:

cat /dev/sda1 |head -n1|cut -b10 >/dev/null && echo sda1 exists

Here is a quick script that I put together:

#!/bin/sh
dmesg_command=dmesg
exec mount /dev/$($dmesg_command | head -n $(($($dmesg_command | grep
-n $1 | cut -f1 -d: | tail -n 1)+2)) | tail -n 1 | cut -f5 -d" ")$2 $3

If that last line was split, please put it back together. I apologize
for the nested parentheses, but I really don't know of any simpler way
to do this.

This script scans the kernel messages for your USB disk and mounts it
by name. So, for example, if I always want to mount my Sandisk Cruzer
Mini on /mnt/cruzer I execute this command:

script_from_above "Cruzer Mini" 1 /mnt/cruzer

You can put this in an alias, one for each drive, to save typing. Get
the name to use (instead of "Cruzer Mini") by manually examining the
output of dmesg. The number 1 indicates which partition to use -
mounting the whole disk is accomplished by setting that value to "".

If you plug in more than one device with the same model name string,
this will only mount the one that was plugged in last. I know of only
3 ways to differentiate devices with the same model name:

1) Manually: magnetic disks make noise, so by accessing one and
listening you can tell which one is connected to sdb or sda. Some
drives have lights on them, which blink when you write to them.
2) Using the file or md5sum commands to examine the contents of the
disk. This will not work if the drive contents are unknown or if all
of the drives have the same contents.
3) Looking through /proc/scsi. This is not 100% reliable but does work
most of the time.

To do 2) and 3) automatically would require significant modification
to the above script. Maybe someone can add that functionality later.
Passing any additional options to mount would also require
modification of the script. For now you'll have to make do with the
existing script and manual use of the above methods.

The above script uses dmesg. dmesg is not always reliable but should
work 90% of the time. You can improve reliability of this method by
using the klogd system and additional dmesg parameters to make sure
that dmesg always has the information that this script needs. An
alternate script could use /proc/scsi to check for devices, but that
would be more complex and require that /proc be mounted. However, it
could work where dmesg fails.

If you were able to follow this entire message you deserve a reward. I
undoubtedly did not write clearly enough for it to be understandable.
Please ask for clarification if you did not understand any part of the
above.

David

P.S. Christof, is your computer's clock set correctly? My mail system
says that I received this message at about 9 this morning but the
message is dated 6 o'clock tonight. Could this be a timezone problem
(GMT instead of local time, or me being halfway around the world from
Czechoslovakia)?




Archive powered by MHonArc 2.6.24.

Top of Page