Sponsored Content
Top Forums Shell Programming and Scripting if (disk is mounted) unmount if (disk is unmounted) mount Post 302503812 by mirni on Friday 11th of March 2011 06:51:53 PM
Old 03-11-2011
I don't know what is 'disktool'. It's not installed on my CentOS box, therefore I'd like to present a more general solution:
When a filesystem gets mounted, there is a record about it in /etc/mtab.
Each partition of your disk is gonna be represented by a special file /dev/sdb1 or /dev/sdc4, etc.. (sd* for SATA drives, at least).
Now this can change, depending on what devices you have currently plugged in, so it's not good to depend that your disk is always gonna be /dev/sdb1. A reliable way to find out which /dev/sd* is your disk is to find out the UUID of your disk:
Unmount and unplug it and do
Code:
ls -l /dev/disk/by-uuid

Then plug it in and issue the same command. You'll see one more entry (my disk is 886C3A4E6C3A3772), that's the uuid of your device. ls -l also shows you that it is a symbolic link to that special file /dev/sd*.
Now let's put all this info together:

Code:
#!/bin/bash

device=`ls /dev/disk/by-uuid/886C3A4E6C3A3772 -l | awk -F/ '{print $NF}'`  #find which file in /dev/ is my disk

if [ _"`grep /dev/$device /etc/mtab`" = _ ] ; then
    mounted=0 # there is no record of this device in /etc/mtab
    echo "Device /dev/$device is not mounted"
    mount  /dev/$device /my/mountpoint
else
    mounted=1 # record found => mounted
    echo "Device /dev/$device is mounted"
    umount  /dev/$device
fi

You may need root privileges to do the mounting though.
The disadvantage of this script is that you have to hardcode the UUID of your device in it. To abstract from this, you could
find the device by looking at the last device plugged in. Like this:
Code:
device=`ls /dev/disk/by-uuid -ltr | tail -1 | awk -F/ '{print $NF}'`

ls -trl will print the files in long format (-l), sorted by date (-t), in reverse order (-r), so you'll have the last plugged-in device last. tail -1 will grab just the last line of the output of ls command.

Hope this helps.

mounted=1 is just a flag in case you wanted to do some more bash magic outside the if statement
 

10 More Discussions You Might Find Interesting

1. HP-UX

Unmount and remove all Logical vol.Volume group and physical disk

Hi, Someone please help me with how i can unmount and remove all the files systems from a cluster. This is being shared by two servers that are active_standby. (3 Replies)
Discussion started by: joeli
3 Replies

2. HP-UX

/usr out of disk space need to unmount/ expand volume /mount

Greetings, I am running HP-UX 10.2 and /usr is out of disk space already. I installed IE 5.0 for UNIX on my machine under /usr and browsed the Internet for a while and presto no more disk space. I have plenty of hard disk space on my computer so would like to expand the size of the volume. The... (5 Replies)
Discussion started by: Dirk_
5 Replies

3. SuSE

External USB disk cannot be mounted

Hi, I am running Suse on a fujitsu server. The problem is that it will no fully load the usb external disk. When plugged in, dmesg shows that indeed a usb disk has been plugged in ,but gives no devpath e.g sda,sdb. lsusb shows the disk vendor (western digital) but nothing else.Whats goin on... (2 Replies)
Discussion started by: ulemsee
2 Replies

4. OS X (Apple)

Can't Mount Disk / Image after bad unmount

I have had a little issue with one of my disks, the usb cacble was pulled out and one of the external drives on it would no longer mount. I used First Aid and it verified and repaired both OK / nothing to do). After lots of messing around and not being able to mount I used Drive Genius 2 and that... (1 Reply)
Discussion started by: Cranie
1 Replies

5. OS X (Apple)

When a disk won't 'Unmount"

hdiutil detach -Force disk&number (ex:hdiutil detach -Force disk1) (3 Replies)
Discussion started by: unimachead
3 Replies

6. Shell Programming and Scripting

Kill shell script when host program not running/disk unmounted

I have my Mac OS X program executing a shell script (a script that copies files to a drive). I want to make it so that the shell script automatically kills itself if it finds that the host .app is not running OR kill itself if the drive that it is copying files to has been unmounted. Right now what... (2 Replies)
Discussion started by: pcwiz
2 Replies

7. Shell Programming and Scripting

Unmount USB disk

Hi, How can I unmount an usb disk using the command umount? (3 Replies)
Discussion started by: Guccio
3 Replies

8. UNIX for Dummies Questions & Answers

How to unmount a folder and do check disk only in that path

I need to check if a folder has some kind of logic disk problems. I have a FreeBSD machine where the (root)\tmp\TEST folder has some file created by a script that i cannot delete. If i rename the tmp\TEST folder then i can delete them (apparently) but if i recreate a folder in tmp directory... (2 Replies)
Discussion started by: mirrorx
2 Replies

9. Solaris

How to modify a disk with mounted partitions?

Hi Guys, I was wondering; can a disk with mounted partitions be modified? If yes, how can this be done? Thanks lots (1 Reply)
Discussion started by: cjashu
1 Replies

10. AIX

Mounted and unmounted

Hi Guys I'm new here, and I need urgent help. This my first steps to be Aix admin and I have this task -instal Oracle database on Aix machine and create mounting point /u02 of size 100GB for Oracle Standalone database installation. -download and install the following OS patches - IV42025... (5 Replies)
Discussion started by: khaled_ly84
5 Replies
mounted.ocfs2(8)						OCFS2 Manual Pages						  mounted.ocfs2(8)

NAME
mounted.ocfs2 - Detects all OCFS2 volumes on a system. SYNOPSIS
mounted.ocfs2 [-d] [-f] [device] DESCRIPTION
mounted.ocfs2 is used to detect OCFS2 volume(s) on a system. This tool assumes that the nodes detected on the system are part of the same cluster as the one specified in /etc/ocfs2/cluster.conf. OPTIONS
-d Lists the OCFS2 volumes along with their labels and uuids. If a device is not specified, it scans all the devices in /proc/parti- tions. -f Lists the OCFS2 volumes along with the list of nodes that have mounted the volume. The node names are listed if the O2CB cluster is online. Note: As this information is gathered using dirty-read, the information may be stale. EXAMPLES
[root@node1 ~]# mounted.ocfs2 -d Device FS UUID Label /dev/sdb1 ocfs2 26d5dcdf-6d87-45a7-b01a-b98a7791bd61 logs_0405 [root@node1 ~]# mounted.ocfs2 -f Device FS Nodes /dev/sdb1 ocfs2 node15, node1 SEE ALSO
mkfs.ocfs2(8) fsck.ocfs2(8) tunefs.ocfs2(8) debugfs.ocfs2(8) ocfs2console(8) o2cb(7) AUTHORS
Oracle Corporation COPYRIGHT
Copyright (C) 2004, 2010 Oracle. All rights reserved. Version 1.4.3 February 2010 mounted.ocfs2(8)
All times are GMT -4. The time now is 10:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy