cancel
Showing results for 
Search instead for 
Did you mean: 

usb-storage question

SOLVED
Go to solution

usb-storage question

Hi,

I have both a Fuji digital camera and a Lexar jump disk. Both work fine on my evo using the usb-storage module. But if I first attach one and then the other, I can't use the second without first removing the usb-storage module and then reinserting it. This is not too much of a problem for me except it requires becoming root. I noticed a similar issue with first attaching my digital camera and later attaching my brother-in-law's. Somehow something is set on attaching one device that doesn't get reset on attaching a different device.

The problem I forsee would arise if I use the camera and later my wife wants to use the jumpdisk, since for her to become root and mess with modules is more than she is comfortable with.

So the question: is there some way to reload/reset the mod-usb module without becoming root? I would then build this into a script to be run from a button on the screen.
I'm nervous about making things suid root, but maybe this is the only way?

Mike
7 REPLIES
Stuart Browne
Honored Contributor

Re: usb-storage question

If you are doing all of this at the console, then '/etc/security/console.perms' and '/etc/security/console.apps/' are your friends! (Making a brash assumption that you are using some form of RedHat distribution).

If so, you shoul dbe able to (as you say) create some scripts, and have them run with root privelages because they are being done from the console! Much fun.

Personally, I think this would be a good thing to talk to the people at 'http://www.linux-usb.org/' about, and see if they have any solutions (mailing list archives at http://www.linux-usb.org/mailing.html).
One long-haired git at your service...
Mark Grant
Honored Contributor

Re: usb-storage question

Mike,

I would be nervous about setuid scripts too, as you can't make them :)

You would need a little C wrapper around your script or use "sudo".

However, I have never experienced this kind of thing with usb (because I have never used usb on Linux) but I would have thought the whole thing might be achieved with "cron" instead.

If you have usb as loadable modules, you could put a "rmmod -a" to run every so often from cron. This will unload unused modules.

If it really needs to be a script then just run that from cron.
Never preceed any demonstration with anything more predictive than "watch this"

Re: usb-storage question

Stuart, I'm running Debian, so don't have these perm files. Maybe Debian has something similar?

Going to linux-usb is a good idea. I'll probably try that.

Mark, using cron sounds interesting. What I might try is a cron job to check if something is attached, and then if not, remove the module. Since the problem is unlikely to arise often, probably every 15 minutes or so would do it.

So some related questions:

1. Can I have both devices attached at once? Perhaps one on /dev/sda1 and one on /dev/sda2? This doesn't seem to work as I have it since I need to reload dev-usb between devices.

2. Can I have it so a plugged in device goes to a specific device? Right now the first device is always sda1, but to do the previous thing it would be nice to say that the camera, say, always goes to sda1 and the jumpdisk to sda2.

Mark Grant
Honored Contributor

Re: usb-storage question

Mike I can't really answer your usb questions because as I said, I haven't really played with it at all. However, you don't need to make a script to check if the module is active because the "rmmod -a" command won't unload modules that are in use or needed.
Never preceed any demonstration with anything more predictive than "watch this"

Re: usb-storage question

Hi,

Mark's solution is certainly the simplest; just put rmmod usb-storage in the crontab to run every few minutes. But just for fun I wrote the following script to tell me what is using the module.

Mike


#!/bin/sh
# script to check for attached usb-storage devices
# attempt to remove the usb-storage module if not in use

attached="false"
if grep ^usb-storage /proc/modules
then
echo usb-storage module loaded

# look for someone using it
for i in `ls /proc/scsi/usb-storage-0`
do
grep Vendor /proc/scsi/usb-storage-0/$i
if grep "Attached: Yes" /proc/scsi/usb-storage-0/$i
then
attached="true"
fi
done

if [ $attached == "true" ]
then
echo usb-storage in use
else
echo usb-storage not in use, attempting module removal
# check if you have the power to remove it
if [ $USER == root ]
then
/sbin/rmmod usb-storage
else
echo you need to be root to remove modules
fi
fi
else
echo usb-storage module not loaded
fi
Alejandro Lopez_2
Occasional Contributor
Solution

Re: usb-storage question

Did you try with usbmgr? or hotplug?
Is a daemon wich un/load modules as is requested by any device conected to the usb port.
I actually use usbmgr because hotplug has some problems loading the USB controller modules. (I use Debian/Sid)

Re: usb-storage question

Indeed, hotplug or usbmgr seem to be the right way to do it. usbmgr seems to conflict with hotplug; so I didn't want to try it since hotplug works for me with other things.

The hotplug documentation really sucks, but with a bit of help from google, I put together a script that seems to work ( be careful to correct line wrapping from the posting software):


#! /bin/sh
# remove usb_storage module when device is removed
# adapted from Martin Lohmeier's post
# http://marc.theaimsgroup.com/?l=linux-hotplug-devel&m=108058759316716&w=2
#
# put this in /etc/hotplug/usb/usb-storage
# the hotplug documentation really ought to explain this better
#
if [ "$ACTION" = "add" ]; then
cp /etc/hotplug/usb/usb-storage $REMOVER
fi

if [ "$ACTION" = "remove" ]; then
touch /tmp/usb_has_been_removed # some debugging
rmmod usb-storage
fi