Avatar of Mr_Fulano
Flag for United States of America asked on

How to programmatically determine if a Disk Image is Mounted or Not Mounted.

Hello, I have a Linux question. - Please assume I have a disk image mounted and everything is working fine. After I'm done working with my image file, I then want to unmount that file.  I issue the umount command and it works perfectly.  

However, let assume I first want to check and verify my image (or any image file) is mounted at a specific mount point... for this example, lets assume the mount point is /mnt/z. 

Is there a programatic way to verify that an image file is mounted at a mount point?  I know I can check whether the directory contains files, but I want to know if those files belong to an image.

If I wanted to verify whether a directory exists, I could use the following command at the command prompt:

if [ -d /mnt/z ]; then echo 1; else echo 0; fi

Open in new window

This command would echo 1 if the /mnt/z directory exists and 0 if it does not. 

So, is there a way to verify that a disk image file (i.e. DD, ISO etc) is indeed mounted and that it's an image file?    If so, how would I do that?  

I'm hoping that there is a "flag" of some sort that I can query to determine if that flag set for a mounted image or not set for no image at that mount point.

Ideally, I'm looking for something like this... : 

if [ /mnt/z contains a mounted image]; then echo 1; else echo 0; fi

Open in new window

I know that I can issue the following to get ALL mounted drives on the system, but that will list all the file systems. I would like to limit it only to my one mount point (i.e. /mnt/z)

mount | column -t 

Open in new window

I can also issue the following to check the SOURCE of a mount point, which is kind of close to what I need:

findmnt -S /dev/loop0

Open in new window

And this above will give my my mount point, but how would I use that in an IF statement to 

check if my mount point was indeed mounted...?   I am not too skilled in writing Linux IF statements.

Any suggestions would be greatly appreciated. 

I know most of you may say... "just look at the mount point and if it contains files, then its there..."  YES!  I understand that, but let's assume you're writing a script and the computer is going to unmount that image. You would want to check if an image is indeed mounted before issuing the unmount command. -- Similarly, if you were programming code and wanted to put a file into a folder, you would want to check if that particular folder exists before you try to write to it. Otherwise, you would throw an exception.  I'm just trying to programmatically check ahead. 

Thanks you!


Avatar of undefined
Last Comment

8/22/2022 - Mon
Dr. Klahn

Stat the file.   If there's an error, the return code will give more detailed information.  If the stat succeeds, then the file is available at that mount point.


Somewhat similar example code below, where the last access time of the file is of interest.  In this case the file is known to exist so the stat error code is not checked.

  struct stat statdata;
  struct tm *wltmp;

  stat(&sh_cidr_whitefile[0], &statdata);
  this_wlmodtime = statdata.st_mtime;

  /* If the mod time has not changed, no need to reload the file */

  if (this_wlmodtime == prev_wlmodtime) return;

Open in new window

Gerwin Jansen

You could just execute the mount command and find out if your mount point exists. You grep for your mount point by using the name or part of the name. So if your mount point would contain loop0 then try this:

if $(mount | grep -q loop0)
then echo Yes 
else echo No

Open in new window

When this works then you could set some variable that indicates the mount exists or try to mount when it is not there.

David Favor

I generally just run mount to see if the mount point exists as expected.

Calling lstat() directly likely works just as well.

Example to determine all mount points associated with the net15 machine (which I'm currently recovering from a dead disk event)...

net17 # mount | egrep net15
root@ on /root/net15-all type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0)

Open in new window

In your case you'd do this... then if you had any results, then the mount point is currently active...

mount | egrep /mnt/z

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Your mount points should be empty, and not contain old files.  If you use automount, those mount points are generated on the fly.  You should not have to manually create a folder to mount.  Those mount folders should be part of your environment variables for builds and compiles, so they don't need to be put into particular hard mount points just to have them work.

Yes, @serialband, your mountpoint *should* be empty, but I have seen more than one occasion where it wasn't. A case where the mountpoint directory *was* originally empty was when the mounted disk contained files maintained by an elderly code management system, and once when the disk was not mounted because of a network problem, the management system saw that the files weren't there so restored them but to the mountpoint directory. The disk was then mounted, hiding the restored copy. We spotted that one before it broke anything, but later network failure blocking a disk mount might have exposed those old files.

Gentlemen, as always you have all provided very good information and advice, but in this particular case, Simon3270 provided the simplest solution, which was what I needed for my code... a true or false type of response from the OS.

Thank you all and stay well.
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.