linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Working on a usb-storage hotplug script
@ 2003-11-29  1:19 Martin
  2003-11-29 12:33 ` reflex
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: Martin @ 2003-11-29  1:19 UTC (permalink / raw)
  To: linux-hotplug

I recently got my first USB flash drive - I have one on loan from work, 
but I finally OWN one, so I am motivated to do something with it now. 
One of the things I want to have working is hotplugging, with automatic 
mounting. The device it's mounted on doesn't matter so much but one of 
the absolutely mandatory requirements is that I not need a fstab entry. 
This became a requirement when I noticed that plugging the device in, 
then unplugging it, then plugging it BACK in resulted in it being 
attached first to sdc and then to sdd. (This is a SCSI system.)

Before I go on, some relevant information:
Linux suckit 2.4.20-xfs-r3 #3 SMP Fri Nov 28 14:20:48 PST 2003 i686 AMD 
Athlon(tm) Processor AuthenticAMD GNU/Linux
(This is the gentoo xfs-sources version of the kernel.)
Hotplug version 20030805-r2
usbcore, usb-ohci, and usb-storage modules all load as they should.

So I began to google for a usb-storage hotplug script which would do 
what I want. Alas, no such thing exists. The closest script I found 
really didn't work to begin with, so I set out to write something. 
Unfortunately there seems to be no simple way to get from the 
information that hotplug will feed one about the connected mass storage 
device to the particular scsi device which it has been attached to.

I began by writing a script which probably looks familiar to everyone 
who's worked on this problem.

#!/bin/bash
echo "ACTION ${ACTION}" >> /tmp/usblog
echo "PRODUCT ${PRODUCT}" >> /tmp/usblog
echo "TYPE ${TYPE}" >> /tmp/usblog
echo "INTERFACE ${INTERFACE}" >> /tmp/usblog
echo "DEVFS ${DEVFS}" >> /tmp/usblog
echo "DEVICE ${DEVICE}" >> /tmp/usblog

This resulted in the following output upon insertion of my flash drive:

ACTION add
PRODUCT ea0/6828/110
TYPE usb
INTERFACE 8/6/80
DEVFS /proc/bus/usb
DEVICE /proc/bus/usb/001/006

By looking in the usb and scsi nodes under /proc I decided that the only 
unique information between the two trees was the serial number.

/proc/bus/usb/devices (as you know)  is an ascii file which contains 
long descriptions of every connected usb device in the system.

Each device begins with a T: line, here is the entry for my flash drive.

T:  Bus\x01 Lev\x01 Prnt\x01 Port\x01 Cnt\x01 Dev#=  6 Spd\x12  MxCh= 0
D:  Ver= 1.10 Cls\0(>ifc ) Sub\0 Prot\0 MxPSd #Cfgs=  1
P:  Vendor\x0ea0 ProdIDh28 Rev= 1.10
S:  Manufacturer=USB    
S:  Product=Flash Disk     
S:  SerialNumber#0760A43F777AB2
C:* #Ifs= 1 Cfg#= 1 AtrÄ MxPwr\x100mA
I:  If#= 0 Alt= 0 #EPs= 3 Cls\b(stor.) Sub\x06 ProtP Driver=usb-storage
E:  AdÅ(I) Atr\x02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad\x02(O) Atr\x02(Bulk) MxPS=  64 Ivl=0ms
E:  AdÉ(I) Atr\x03(Int.) MxPS=   2 Ivl%5ms

our device is 001/006 which is bus/dev. so we can use the T: line to 
find our device. This is useful because there is only one T: line per 
device. After this I went to find the serial number (S:  SerialNumber=*)

Then I went to look in /proc/scsi/usb-storage-* to find devices. 
/proc/scsi/usb-storage-0/1 looks like so:

   Host scsi1: usb-storage
       Vendor: USB    
      Product: Flash Disk     
Serial Number: 230760A43F777AB2
     Protocol: Transparent SCSI
    Transport: Bulk
         GUID: 0ea06828230760a43f777ab2
     Attached: Yes

So we can see that our serial number matches. The first line of the file 
tells me the Host which I will obviously need. Looking in /proc/scsi/scsi:

Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: IBM      Model: DDRS-39130D      Rev: DC1B
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 01 Lun: 00
  Vendor: SEAGATE  Model: ST39140N         Rev: 1498
  Type:   Direct-Access                    ANSI SCSI revision: 02
Host: scsi0 Channel: 00 Id: 06 Lun: 00
  Vendor: IBM      Model: CDRM00203     !K Rev: BZ26
  Type:   CD-ROM                           ANSI SCSI revision: 02
Host: scsi1 Channel: 00 Id: 00 Lun: 00
  Vendor: 64MB     Model: HardDrive        Rev: 1.88
  Type:   Direct-Access                    ANSI SCSI revision: 02

unfortunately the entries here are not really unique in any way if you 
have more than one of the same thing. The Revision from the 
/proc/bus/usb/devices might be used to HELP identify something, but it 
still will not be unique. The question is, what happens when I plug in 
another one? Will it get a new host? Answer is yes. The device also 
shows up in a new usb-storage-? directory under /proc/scsi. Here is its 
entry in usb/devices:

T:  Bus\x01 Lev\x01 Prnt\x01 Port\x01 Cnt\x02 Dev#=  6 Spd\x12  MxCh= 0
D:  Ver= 1.10 Cls\0(>ifc ) Sub\0 Prot\0 MxPSd #Cfgs=  1
P:  Vendor\x0ea0 ProdIDh28 Rev= 1.10
S:  Manufacturer=USB    
S:  Product=Flash Disk     
S:  SerialNumber#0760A43F777AB2
C:* #Ifs= 1 Cfg#= 1 AtrÄ MxPwr\x100mA
I:  If#= 0 Alt= 0 #EPs= 3 Cls\b(stor.) Sub\x06 ProtP Driver=usb-storage
E:  AdÅ(I) Atr\x02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad\x02(O) Atr\x02(Bulk) MxPS=  64 Ivl=0ms
E:  AdÉ(I) Atr\x03(Int.) MxPS=   2 Ivl%5ms

And here is the new entry in /proc/scsi/scsi:

Host: scsi2 Channel: 00 Id: 00 Lun: 00
  Vendor: JetFlash Model: 128MB            Rev: 1.11
  Type:   Direct-Access                    ANSI SCSI revision: 02

Obviously I don't have two identical flash devices to play with, or I 
would be using them.

Does the Port\x01 correspond to the usb-storage-? directory in 
/proc/scsi? That is the only number which seems to have gone from 0 to 
1. I only have two flash devices so I can't tell for sure by installing 
another one. The new device has an entry in usb-storage-1/2 rather than 
1/1 as I would have suspected. The Cnt\x02 leads me to believe that Port 
becomes the usb-storage-? directory and Cnt becomes the filename under 
that directory. Are my assumptions correct? If so I can do away with 
most of my nonsense about the serial number, it was just the first thing 
I came up with.

I do have one further problem before I even start, which is that I don't 
seem to get a remove event when I disconnect a flash drive. Is this a 
known problem with my version of the kernel or of hotplug?



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
@ 2003-11-29 12:33 ` reflex
  2003-11-29 14:06 ` Oliver Neukum
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: reflex @ 2003-11-29 12:33 UTC (permalink / raw)
  To: linux-hotplug

Martin schrieb:

> So I began to google for a usb-storage hotplug script which
> would do what I want.
I have had the same problem and tried do solve it by scanning all
devices automatically: http://vold.sourceforge.net

KDE has a device kio slave which scanns /etc/fstab. So I could
probably add a feature where connected devices don't get mounted
but added to /etc/fstab so they appear on the desktop
automatically.

Drawback is: Vold doesnt know if it has just mounted a usb-drive
or a cdrom. And you will get nasty read-error messages in syslog
if you load the usb-storage driver but have no device attached
etc. They are not severe, I just have to find a way to suppress
them. It still misses an eject program to unmount as user.

> By looking in the usb and scsi nodes under /proc I decided that
> the only unique information between the two trees was the
> serial number.
Are you really sure that the serial is unique? If I remember
correctly the not all devices have serials set.


> I do have one further problem before I even start, which is
> that I don't seem to get a remove event when I disconnect a
> flash drive. Is this a known problem with my version of the
> kernel or of hotplug?
Same here. I think its a bug. But since its not 2.5/2.6-test
related nobody seems to care here :-(


-- 
Clemens Wacha           wacha@gmx.ch           ICQ:12620492

proudly running Debian/GNU Linux. See http://www.debian.org


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
  2003-11-29 12:33 ` reflex
@ 2003-11-29 14:06 ` Oliver Neukum
  2003-11-29 18:05 ` Marco d'Itri
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Oliver Neukum @ 2003-11-29 14:06 UTC (permalink / raw)
  To: linux-hotplug

 
> > By looking in the usb and scsi nodes under /proc I decided that
> > the only unique information between the two trees was the
> > serial number.
> Are you really sure that the serial is unique? If I remember
> correctly the not all devices have serials set.

They are not unique. And the assumption that one hotplug
event correlates with one new device is false. There are
multi-Lun devices and scsi-to-usb bridges.
You may also connect a tape or other non disk device.

> > I do have one further problem before I even start, which is
> > that I don't seem to get a remove event when I disconnect a
> > flash drive. Is this a known problem with my version of the
> > kernel or of hotplug?
> Same here. I think its a bug. But since its not 2.5/2.6-test
> related nobody seems to care here :-(

What kind of device do you disconnect?

	Regards
		Oliver



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
  2003-11-29 12:33 ` reflex
  2003-11-29 14:06 ` Oliver Neukum
@ 2003-11-29 18:05 ` Marco d'Itri
  2003-11-30 11:19 ` reflex
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Marco d'Itri @ 2003-11-29 18:05 UTC (permalink / raw)
  To: linux-hotplug

On Nov 29, Martin <drink@hyperlogos.org> wrote:

 >unfortunately the entries here are not really unique in any way if you 
 >have more than one of the same thing. The Revision from the 
 >/proc/bus/usb/devices might be used to HELP identify something, but it 
 >still will not be unique. The question is, what happens when I plug in 
 >another one? Will it get a new host? Answer is yes. The device also 
 >shows up in a new usb-storage-? directory under /proc/scsi. Here is its 
 >entry in usb/devices:
You really want to use a 2.6 kernel. In sysfs you will find everything
needed to automatically mount your device.

-- 
ciao, |
Marco | [3363 malEDvGeptZQA]


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (2 preceding siblings ...)
  2003-11-29 18:05 ` Marco d'Itri
@ 2003-11-30 11:19 ` reflex
  2003-11-30 19:38 ` Greg KH
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: reflex @ 2003-11-30 11:19 UTC (permalink / raw)
  To: linux-hotplug

Oliver Neukum schrieb:

> > > I do have one further problem before I even start, which is
> > > that I don't seem to get a remove event when I disconnect a
> > > flash drive. Is this a known problem with my version of the
> > > kernel or of hotplug?
> > Same here. I think its a bug. But since its not 2.5/2.6-test
> > related nobody seems to care here :-(
> 
> What kind of device do you disconnect?

An Apacer USB-Flash Drive 256MB USB2.0

cat /etc/hotplug/usb/usb-storage
#!/bin/sh
logger -t "USB-Drive" "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

/var/log/syslog
-->plug in:

Nov 30 12:10:45 lapis kernel: hub.c: new USB device 10:19.0-1, assigned address 4
Nov 30 12:10:45 lapis kernel: usb-storage: act_altsettting is 0
Nov 30 12:10:45 lapis kernel: usb-storage: id_index calculated to be: 101
Nov 30 12:10:45 lapis kernel: usb-storage: Array length appears to be: 103
Nov 30 12:10:45 lapis kernel: usb-storage: USB Mass Storage device detected
Nov 30 12:10:45 lapis kernel: usb-storage: Endpoints: In: 0xc49ce6e0 Out: 0xc49ce6f4 Int: 0x00000000 (Period 0)
Nov 30 12:10:45 lapis kernel: usb-storage: Found existing GUID 1005b1130070b00010b10320
Nov 30 12:10:45 lapis kernel: WARNING: USB Mass Storage data integrity not assured
Nov 30 12:10:45 lapis kernel: USB Mass Storage device found at 4
Nov 30 12:10:48 lapis usb.agent[639]: kernel driver usb-storage already loaded
Nov 30 12:10:48 lapis USB-Drive: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

-->plug out:

Nov 30 12:17:26 lapis kernel: usb.c: USB disconnect on device 10:19.0-1 address 4
Nov 30 12:17:26 lapis kernel: usb-storage: storage_disconnect() called
Nov 30 12:17:26 lapis kernel: usb-storage: -- releasing main URB
Nov 30 12:17:26 lapis kernel: usb-storage: -- usb_unlink_urb() returned -19


/proc/bus/usb/devices
T:  Bus\x02 Lev\x01 Prnt\x01 Port\0 Cnt\x01 Dev#=  5 Spd\x12  MxCh= 0
D:  Ver= 2.00 Cls\0(>ifc ) Sub\0 Prot\0 MxPSd #Cfgs=  1
P:  Vendor\x1005 ProdID±13 Rev= 1.00
S:  Product=USB Flash Drive
S:  SerialNumber\a0B00010B10320
C:* #Ifs= 1 Cfg#= 1 AtrÄ MxPwrP0mA
I:  If#= 0 Alt= 0 #EPs= 2 Cls\b(stor.) Sub\x06 ProtP Driver=usb-storage
E:  AdÅ(I) Atr\x02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad\x02(O) Atr\x02(Bulk) MxPS=  64 Ivl=0ms

regards
Clemens
(sorry for the long lines)
-- 
Clemens Wacha           wacha@gmx.ch           ICQ:12620492

proudly running Debian/GNU Linux. See http://www.debian.org


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (3 preceding siblings ...)
  2003-11-30 11:19 ` reflex
@ 2003-11-30 19:38 ` Greg KH
  2003-12-09  9:05 ` Wout Mertens
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Greg KH @ 2003-11-30 19:38 UTC (permalink / raw)
  To: linux-hotplug

On Fri, Nov 28, 2003 at 05:19:30PM -0800, Martin wrote:
> I recently got my first USB flash drive - I have one on loan from work, 
> but I finally OWN one, so I am motivated to do something with it now. 
> One of the things I want to have working is hotplugging, with automatic 
> mounting. The device it's mounted on doesn't matter so much but one of 
> the absolutely mandatory requirements is that I not need a fstab entry. 
> This became a requirement when I noticed that plugging the device in, 
> then unplugging it, then plugging it BACK in resulted in it being 
> attached first to sdc and then to sdd. (This is a SCSI system.)

Have you looked at devlabel?  It should solve your problem for you.

thanks,

greg k-h


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?  SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (4 preceding siblings ...)
  2003-11-30 19:38 ` Greg KH
@ 2003-12-09  9:05 ` Wout Mertens
  2003-12-09 16:09 ` Olaf Hering
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Wout Mertens @ 2003-12-09  9:05 UTC (permalink / raw)
  To: linux-hotplug

Hi,

I was wondering what the current state of auto-mount-inserted-devices
is.

I posted my script which is run as a hotplug agent for storage some time
ago, someone else had one along the same lines, Mandrake uses devfsd to
call their configuration scripts and the fstab editor, Lindows has
something, Suse as well, there is the vold project which uses polling, ...

It sometimes seems that everyone and their brother has their own
solution :(

Is there some standardized way of solving this in the works? Perhaps
based on sysfs?

Issues I know of hotplug-automounting in the 2.4 world:
- When run by usb.agent, you need to have a different name than the
  kernel module usb-storage, because otherwise you only get called when
  that module isn't loaded.

- When run by usb.agent, you don't know what the device is that sd_mod
  made out of it. You need to either check all attached devices or do
  some creative grepping on the serial number and hope that the device
  is unique at this point in time.

- When run by devfsd, you only get called when sd_mod is loaded. If it
  doesn't get loaded for some reason, you don't get called.

- sd_mod is very bad about detecting when a device has gone or replaced
  by another. You need to kick it with something like "dd if=<device>
  of=/dev/null bsQ2 count=1" for it to see new partition layouts. This
  can be done from devfsd though. Can it be done from udev?

- If you don't use supermount to mount the devices, unplugging them
  without closing all filedescriptors (including standing in subdirs)
  makes the kernel upset, and you have to umount -l or -f. Only,
  supermount is a hack that changes Unix semantics :(.

Comments?

Wout.

Nov 30, an infinite number of monkeys worshipping Greg KH pounded out:

> On Fri, Nov 28, 2003 at 05:19:30PM -0800, Martin wrote:
> > I recently got my first USB flash drive - I have one on loan from work,
> > but I finally OWN one, so I am motivated to do something with it now.
> > One of the things I want to have working is hotplugging, with automatic
> > mounting. The device it's mounted on doesn't matter so much but one of
> > the absolutely mandatory requirements is that I not need a fstab entry.
> > This became a requirement when I noticed that plugging the device in,
> > then unplugging it, then plugging it BACK in resulted in it being
> > attached first to sdc and then to sdd. (This is a SCSI system.)
>
> Have you looked at devlabel?  It should solve your problem for you.
>
> thanks,
>
> greg k-h
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: SF.net Giveback Program.
> Does SourceForge.net help you be more productive?  Does it
> help you create better code?  SHARE THE LOVE, and help us help
> YOU!  Click Here: http://sourceforge.net/donate/
> _______________________________________________
> Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
> Linux-hotplug-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
>


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (5 preceding siblings ...)
  2003-12-09  9:05 ` Wout Mertens
@ 2003-12-09 16:09 ` Olaf Hering
  2003-12-09 16:24 ` Greg KH
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Olaf Hering @ 2003-12-09 16:09 UTC (permalink / raw)
  To: linux-hotplug

 On Tue, Dec 09, Wout Mertens wrote:

> Comments?

forget 2.4, work on 2.6. 2.6 will give you a block event if there is
something of interrest.

-- 
USB is for mice, FireWire is for men!

sUse lINUX ag, n√úRNBERG


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (6 preceding siblings ...)
  2003-12-09 16:09 ` Olaf Hering
@ 2003-12-09 16:24 ` Greg KH
  2003-12-10 19:29 ` Wout Mertens
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Greg KH @ 2003-12-09 16:24 UTC (permalink / raw)
  To: linux-hotplug

On Tue, Dec 09, 2003 at 10:05:29AM +0100, Wout Mertens wrote:
> Hi,
> 
> I was wondering what the current state of auto-mount-inserted-devices
> is.
> 
> I posted my script which is run as a hotplug agent for storage some time
> ago, someone else had one along the same lines, Mandrake uses devfsd to
> call their configuration scripts and the fstab editor, Lindows has
> something, Suse as well, there is the vold project which uses polling, ...
> 
> It sometimes seems that everyone and their brother has their own
> solution :(

What's wrong with devlabel?  I thougth that's what most people are
using, as I think it solves the problems you are looking at.

And I don't know anything about devfs :)

thanks,

greg k-h


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (7 preceding siblings ...)
  2003-12-09 16:24 ` Greg KH
@ 2003-12-10 19:29 ` Wout Mertens
  2003-12-10 19:41 ` Wout Mertens
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Wout Mertens @ 2003-12-10 19:29 UTC (permalink / raw)
  To: linux-hotplug

Yesterday at 17:09, Olaf Hering ponderously produced:

>  On Tue, Dec 09, Wout Mertens wrote:
>
> > Comments?
>
> forget 2.4, work on 2.6. 2.6 will give you a block event if there is
> something of interrest.

Well, you're right. I just created a hotplug script that will automount
all block devices that get attached. I use this behaviour to use a USB
key as a token for connecting to a VNC session from a thin client.

It's way cleaner than the 2.4 version...

Here's the script, comments?

=====================#!/bin/sh
#
# Automount hotplugged block devices, by Wout Mertens.
#
# Linux v2.6 version
# This script is released under version 2 of the GPL.

# To install this, either make it /sbin/hotplug, or, if you have
# linux-hotplug installed, make it /etc/hotplug.d/block/automount.hotplug .
# It needs to be executable. I tested it with busybox's ash.
#
# The devices will be mounted for the console user if applicable.
#
# To work, this needs:
# - v2.6 kernel support:
#   - hotplugging, /proc mounted, /sys mounted
#   - filesystems that will be mounted, like vfat
# - sh, echo, tr, stat or ls, getent or grep passwd,
#   mount, umount, mkdir, rmdir. logger is used if there.
#
# Possible improvements:
# - Create a desktop entry for the device.
# - Call a notifier script when mounting/unmounting. This could create the
#   desktop entry by itself.
# - Mount as supermount if available. This will improve behaviour for
#   synchronous writes and hanging mounts, although umount -lf already
#   does a good job.
# - Detect filesystem on device, so filesystems that don't need a mounting
#   user can skip user detection.

# Debugging
# set -xv
# exec > /tmp/hotplug.$$ 2>&1

PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH

# We only handle block device messages
[ "$1" != "block" ] && exit 0

# Proclaim stuff to the world
mesg () {
	logger -t $0 "$*"
}

# Is path already mounted?
is_path_mounted () {
	while read dev path rest
	do
		if [ "$path" = "$1" ]
		then
			return 0
		fi
	done < /proc/mounts
	return 1
}

# Get mounted path for a device
get_mounted_path () {
	while read dev path rest
	do
		if [ "$dev" = "$1" ]
		then
			echo "$path"
			return 0
		fi
	done < /proc/mounts
	return 1
}

# Wait for a file to appear
wait_for () {
	local count=0
	while [ $count -lt 10 ] && [ ! -e "$1" ]
	do
		sleep 1
	done
	[ $count -eq 10 ] && return 1
	return 0
}

# Remove strange characters from a filename
clean_filename () {
	echo $1 | tr " /?*\"<>" "_______"
}

# Figure out the device to mount
set `echo $DEVPATH | tr / " "`
[ $# -ne 3 ] && exit 0
DEVICE=/dev/$3

if [ "$ACTION" = "remove" ]
then
	# Unmount it
	dir=`get_mounted_path $DEVICE`
	[ -d "$dir" ] || exit 1
	umount -lf "$dir"
	rmdir "$dir"
	exit 1
fi

if [ "$ACTION" = "add" ]
then
	# Mount it

	# Make sure we have support for vfat
	modprobe -q vfat

	wait_for /sys/$1/$2/device/vendor || exit 1

	# Find the name
	PRODUCT=`cat /sys/$1/$2/device/model`
	if [ -z "$PRODUCT" ]
	then
		PRODUCT=generic
	fi
	# Find out where we mount it
	MOUNTPATH=/mnt/usb/`clean_filename "$PRODUCT"`
	if is_path_mounted "$MOUNTPATH"
	then
		count=1
		while is_path_mounted "${MOUNTPATH}_${count}"
		do
			count=$(( $count + 1 ))
		done
		MOUNTPATH="${MOUNTPATH}_${count}"
	fi
	# Make sure it's a directory
	if [ -e "$MOUNTPATH" ]
	then
		if [ ! -d "$MOUNTPATH" ]
		then
			mesg "$MOUNTPATH exists but is not a directory"
			exit 1
		fi
	else
		mkdir -p "$MOUNTPATH"
		if [ $? -ne 0 ]
		then
			mesg "Could not create mountpoint $MOUNTPATH"
			exit 1
		fi
	fi
	# Find out who we are going to mount it as
	CONSOLEUSER=`stat -c%U /dev/console 2>/dev/null`
	if [ -z "$CONSOLEUSER" ]
	then
		set `ls -l /dev/console`
		CONSOLEUSER=$3
	fi
	[ -n "$CONSOLEUSER" ] || CONSOLEUSER=root
	PASSWD=`getent passwd $CONSOLEUSER 2>/dev/null`
	if [ -z "$PASSWD" ]
	then
		PASSWD=`grep "$CONSOLEUSER" /etc/passwd||grep root /etc/passwd`
	fi
	if [ -z "$PASSWD" ]
	then
		mesg "Could not get password entry for $CONSOLEUSER"
		exit 1
	fi
	set `echo $PASSWD | tr : " "`
	if [ $# -lt 4 ]
	then
		mesg "Bad password entry for $CONSOLEUSER"
		exit 1
	fi

	# These options should prevent abuse and make it writeable for the
	# console user.
	MOUNTOPTS="-s -osync,nosuid,umask\a7,uid=$3,gid=$4"
	mesg Mounting $DEVICE on $MOUNTPATH, options $MOUNTOPTS

	mount $MOUNTOPTS $DEVICE $MOUNTPATH
fi
=====================

-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (8 preceding siblings ...)
  2003-12-10 19:29 ` Wout Mertens
@ 2003-12-10 19:41 ` Wout Mertens
  2003-12-10 19:49 ` Olaf Hering
  2003-12-11 15:37 ` Wout Mertens
  11 siblings, 0 replies; 13+ messages in thread
From: Wout Mertens @ 2003-12-10 19:41 UTC (permalink / raw)
  To: linux-hotplug

Yesterday at 08:24, Greg KH ponderously produced:

> On Tue, Dec 09, 2003 at 10:05:29AM +0100, Wout Mertens wrote:
> > Hi,
> >
> > I was wondering what the current state of auto-mount-inserted-devices
> > is.
> >
> > I posted my script which is run as a hotplug agent for storage some time
> > ago, someone else had one along the same lines, Mandrake uses devfsd to
> > call their configuration scripts and the fstab editor, Lindows has
> > something, Suse as well, there is the vold project which uses polling, ...
> >
> > It sometimes seems that everyone and their brother has their own
> > solution :(
>
> What's wrong with devlabel?  I thougth that's what most people are
> using, as I think it solves the problems you are looking at.
>
> And I don't know anything about devfs :)

Devlabel needs to know about the device beforehand. I can't quickly copy
some pictures to my friends USB stick that way.

Devlabel tries to do something that's important for servers with loads
of drives. It is irrelevant, however, for the behaviour in the previous
paragraph. I insert the key, I copy stuff to the mountpoint, preferrably
through clicking on some icon on the desktop, done. I'll never need that
mountpoint again. It doesn't matter if it is on /dev/sda1 or
/dev/sdz6...

updfstab is another way of handling it, but then the user must remember
to mount first, and unmount afterwards. My script mounts it for the
owner of /dev/console, which means that if you plug in the key before
you're logged in, you don't get access to it.

In any case, I'm happy to see that Linux 2.6 has gotten more much robust
for hotplugging :)

Wout.


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (9 preceding siblings ...)
  2003-12-10 19:41 ` Wout Mertens
@ 2003-12-10 19:49 ` Olaf Hering
  2003-12-11 15:37 ` Wout Mertens
  11 siblings, 0 replies; 13+ messages in thread
From: Olaf Hering @ 2003-12-10 19:49 UTC (permalink / raw)
  To: linux-hotplug

 On Wed, Dec 10, Wout Mertens wrote:

> Yesterday at 17:09, Olaf Hering ponderously produced:
> 
> >  On Tue, Dec 09, Wout Mertens wrote:
> >
> > > Comments?
> >
> > forget 2.4, work on 2.6. 2.6 will give you a block event if there is
> > something of interrest.
> 
> Well, you're right. I just created a hotplug script that will automount
> all block devices that get attached. I use this behaviour to use a USB
> key as a token for connecting to a VNC session from a thin client.
> 
> It's way cleaner than the 2.4 version...
> 
> Here's the script, comments?

to make it more generic, use /proc/self/mounts and sed instead of tr.

-- 
USB is for mice, FireWire is for men!

sUse lINUX ag, n√úRNBERG


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: Working on a usb-storage hotplug script
  2003-11-29  1:19 Working on a usb-storage hotplug script Martin
                   ` (10 preceding siblings ...)
  2003-12-10 19:49 ` Olaf Hering
@ 2003-12-11 15:37 ` Wout Mertens
  11 siblings, 0 replies; 13+ messages in thread
From: Wout Mertens @ 2003-12-11 15:37 UTC (permalink / raw)
  To: linux-hotplug

Yesterday at 20:49, Olaf Hering sat down and wrote:

>  On Wed, Dec 10, Wout Mertens wrote:
>
> > Yesterday at 17:09, Olaf Hering ponderously produced:
> >
> > >  On Tue, Dec 09, Wout Mertens wrote:
> > >
> > > > Comments?
> > >
> > > forget 2.4, work on 2.6. 2.6 will give you a block event if there is
> > > something of interrest.
> >
> > Well, you're right. I just created a hotplug script that will automount
> > all block devices that get attached. I use this behaviour to use a USB
> > key as a token for connecting to a VNC session from a thin client.
> >
> > It's way cleaner than the 2.4 version...
> >
> > Here's the script, comments?
>
> to make it more generic, use /proc/self/mounts and sed instead of tr.

Sure :) Here are the changed lines:

====================
done < /proc/self/mounts
done < /proc/self/mounts

echo $1 | sed 's/[ /?*\"<>]/_/g'
set `echo $DEVPATH | sed 's/\// /g'`
set `echo $PASSWD | sed 's/:/ /g'`
====================

Playing with it, I see that 2.6 is very robust, and the only problem is
that when you mount something as "sync", it doesn't sync the directory
entry until after you type "sync". It does write all the data first
though. Sigh. This is where supermount_ng comes in.

I realize that the cleaner way would be to rewrite fstab and let
KDE/Gnome help the user with mounting/unmounting. But that won't help
for USB login tokens...

Wout.


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id\x1278&alloc_id371&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2003-12-11 15:37 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-29  1:19 Working on a usb-storage hotplug script Martin
2003-11-29 12:33 ` reflex
2003-11-29 14:06 ` Oliver Neukum
2003-11-29 18:05 ` Marco d'Itri
2003-11-30 11:19 ` reflex
2003-11-30 19:38 ` Greg KH
2003-12-09  9:05 ` Wout Mertens
2003-12-09 16:09 ` Olaf Hering
2003-12-09 16:24 ` Greg KH
2003-12-10 19:29 ` Wout Mertens
2003-12-10 19:41 ` Wout Mertens
2003-12-10 19:49 ` Olaf Hering
2003-12-11 15:37 ` Wout Mertens

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).