Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[TIP] Script per smontare devices occupati
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Risorse italiane (documentazione e tools)
View previous topic :: View next topic  
Author Message
Dhaki
Guru
Guru


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

PostPosted: Wed Dec 08, 2004 2:20 pm    Post subject: [TIP] Script per smontare devices occupati Reply with quote

Tradotto da questo topic. Mi sembrava una cosa interessante, spero non ci sia già qualcosa di simile :) .
--------------------------------------------------------------------
Ho deciso di condividere questo script nel caso che qualcuno abbia il mio stesso problema. Non potevo smontare alcune delle mie periferiche (hard drivers/ipod/...) perché erano sempre occupate. Ho quindi scritto uno shell script che può risultare molto utile per risolvere alcune di queste situazioni.

Prima lo script tenta di smontare il drive normalmente. Se fallisce, prova riavviando famd che é il problema più frequente. Se fallisce allora prova riavviando xinetd. Se fallisce ancora allora usa il comando "fuser -ki <il-tuo-drive-occupato>" che chiede se vuoi killare tutti i processi che stanno usando quella directory. Sii prudente con questo - sarebbe meglio sapere che processi stanno usando quella directory prima di killarli. Se fallisce anche questo, probabilmente é perché non puoi killare tutti i processi, quindi ti chiede se vuoi smontare pigramente (lazily, non sapevo come tradurlo esattamente) il device, rimuovendo il drive anche se ci sono processi che lo stanno usando. Non sono un esperto, quindi ditemi se ci sono miglioramenti da fare.

UTILIZZO:
Code:
smartumount directory-da-smontare


ESEMPIO:
Code:
smartumount /mnt/ipod


Ed ecco il codice:
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 Dhaki on Sat Dec 18, 2004 9:21 pm; edited 2 times in total
Back to top
View user's profile Send private message
Tiro
l33t
l33t


Joined: 14 Feb 2003
Posts: 752
Location: italy

PostPosted: Wed Dec 08, 2004 2:35 pm    Post subject: Reply with quote

grandioso! :)
Back to top
View user's profile Send private message
Wave2184
Apprentice
Apprentice


Joined: 02 Nov 2003
Posts: 189
Location: Roma

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

bel lavoro...era un idea che stava nel cassetto della mia scrivania da tempo ma il tempo è quello che è...e ne ho poco...

è un problema che mi capita spesso....

appena posso lo provo...

bel lavoro....
_________________
LINUX:sbav:

Powered by Gentoo 2004.2
Powered by Ubuntu 5.10
Back to top
View user's profile Send private message
fedeliallalinea
Administrator
Administrator


Joined: 08 Mar 2003
Posts: 30822
Location: here

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

Aggiunto ai post utilissimi sezione tips
_________________
Questions are guaranteed in life; Answers aren't.
Back to top
View user's profile Send private message
oRDeX
Veteran
Veteran


Joined: 19 Oct 2003
Posts: 1325
Location: Italy

PostPosted: Wed Dec 08, 2004 4:22 pm    Post subject: Reply with quote

Molto utile direi..però penso una cosa: Questo script andrebbe usato solo in caso di emergenza, perchè se un device risulta occupato potrebbe essere utilizzato da qualche processo che magari al momento non viene in mente, e che se killato ci creerebbe problemi.
Quindi penso che bisognerà essere moltoooo prudenti nel suo utilizzo.
Back to top
View user's profile Send private message
stefanonafets
l33t
l33t


Joined: 10 Feb 2003
Posts: 644

PostPosted: Wed Dec 08, 2004 6:42 pm    Post subject: Reply with quote

Domanda stupida, invece di usare
Code:
 
if [ `whoami` != "root" ]; then
   echo "You must run this as root"
   exit
fi

nn sarebbe + giusto usare
Code:

if [ "$UID" != "0" ]; then
     [...]


E' uguale? ()
_________________
registered Linux user number #411324
sed 's/ke/che/g'

<The Deployment Slave is initializing>
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 7:35 pm    Post subject: Reply with quote

stefanonafets wrote:
Domanda stupida, invece di usare
Code:
 
if [ `whoami` != "root" ]; then
   echo "You must run this as root"
   exit
fi

nn sarebbe + giusto usare
Code:

if [ "$UID" != "0" ]; then
     [...]


E' uguale? ()

Bé... non penso siano molti a cambiare nome a root, quindi in pratica é la stessa cosa. Piu che altro penso sia una questione di sintassi, come per esempio:
Code:
: > file
cat /dev/null > file

che sono la stessa cosa.

Quote:
Questo script andrebbe usato solo in caso di emergenza, perchè se un device risulta occupato potrebbe essere utilizzato da qualche processo che magari al momento non viene in mente, e che se killato ci creerebbe problemi.

Questo é previsto, infatti:
Code:
You should look up each process before you answer. use the
following command in a separate shell:"
echo
echo "   ps -e | grep <pid>"

Ti consiglia di controllare i processi attivi prima di usare le maniere forti.
Back to top
View user's profile Send private message
FonderiaDigitale
Veteran
Veteran


Joined: 06 Nov 2003
Posts: 1710
Location: Rome, Italy

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

Dhaki wrote:
Bé... non penso siano molti a cambiare nome a root, quindi in pratica é la stessa cosa. Piu che altro penso sia una questione di sintassi, come per esempio:
Code:
: > file
cat /dev/null > file

che sono la stessa cosa.


beh oddio, il paragone stona: whoami e' un programma invocato dall'esterno, mentre UID e' una variabile d'ambiente a sola lettura!
_________________
Come disse un amico, i sistemisti sono un po' come gli artigiani per l'informatica :)
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 10:35 pm    Post subject: Reply with quote

FonderiaDigitale wrote:
Dhaki wrote:
Bé... non penso siano molti a cambiare nome a root, quindi in pratica é la stessa cosa. Piu che altro penso sia una questione di sintassi, come per esempio:
Code:
: > file
cat /dev/null > file

che sono la stessa cosa.


beh oddio, il paragone stona: whoami e' un programma invocato dall'esterno, mentre UID e' una variabile d'ambiente a sola lettura!

si infatti non ero molto sicuro di quello che calzasse a pieno... :oops: perdonate ciò che dissi...
Back to top
View user's profile Send private message
stefanonafets
l33t
l33t


Joined: 10 Feb 2003
Posts: 644

PostPosted: Thu Dec 09, 2004 2:34 pm    Post subject: Reply with quote

Bè, una prova pratica (senza senso):

I script
Code:

#!/bin/bash

if [ "$UID" != 0 ]; then
        echo "You arn't root"
fi


II script
Code:

#!/bin/bash

if [ `whoami` != "root" ]; then
        echo "You arn't root"
fi


Proviamo i tempi:

Code:
#time sh script1.sh
You arn't root

real    0m0.017s
user    0m0.000s
sys     0m0.010s

#time sh script2.sh
You arn't root

real    0m0.121s
user    0m0.000s
sys     0m0.040s


A voi (io nn sono abb. competente) l'ardua sentenza (perchè mai aspettare i posteri in questo caso? :D )
_________________
registered Linux user number #411324
sed 's/ke/che/g'

<The Deployment Slave is initializing>
Back to top
View user's profile Send private message
motaboy
Developer
Developer


Joined: 15 Dec 2003
Posts: 1483

PostPosted: Thu Dec 09, 2004 4:15 pm    Post subject: Reply with quote

l'opzione -l non e' proprio eccezionale. in alcuni casi porta il tuo VFS in uno stato inconsistente. per esempio usandolo coi lettori cd, se dai l'umount -l e ne monti poi uno diverso mentre un processo stava ancora accedendo al vecchio si ha del garbage al posto dei nomi dei files e non e' possibile leggere nulla. Non so se e' un problema del mio kernel o se sia una cosa correggibile o meno.
_________________
...
Back to top
View user's profile Send private message
Dhaki
Guru
Guru


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

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

motaboy wrote:
l'opzione -l non e' proprio eccezionale. in alcuni casi porta il tuo VFS in uno stato inconsistente. per esempio usandolo coi lettori cd, se dai l'umount -l e ne monti poi uno diverso mentre un processo stava ancora accedendo al vecchio si ha del garbage al posto dei nomi dei files e non e' possibile leggere nulla. Non so se e' un problema del mio kernel o se sia una cosa correggibile o meno.

Uhm... sarebbe meglio che metta un avvertimento nello script allora. Magari prima qualcuno potrebbe confermare/smentire con qualche prova empirica?

O altrimenti, avete altri metodi da segnalare??

EDIT: A proposito di supermount qualcuno sa darmi qualche info in piu? Questo script funziona lo stesso o ci sono accorgimenti da fare? Io non lo uso... quindi non saprei.
Back to top
View user's profile Send private message
rota
l33t
l33t


Joined: 13 Aug 2003
Posts: 960

PostPosted: Fri Dec 10, 2004 9:20 pm    Post subject: Reply with quote

m... era ora che uscisse sta cosa,....... :wink:
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Forum italiano (Italian) Risorse italiane (documentazione e tools) 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