Welcome! Log In Create A New Profile

Advanced

Automount USB drives

Posted by Almaz 
Automount USB drives
January 02, 2014 04:12AM
Is it possible to remove usb drive without unmounting and plug it back again so it'll get mounted again to the same share? Created udev rule so my usb drive always appears as /dev/myntfs. I've been trying to setup autofs but I can't get it to work with ntfs-3g, it just doesn't show up in fdisk -l. Can anyone show an example? Fstab works great but it's not for me, I'm trying to setup for a relative who doesn't know how to use linux. If I mount it manually then everything is working fine except autofs. It must be plug'n'play. Also tried usbmount but everytime I unplug usb drive and plug it back, it keeps changing path so it won't work. Path must be always the same when hot plugging.



Edited 1 time(s). Last edit at 01/02/2014 04:15AM by Almaz.
Re: Automount USB drives
January 02, 2014 06:05AM
Udev solution was posted here (long time ago). I will have to look for it in my bookmarks.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 02, 2014 10:32AM
Here is how I set up my rootfs to automount USB drives. If you want a true hot-plug that people can just pull the drive out any time, then always use the 'sync' option in pmount. The 'sync' option will result in slower write speed, but it will ensure no corruption:
How to automount USB drives

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 02, 2014 06:32PM
Awesome, it works great with ext2/3 drives but it doesn't work with NTFS-3G :( I'll see if I can find a work around with ntfs-3g
Re: Automount USB drives
January 02, 2014 06:50PM
Another problem which I noticed with playing udev my system becomes read-only until I reboot. Usually it happens when I hot plug and unplug usb ntfs drive a few times. For example when I try to install something I get.

W: Not using locking for read only lock file /var/lib/dpkg/lock
E: Unable to write to /var/cache/apt/
E: The package lists or status file could not be parsed or opened.

If I try to edit any files in /etc directory I get the message it's read-only until I reboot. What could be a problem, I just can't pin point.
Re: Automount USB drives
January 03, 2014 12:44AM
I use this one and works well for all partition types

http://forum.doozan.com/read.php?2,13896,13970#msg-13970
Re: Automount USB drives
January 03, 2014 01:07AM
Thanks Dinjo. It works well except NTFS drives speed are very slow 6MB. A quick modification to the script now it's 22MB.


KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Do not mount devices already mounted somewhere else to avoid entries for all your local partitions in /media
ACTION=="add", PROGRAM=="/bin/grep -q ' /dev/%k ' /proc/self/mountinfo", GOTO="media_by_label_auto_mount_end"

# Open LUKS partition if necessary
PROGRAM=="/sbin/blkid -o value -s TYPE %N", RESULT=="crypto_LUKS", ENV{crypto}="mapper/", ENV{device}="/dev/mapper/%k"
ENV{crypto}=="", ENV{device}="%N"
ACTION=="add", ENV{crypto}!="", PROGRAM=="/usr/bin/xterm -display :0.0 -e 'echo Password for /dev/%k; /sbin/cryptsetup luksOpen %N %k'"
ACTION=="add", ENV{crypto}!="", TEST!="/dev/mapper/%k", GOTO="media_by_label_auto_mount_end"

# Global mount options
ACTION=="add", ENV{mount_options}="noatime"
# Filesystem-specific mount options
ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="vfat", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"
ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="ntfs", ENV{mount_options}="%E{mount_options},utf8,big_writes,gid=100,umask=002"
ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="ext4", ENV{mount_options}="%E{mount_options},data=writeback,barrier=0,nobh,errors=remount-ro"

# Get label if present, otherwise assign one
PROGRAM=="/sbin/blkid -o value -s LABEL %E{device}", ENV{dir_name}="%c"
# Use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="hd-%k"

# Mount the device
ACTION=="add", ENV{dir_name}!="", RUN+="/bin/mkdir -p '/media/%E{dir_name}'", RUN+="/bin/mount -o %E{mount_options} /dev/%E{crypto}%k '/media/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'"
ACTION=="remove", ENV{crypto}!="", RUN+="/sbin/cryptsetup luksClose %k"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# Exit
LABEL="media_by_label_auto_mount_end"



Edited 1 time(s). Last edit at 01/03/2014 01:09AM by Almaz.
Re: Automount USB drives
January 03, 2014 01:53AM
ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="ntfs", ENV{mount_options}="%E{mount_options},utf8,big_writes,gid=100,umask=002"

FYI, regarding big_writes, this should be changed on-the-fly with bind -o remount to optimize the transfer speed, depending on what the usual mode of operation is.

Your ntfs-3g behaves very strange. I never have this problem with the udev rules above.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 03, 2014 01:56AM
Bodhi, can you be a little bit more specific? What exactly seems to be strange? Which logs would you like to see?
Re: Automount USB drives
January 03, 2014 02:00AM
Almaz,

The behavior you decribed in this post above: http://forum.doozan.com/read.php?2,14617,14634#msg-14634.

But since you are using dinjo's version without problem, then I guess you can ignore it.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 04, 2014 09:11PM
bodhi Wrote:
-------------------------------------------------------
> Almaz,
>
> The behavior you decribed in this post above:
> http://forum.doozan.com/read.php?2,14617,14634#msg
> -14634.
>
> But since you are using dinjo's version without
> problem, then I guess you can ignore it.


You are right if I use big_writes in udev then my system becomes read only but without big_writes my ntfs drives speed only 6MB instead of 20+ MB with big_writes. I'm using ntfs-3g, what should I check to get automount ntfs working properly?
Re: Automount USB drives
January 04, 2014 11:22PM
I written this script to mount any disk with label after the kernel has booted to rc.local. This is useful to handle the mounting of HDDs that had timed out error during boot. This script should be excuted in /etc/rc.local.

cat /etc/rc.local
root@Dockstar:~# cat /etc/rc.local 
#!/bin/sh -e 
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

/root/block-storage.mount
…

exit 0


cat /root/block-storage.mount
#!/bin/sh
# Copyright © 2013 bodhi
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# block-storage.mount
#
# Mount all block devices to /media using its label.
#
# Assumption: - rootfs partition has label "rootfs" so it will be skipped
#

label_list=`ls -1 /dev/disk/by-label/`
logger -s -i "Mounting all unmounted disks with label: $label_list (skip rootfs)"

for disk_label in $label_list; do
   
   if [ $disk_label != "rootfs" ]; then
   
      if  ! /bin/mountpoint /media/$disk_label -q ; then
         if [ ! -d /media/$disk_label ]; then
            mkdir /media/$disk_label
         fi

         FSTYPE=`udevadm info --query=property --name=/dev/disk/by-label/$disk_label | grep ID_FS_TYPE | cut -c 12-`

         if  [ $FSTYPE = "ntfs" ]; then
            mount -L $disk_label -o noatime,big_writes /media/$disk_label
            logger -s -i Mounted $disk_label
         else
            mount -L $disk_label -o noatime /media/$disk_label
            logger -s -i Mounted $disk_label
         fi

      fi
   fi

done

logger -s -i "Done mounting disks with label"  

exit 0

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)



Edited 1 time(s). Last edit at 01/05/2014 03:10AM by bodhi.
Re: Automount USB drives
January 04, 2014 11:39PM
Thanks Bodhi
Re: Automount USB drives
January 05, 2014 01:07AM
Sorry looks like the script was cut off when I posted. Will post the corrected version.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 05, 2014 03:13AM
I've updated the post above.

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 19, 2014 11:53AM
bohdi,
This is my current startup mount script. I still use pmount but you see its expanded since my original those years ago. It cleans up all stale mount points left over from an ugly shutdown before mounting the connected devices. And I found a few bugs with the original related to disk labels with spaces which required additional quoting and changing IFS to \n as I still make use of bash arrays. I do like your use of udevadm instead of blkid. I haven't touched this script in a while. It may be time for an update :)

#!/bin/bash

# remove any stale pmount locations
PMOUNT=".created_by_pmount"
ROOT="/media"
OLDIFS="$IFS"
IFS=$'\n'
for DIR in $( ls -d1 $ROOT/* )
do
  # a stale pmount location has exactly two entries
  # /media/foo
  # /media/foo/.created_by_pmount
  TEST=( `find "$DIR"` )
  if [ ${#TEST[@]} = 2 ] &&
     [ "${TEST[0]}" = "$DIR" ] &&
     [ "${TEST[1]}" = "$DIR/$PMOUNT" ];
     then
       # remove in the safest possible way
       # instead of using rm -fR $DIR
       echo "Removing stale pmount: $DIR"
       rm "${TEST[1]}"
       rmdir "${TEST[0]}"
  fi
done

# find pmount binary
PMOUNT=`which pmount`
if [ -z "$PMOUNT" ]
then
 echo "Error: cant find pmount: $0"
 IFS="$OLDIFS"
 exit 1
fi

pmount()
{
  local IFS="$OLDIFS"
  echo "Attempting to pmount: $DEV"
  BLKID=( `/sbin/blkid "$ROOT/$DEV"` )
  echo "${BLKID[@]}"
  for ITEM in ${BLKID[@]}
  do
    #echo "$ITEM"
    TEST=`expr match "$ITEM" 'TYPE=\"\(.*\)\"'`
    TYPE=${TEST//\"/}
    # only pmount partitions with a valid file system
    # which is not swap
    if [ -n "$TYPE" ] && [ "$TYPE" != "swap" ]
    then
      echo `$PMOUNT -t $TYPE $OPTIONS "$ROOT/$DEV" "$DEV"`
      break
    fi
  done
}

# pmount any usb storage devices attached at boot
OPTIONS="--sync --noatime --umask 000"
ROOT="/dev/usb-storage"
for DEV in $( ls -1 $ROOT )
do
  pmount
done

IFS="$OLDIFS"
exit 0
Re: Automount USB drives
January 20, 2014 12:31PM
kraqh3d,

Thanks, indeed I have not thought about the case where the disk label has embedded spaces!

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Re: Automount USB drives
January 20, 2014 02:55PM
Heh, neither did I until it bit me :)
Re: Automount USB drives
January 20, 2014 11:31PM
And good to see you're back :)

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)
Author:

Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: