OpenWrt Forum Archive

Topic: auto mount usbstik on plugin

The content of this topic has been archived on 20 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hey,

my question is: Is it possible to mount a usbstick automatic to some directory?

I want to just plug in a usbstick an have it immediately available as a samba-share, without typing "mount" in the console.

Andy ideas?
In "/etc/hotplug.d/usb#" are some interesting scripts. Is it possible to tweak something here?


*thetruelighthouse

Found a solution:

I used parts of this idea: https://dev.openwrt.org/ticket/3432

Edit the file /etc/hotplug.d/usb/10-usb-storage like this:

#!/bin/sh

# Copyright (C) 2006 OpenWrt.org
case "$ACTION" in
    add)
        [ -n "${INTERFACE}" ] &&
        [ "$(expr substr ${INTERFACE} 1 2)" == "8/" ]  && {
            /sbin/usb-storage add&
        }
        ;;
    remove)
        [ -n "${INTERFACE}" ] &&
                [ "$(expr substr ${INTERFACE} 1 2)" == "8/" ]  && {
                        /sbin/usb-storage remove&
                }
        ;;
    *)
        logger "unknown action: $ACTION"
        ;;
esac

Edit the file /sbin/usb-storage like this:

#!/bin/sh

tstamp=$(date +%s)

case "$1" in
    add)
        echo "f" > /proc/diag/led/power
        parts=$(ls /dev/scsi/host*/bus0/target0/lun0/part*)
        for part in $parts; do
            logger "Checking partition $part"
            if [ ! -z "$(mount | grep $part)" ]; then
                logger "$part already mounted... aborting"
                continue
            fi
            host=$(echo $part | sed -e "s/.*host\(.\).*/\1/")
            spart=$(basename $part)
            mp=${spart}-${host}
            logger "Trying to mount $spart on host $host"
            if [ ! -d /mnt/$mp ]; then
                logger "Creating mountpoint /mnt/$mp"
                mkdir /mnt/$mp
            fi
            mount -o sync $part /mnt/$mp && logger "$part mounted as /mnt/$mp" || logger "failed to mount $part"
            for handler in /etc/usb-handlers.d/*; do
                if [ -x $handler ] ; then
                    logger "Running add handler: $handler"
                    $handler add /mnt/$spart
                fi
            done
        done
        echo "1" > /proc/diag/led/power
        ;;
    remove)
        for handler in /etc/usb-handlers.d/*; do
            logger "$handler"
            if [ -x $handler ] ; then
                logger "Running remove handler: $handler"
                $handler remove /mnt/$spart
            fi
        done    
        # The system won't remove the /dev nodes until the device is unmounted
        # dd 1 block and check if it succeeds
        mounts=$(mount | grep "/dev/scsi/host" | cut -d " " -f 1)
        logger "Unmounting ro: $mounts"
        for mnt in $mounts; do
            logger "Checking $mnt"
            dd if=$mnt of=/dev/null bs=1 count=1 > /dev/null; ret=$?
            if [ $ret -gt 0 ]; then
                logger "dd failed on $mnt, trying to unmount"
                spart=$(mount | grep $mnt | cut -d ' ' -f 3)
                umount "$spart" && \
                logger "$mnt unmounted" && \
                logger "remove $spart dir from /mnt/" && \
                rm -rf $spart || \
                logger "Could not umount $mnt"
            fi
        done
        echo "0" > /proc/diag/led/power
        ;;
    *)
        logger "unknown"
esac

Now every time you plug in an USB-Stick oder USB-HD there will be created a mount-point in /mnt/.
After you unplug the device it will try to unmount it and removes the mount-folder in /mnt/.

I hope this helps someone!
Have fun!

thetruelighthouse, your script is greeeat for me! I will try it later..
thanks..

but I got a question, what is the /etc/usb-handlers.d/* mean?
I do not have this folder on my wrt..

(Last edited by doggie on 3 Jul 2008, 10:42)

doggie, good question!

I didn't care...
I never got a logmessage like "Running add handler:..."

I you look here: https://dev.openwrt.org/ticket/3432

anonymous  wrote:

this also enables handlers on adding removing usb devices in /etc/usb-handlers.d/
i use it to scan newly inserted sticks for music, symlink it and update mpd accordingly

Or look here: http://wiki.openwrt.org/UsbAudioHowto2 at 1.4 HOWTO
"/etc/usb-handlers.d/" is also mentioned.

looks like is is an self-written handler...
it tries to start a script "add" and "remove"
I guess on his machine this scripts contain some commands to control mpc...

Hi thetruelighthouse,

thanks for your scripts.
This is exactly what I was looking for.

Unfortunately, they do not work as I would expect.

The output of "mount" of my WL500gx is:

rootfs on / type rootfs (rw)
/dev/root on /mnt/rom type squashfs (ro)
none on /dev type devfs (rw)
none on /proc type proc (rw)
none on /tmp type tmpfs (rw,nosuid,nodev)
none on /dev/pts type devpts (rw)
/dev/mtdblock/4 on /jffs type jffs2 (rw)
mini_fo:/jffs on /mnt type mini_fo (rw)
/dev/scsi/host0/bus0/target0/lun0/part2 on / type ext3 (rw,noatime)
none on /proc/bus/usb type usbfs (rw)
/dev/scsi/host0/bus0/target0/lun0/part1 on /usb type vfat (rw)

However if I insert a usb stick, it recognizes it, but does not mount it automatically.
Output of "fdisk -l":

Disk /dev/scsi/host0/bus0/target0/lun0/disc: 1043 MB, 1043333120 bytes
255 heads, 63 sectors/track, 126 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

                                 Device Boot      Start         End      Blocks   Id  System
/dev/scsi/host0/bus0/target0/lun0/part1               1          96      771088+   b  W95 FAT32
/dev/scsi/host0/bus0/target0/lun0/part2              97         126      240975   83  Linux

Disk /dev/scsi/host1/bus0/target0/lun0/disc: 20.0 GB, 20003880960 bytes
255 heads, 63 sectors/track, 2432 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

                                 Device Boot      Start         End      Blocks   Id  System
/dev/scsi/host1/bus0/target0/lun0/part1   *           1        2431    19526976    c  W95 FAT32 (LBA)

The output put "mount" is still identical. I would expect to find a new entry in the output of "mount". Also it appears the script "/sbin/usb-storage" creates a new subdirectory in /mnt, but I can't find anything new in there either.

Which file access permissions are required for "/sbin/usb-storage" and "/etc/hotplug.d/usb/10-usb-storage"?

Do I need to register / enable it like a service in "/etc/init.d" somewhere?

What might have gone wrong? Any help is greatly appreciated.

Thanks in advance,

slowjoe

Perhaps I should mention that I use Kamikaze 7.09. Did not notice that this thread is in the WhiteRussian section of this forum. Found it when using Google...

- slowjoe

hey slowjoe,

the files "/sbin/usb-storage" and "/etc/hotplug.d/usb/10-usb-storage" are marked as executable on my WL-500G Premium. The user is root.
I'm using KAMIKAZE (7.09) on my box as well... (probably a wrong forum section)

Check the permissions for your "/mnt/"-Folder.
Try to debug the script. It drops some useful information to the log. (try "logread -r" when you plug your usb-device.)

The script will only mount USB-devices with an USB-ID starting with "8/"...

The file "/etc/hotplug.d/usb/10-usb-storage" will be executed as soon as you plug in a usb-device.
Add a line like " logger "my test: $ACTION" " and you will see if the script is called successfully...

thetruelighthouse

Thanks for your update.
Since I modified the file permissions the script works well!

This script is a great relief! Thank you again!

The discussion might have continued from here.