Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
USB Stick Automounter
View unanswered posts
View posts from last 24 hours

Goto page Previous  1, 2, 3  Next  
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
drekka
Tux's lil' helper
Tux's lil' helper


Joined: 06 Jul 2003
Posts: 111

PostPosted: Thu Dec 18, 2003 4:29 am    Post subject: Reply with quote

Hi guys, interesting discussion (I'm still waiding through it ;-) But I thought I might throw a small fly in your ointment ;-)

I have a Sony DSC which hooks up via USB and appears as another drive just like my TwinMos USB stick. The TwinMos always appears on 4 (i.e. sda4) and the Sony always appears on 1 (i.e. sda1) However !!! after booting the first device plugged in get sda and the second gets sdb. So depending on the order I attach the devices, the TwinMos can be either sda4 or sdb4 and the camera can be either sda1 or sdb1.

As you might guess this makes doing fstab entries a little tricky, not to mention any auto mounters. How do your scripts handle the possibility of different sd's? I also presume that as you keep adding devices, they would appear as sdc, sdd, sde, etc. How would you work out what mount points to assign ? and what about if you always wanted the camera on /mnt/usb/camera regardless of whether it was on sda1 or sdb1 ???

he he he ;-)

cio
Derek.
_________________
Regards,
Derek

<nb>Reality is not what you think</nb>
Back to top
View user's profile Send private message
genotix
n00b
n00b


Joined: 10 Dec 2003
Posts: 13
Location: The Netherlands

PostPosted: Thu Dec 18, 2003 6:48 am    Post subject: Reply with quote

drekka wrote:
Hi guys, interesting discussion (I'm still waiding through it ;-) But I thought I might throw a small fly in your ointment ;-)

I have a Sony DSC which hooks up via USB and appears as another drive just like my TwinMos USB stick. The TwinMos always appears on 4 (i.e. sda4) and the Sony always appears on 1 (i.e. sda1) However !!! after booting the first device plugged in get sda and the second gets sdb. So depending on the order I attach the devices, the TwinMos can be either sda4 or sdb4 and the camera can be either sda1 or sdb1.

As you might guess this makes doing fstab entries a little tricky, not to mention any auto mounters. How do your scripts handle the possibility of different sd's?


Look at the line : usbstick="sdb"
As soon as you plug in your USB stick / card the hotplug handler adds a device to the /dev/ directory.
In your case sda1/4 or sdb1/4.
The usbstick variable is the "trigger" that makes the stick mount or unmount. It simply looks at the /dev dir and if e.g. /dev/sdb exists the stick is detected and will try to mount your device.

So : Adapt the script to your environment so you will be able to use it.

Quote:
I also presume that as you keep adding devices, they would appear as sdc, sdd, sde, etc. How would you work out what mount points to assign ? and what about if you always wanted the camera on /mnt/usb/camera regardless of whether it was on sda1 or sdb1 ???

he he he ;-)

cio
Derek.

_________________
Linux IS userfriendly,
It's only very selective about who it's friends are.

-=@@D=-
Back to top
View user's profile Send private message
wmertens
n00b
n00b


Joined: 16 Dec 2003
Posts: 7
Location: Belgium

PostPosted: Thu Dec 18, 2003 7:59 am    Post subject: Reply with quote

drekka wrote:

I have a Sony DSC which hooks up via USB and appears as another drive just like my TwinMos USB stick. The TwinMos always appears on 4 (i.e. sda4) and the Sony always appears on 1 (i.e. sda1) However !!! after booting the first device plugged in get sda and the second gets sdb. So depending on the order I attach the devices, the TwinMos can be either sda4 or sdb4 and the camera can be either sda1 or sdb1.

As you might guess this makes doing fstab entries a little tricky, not to mention any auto mounters. How do your scripts handle the possibility of different sd's? I also presume that as you keep adding devices, they would appear as sdc, sdd, sde, etc. How would you work out what mount points to assign ? and what about if you always wanted the camera on /mnt/usb/camera regardless of whether it was on sda1 or sdb1 ???


Well, if you use Linux 2.6, use my script
http://dev.gentoo.org/~wmertens/automount.hotplug :wink:

If you use 2.4, you'll have to use the inferior http://dev.gentoo.org/~wmertens/automount_usb-linux2.4

Read the scripts for how to install them. Both of them handle any device, any partition, automatically, and they always get mounted at /mnt/usb/<devicename> 8)

Cheers,

Wout.
Back to top
View user's profile Send private message
sasquach
n00b
n00b


Joined: 01 Jun 2002
Posts: 9

PostPosted: Thu Dec 18, 2003 6:08 pm    Post subject: usb-storage without mounting... Reply with quote

:twisted: I use mtools to grab the photos from my camera... no mounting needed...
Back to top
View user's profile Send private message
genotix
n00b
n00b


Joined: 10 Dec 2003
Posts: 13
Location: The Netherlands

PostPosted: Thu Dec 18, 2003 7:04 pm    Post subject: Re: usb-storage without mounting... Reply with quote

sasquach wrote:
:twisted: I use mtools to grab the photos from my camera... no mounting needed...


Isn't that an acquire device tool? :idea:
_________________
Linux IS userfriendly,
It's only very selective about who it's friends are.

-=@@D=-
Back to top
View user's profile Send private message
sasquach
n00b
n00b


Joined: 01 Jun 2002
Posts: 9

PostPosted: Thu Dec 18, 2003 7:23 pm    Post subject: Re: usb-storage without mounting... Reply with quote

I don't know a term like "acquire device tool", sorry...

Mtools reads media by interpreting the filesystem itself, so for that tool, block access to the device is all it needs.
Back to top
View user's profile Send private message
agrippa_cash
Tux's lil' helper
Tux's lil' helper


Joined: 08 May 2003
Posts: 143
Location: Los Angeles

PostPosted: Sat Dec 20, 2003 8:52 pm    Post subject: breifly offtopic Reply with quote

Quote:
I don't know a term like "acquire device tool", sorry...
You sound like 'Dr. Sbaitso.' (or an old Sierra game).

I just installed the script, and when I plug in my pendrive I get /mnt/usb/generic and no subdirectories or files. Any thoughts?
Back to top
View user's profile Send private message
sasquach
n00b
n00b


Joined: 01 Jun 2002
Posts: 9

PostPosted: Sat Dec 20, 2003 9:46 pm    Post subject: Re: breifly offtopic Reply with quote

agrippa_cash wrote:
You sound like 'Dr. Sbaitso.' (or an old Sierra game).


My favourite game is linux, I don't know that "Dr." and that game.

And I may sound strange because english is not my native tounge.
Back to top
View user's profile Send private message
wmertens
n00b
n00b


Joined: 16 Dec 2003
Posts: 7
Location: Belgium

PostPosted: Mon Dec 22, 2003 11:24 am    Post subject: Re: breifly offtopic Reply with quote

agrippa_cash wrote:

I just installed the script, and when I plug in my pendrive I get /mnt/usb/generic and no subdirectories or files. Any thoughts?


Hmmm... What is the output of "mount" ?
and do you know what filesystem is on the pendrive? I basically only tested it with vfat....
The generic indicates that it couldn't find a product name. Maybe there's a problem with your pendrive? Can you mount it manually?

If you want to know what's going on when it tries to mount the pendrive, add the following lines near the beginning of the script:
Code:
set -xv
exec >/tmp/automount_output.$$ 2>&1


This will make a bunch of /tmp/automount_output.PID files, one for each invocation of the script. You can take a look at these to see what is not working.

Cheers,

Wout.
Back to top
View user's profile Send private message
genotix
n00b
n00b


Joined: 10 Dec 2003
Posts: 13
Location: The Netherlands

PostPosted: Mon Dec 22, 2003 3:52 pm    Post subject: Re: breifly offtopic Reply with quote

agrippa_cash wrote:
Quote:
I don't know a term like "acquire device tool", sorry...
You sound like 'Dr. Sbaitso.' (or an old Sierra game).

I just installed the script, and when I plug in my pendrive I get /mnt/usb/generic and no subdirectories or files. Any thoughts?


Use my script :lol:
It's a bit more simple.
_________________
Linux IS userfriendly,
It's only very selective about who it's friends are.

-=@@D=-
Back to top
View user's profile Send private message
SNo0py
Apprentice
Apprentice


Joined: 12 Jul 2002
Posts: 270
Location: Vienna, Austria

PostPosted: Tue Dec 23, 2003 8:13 am    Post subject: Reply with quote

Hm, I have a very strange problem. I mount my USB Stick, I can write files and I can remount the stick. But as soon as I unplug the stick (after unmounting of course) from the computer, he looses all his files :(

No way to store anything on the Stick... All files are lost...

Any solution?

Thanks!
_________________
Sex is like hacking. You get in, you get out, and you hope you didnt leave something behind that can be traced back to you.
Back to top
View user's profile Send private message
wmertens
n00b
n00b


Joined: 16 Dec 2003
Posts: 7
Location: Belgium

PostPosted: Tue Dec 23, 2003 5:40 pm    Post subject: Reply with quote

SNo0py wrote:
Hm, I have a very strange problem. I mount my USB Stick, I can write files and I can remount the stick. But as soon as I unplug the stick (after unmounting of course) from the computer, he looses all his files :(

No way to store anything on the Stick... All files are lost...


I've seen this happen with Linux 2.4.21 and below. Try using 2.4.22 or up.

And try typing "sync" a lot :)

Wout.
Back to top
View user's profile Send private message
agrippa_cash
Tux's lil' helper
Tux's lil' helper


Joined: 08 May 2003
Posts: 143
Location: Los Angeles

PostPosted: Fri Dec 26, 2003 7:01 pm    Post subject: Reply with quote

sasquach: Your English is perfecty acceptable, and very few people played Dr. Sbaitso (as I remember it came free with Sound Blasters once upon a time), the Kings Quest series of Sierra Games was a lot of fun back then too.<p>That aside I am going to reinstall hotplug, at the moment I don't get any "/tmp/automount_output." and my HOTPLUG hangs when I try to restart the service. I suspect that love-sources omission of supermount support is the culpret. So I'll give it another shot.
Back to top
View user's profile Send private message
SNo0py
Apprentice
Apprentice


Joined: 12 Jul 2002
Posts: 270
Location: Vienna, Austria

PostPosted: Mon Dec 29, 2003 10:32 am    Post subject: Reply with quote

wmertens wrote:

I've seen this happen with Linux 2.4.21 and below. Try using 2.4.22 or up.

And try typing "sync" a lot :)

Wout.

WOW!!! Thank you!!!! Now everything works fine!!!
_________________
Sex is like hacking. You get in, you get out, and you hope you didnt leave something behind that can be traced back to you.
Back to top
View user's profile Send private message
wmertens
n00b
n00b


Joined: 16 Dec 2003
Posts: 7
Location: Belgium

PostPosted: Mon Dec 29, 2003 7:33 pm    Post subject: Reply with quote

agrippa_cash wrote:
That aside I am going to reinstall hotplug, at the moment I don't get any "/tmp/automount_output." and my HOTPLUG hangs when I try to restart the service. I suspect that love-sources omission of supermount support is the culpret. So I'll give it another shot.


The automount_output is only there when you put the code i gave some posts up in the script (the exec >/tmp/automount_output 2>&1 stuff).

Supermount can not be the reason, in fact, I recently tested it but my script didn't work with it :roll: . Anyway, when hotplug hangs, try looking at the output of pstree and try to find out what it is doing while it hangs.

Cheers,

Wout.
Back to top
View user's profile Send private message
r2dtu
Tux's lil' helper
Tux's lil' helper


Joined: 23 Aug 2002
Posts: 87

PostPosted: Thu Jan 01, 2004 6:50 pm    Post subject: Reply with quote

wmertens wrote:

Supermount can not be the reason, in fact, I recently tested it but my script didn't work with it

Your script is working great for me with supermount and kernel 2.6.1-rc1. I've only made one change:
Code:

-      MOUNTOPTS="-s -t supermount -onoatime,nosuid,umask=077,uid=$3,gid=$4"
+      MOUNTOPTS="-s -t supermount -odev=$DEVICE,--,noatime,nosuid,umask=077,uid=$3,gid=$4"

Thanks!
Back to top
View user's profile Send private message
MrZammler
Tux's lil' helper
Tux's lil' helper


Joined: 24 Jun 2003
Posts: 108
Location: The Island of Crete

PostPosted: Tue Jan 20, 2004 9:32 am    Post subject: Reply with quote

wmertens wrote:


Read the scripts for how to install them. Both of them handle any device, any partition, automatically, and they always get mounted at /mnt/usb/<devicename> 8)



Nice script, but I could find where I need to have it. /etc/hotplug/usb ?

Thanks.
Back to top
View user's profile Send private message
vdboor
Guru
Guru


Joined: 03 Dec 2003
Posts: 592
Location: The Netherlands

PostPosted: Thu Apr 29, 2004 12:38 pm    Post subject: Reply with quote

Until now, I always had a desktop icon on my desktop to mount usb sticks, but I'm really interested in auto-mounting the device with the hotplug system. However, I was wondering; how can the device be mounted as the current active user? (the one behind the console, or X desktop?)

My fstab entry autotically applies a 0077 umask on all removable media, and sets the owner to the user who issued the mount command...
Code:
/dev/sda1       /mnt/usbstick   auto    rw,noauto,user,exec,nosuid,nodev,umask=0077    0 0


Thanks in advise.
_________________
The best way to accelerate a windows server is by 9.81M/S²
Linux user #311670 and Yet Another Perl Programmer

[ screenies | Coding on KMess ]
Back to top
View user's profile Send private message
MasquedAvenger
Guru
Guru


Joined: 21 Aug 2003
Posts: 558
Location: Southern California

PostPosted: Fri Apr 30, 2004 6:00 am    Post subject: Reply with quote

Or, use the sync option in fstab, and it will continue working until all the data is on the drive. Very handy for removeable media such as this ;)

James
Back to top
View user's profile Send private message
malenko
n00b
n00b


Joined: 01 Nov 2003
Posts: 15

PostPosted: Sat May 01, 2004 11:01 am    Post subject: script bugfig Reply with quote

There are some issues with supermount it the listed script. The device field for supermount should be "none" and the real device must be passed in the dev=... option. When umounting, parsing must be done acordingly. Little cleanup. You can download the script from http://www.ms.mff.cuni.cz/~malej9am/prog/automount.hotplug (temporary).

Code:

#!/bin/sh
#
# Automount hotplugged block devices, by Wout Mertens (wmertens@gentoo.org)
#
# 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, sed, 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.
# - Edit fstab instead, and give the "user" option. This depends more on
#   the user/desktop knowing what he/it is doing.
# - Mount as supermount if available. This will improve behaviour for
#   synchronous writes and hanging mounts, although umount -lf already
#   does a good job. UPDATE: added, but untested. Let me know if it works.
# - Detect filesystem on device, so filesystems that don't need a mounting
#   user can skip user detection.

#ChangeLog:
#2004-05-01: Jaromir Malenko <malenko@email.cz>
#   FIX: Supermount
#   Cleanup
#Previous changes: Wout Mertens (wmertens@gentoo.org)

# 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/self/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
      # Supermount
      if [ "$dev" = "none" ] && echo "$rest" | grep "$1" 2>&1 >/dev/null
      then
         echo "$path"
         return 0
      fi
   done < /proc/self/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 () {
   # Note the lack of quotes around $1, this strips off spaces.
   echo $1 | sed 's/[ /?*\"<>]/_/g'
}

# Figure out the device to mount
set `echo $DEVPATH | sed 's/\// /g'`
[ $# -ne 3 ] && exit 0
DEVICE=/dev/$3

case $ACTION in

remove)
   # Is it ours?
   dir=`get_mounted_path $DEVICE`
   echo "$dir" | grep -q ^/mnt/usb || exit 0

   # Unmount it
   [ -d "$dir" ] || exit 1
   mesg Unmounting: umount -lf "$dir"
   umount -lf "$dir"
   rmdir "$dir"
   exit 1
   ;;

add)
   # Is it a usb device? Exit if not.
   ls -l /sys/$1/$2/device | sed 's/^.* -> //g' | grep -q usb || exit 0
   
   # 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 | sed 's/:/ /g'`
   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.
   if grep -q supermount /proc/filesystems
   then
      MOUNTOPTS="-s -t supermount -odev=$DEVICE,--,noatime,nosuid,umask=077,uid=$3,gid=$4 none"
   else
      MOUNTOPTS="-s -onoatime,sync,dirsync,nosuid,uid=$3,gid=$4 $DEVICE"
   fi
   mesg Mounting $DEVICE on $MOUNTPATH: mount $MOUNTOPTS $MOUNTPATH

   mount $MOUNTOPTS $MOUNTPATH
   ;;
   
*)
   mesg automount.hotplug $ACTION event not supported
   exit 1
   ;;
esac
Back to top
View user's profile Send private message
Nate_S
Guru
Guru


Joined: 18 Mar 2004
Posts: 414

PostPosted: Sat May 01, 2004 9:10 pm    Post subject: Reply with quote

I noticed in windows xp recently an option for disabling or enabling write cacheing on the disk (though, of course, being windows, it was not called that, that would be too simple) "optimize for performance" where you have to click a button in the taskbar, or "Optimise for safe unplugging." So windows is no different from linux in this respect, ifyou have write cacheing on for performance, you need to unmount first. And unplugging it while writing is never a good idea :wink:

A better option at this point might be to use udev/dbus/hal, I hear it's getting fairly close to being stable.
Back to top
View user's profile Send private message
GlennM
Tux's lil' helper
Tux's lil' helper


Joined: 11 Nov 2003
Posts: 94
Location: Wisconsin, USA

PostPosted: Sun May 02, 2004 9:11 pm    Post subject: Reply with quote

I've got a question about the automount.hotplug script linked above. It works, but I need some help with tweaking...

It's mounting my USB drive as msdos, and not vfat. I don't see any way to force vfat. Is this possible?

Thanks!

EDIT: Removed my other question, my mistake...

EDIT2: D'oh, maybe since I don't have supermount patched in, I should add "-t vfat" to the MOUNTOPTS at the end...all working now, what a great script.
Back to top
View user's profile Send private message
craftyc
Guru
Guru


Joined: 23 May 2002
Posts: 443
Location: Behind You.

PostPosted: Mon May 03, 2004 11:38 am    Post subject: Reply with quote

Just one question and some praise.

For me the device is always mounted as root, since that is what "stat -c%U /dev/console" returns. Is there something wrong with my system or the script?

Excellent script malenko. Just made my life easier.
_________________
Postcount ++
Back to top
View user's profile Send private message
vdboor
Guru
Guru


Joined: 03 Dec 2003
Posts: 592
Location: The Netherlands

PostPosted: Mon May 03, 2004 12:59 pm    Post subject: Reply with quote

craftyc wrote:
Just one question and some praise.

For me the device is always mounted as root, since that is what "stat -c%U /dev/console" returns. Is there something wrong with my system or the script?

Excellent script malenko. Just made my life easier.


I've disabled the pam_session module in /etc/pam.d, because I got sick of pam changing the permissions of the /dev/ files automaticallty.. It seams that I have the same problem here..

Does anyone know a better way to detect which user is currently behind the keyboard? A quick fix might be something like grepping the who output, but this hack is rather incomplete:

Code:
who | grep ":0" | cut -f 1 -d " "


I hope someone is more creative then I am, because I would like to use this automount script myself too!
_________________
The best way to accelerate a windows server is by 9.81M/S²
Linux user #311670 and Yet Another Perl Programmer

[ screenies | Coding on KMess ]
Back to top
View user's profile Send private message
craftyc
Guru
Guru


Joined: 23 May 2002
Posts: 443
Location: Behind You.

PostPosted: Mon May 03, 2004 3:52 pm    Post subject: Reply with quote

vdboor wrote:

I've disabled the pam_session module in /etc/pam.d, because I got sick of pam changing the permissions of the /dev/ files automaticallty.. It seams that I have the same problem here..

Does anyone know a better way to detect which user is currently behind the keyboard? A quick fix might be something like grepping the who output, but this hack is rather incomplete:

Code:
who | grep ":0" | cut -f 1 -d " "


I hope someone is more creative then I am, because I would like to use this automount script myself too!


I have a work around for this problem, however it does raise some security issues. Simply change the umask to 000 instead of 077. Since I am the only person using this system it is not a big issue for me. It may be for you.

Also the supermount thing doesn't work correctly. It doesn't unmount the directory, so I get a bunch of directories in /mnt/usb/.
_________________
Postcount ++
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum