LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2007, 12:51 PM   #1
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Rep: Reputation: 38
Verifying umount?


I want to be able to mount, umount, then verify the mount directory is no longer mounted, before deleting it (rmdir), all via script. How can I verify that it's no longer mounted so I don't accidentally blow away the data it was mounted to?
 
Old 02-28-2007, 12:57 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
rmdir by itself deletes only empty directories (otherwise returns an error message). From a script you can verify a mounted/unmounted device by the mount command (without any option), e.g. by something like
Code:
if [ `mount | grep /dev/fd0 | wc -l` -eq 0 ] then
   rmdir /mnt/floppy
fi
assuming /dev/fd0 the device (floppy) to be unmounted and /mnt/floppy the mount point. You can also use a while loop to check until the device is truly unmounted then execute the rmdir command.

Last edited by colucix; 02-28-2007 at 01:04 PM.
 
Old 02-28-2007, 12:59 PM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 234Reputation: 234Reputation: 234
Simply type mount or cat /etc/mtab


But why would you want to remove the directory? If it's a common mount that you'll reuse, it's harmless sitting there with no data within it.
 
Old 02-28-2007, 01:50 PM   #4
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by trickykid
Simply type mount or cat /etc/mtab


But why would you want to remove the directory? If it's a common mount that you'll reuse, it's harmless sitting there with no data within it.
trickykid, I want the script to clean up after itself.

Thanks guys, I'll give that a look at.
 
Old 02-28-2007, 02:10 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Perhaps you should be looking at the automount system and udev to automate mounts for various devices. Typically, automount automatically mounts things when they're referenced, and umounts them after they have been unused for a specified period of time. (Ten minutes by default, if I remember correctly.) Then your script would have no need to "clean up" after itself.
 
Old 02-28-2007, 02:17 PM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I'm also not understanding why an empty directory needs to be "cleaned up"...

Also, if you're going to use colucix's script, I would suggest grep-ing for the directory name (rather than device) before you remove it. (Otherwise you may be testing for a device that was never mounted, and poof.)

Last edited by anomie; 02-28-2007 at 02:19 PM.
 
Old 02-28-2007, 03:08 PM   #7
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by anomie
I'm also not understanding why an empty directory needs to be "cleaned up"...

Also, if you're going to use colucix's script, I would suggest grep-ing for the directory name (rather than device) before you remove it. (Otherwise you may be testing for a device that was never mounted, and poof.)
The script is a backup script that creates 2 directories for each server, then mounts a source and destination Windows share to each, respectively. Files are transferred across, then I umount the directories. After that I want to remove the directories. I will have multiple scripts moving across multiple servers and destinations, and I don't want the clutter of the temporary directories. To me it's the same as leaving a candy wrapper on the table after you've finished the candy.
 
Old 02-28-2007, 03:13 PM   #8
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I hear what you're saying, but to follow along with the candy wrapper analogy:
  • The candy wrapper is laying on a table in the attic (/mnt directory) which you will never see.
  • Throwing out that candy wrapper also may introduce the potential risk of losing a large handful of your best candy.

If you just create the directories by hand and re-use them each time, this headache will be over.

Anyway, that's my 5 cents. To each his own.
 
Old 02-28-2007, 03:36 PM   #9
iamnothere
Member
 
Registered: Feb 2007
Location: UK
Distribution: Slamd 64, Slackware
Posts: 46

Rep: Reputation: 16
What's the big deal?

umount /mnt/dir && rmdir /mnt/dir
 
Old 03-01-2007, 07:43 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by anomie
Also, if you're going to use colucix's script, I would suggest grep-ing for the directory name (rather than device) before you remove it. (Otherwise you may be testing for a device that was never mounted, and poof.)
Thank you for pointing out, anomie. Indeed, my suggestion implies you already know which is the device being umounted (on the other hand in the OP situation a previous umount command has been issued). So - litteraly - if you not find it in the list of the mounted devices (that is the umount has been accomplished), then you can safely remove the mount pount (not the device itself). Furthermore, using the rmdir command ensures the directory is empty before trying to remove.

BTW the solution proposed by iamnothere is far more elegant! Why I never think at &&...!?!

Last edited by colucix; 03-01-2007 at 07:48 AM.
 
Old 03-01-2007, 09:33 PM   #11
SlowCoder
Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Fedora (Desktop), CentOS (Server), Knoppix (Diags)
Posts: 934

Original Poster
Rep: Reputation: 38
Thanks for all the info. I didn't realize rmdir only removes empty directories by default.

P.S. to iamnothere: The big deal is that while I can mount the source as read-only, I must mount the destination as writable. If for some reason the umount failed and my script issued rmdir or rm -rf on the destination directory, my destination data would be murdered. Had it happen once. Don't want to do it again.
 
Old 03-02-2007, 11:30 AM   #12
iamnothere
Member
 
Registered: Feb 2007
Location: UK
Distribution: Slamd 64, Slackware
Posts: 46

Rep: Reputation: 16
Quote:
Originally Posted by SlowCoder
Thanks for all the info. I didn't realize rmdir only removes empty directories by default.

P.S. to iamnothere: The big deal is that while I can mount the source as read-only, I must mount the destination as writable. If for some reason the umount failed and my script issued rmdir or rm -rf on the destination directory, my destination data would be murdered. Had it happen once. Don't want to do it again.
But, as others pointed out and you now realize, rmdir only removes empty directories (not just by default, it _cannot_ remove non-empty directories). You can rmdir / as root with no ill effects, since nothing will happen. Obviously, rm -fr is a different story, and needs to be used with care.

Another solution to ensure a mount point is unmounted is to stat it, then stat the parent directory. stat returns the device number (major/minor), so if the're different, the directory has a file system mounted on it, if the're the same, then clearly not.

e.g.

Quote:
if [[ $(stat -c %d /mnt) == $(stat -c %d /mnt/point) ]]; then
# ... fs is unmounted
else
# umount failed, try again...
fi

Last edited by iamnothere; 03-02-2007 at 11:32 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Verifying Archives onelung02 Linux - Software 1 06-26-2006 12:23 PM
verifying a login w/ /etc/shadow exodist Linux - Security 4 11-17-2005 02:20 PM
Verifying drive integrity jdhar Linux - Software 7 09-22-2005 10:28 AM
Verifying IPTable rules... Ateo Linux - Networking 1 02-02-2005 04:33 PM
Verifying CD's Caysho Linux - General 1 12-24-2004 09:14 PM


All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration