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
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
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
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
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!
if $(mount | grep -q loop0)
then echo Yes
else echo No
fi
When this works then you could set some variable that indicates the mount exists or try to mount when it is not there.net17 # mount | egrep net15
root@51.79.16.116:/mnt on /root/net15-all type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
mount | egrep /mnt/z
https://man7.org/linux/man-pages/man2/lstat.2.html
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.
Open in new window