WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Automount USB with UDEV rules, Problem [SOLVED WITH RESTRICTIONS]  (Read 35914 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #45 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.

« Last Edit: March 06, 2013, 08:09:14 PM by Rich »

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #46 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.
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #47 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.

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #48 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?
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #49 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

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #50 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?
 
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #51 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

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #52 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
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #53 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?

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #54 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??!!
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #55 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
« Last Edit: March 09, 2013, 02:28:49 AM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #56 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.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #57 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.

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #58 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!
« Last Edit: March 09, 2013, 07:06:20 AM by mindmachine »
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11639
Re: Automount USB with UDEV rules, Problem
« Reply #59 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