Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
script to umount busy devices
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
themoebius
Tux's lil' helper
Tux's lil' helper


Joined: 10 Mar 2004
Posts: 117

PostPosted: Wed Dec 08, 2004 11:48 am    Post subject: script to umount busy devices Reply with quote

I thought I'd post this hear in case anyone else was having the same problems I was. I couldn't umount my external hard drives/ipod/etc because they were always busy. I wrote a shell script that should be user-friendly enough and should take care of every situation. Copy/paste the code below into a text editor and save it as smartumount in your /usr/bin dir (as root) make sure it's executeable by using the command "chmod +x /usr/bin/smartumount"

First the script tries to umount the drive normally. It that fails, it tries to restart famd which is the most common problem. If that fails it tries to restart xinetd. If that fails it uses the command "fuser -ki <your-busy-drive>" which asks you if you want to kill each process that's using that folder. Be careful with this - you should know what each process is before you kill it. If that fails, probably because you didn't kill all the processes, it asks you if you want lazily unmount the volume, which removes the drive even though processes are still using it. I'm no expert, so let me know if I should make any improvements. Feel free to post this wherever you want.

USAGE:
smartumount folder-to-unmount

Example:
smartumount /mnt/ipod

Code:

#!/bin/bash

if [ `whoami` != "root" ]; then
   echo "You must run this as root"
   exit
fi

dir=$1

# first try unmounting it without doing anything special
testumount=`umount $dir 2>&1`
if [ `echo $testumount | grep "not mounted" | wc -l` -gt 0 ]; then
   echo "$1 isn't mounted, exiting"
   exit
fi

if [ `echo $testumount | grep busy | wc -l` -gt 0 ]; then
   echo "Having trouble, checking famd..."
else
   echo "unmounted $dir without any trouble..."
   exit
fi

# check famd
if [ `ps -e |grep famd|wc -l` -gt 0 ]; then
   /etc/init.d/famd restart
   # try unmounting again
   if [ `umount $dir 2>&1 | wc -l` -gt 0 ]; then
      echo "I tried restarting famd, but that didn't work. checking xinetd"
   else
      echo "Unmounted $dir by restarting famd"
      exit
   fi
else
   echo "famd isn't running so it couldn't be that.."
fi


# check xinetd
if [ `ps -e | grep xinetd | wc -l` -gt 0 ]; then
   /etc/init.d/xinetd stop
   # try unmounting again
   if [ `umount $dir 2>&1 | wc -l` -lt 1 ]; then
      /etc/init.d/xinetd start
      echo "Unmounted $dir by restarting xinetd"
      exit
   fi
   /etc/init.d/xinetd start
   echo "I tried stopping xinetd, but that didn't work."

else
   echo "xinetd isn't running so it couldn't be that.."
fi

echo
echo "I'm going to list processes that are using the folder you are
trying to umount. Answer whether you'd like to kill them or not.
You should look up each process before you answer. use the
following command in a separate shell:"
echo
echo "   ps -e | grep <pid>"
echo
echo "where <pid> is the number of the process."
echo
fuser -ki $dir
echo
if [ `echo $testumount | grep busy | wc -l` -gt 0 ]; then
   echo "OK, this is the last resort. Do you want to umount the volume using "
   echo "the -l option? According to the umount man page -l means:"
   echo
   echo "   Lazy unmount. Detach the filesystem from the filesystem
   hierarchy now, and cleanup all references to the filesystem as
   soon as   it is not busy anymore.  (Requires kernel 2.4.11 or later.)"
   echo
   echo "Use -l option? (y\n)"
   read yn
   if [ $yn == "y" ]; then
      umount -l $dir
   else
      echo "OK. Sorry I couldn't help"
   fi
else
   echo "You're all good :)"
fi

exit



Last edited by themoebius on Thu Dec 09, 2004 6:43 pm; edited 2 times in total
Back to top
View user's profile Send private message
Dhaki
Guru
Guru


Joined: 16 Jun 2004
Posts: 325
Location: Ticino - CH

PostPosted: Wed Dec 08, 2004 1:30 pm    Post subject: Re: script to umount busy devices Reply with quote

Nice!! :D

You have made a mistake in this part:
themoebius wrote:
Code:

#!/bin/bash
[CUT]
# check xinetd
if [ `ps -e | grep xinetd | wc -l` -gt 0 ]; then
   /etc/init.d/xinetd stop
   # try unmounting again
   if [ `umount $dir 2>&1 | wc -l` -lt 1 ]; then
      /etc/init.d/xinetd start
      echo "Unmounted $1 by restarting famd"
      exit
[CUT]

This "echo "Unmounted $1 by restarting famd"" should be this "echo "Unmounted $1 by restarting xinetd"", right? I'm translating the script in italian :D (I'm italian... so sorry for my english :( ...)
Back to top
View user's profile Send private message
themoebius
Tux's lil' helper
Tux's lil' helper


Joined: 10 Mar 2004
Posts: 117

PostPosted: Wed Dec 08, 2004 9:48 pm    Post subject: Reply with quote

yes, you're right I'll fix that.
Back to top
View user's profile Send private message
Appletalk
n00b
n00b


Joined: 08 Dec 2004
Posts: 1

PostPosted: Wed Dec 08, 2004 10:15 pm    Post subject: Reply with quote

Looks fine. Anyway, you could have used supermount.
The supermount-ng patch for the latest kernel is available at Con Kolivas kernel patch homepage.

Have a look:

http://ck.kolivas.org/patches/2.6/2.6.9/2.6.9-ck3/patches/
http://ck.kolivas.org/patches/2.6/2.6.10/2.6.10-rc3/patches/

Thanks for the effort. It'll be very useful for those who are not interested in patching the kernel source (automount is always available, but it's not the same)[/img]
Back to top
View user's profile Send private message
leha
n00b
n00b


Joined: 15 Apr 2004
Posts: 29

PostPosted: Wed Dec 08, 2004 11:25 pm    Post subject: Reply with quote

Quote:

echo
echo " ps -e | grep <pid>"
echo
echo "where <pid> is the number of the process."
echo
fuser -ki /mnt/music
echo


It looks like there is a mistake (from your test version probably )in the above part. Should not it be "fuser -ki $dir" ? instead of
Quote:
fuser -ki /mnt/music


It is really nice script though, I did not know about all this stuff.

Thank you
Back to top
View user's profile Send private message
themoebius
Tux's lil' helper
Tux's lil' helper


Joined: 10 Mar 2004
Posts: 117

PostPosted: Wed Dec 08, 2004 11:28 pm    Post subject: Reply with quote

yes indeed. Another error fixed...
Back to top
View user's profile Send private message
dkure
n00b
n00b


Joined: 20 Apr 2004
Posts: 51
Location: Sydney, Australia

PostPosted: Thu Dec 09, 2004 3:56 pm    Post subject: Reply with quote

Code:
   if [ `umount $dir 2>&1 | wc -l` -lt 1 ]; then
      /etc/init.d/xinetd start
      echo "Unmounted $1 by restarting famd"
      exit


Still have an error in there

It should be
Code:

      echo "Unmounted $1 by restarting xinetd"


Looks like a good script :D.
Back to top
View user's profile Send private message
JazzSax006
n00b
n00b


Joined: 03 Jun 2003
Posts: 38

PostPosted: Fri Dec 10, 2004 8:39 pm    Post subject: Reply with quote

Excellent! Thanks for posting this
Back to top
View user's profile Send private message
Chaosite
Guru
Guru


Joined: 13 Dec 2003
Posts: 540
Location: Right over here.

PostPosted: Fri Dec 10, 2004 8:58 pm    Post subject: Reply with quote

whats wrong with lazy umount...?
Code:
umount -l ${mntdir}
Back to top
View user's profile Send private message
smartdreamer
n00b
n00b


Joined: 17 Nov 2004
Posts: 17

PostPosted: Sat Dec 11, 2004 9:36 pm    Post subject: Reply with quote

Chaosite wrote:
whats wrong with lazy umount...?
Code:
umount -l ${mntdir}


Maybe you want the CD stocked before famd decides it stopped monitoring it. :wink:

Remember you can't open a data CD in GNU/Linux unless you umount it.
Back to top
View user's profile Send private message
kamikaze04
Guru
Guru


Joined: 28 Mar 2004
Posts: 366
Location: Valencia-Spain

PostPosted: Sun Dec 12, 2004 1:15 pm    Post subject: Reply with quote

Nice script!!!

Thanks!!
Back to top
View user's profile Send private message
Bekker
n00b
n00b


Joined: 04 Sep 2004
Posts: 46

PostPosted: Tue May 24, 2005 8:46 pm    Post subject: Reply with quote

I really appreciate this script. However it is mind boggling that you need these hacks just to unmount a stupid USB stick! If we want Gentoo ready for the desktop we really need to fix fam's behaviour.

Nap
Back to top
View user's profile Send private message
Sheepdogj15
Guru
Guru


Joined: 07 Jan 2005
Posts: 430
Location: Backyard

PostPosted: Tue May 24, 2005 9:29 pm    Post subject: Reply with quote

sweet! when i get my new install of Gentoo up, i'm definitely going to have this script handy

i've had a usb hdd that sometimes is a pain in the rear to remove.
_________________
Sheepdog
Why Risk It? | Samba Howto
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
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