WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
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!
« Last Edit: March 16, 2013, 05:27:54 AM by mindmachine »
------------------------------------------------------------
mindmachine

Offline Rich

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

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #2 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...)
« Last Edit: March 02, 2013, 11:02:44 AM by mindmachine »
------------------------------------------------------------
mindmachine

Offline Rich

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

Offline mindmachine

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

Offline Rich

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

Offline mindmachine

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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11629
Re: Automount USB with UDEV rules, Problem
« Reply #7 on: March 02, 2013, 01:19:58 PM »
Hi mindmachine
Does /etc/fstab say your drives are being mounted  ro?

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #8 on: March 02, 2013, 01:23:02 PM »
it gives noauto,users,exec for sdb1 (ntfs-Filesystem)
------------------------------------------------------------
mindmachine

Offline Rich

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

Offline Rich

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

Offline mindmachine

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

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11629
Re: Automount USB with UDEV rules, Problem
« Reply #12 on: March 02, 2013, 02:44:45 PM »
Hi mindmachine
What does this command return:
Code: [Select]
cat /etc/mtab | grep sdb1

Offline mindmachine

  • Newbie
  • *
  • Posts: 45
Re: Automount USB with UDEV rules, Problem
« Reply #13 on: March 02, 2013, 02:49:52 PM »
Nuthin :-)
------------------------------------------------------------
mindmachine

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11629
Re: Automount USB with UDEV rules, Problem
« Reply #14 on: March 02, 2013, 02:58:42 PM »
Then your drive (sdb1 ntfs, right?) is not mounted.