USB and safe umount script - possibly solved

Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware
Author Message
MrAlvin
DD-WRT User


Joined: 07 Jan 2007
Posts: 457
Location: Denmark

PostPosted: Fri Jan 09, 2009 22:08    Post subject: USB and safe umount script - possibly solved Reply with quote
Having begun using USB drives with my Asus Router over the last months, I have been experiencing some troubles with keeping the partitions running smoothly. And I see that a few in the forums have also lately had some trouble with files not closing properly upon shutdown/reboot/unplug etc. so I decided to do some testing.

This is what I have come up with:
Code:
# un-mount drives/folders on the USB drive
echo "Un-mounting /mnt and /opt"
 check1='/opt/etc'
 check2='/mnt/opt'
 aw=1
 while [ $aw -lt 10 ]
 do
   umount -rl /opt
   umount -rl /mnt
   if [ -d $check1 ]; then
     echo "/opt still mounted"
     aw=`expr $aw + 1`
   elif [ -d $check2 ]; then
     echo "/mnt still mounted"
     aw=`expr $aw + 1`
   else
     echo "Un-mount done"
     exit # if everything goes right, the script will exit in this line.
   fi
   sleep 2
   echo testing $aw
 done
echo "Failed to un-mount USB drives"


Umount happens in a loop with a counter (aw) so it is possible to specify some kind of timeout before it goes into an endless loop.
Getting the arithmetic to work took hours of testing, as a space in the wrong place (or one space missing) will cause it to NOT do the math bit AT ALL.

Just for the fun of it, I tested with the umount commands using both -l (lazy) and -r (if umount fail, then immediately try to remount as read only) parameters.

I wanted to un-mount two drive mounts, so there are two tests in the script, and the loop will keep trying until both mounts are un-mounted. More tests can easily be added to accommodate for more drive mounts being un-mounted properly.


The Positive Surprise
Much to my surprise, this script seems to be able to un-mount drives even when I run the script from the drive it self. Like /mnt being the active folder, and I activate sh unplug.usb (the script is part of my unplug.usb script, but I think it will eventually also become a part of my reboot command).


I am curious to hear if others will have the same luck as I have had with this "safe-umount-script".
Sponsor
blueman2
DD-WRT User


Joined: 24 Nov 2008
Posts: 62

PostPosted: Fri Jan 09, 2009 23:40    Post subject: Reply with quote
I have a similar script, but I find for it to work, I need to first kill all processes which rely on my opt or shared drive directory. For example, if I leave smbd running, I cannot unmount the directory which I am sharing via smbd. Same for vftpd.

So I add 'killall' for any such process at the start of my unmount script.
MrAlvin
DD-WRT User


Joined: 07 Jan 2007
Posts: 457
Location: Denmark

PostPosted: Sat Jan 10, 2009 0:49    Post subject: Reply with quote
I also have the killall commands at the beginning of my unplug.usb script.

Just for fun I tried to run just the umount script above. I even ran the script from /mnt/unplug.usb location.
And then tested with mount and ps command to see the status of things.

Mount showed both /mnt and /opt gone, despite the fact that ps showed both proftpd, smdb and nmdb still running. I can also still get a FTP connection, but naturally it shows no files, as the HD really IS un-mounted.

Seems like the umount -rl command really is a somewhat ultimate-force umount command.

Using WinSCP I copied a large file to /mnt/data, and in the middle of the operation ran the umount command. The whole file continued to be copied, but immediately after finishing. the drive was unmounted, so WinSCP could not even update its file list.

and
Code:
root@DD-WRT:/# /jffs/sbin/e2fsck -p  /dev/scsi/host0/bus0/target0/lun0/part1
optware: clean, 62/50200 files, 17325/200780 blocks

reports a clean bill of health.

Again I am positively surprised.


Last edited by MrAlvin on Sat Jan 10, 2009 0:57; edited 1 time in total
AtMi
DD-WRT User


Joined: 04 Jan 2009
Posts: 90
Location: Vilnius, Lithuania

PostPosted: Sat Jan 10, 2009 0:54    Post subject: Reply with quote
I used from you MrAlvin -rl ( thanks )option (only "diskas" I left without theses options, there is my data) and it is my shutdown script:

wrote:

#!/bin/sh

log=/tmp/USB.log
diskastestpath=/mnt/diskas/.config
opttestpath=/opt/share
mnttestpath=/mnt/opt/share/doc/transmission
maxtime=10
echo "--------------------------------------------------------------------"
echo "--------------------------------------------------------------------" >> $log
echo "Welcome to Un-mount USB on $(date)"
echo "Welcome to Un-mount USB on $(date)" >> $log
echo ""
echo "" >> $log

# stop all programs that has open files on the USB drive

echo "Stopping Torrent"
killall transmission-daemon
n=1
while true
do
[ -z "`pidof transmission-daemon`" ] && break
[ $n ge $maxtime ] && break
sleep 1
let n+=1
done
echo "Torrent stoped in $n sec $(date)"
echo "Torrent stoped in $n sec $(date)" >> $log

echo "Stopping smbd"
killall smbd
n=1
while true
do
[ -z "`pidof smbd`" ] && break
[ $n ge $maxtime ] && break
sleep 1
let n+=1
done
echo "smbd stoped in $n sec $(date)"
echo "smbd stoped in $n sec $(date)" >> $log

killall nmbd
n=1
while true
do
[ -z "`pidof nmbd`" ] && break
[ $n ge $maxtime ] && break
sleep 1
let n+=1
done
echo "nmbd stoped in $n sec $(date)"
echo "nmbd stoped in $n sec $(date)" >> $log

killall proftpd
n=1
while true
do
[ -z "`pidof proftpd`" ] && break
[ $n ge $maxtime ] && break
sleep 1
let n+=1
done
echo "ProFTPd stoped in $n sec $(date)"
echo "ProFTPd stoped in $n sec $(date)" >> $log

echo "Un-mounting /mnt/diskas"
umount /mnt/diskas
n=1
while true
do
[ ! -e $diskastestpath ] && break
[ $n -ge $maxtime ] && break
sleep 1
let n+=1
done
echo "DISKAS un-mounted in $n sec $(date)"
echo "DISKAS un-mounted in $n sec $(date)" >> $log

echo "Un-mounting /opt"
umount -rl /opt
n=1
while true
do
[ ! -e $opttestpath ] && break
[ $n -ge $maxtime ] && break
sleep 1
let n+=1
done
echo "OPT un-mounted in $n sec $(date)"
echo "OPT un-mounted in $n sec $(date)" >> $log

echo "Un-mounting /mnt"
umount -rl /mnt
n=1
while true
do
[ ! -e $mnttestpath ] && break
[ $n -ge $maxtime ] && break
sleep 1
let n+=1
done

echo "MNT un-mounted in $n sec $(date)"
echo "MNT un-mounted in $n sec $(date)" >> $log
echo "------------------------------------------------"
echo "------------------------------------------------" >> $log
echo "MOUNTED IF 10sec"
echo "MOUNTED IF 10sec" >> $log
echo "------------------------------------------------"
echo "------------------------------------------------" >> $log

I use test for all "killall" commands.
blueman2 wrote:

I cannot unmount the directory which I am sharing via smbd. Same for vftpd.

You can run umoun command like MrAlvin did - with -rl option, for my this option helped.

_________________
WRT600N V1.1 - 12672_mega_WRT610N
frater
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 2777

PostPosted: Sat Jan 10, 2009 1:14    Post subject: Reply with quote
Why aren't you using seperate scripts in /opt/etc/init.d that start with K10 or K90. This is trying to re-invent the wheel (nothing wrong with trying, but this almost never leads to a better alternative, I'm talking about the wheel now)

Many times these scripts are already provided for you by optware and you can control the sequence by using the number...

if a K* script is not provided but a S* script is, a symbolic link to the S* script is enough.....

for instance:

Code:
cd /opt/etc/init.d
ln -s S80transmission K20transmission
ln -s S30samba K60samba


write a K90unmount to unmount your disks

Your shutdown script only needs to contain this code:
Code:
for f in /opt/etc/init.d/K* ; do
[ -x $f ] && $f stop
done

_________________
Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge

DD-WRT v24-sp2 vpn (c) 2010 NewMedia-NET GmbH
Release: 12/16/10 (SVN revision: 15758M)
MrAlvin
DD-WRT User


Joined: 07 Jan 2007
Posts: 457
Location: Denmark

PostPosted: Sat Jan 10, 2009 1:27    Post subject: Reply with quote
Hi frater,
thanks for reminding us of the "best practice" way.

Now that we know the umount -rl command works with a K90unmount script (even when it is placed on /opt), we'll probably move towards the better init.d way of doing things. Wink
frater
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 2777

PostPosted: Sat Jan 10, 2009 2:19    Post subject: Reply with quote
MrAlvin wrote:
Hi frater,
thanks for reminding us of the "best practice" way.


hahahaha
I rarely look at who I'm writing to and just focus on the subject of the thread. Laughing

Just want to share my positive experience....

I'm also using a reverse proxy on my router for half a year now, but I just can't persuade Brainslayer or Eko to incorporate it in the mega-build...

Well... the first step is taken recently.... and we have the engine in place.... Now the chassis, some wheels (scripts) and finally the body (webpage)
Code:
# which pound
/usr/sbin/pound

_________________
Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge

DD-WRT v24-sp2 vpn (c) 2010 NewMedia-NET GmbH
Release: 12/16/10 (SVN revision: 15758M)
nnwb
DD-WRT Novice


Joined: 19 Aug 2008
Posts: 3

PostPosted: Sat Feb 07, 2009 23:12    Post subject: Reply with quote
Sorry, I am confused.

Quote:

if a K* script is not provided but a S* script is, a symbolic link to the S* script is enough.....

for instance:

Code:
cd /opt/etc/init.d
ln -s S80transmission K20transmission
ln -s S30samba K60samba


I don't understand how it should work.

As an example, the S20 startscript for OpenVPN...
There is no stop sequence in this script.
If I add a K* symlink to this script, why should it stop OpenVPN and don't start a new instance of it? Shocked Question
frater
DD-WRT Guru


Joined: 07 Jun 2006
Posts: 2777

PostPosted: Sun Feb 08, 2009 6:08    Post subject: Reply with quote
Quote:
There is no stop sequence in this script.
If I add a K* symlink to this script, why should it stop OpenVPN and don't start a new instance of it?


Most of the time (almost all S* scripts I've seen) have a section for start, stop, restart and status.

If it doesn't, you have to upgrade the script.

BTW.. It also needs the part of the code where it creates the parameter if it's not given.

Code:
if [ -z "$1" ] ; then
    case `echo "$0" | sed 's:^.*/\(.*\):\1:g'` in
        S??*) rc="start" ;;
        K??*) rc="stop" ;;
        *) rc="usage" ;;
    esac
else
    rc="$1"
fi

_________________
Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge

DD-WRT v24-sp2 vpn (c) 2010 NewMedia-NET GmbH
Release: 12/16/10 (SVN revision: 15758M)
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum