Tiny Core Linux

General TC => General TC Talk => Topic started by: mindmachine on March 02, 2013, 10:39:57 AM

Title: Automount USB with UDEV rules, Problem [SOLVED WITH RESTRICTIONS]
Post by: mindmachine on March 02, 2013, 10:39:57 AM
Hello everyone,
using Tinycore Linux on an Igel TC device, I tried modifying the UDEV rules according to the given hints in this forum. USB sticks are recognized but I'm experiencing quite some problems.
TC is installed on a CF card getting mounted as sda1 at boot. So when I plug in a new stick, the contents of the freshly plugged in USB stick fill up my /mnt-Folder, sda1 is removed or overwritten and after unplugging, contents still stay in /mnt.
What do I do wrong?
I assure, that the xxx and yyy scripts are correctly copied and also the 98-tc.rules is identically to the given information.

Thanks for your help
mindmachine

EDIT AS OF 2013-03-16

Rich developed a working script to mount USB devices with ntfs/fat/fat32 aso. filesystems when plugged in. There is also a working unmount script to remove mountpoints and devices after unplugging. Please note, that using these scripts may (or better will) be a risc for your USB-sticks, as there is no working routine in Linux to avoid data loss. It might well be, that your sticks get unreadable after plugging out.  If your stick already has got any problems (speaking in terms of being unclean), it won't get mounted, and it would get refused by the driver. Using ntfsfix would be partially helpful in this case but it is widely recommended to use a windows chkdsk command to repair it first. Thanks to tinypoodle for this important hint and the given explanations. You can find these later in this thread.
Quote
So integrity of your data is at risk if you use the following method
Anyway, here is what to do:
Download and install ntfsprogs to be run at boottime.
Remove ro-ntfs-driver from kernel, so the correct driver will be used:
Quote
sudo modprobe -r ntfs
Change "/etc/udev/rules.d/98-tc.rules":
Quote
KERNEL=="ram*", SUBSYSTEM=="block", GOTO="tc.rules_end"
KERNEL=="loop*", SUBSYSTEM=="block", GOTO="tc.rules_end"
ACTION=="add",      SUBSYSTEM=="block",   RUN+="/bin/sh -c '/bin/echo %k >> /tmp/automountqueue'"
ACTION=="remove",   SUBSYSTEM=="block",   RUN+="/bin/sh -c '/usr/local/sbin/usb-umount %k'"
LABEL="tc.rules_end"
Create the following scripts in "/usr/local/sbin"
usb-mount:
Quote
#!/bin/sh
QUEUE=/tmp/automountqueue
[ ! -e "$QUEUE" ] && mkfifo $QUEUE

while true
do
   for i in `cat $QUEUE`
   do
      echo `date +"%T"`" Request to mount $i" >> /home/tc/mounttime
      rebuildfstab
      i=$(cat /etc/fstab | grep -w $i | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
      echo `date +"%T"`" Cat returned $i" >> /home/tc/mounttime
      [ -z $i ] && continue
      echo `date +"%T"`" Mounting $i" >> /home/tc/mounttime
      mntcommand="mount /dev/$i /mnt/$i"
      grep $i /etc/fstab | grep -q ntfs
      [ 0 -eq $? ] && mntcommand="/usr/local/bin/ntfsmount /dev/$i /mnt/$i"
      grep -q $i /etc/mtab
      [ 0 -ne $? ] && $mntcommand >>/home/tc/mount.txt 2>&1
   done
   echo `/usr/local/bin/date +"%T"`" Done" >> /home/tc/mounttime
done

If you don't care for data integrity and need usb devices get mounted in any case, replace
Quote
[ 0 -eq $? ] && mntcommand="/usr/local/bin/ntfsmount /dev/$i /mnt/$i"
by
Quote
[ 0 -eq $? ] && mntcommand="/usr/local/bin/ntfsmount -o force /dev/$i /mnt/$i"
and your devices will get mounted if dirty or not. You might have to use a forensic tool afterwards to rescue your data (encase, xways...)!!!
usb-umount:
Quote
#!/bin/sh
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) ## create correct variable i in case partition number is missing
do
[ -z $i ] && continue
umount /mnt/$i
sleep 1
rmdir /mnt/$1
rebuildfstab
done
Make the scripts executable:
Quote
chmod +x /usr/local/sbin/usb-mount
Relodad Udev-Rules:
Quote
sudo udevadm control --reload-rules
Add a line in your /opt/bootlocal.sh file:
Quote
/usr/local/sbin/usb-mount.sh &
Don't forget to make changes persistant!
Try if it works by either rebooting or by running the usb-mount script by hand (use "sh /usr/local/sbin/usb-mount &" so it will run in background).

Good luck!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 10:53:43 AM
Hi mindmachine
Most members will probably not know what those xxx and yyy scripts are. I happen to remember that post by
bmarkus so here is a link to it for others to reference:
http://forum.tinycorelinux.net/index.php/topic,9622.msg52676.html#msg52676
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 11:00:28 AM
Thank you  ;)! Now as  tried again, I get an even more weird output. t looks, as if the contents of /mnt are identical to the contents of the /dev directory after plugging in a regular ntfs-usb-stick. How could this happen??
After unplugging, /mnt looks normal again with my nfs share and sda1 (thank god!, first time,  I tried this, it deleted my nfs-share "music" with more than 40 GB of sound files, too. Fortunately I had a complete backup...)
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 11:35:13 AM
Hi mindmachine
Quote
contents of the freshly plugged in USB stick fill up my /mnt-Folder, sda1 is removed or overwritten and after unplugging, contents still stay in /mnt.
I don't know why, but I think I can tell you what happened. It sounds like instead of the drive being mounted to
/mnt/MOUNTPOINT it was mounted to /mnt.
Quote
as if the contents of /mnt are identical to the contents of the /dev directory after plugging in a regular ntfs-usb-stick. How could this happen??
That sounds like /dev was remounted over /mnt. If I were you I would carefully review the rules and scripts to
make sure you copied them correctly, preferably by copying and pasting. You didn't mention it, but you should
probably install ntfs-3g.tcz too.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 12:17:19 PM
Got it working, bloody typo!!! Grrrrr! :-) Isn't it amaziong, doublechecked it three times and still overlooked a missing space.
Thanks a lot for your help....
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 12:45:40 PM
Hi mindmachine
Glad you got it working. An extra space strategically placed in a command can cause problems, as witnessed in
this thread:
http://forum.tinycorelinux.net/index.php/topic,10148.msg58947.html#msg58947
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 01:03:00 PM
True, it's like chmod 777 /* instead of chmod 777 *. Byebye system!

Another question. How to automount USB-devices writable? -rw or -o rw doesnt work with the script, right?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 01:19:58 PM
Hi mindmachine
Does /etc/fstab say your drives are being mounted  ro?
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 01:23:02 PM
it gives noauto,users,exec for sdb1 (ntfs-Filesystem)
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 01:28:14 PM
Hi mindmachine
You of course have ntfs-3g.tcz installed. Changing the mount command to something like this might work:
Code: [Select]
mount -t auto -o rw /dev/$1 /mnt/$1
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 01:42:13 PM
Hi mindmachine
You may also have to create a link:
Code: [Select]
sudo ln /usr/local/sbin/mount.ntfs-3g /usr/local/sbin/mount.ntfsbecause fstab lists the file system type as ntfs not ntfs-3g.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 02:31:50 PM
Nope, linked the file (and checked the path, it is okay) and changed the mount-file to -t auto -o rw.
It's still mounted rwxr-xr-x. :-(.
Ext3 and Ext2 partitions get mounted rw.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 02:44:45 PM
Hi mindmachine
What does this command return:
Code: [Select]
cat /etc/mtab | grep sdb1
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 02:49:52 PM
Nuthin :-)
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 02:58:42 PM
Then your drive (sdb1 ntfs, right?) is not mounted.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 03:04:58 PM
Yeah, it is. It shows in /mnt as sdb1?? Strange, isn' t it? But , alas, I just notice, that it doesn't show any files. What is going on?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 03:08:56 PM
Hi mindmachine
What you see is the mount point that was created. If it's not listed in mtab, it's not mounted.
Let's see what messages this returns:
Code: [Select]
sudo mount -t auto -o rw /dev/sdb1 /mnt/sdb1
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 03:14:06 PM
Failed, invalid argument...
Title: Re: Automount USB with UDEV rules, Problem
Post by: tinypoodle on March 02, 2013, 03:15:54 PM
It's still mounted rwxr-xr-x. :-(.
That is read-write.
However, no direct conclusions can be drawn from file permissions towards mount options.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 02, 2013, 03:24:53 PM
Hi mindmachine
How about:
Code: [Select]
sudo mount /dev/sdb1 /mnt/sdb1
cat /etc/mtab | grep sdb1
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 03:25:32 PM
Yeah, but just for root. If I try to hand this over network as a share, you unfortunately don't get write permissions.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 02, 2013, 03:29:06 PM
This also gives an invalid argument. I'll stop here for now, as I have to take care for the kids. I'll try again tomorrow or later on. It might be, that the mount point sdb1, as beeing created by udev rules atm is causing trouble. So I will go back to stock version ov the udev rule, create mouhnt point by hand and try to use the recommended arguments.

Thanks for your help by now. It is always a pleasure to find helpful people on the web!
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 09:44:02 AM
Did some testing. Mountig by cmd using -t ntfs-3g works well. Mounting with -t auto gives "invalid argument". So I can't use the udev-scripts as I do use ext2, vfat and ntfs sticks. What can be done?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 09:58:50 AM
Hi mindmachine
Does:
Code: [Select]
sudo mount -t ntfs /dev/sdb1 /mnt/sdb1work?
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 12:06:39 PM
It's getting weird. Drive mounts with -t ntfs but now we got dr-x------ as file permissions??!!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 12:34:12 PM
Hi mindmachine
How about just
Code: [Select]
mount /mnt/sdb1
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 01:07:29 PM
That works. Drive is now mounted rw. How to put this in the udev script?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 01:14:46 PM
Hi mindmachine
Try changing
Code: [Select]
mount /dev/$1 /mnt/$1to:
Code: [Select]
mount /mnt/$1Also remember to add:
Code: [Select]
ln /usr/local/sbin/mount.ntfs-3g /usr/local/sbin/mount.ntfsto your  /opt/.filetool.lst  file.
I'll be back later this evening if you need more help.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 01:18:54 PM
I already tried that, but again the drive is mounted read only by using the script (rtesp. it is not mounted, as there is no entry in mtab). Symlink is also already set and the folders are included for backup. This is really pestering me!!  ;D
Furthermore as there is an entry for sdb1 within fstab but still sdb1 doesn't mount via script. I already tried to increase "sleep" but that didn't do the trick. So my automount script starts obviously, running rebuildfstab, but isn't able to execute the mount /mnt/$1 command.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 05:23:24 PM
Hi mindmachine
Quote
Also remember to add:
Code: [Select]
ln /usr/local/sbin/mount.ntfs-3g /usr/local/sbin/mount.ntfsto your  /opt/.filetool.lst  file.
Sorry, I meant to say to add that to your  /opt/bootlocal.sh  file, NOT  /opt/.filetool.lst.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 07:14:00 PM
That should not be the problem. I run a backup of /usr/local/sbin, so the link is persistent....
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 07:38:40 PM
Hi mindmachine
Since you say:
Code: [Select]
mount /mnt/sdb1works, the only think that comes to mind is the mount point not being created in time. Try changing the
script to this:
Code: [Select]
#!/bin/sh
rebuildfstab
while [ ! -e "/mnt/$1" ] do; sleep 1; done  # Wait for mount point to be created
mount /mnt/$1
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 08:02:10 PM
The problem seems another one. If I try to run the mount script from command line, it reports an error like "/mnt/ can't be found in fstab.So it seems, as if $1 isn't accepted as correct term for this. If I drasticalley change the script to mount /mnt/sdb1, it gets mounted flawlessly...
I already tried to increase the sleep thing as this problem also came to my mind, but that did not help. So it's got something to do with that $1 placeholder.

Going to bed now, will try again tomorrow evening.

Thank you and Good night!!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 08:06:48 PM
Hi mindmachine
You didn't leave a space between  /mnt/  and  $1 , did you?
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 08:11:15 PM
No :-).
Title: Re: Automount USB with UDEV rules, Problem
Post by: althalus on March 03, 2013, 08:11:26 PM
The problem seems another one. If I try to run the mount script from command line, it reports an error like "/mnt/ can't be found in fstab.So it seems, as if $1 isn't accepted as correct term for this. If I drasticalley change the script to mount /mnt/sdb1, it gets mounted flawlessly...
I already tried to increase the sleep thing as this problem also came to my mind, but that did not help. So it's got something to do with that $1 placeholder.

Going to bed now, will try again tomorrow evening.

Thank you and Good night!!
It would be a big help, I think, to copy and paste bits of command line sessions so people can see exact error messages, and the actual command that you ran. Did you include the device name when running the mount script from command line? (That's what the %k in the udev rule does)
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 03, 2013, 08:14:44 PM
Hi althalus
Excellent points.

@mindmachine: As suggest by althalus, does:
Code: [Select]
xxx sdb1work?
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 03, 2013, 08:20:31 PM
Yeah, I totally agree, but I do use a very minimalistic install of TC and access the internet from another machine. That would make me write down every input/output instead of copy paste. So please have mercy with me. I could transfer it by USB, if I could, lol!!!

But to make it clear. Something between ntfs-3g and udev doesn't work as expected. So /mnt/$1 (without space inbetween) is not recognized and I get an "mount: can't find /mnt/ in /etc/fstab". $1 is obviously completely ignored. Using ext3 and ext4 filesystems, it works.

Have got to correct me. If I run "sudo sh /foo sdb1" it gets actually mounted. But why the bl**** He** doesn it work by using the script when it is called by 98-udev-rule ???

Another edit (I found the wysiwyg editor now, sorry for the mess in earlier posts, took wrong button for editing, gotten very late yesterday):
By fiddling around with this again, I noticed the following:
Running the usb-mount script in /usr/local/sbin
Quote
#!/bin/sh
rebuildfstab
sleep 3
mount /mnt/$1
and increasing the sleep time also delays creation of fstab. So if I increase up to ten, then a rebuild fstab is available after 10 seconds. So may it be, that the given mount command doesn't find the correct entry in fstab and therefore can't mount an ntfs-drive?
Mount point in fstab is available just after ten seconds then, but there is no entry in mtab, so running
Quote
sudo usb-mount sdb1
does mount sdb1 correctly with rw permission but NOT when called by script. sdb1 again is created in /mnt/ but is, of course, empty.
Would it make sense, to try something like
Quote
KERNEL=="ram*", SUBSYSTEM=="block", GOTO="tc.rules_end"
KERNEL=="loop*", SUBSYSTEM=="block", GOTO="tc.rules_end"
ACTION=="add", SUBSYSTEM=="block", RUN+="/bin/sh -c '/usr/sbin/rebuildfstab'", RUN+="/bin/sh -c '/usr/local/sbin/usb-mount %k'"
ACTION=="remove", SUBSYSTEM=="block", RUN+="/bin/sh -c '/usr/local/sbin/usb-umount %k'"
LABEL="tc.rules_end"
and remove the rebuildfstab command from the usb-mount script? Is it possible to run two commands as follw ups in an udev rule?

And what finally drives me completely nuts is, that using the script on ext3 ext4 drives is working as expected.
 
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 05, 2013, 05:58:16 PM
Okay I did trace down the problem, but don't find a proper solution, perhaps someone could help.

I split the rebuildfstab-command and the usb-mount command within the udevrule like this:
Quote
ACTION=="add", SUBSYSTEM=="block", RUN+="/bin/sh -c '/usr/sbin/rebuildfstab'", RUN+="/bin/sh -c '/usr/local/sbin/usb-mount %k'"
my usb-mount script looks like this:
Quote
while [ ! -e "/mnt/$1" ]
do
sleep 1
done
mount /mnt/$1

Then I used "top" to watch whats happening, when plugging in an USB-Stick. So this is happening (removed PID and stuff):
Quote
/bin/sh -c /usr/local/sbin/usb-mount sdb
{exe} ash /usr/local/sbin/usb-mount sdb
As you can see "%k" from 98-tc-rules does hand over sdb, but not the correct number, so the scripts stays in a loop and drive doesn't get mounted, as there is no "sdb" in fstab, just an "sdb1".

How could this be handled?
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 06, 2013, 02:20:16 PM
%PUSH%  :o
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 06, 2013, 03:12:35 PM
Hi mindmachine
First of all, nice detective work figuring out %k was not returning a partition. A similar problem was noted here, you
might recognize the author:  http://xfce.10915.n7.nabble.com/Help-to-get-automount-working-4-10-td11681.html
He states:
Quote
USB stick used during test is formatted on WINDOWS xp and doesn't have
correct partition table, it is why LINUX is handling it as /dev/sdc.
If you haven't already read it, this may be of some use:
http://forum.tinycorelinux.net/index.php/topic,9037.msg49251.html#msg49251
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 06, 2013, 04:34:47 PM
Thanks for your reply. It was driving me nuts.
Anyway, the sticks were created by gparted on a Linux machine, still they did not work. Attaching an archos mp3-Player works :-), a simple USB-device does not.

I already tried to use %k%n, as I read somewhere that %n (in spite of being obsolete) would return the correct partition number. That did not work somehow. Script wouldn't work for Vfat stuff, anyway.
Nicking the udev methods and scripts from another distro woulden't be an option? But Tinycore wouldn't be that tiny afterwards anymore, maybe?? :-)

I'll do some more reading, but as far as a quick check brought up, Bela also hasn't found a proper solution yet?
Anyway, now I've got at least some new ideas to work on. As long as we get the correct block device via %k, we might be able to use this information to run a query for the correct number for mounting.

Thanks again!



Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 06, 2013, 04:42:46 PM
by the way
Quote
nice detective work
that's my day to day job :-) (Forensics)
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 06, 2013, 05:11:54 PM
Hi mindmachine
Maybe something like this:
Code: [Select]
while [ ! -e "/mnt/$1*" ]
do
sleep 1
done
for i in `ls /mnt/$1[1-9]
sleep 1
do
mount /mnt/$i
done
This is untested and I'm a lousy scripter so some of my syntax may be incorrect. The idea is that when the first loop
finds any entry that begins with $1, it drops down to the second loop which mounts everything that begins with $i.
The extra sleep command is there in case the drive has multiple partitions to allow rebuildfstab to finish finding
everything.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 06, 2013, 05:16:27 PM
I will try this and report, what happened. Excellent idea, though!! I was thinking of something like this, but been more on the UDEV side, but of course, your solution is much more straight forward!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 06, 2013, 05:25:34 PM
Hi mindmachine
If this works, it might be a good idea to add an  if  clause that  greps for  $i  in  mtab  and only mounts if  $i  is not
found so you don't try to double mount the same device. I don't know if udev will call your script twice if a drive
has two partitions, but if it does, that will take care of it.

Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 06, 2013, 05:39:05 PM
Yeah, that could be done via mtab... I'll try this tomorrow or on weekend, good point, anyway.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 06, 2013, 08:13:08 PM
Hi mindmachine
Hmmm, look at that. I had the sentence all worked out in my mind and managed to drop the mtab part when
I typed it. Thanks for pointing it out, post corrected.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 07, 2013, 02:04:41 PM
Well, here are the scripts. Problem is, they don't work :-). Problem is, mountpoints in fstab don't get created, no matter how long I tell the second script to wait before starting. So we never get a variable i! :-(
Why?? Is this a special udev thing?
Now here is the relvant line from 98-tc.rules:
Quote
ACTION=="add", SUBSYSTEM=="block",  RUN+="/bin/sh '/usr/sbin/rebuildfstab'", RUN+="/bin/sh -c '/usr/local/sbin/usb-mount %k'"

Here is the mount script.
Quote
#!/bin/sh
sleep 3 ##wait for rebuild of fstab and creation of dir in /mnt, BUT WE WAIT FOREVER :-)

for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) ## create correct variable
do
while [ ! -e '"grep $i" "/etc/mtab"' ] # check if already mounted
do
mount /mnt/$i ## if mtab is empty mount device
sleep 1
done

and the umount-script

Quote
#!/bin/sh
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) ## create correct variable i in case partition number is missing
do
umount /mnt/$i
sleep 1
rmdir /mnt/$1
rebuildfstab
done

 :'( Anyone got an idea, why rebuildfstab doesn't finish before the script is called?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 07, 2013, 02:48:18 PM
Hi mindmachine
Code: [Select]
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) ## create correct variable
do
while [ ! -e '"grep $i" "/etc/mtab"' ] # check if already mounted
do
mount /mnt/$i ## if mtab is empty mount device
sleep 1
done
The while loop does not belong in there and will probably run forever the way it's written. Plus you have 2 do
statements and only one done statement.
Code: [Select]
#!/bin/sh
sleep 3 ##wait for rebuild of fstab and creation of dir in /mnt, BUT WE WAIT FOREVER :-)

for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) ## create correct variable
do
    if grep $i /etc/mtab
    then
        continue    # $i already exists in mtab, move on to next value in $i
    fi
    mount /mnt/$i ## if mtab is empty mount device
    sleep 1
done
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 07, 2013, 04:32:22 PM
You are right, shame on me, I did upload an old test script....

Still the main problem is unsolved. It doesn't matter where I execute the rebuildfstab, udev rule or script. It results in both cases in waiting for the script to finish, before rebuildfstab pipes the values into fstab.
So we never get a working %i.  Is there a bash command to force rebuildfstab to finish, before we go on?
 
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 07, 2013, 04:51:22 PM
Hi mindmachine
This will continue to execute while rebuildfstab is still running:
Code: [Select]
while pidof rebuildfstab; do sleep 1; done
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 08, 2013, 04:55:18 AM
Okay, it won't work for unkonwn reasons. One question coming to my mind is the "grep $i /etc/mtab" part. Should this not be like: "as long as $i is NOT in mtab, go on"? This looks more like "if $i IS in mtab, go on...
Wouldn't this have to be corrected to
Quote
if
[ ! -e '"grep $i" "/etc/mtab"']
then
continue
fi

I do have to check fstab for blockdevices, as checking /mnt/ would return an unwanted slash like sdb1/ oder sdb/ so I would have to tweak the result. In fstab it doesn't matter 'cause cutting just letter 6-9 as it goes like /dev/sdb with blank or /dev/sdb1, so it returns in any case either sdb or sdb1. I also tried to create a file like "echo $i  > /tmp/var.txt" and check the output. That also works, but somehow, drive still doesn't get mounted. I'm at an end right here and don't know, how to go on.
Anyway, here is the script:
Quote
#!/bin/sh
rebuildfstab
while
pidof rebuildfstab
do
sleep 1
done
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
if
grep $i /etc/mtab
then
continue
fi
mount /mnt/$i
done
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 08, 2013, 12:34:49 PM
Hi mindmachine
The  continue  statement does not mean proceed forward. It means go back to the  for  statement and get the
next value in $i.
Quote
[ ! -e '"grep $i" "/etc/mtab"']
The  -e  tests for the existence of a file, and will likely always evaluate to false as used. The added  !  would force it to always
be true.
Try
Code: [Select]
#!/bin/sh
rebuildfstab
while pidof rebuildfstab
do
sleep 1
done
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
grep -q $i /etc/mtab
[ 0 -ne $? ] && mount /mnt/$i
done
The  $?  is the return code from grep. If  $i  is found, it equals zero, otherwise it's one.
If you don't mind my asking, are you by any chance German?
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 08, 2013, 06:00:10 PM
Quote
If you don't mind my asking...
No, I don't mind :-). Google for "Bavaria".  How did you know? By my accent or due to the fact that I wear lederhosen, a ridiculous hat and have a krauty smell?  ::)
Is this Long Island location true? I've been to the states just once, more than 20 years ago, to a place about 200 miles west of Chicago.

Thank you for the detailed explanation of the used commands and rules. I've never been a lot into scripting, use it once in a while, find it quite interesting and usually nick my code snippets from the web :-)!

Still, the script doesn't do as expected and I don't know why. Executed from cmd it works, if provided with sdb sdb1 or whatever. If run by udev, it does not. %k and $i, when echoed to a file turn out to be correct, fstab is created, mountpoint is created, but the drive fails to be mounted.
Normally I would expect the script to stay in a loop as long as sdb1 is not found in mtab, but ps -A doesn't show it. So I replaced the mount command by an echo "nonono >> /tmp/nono.txt" and this file is written (once). So the mount command for some reasons would be executed (even if it returns some kind of error) but whatever happens after that, I don't know. It should stay in a loop, until mtab has changed, shouldn't it? So normally, nono.txt would be filled up with nononos forever, but this also doesn't happen.

Even if I replace the $i with sdb1, it doesn't get mounted. So your script (which is quite a cool and very short piece of work as I have to admit) is definitely working but we are for unknown reasons not able to execute the mount command this way. What I do not understand is, why it does work, when executed by cmdline...
Frustrating, isn't it??!!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 08, 2013, 08:38:40 PM
Hi mindmachine
Quote
How did you know?
Observation and a hunch. You used the word  oder  instead of  or. My parents are German and I still remember a
few words. Yes, I'm on Long Island.
Quote
I've never been a lot into scripting, use it once in a while, find it quite interesting and usually nick my code snippets from the web :-)!
My scripting skills leave a lot to be desired. I often look through roberts scripts for inspiration when I have trouble
doing something.
Quote
Still, the script doesn't do as expected and I don't know why. Executed from cmd it works, if provided with sdb sdb1 or whatever. If run by udev, it does not. %k and $i, when echoed to a file turn out to be correct, fstab is created, mountpoint is created, but the drive fails to be mounted.
Actually, the fact that it works from the command line sounds like a major step in the right direction.
It sounds like the mount command is failing for some reason. Try changing the mount command in the script to:
Code: [Select]
[ 0 -ne $? ] && mount /mnt/$i &>/home/tc/mount.txtThen plug in the drive and see if  mount.txt  provides any insight.

     [EDIT]: Added path to mount.txt
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 09, 2013, 02:30:21 AM
Hi mindmachine
I normally just use the mount tool, but I decided to try this on my own machine. This is the mount script I used:
Code: [Select]
#!/bin/sh
echo `date +"%T"` >> /home/tc/mounttime
echo $1 >> /home/tc/mounttime
rebuildfstab
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
echo "Cat returned $i" >> /home/tc/mounttime
grep -q $i /etc/mtab
[ 0 -ne $? ] && mount /mnt/$i
done
And I modified the  add  line in  /etc/udev/rules.d/98-tc.rules  like this:
Code: [Select]
KERNEL=="ram*", SUBSYSTEM=="block", GOTO="tc.rules_end"
KERNEL=="loop*", SUBSYSTEM=="block", GOTO="tc.rules_end"
#ACTION=="add",         SUBSYSTEM=="block",     RUN+="/bin/sh -c '/usr/sbin/rebuildfstab'"
ACTION=="add",          SUBSYSTEM=="block",     RUN+="/bin/sh -c '/home/tc/mount.sh %k'"
ACTION=="remove",       SUBSYSTEM=="block",     RUN+="/bin/sh -c '/usr/sbin/rebuildfstab'"
LABEL="tc.rules_end"
None of my thumb drives are NTFS but the FAT16 drive I plugged in was detected and mounted. One interesting
thing I noticed is that the mount script got called twice. You'll note I log some info to a file called  mounttime:
Code: [Select]
tc@box:~$ cat mounttime
01:55:22
sdc
01:55:25
sdc1
Cat returned sdc1
tc@box:~$
It shows the first time it's called with  sdc  which is not found when catting fstab. The second time it's called with  sdc1
which is found in fstab.
Try redirecting the output of the mount command as shown at the end of my previous post.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 09, 2013, 02:49:15 AM
Hi mindmachine
I forgot to mention, while I was playing with the script, I realized that this:
Code: [Select]
while pidof rebuildfstab
do
sleep 1
done
was not necessary. If rebuildfstab were run in the background, it would be need. Since it's run in the foreground, it
will never execute because rebuildfstab will already have finished executing.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 09, 2013, 06:14:07 AM
Hi Rich,
true, Fat16/32 is mounted without any problems but using an ntfs stick, we still receive
Quote
mount: mounting /dev/sdb1 on /mnt/sdb1 failed: No such device
just once (!) via mount.txt ???? So obviously, if run two times (cause of the two variables sdb and sdb1) one message is either not reported or there is actually no output.
So the command is executed, but somehow there is something relevant missing or some fact not known. 
I checked fstab by adding
Quote
[ 0 -ne $? ] && mount /mnt/$i &>/home/tc/mount.txt && cat /etc/fstab > /home/tc/fstab.txt
to our script and it shows, that at this time, sdb1 is already added correctly reccognized as ntfs-3g. I also checked via
fdisk -l &> /home/tc/fdisk for the existence of sdb1 before we start the grep mtab loop and it is also there.
So, we've got the correct fstab entry, we've got teh correct parameter, we've got the blockdevice recognized by the system, the script run by hand is working....
So, what is the cause for sdb1 not getting mounted?

EDIT: One thing I notice is, that mounting the drive manually will create an "fuseblk" entry in mtab. I don'Ät know if this is of any relevance, but just in case!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 09, 2013, 12:43:50 PM
Hi mindmachine
Code: [Select]
So obviously, if run two times (cause of the two variables sdb and sdb1) one message is either not reported or there is actually no output.The mount command should only have run once, when  sdb1  was passed in. I just reformatted a thumb drive to
NTFS using gparted. When I plugged it in, it auto mounted. Change the script to this:
Code: [Select]
#!/bin/sh
echo `date +"%T"` >> /home/tc/mounttime.txt
echo $1 >> /home/tc/mounttime.txt
rebuildfstab
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
echo "Cat returned $i" >> /home/tc/mounttime.txt
echo `cat /proc/filesystems` >> /home/tc/mounttime.txt
grep -q $i /etc/mtab
[ 0 -ne $? ] && mount /mnt/$i >>/home/tc/mounttime.txt 2>&1
done
Then, plug in the drive, and attach the  mounttime.txt file to your next post.
I'm using ntfsprogs, so if you want to try something else:
1. Remove ntfs-3g.tcz from your onboot.lst
2. install ntfsprogs.tcz
3. Comment out the  ln /usr/local/sbin/mount.ntfs-3g /usr/local/sbin/mount.ntfs  in your  bootlocal.sh
4. Reboot and and see if the drive mounts when you plug it in
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 09, 2013, 07:17:28 PM
Yep, man you did it!!! Here is my mounttime.txt:

Quote
tc@box:~$ cat mounttime.txt
01:02:03
sdb
01:02:05
sdb1
Cat returned sdb1
nodev sysfs nodev rootfs nodev bdev nodev proc nodev cgroup nodev cpuset nodev tmpfs nodev sockfs nodev usbfs nodev pipefs nodev anon_inodefs nodev rpc_pipefs nodev devpts nodev ramfs ext3 ext2 ext4 cramfs vfat iso9660 nodev nfs nodev nfs4 nodev fuse fuseblk nodev fusectl nodev pstore nodev mqueue squashfs ntfs

It's mounted r-xr-xr-x atm, though. mount -w doesn't help. Probably no other parameter as well, as I assume, that the driver doesn't support this?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 10, 2013, 01:18:03 AM
Hi mindmachine
I've been fighting with this for hours and I can't get it to auto mount in RW mode. It's probably something simple
but I'm not seeing it. I'm afraid I'm at my wits end on this one. Maybe someone else has some ideas.
Title: Re: Automount USB with UDEV rules, Problem
Post by: tinypoodle on March 10, 2013, 01:24:21 AM
Have you excluded that the read-only kernel driver would be in use?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 10, 2013, 01:48:29 AM
Hi tinypoodle
Now I have, lsmod shows it is being used. What I don't understand is why:
Code: [Select]
sudo ntfsmount /dev/$i /mnt/$imounts the drive RW from the command line but not when run from the script (with or without sudo).
Running the script:
Code: [Select]
#!/bin/sh
echo `date +"%T"` >> /home/tc/mounttime
echo $1 >> /home/tc/mounttime
rebuildfstab
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
echo "Cat returned $i" >> /home/tc/mounttime
mntcommand="mount /dev/$i /mnt/$i"
grep $i /etc/fstab | grep -q ntfs
[ 0 -eq $? ] && mntcommand="sudo /usr/local/bin/ntfsmount /dev/$i /mnt/$i"
grep -q $i /etc/mtab
[ 0 -ne $? ] && $mntcommand >>/home/tc/mount.txt 2>&1
done
from the command line using sudo and passing in sdc1 mounts it RW.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 10, 2013, 01:55:34 AM
Hi tinypoodle
Just a clarification, when udev calls the script, it mounts RO. Udev rule:
Code: [Select]
ACTION=="add",          SUBSYSTEM=="block",     RUN+="/bin/sh -c '/home/tc/mount.sh %k'"I'm guessing maybe it's how the run command is calling the script.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 10, 2013, 04:36:45 AM
Just a short comment, as I'm away for a 60th birthday the rest of the day....
This is what I experienced before. So using ntfs-3g doesn't mount at all, when using the script, but mounts rw, when used by cmdline. Using the ntfsprogs mounts (at least) ro by script, and rw, when called from cmdline. So this wopuld actually point to the udev-rule. That's where we should focus on. Maybe, we could write some local udev rule that takes care of this problem?

EDIT: Another thing I found out about UDEV is, that it is effectively paused while any commands, started by the "RUN+=" list assignment, are executed.
Source:
Quote
http://www.reactivated.net/writing_udev_rules.html#external-run

So it might be, that some udev command missing at this time or being executed after completion of our mount-script will have undesired effect? I#m not at home, but perhaps
Quote
sudo udevadm --debug monitor
might spit out something helpful? (Information mostly for me to try this evening :-)!)
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 12, 2013, 09:29:07 PM
Hi mindmachine
I think I have some answers, execute:
Code: [Select]
tc@box:~/tcprovides$ lsmod
Module                  Size  Used by    Tainted: P 
ntfs                   69632  0
cifs                  180224  2
speedstep_lib          12288  0
If the ntfs module shows up in the list, make sure you don't have any NTFS drives mounted, and then execute:
Code: [Select]
sudo modprobe -r ntfsThat will remove the RO driver. This version of the script auto mounted RW when I plugged in the drive:
Code: [Select]
#!/bin/sh
echo `date +"%T"` >> /home/tc/mounttime
echo $1 >> /home/tc/mounttime
rebuildfstab
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
echo "Cat returned $i" >> /home/tc/mounttime
mntcommand="mount /dev/$i /mnt/$i"
grep $i /etc/fstab | grep -q ntfs
[ 0 -eq $? ] && mntcommand="/usr/local/bin/ntfsmount /dev/$i /mnt/$i"
grep -q $i /etc/mtab
[ 0 -ne $? ] && $mntcommand >>/home/tc/mount.txt 2>&1
done
Turns out the mount applet loads the RO driver if you use it to mount an NTFS drive. Why it defaults to the
RO driver when ntfsmount is run from a script called by udev, I don't know.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 13, 2013, 04:08:54 AM
Hi Rich!
Yes, I can confirm, that this solution works. lsmod did not give any results to ntfs, but blackmarking the driver made the device mount as expected. Cool :-)! THANK YOU! I really feel sorry, that my problems, whenever posted in a forum, turn out to be a really pestering annoyance :-). I appreciate your help a lot and thanks for your time (hours, I suppose, if not days??) and effort!!!!!

Remaining problem is, when you plug in an external USB-drive with more than one partition, only the last one will use the ntfsmount command, the rest is mounted the "regular" way, that is "being not mounted at all" :-) . But I will think about this myself, where to include the correct loop until all partitions are mounted.

Next thing is, how do we deal with data integrity problems?  In another post, this problem is mentioned, as far as I do remember this correctly. We could take advantage of ntfscheck in those cases and mount with the -o force parameter afterwards.
Title: Re: Automount USB with UDEV rules, Problem
Post by: tinypoodle on March 13, 2013, 08:26:41 PM
Next thing is, how do we deal with data integrity problems?
forcemount and data integrity appear to be mutually exclusive.
Quote
We could take advantage of ntfscheck
Where have you seen that? Any reference?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 13, 2013, 11:40:24 PM
Hi mindmachine
Quote
I really feel sorry, that my problems, whenever posted in a forum, turn out to be a really pestering annoyance :-).
One mans pestering annoyance is another mans challenge.
Quote
Remaining problem is, when you plug in an external USB-drive with more than one partition, only the last one will use the ntfsmount command, ...
Confirmed, that would be a race condition. Here is a version that fixes it:
Code: [Select]
#!/bin/sh
mypid=$$
echo `date +"%T"`" New PID=$mypid device=$1" >> /home/tc/mounttime
while true
do
sleep 1
otherpid=999999
for i in `pidof mount.sh`
do
[ $i -lt $otherpid ] && otherpid=$i
done
[ $mypid -le $otherpid ] && break
done
echo `date +"%T"`" Current PID=$mypid device=$1" >> /home/tc/mounttime
rebuildfstab
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
do
echo `date +"%T"`" Cat returned $i" >> /home/tc/mounttime
mntcommand="mount /dev/$i /mnt/$i"
grep $i /etc/fstab | grep -q ntfs
[ 0 -eq $? ] && mntcommand="/usr/local/bin/ntfsmount /dev/$i /mnt/$i"
grep -q $i /etc/mtab
[ 0 -ne $? ] && $mntcommand >>/home/tc/mount.txt 2>&1
done
When the script starts, it retrieves the value of its PID. It then drops into an endless loop that sleeps for one second and
then gets the PIDs of any other copies of mount.sh that are running, and saves the pid with the lowest number. It then
compares its own PID with the lowest found. If its own PID is the lowest, it breaks out of the loop, otherwise it loops
again. I reformatted a thumb drive with four partitions, ntfs, ntfs, vfat32, and ext2. Here are the results when plugged in:
Code: [Select]
tc@box:~$ cat ./mounttime
00:16:09 New PID=31830 device=sdc
00:16:10 Current PID=31830 device=sdc
00:16:12 New PID=32132 device=sdc3
00:16:12 New PID=32133 device=sdc1
00:16:12 New PID=32131 device=sdc4
00:16:12 New PID=32134 device=sdc2
00:16:14 Current PID=32131 device=sdc4
00:16:18 Cat returned sdc4
00:16:19 Current PID=32132 device=sdc3
00:16:21 Cat returned sdc3
00:16:22 Current PID=32133 device=sdc1
00:16:24 Cat returned sdc1
00:16:25 Current PID=32134 device=sdc2
00:16:27 Cat returned sdc2
You can see the script being called four times at 00:16:12, followed by each instance of the script executing one at a
time in ascending PID order.

    [EDIT]: Changed script, result of cat command, and updated description.
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 14, 2013, 01:26:52 AM
Hi mindmachine
Before I edited my previous post, the script could still have failed had someone plugged a second device in prior to
all the partitions getting mounted on the first device. The script still has two possible failure modes that I can see.

1. If you look at the results of the cat command, you'll notice the new PIDs don't have to arrive in numerical order. If
it takes more than one second for all the instances of the script to start, a lower numbered PID could sneak in late
and start running before the current instance finishes. I think the chances of that happening are remote, but it is
possible. You could up the sleep command to two seconds if you want a greater safety margin.

2. Though I can't cite a reference, I seem to remember reading that the PID numbers eventually reach a maximum
value and wrap around and recycle unused numbers from the bottom of the range. If you happened to plug in a second
drive just after the PIDs wrap, and the first drive hasn't finished mounting, the script for the second drive will try to
start mounting the second drive. While possible, I think it's highly unlikely.

In both cases, the only result would be unmounted partitions.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 14, 2013, 04:17:29 AM
@ tinypoodle
Quote
forcemount and data integrity appear to be mutually exclusive.
That is true, of course  :)
I'm a bit confused right now, as I vaguely remember me trying to mount a broken ntfs-stick, getting some kind of "boot into Windows TWICE" error and beeing unable to mount. I then ran some command, I remember it to be ntfscheck to fix this problem. Now, doing some research, I can't find any reference to this. Strange, probably me getting old... :)... Sorry!

So what about ntfsfix (what most likely has been the command I used??!!) and the -o force command afterwards? Would ntfsfix be mighty enough to clear the worst problems, keep data integrity on the stick and make it mountable. We could then grep the output of a mount command (like mounttime.txt) for "TWICE" (or whatever) and run ntfsfix and try to mount the drive with -o force afterwards.
Would that be a practicable solution?  How do other distributions deal with these problems? In my strictly personal opinion, a working procedure for USB-automount would be an improvement, wouldn't it? How is this handled, when using the GUI-mount/unmount feature? This problem would occur there as well, if you do have a non integer device, right?

@Rich
Cool!  :)
I agree that a situation as described is quite unlikely. For my purposes, this solution would be fully sufficient and worst case, a device not mounting, would not be a show stopper at all.
For a reason of safety, we could build in a delay, before we start to run the script. If we add "sleep 2" before we start, we would in any case avoid mixing up the mount procedures. As I can see from your output, creation of the process PIDs for your sdc-device is handled within a second. Would a simple delay be an option? We could also check for existing mount processes at start and only delay and wait for the first (second, third..) process to finish, if we get a positive response.
Title: Re: Automount USB with UDEV rules, Problem
Post by: tinypoodle on March 14, 2013, 05:46:34 AM
@ tinypoodle
Quote
forcemount and data integrity appear to be mutually exclusive.
That is true, of course  :)
I'm a bit confused right now, as I vaguely remember me trying to mount a broken ntfs-stick, getting some kind of "boot into Windows TWICE" error and beeing unable to mount. I then ran some command, I remember it to be ntfscheck to fix this problem. Now, doing some research, I can't find any reference to this. Strange, probably me getting old... :)... Sorry!
I can remember having read about plans of a ntfsfsck utility long before ntfs-3g forked from ntfsprogs, but nothing has ever been realized. Simple fact there is no such a thing for Linux.

Quote
So what about ntfsfix (what most likely has been the command I used??!!) and the -o force command afterwards? Would ntfsfix be mighty enough to clear the worst problems, keep data integrity on the stick and make it mountable.
Not safely, IMHO

Quote
We could then grep the output of a mount command (like mounttime.txt) for "TWICE" (or whatever) and run ntfsfix and try to mount the drive with -o force afterwards.
Would that be a practicable solution?
Highly risky.
Have a look at the man page
http://linuxmanpages.net/manpages/fedora17/man8/ntfsfix.8.html
and if you are still not convinced then have a look at the source code of ntfsfix.
It's main achievement is to mark the volume for windows to run chkdisk at next boot.

Quote
How do other distributions deal with these problems?
Not sure why you talk about a problem.
Good practice under UNIX-like operating systems says to mount filesystems only when and as long as needed.
That is exactly where automount could play in (not sure if it would be compatible with core) - which has nothing to do with forcemounting.
http://linux.die.net/man/8/automount
http://tldp.org/HOWTO/Automount-1.html

Another good practice says to mount file systems where writing is not a need read-only. A common example would be to have a dedicated partition for /usr (required to be mounted at boot!) mounted read-only.

Quote
In my strictly personal opinion, a working procedure for USB-automount would be an improvement, wouldn't it?

With traditional scatter install distros, often the root partition gets first mounted read-only and remounted read-write after a fsck at boot time.
I guess there could be a way to implement a similar routine for removable storage which gets attached at runtime, but I can't see a big need for it.

With the emerge of udev there was a project "usbmount".
It is unmaintained and last development took place in 2005...
http://usbmount.alioth.debian.org/#history

Quote
How is this handled, when using the GUI-mount/unmount feature? This problem would occur there as well, if you do have a non integer device, right?

Only if a user actively mounts a non integer device.
What makes all the difference is that with any mount facilitation, be it by GUI, automount, or simply setting "mount" SUID, it is in the full capacity and responsibility of the user if they want a filesystem to be checked first or to mount it read-only or read-write.

Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 14, 2013, 06:15:33 AM
Thanks for clarification!!
Quote
Good practice under UNIX-like operating systems says to mount filesystems only when and as long as needed.

That is true and I fully agree. In my case, I do use TC as a plattform to run rdesktop to access WIN7 running in virtualbox on an linux server (my wife is using it). So to be able to also use USB-drives, I've been looking for a possibility to handle USB-sticks plugged into the Thinclient. These are handed over by network and should be rw.
Maybe I'm completely on a wrong trail to achieve this goal?? There must be some way to make these locally attached drives accessible, but how?
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 14, 2013, 05:11:19 PM
Hi mindmachine
After some thought I decided to approach the problem from a different angle. Rather than trying to fight the
asynchronous nature of the events calling the script multiple times, I changed the script so that it is always running
and handles events synchronously:
Code: [Select]
#!/bin/sh
QUEUE=/tmp/automountqueue
[ ! -e "$QUEUE" ] && mkfifo $QUEUE

while true
do
for i in `cat $QUEUE`
do
echo `date +"%T"`" Request to mount $i" >> /home/tc/mounttime
rebuildfstab
i=$(cat /etc/fstab | grep -w $i | cut -c 6-9) # we check fstab to avoid getting e.g. sdb1/ returned
echo `date +"%T"`" Cat returned $i" >> /home/tc/mounttime
[ -z $i ] && continue
echo `date +"%T"`" Mounting $i" >> /home/tc/mounttime
mntcommand="mount /dev/$i /mnt/$i"
grep $i /etc/fstab | grep -q ntfs
[ 0 -eq $? ] && mntcommand="/usr/local/bin/ntfsmount /dev/$i /mnt/$i"
grep -q $i /etc/mtab
[ 0 -ne $? ] && $mntcommand >>/home/tc/mount.txt 2>&1
done
echo `/usr/local/bin/date +"%T"`" Done" >> /home/tc/mounttime
done
The result after plugging in a thumb drive are:
Code: [Select]
tc@box:~$ cat mounttime
16:00:21 Request to mount sdc
16:00:25 Cat returned
16:00:25 Done
16:00:25 Request to mount sdc2
16:00:27 Cat returned sdc2
16:00:27 Mounting sdc2
16:00:28 Request to mount sdc1
16:00:30 Cat returned sdc1
16:00:30 Mounting sdc1
16:00:30 Request to mount sdc3
16:00:32 Cat returned sdc3
16:00:32 Mounting sdc3
16:00:33 Request to mount sdc4
16:00:34 Cat returned sdc4
16:00:34 Mounting sdc4
16:00:35 Done
SDC is skipped, and SDC2, SDC1, SDC3, and SDC4 are processed one at a time, no race conditions.
The add rule in  /etc/udev/rules.d/98-tc.rules  looks like this:
Code: [Select]
ACTION=="add", SUBSYSTEM=="block", RUN+="/bin/sh -c '/bin/echo %k >> /tmp/automountqueue'"The script must be run as root, and can be started automatically by your bootlocal.sh script.
Title: Re: Automount USB with UDEV rules, Problem
Post by: mindmachine on March 15, 2013, 09:41:34 AM
Just tested your script. It works nicely, when devices are plugged in to a running system. But when already plugged in at boot, they don`t get mounted at all, although it might be, that I made a mistake somewhere, by perhaps placing it on the wrong place in bootlocal.sh? Could someone please recheck this?
By plugging in while system is running, it mounts ntfs, fat16 and fat32 without problems as rewritable. 
EDIT:
For anyone willing to use the above script by Rich, here is the working umount script:
Quote
#!/bin/sh
for i in $(cat /etc/fstab | grep $1 | cut -c 6-9) ## create correct variable i in case partition number is missing
do
[ -z $i ] && continue
umount /mnt/$i
sleep 1
rmdir /mnt/$1
rebuildfstab
done
Edit as of "my brain came back"
Quote
But when already plugged in at boot, they don`t get mounted at all
Well, stupid me, of course it won't get mounted .... lol!!! Sorry!!
Title: Re: Automount USB with UDEV rules, Problem
Post by: Rich on March 16, 2013, 01:40:50 AM
Hi mindmachine
I forgot to mention you want the script to run in the background, so if you called the script automount.sh, your
bootlocal.sh would contain:
Code: [Select]
/Path/To/automount.sh &Without the  &  symbol, the script will run in the foreground and any commands in bootlocal.sh that are listed after
automount.sh will not execute.
Title: Re: Automount USB with UDEV rules, Problem [SOLVED WITH RESTRICTIONS]
Post by: mindmachine on March 16, 2013, 08:22:51 AM
Yepp, I know. Had done that already...
I published your solution at post 1 and also made clear that it may be potentially harmful to usb devices. Still it is a solution and as I can't think of a better way to get USB-stick automounted, this will do :-).

Many thanks for your friendly support. I appreciate this a lot and did learn again a lot about scripting and the way, Linux behaves.