USB OTG mount unmount drive scripts

Shardul SethShardul Seth wrote on 14 Jun 2012 16:10

Summary

You will need a phone / tab with USB OTG enabled, or a custom kernel or modules to enable the function..

Tested with Sony Ericsson Xperia 2011 line devices with custom kernel :)

Script

USB Mount Drives

#!/system/bin/sh
#
# Mount USB OTG Storage device - shardul_seth@xda
#
sdn=0
if [ "`ls -l /dev/block/sda? 2>/dev/null | grep sda`" ]; then
    for sd in `ls /dev/block/sda?`; do 
        if [ ! -d "/sdcard/usb_drive_$sdn" ]; then 
            mkdir /sdcard/usb_drive_$sdn
            chmod 777 /sdcard/usb_drive_$sdn
            mount $sd /sdcard/usb_drive_$sdn
            echo "$sd mounted on /sdcard/usb_drive_$sdn"
            sdn=`expr $sdn + 1`
            if [ ! -z $SM_VERSION ]; then echo showToastLong "$sdn Drives mounted!" >&$SM_GUIFD; fi
        fi
    done
else
    echo "No drive found! Exiting.."
    if [ ! -z $SM_VERSION ]; then echo showToastLong "No drive found! Exiting.." >&$SM_GUIFD; fi
    exit 1
fi
exit 0

USB Unmount Drives

#!/system/bin/sh
#
# Unmount USB OTG Storage device - shardul_seth@xda
# IMPORTANT: Run as Root
#

if [ ! -z $SM_VERSION ]; then 
echo showYesNoDialog \"USB OTG\" \"Unmount USB OTG drive?\" >&$SM_GUIFD
read res <&$SM_GUIFD
    if [ "$res" != "y" ]; then 
        exit 0
    fi
fi
sdn=0
if [ "`ls -l /dev/block/sda? 2>/dev/null | grep sda`" ]; then
    for sd in `ls /dev/block/sda?`; do
        umount $sd
        sleep 2s
        if [ ! "`mountpoint /sdcard/usb_drive_$sdn | grep -c "not"`" ]; then umount -f $sd ; fi
        sleep 2s
        if [ -d "/sdcard/usb_drive_$sdn" ]; then 
            rmdir /sdcard/usb_drive_$sdn
            echo "$sd umounted from /sdcard/usb_drive_$sdn"
            sdn=`expr $sdn + 1`
            if [ ! -z $SM_VERSION ]; then echo showToastLong "$sdn Drives unmounted!" >&$SM_GUIFD; fi
        fi
    done
else
    echo "No mounted USB drive found! Exiting.."
    if [ ! -z $SM_VERSION ]; then echo showToastLong "No mounted USB drive found! Exiting.." >&$SM_GUIFD; fi
    exit 1
fi
exit 0
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License