How can I see if a (ext3) filesystem is mounted by asking the filesystem directly (i.e. the same way that the system does when it boots and sees that it was not unmounted cleanly)? Checking the output of mount
is no good because the filesystem might be mounted by a virtual machine.
I know I can run fsck
and it will abort if the filesystem is mounted, but I don't need to actually check the filesystem.
The file system doesn't know if it's mounted or not, only if it's dirty or clean. I'm not sure what OS the VM is running, but some can mount it read-only and it will stay clean. Also it could have been improperly dismounted, thus dirty, but not actually in use.
The filesystems at boot in most distributions use
mount -a
. It mounts all filesystems in fstab withauto
specified, which is part of thedefault
specification.For ext2/ext3,
tune2fs
can modify and display the settings that cause the filesystem to be fscked. For example,-c
allows you to specify how many mounts until the filesystem is fscked. Ultimately, your assumption regarding the boot process is not accurate.mount
is the solution to see if it's mounted. With ext3, I do not believe the "mount status" is stored with the partition. If you describe why you're incertain be it shared storage or NFS, we might be able to provide a recommendation applicable to your specific situation.Rethink your architecture. If you need to access a file system from multiple nodes, wrap it in LVM. You then have a couple options.
It doesn't then matter what ext3 provides; you can restrict access a layer lower.
This depends on the filesystem. Some filesystems notice if they are "mounted" and store a bit in a filesystem specific information region. Most filesystems do not, as this would impede correct operation on a hard reboot (the filesystem would still think it is mounted). Some filesystems remember more information than "currently mounted". For instance, ZFS remembers if it is mounted, and what computer has last mounted it, so a hard reboot works correctly, but accidentally mounting a mounted ZFS filesystem over a SAN is harder to do.
why not write a script using
FSSTAT=df | grep <filesystem mount point>
and then test it using
if [ ! -n "$FSSTAT" ]
where you would take decision based on resultyou can loop on the filesystems you want to test either by explicitly mentioning them in your script or by grepping the values from the output of
mount
command as in:for FS in <replace with back quote> mount | grep ext3 | cut -c 26-43 <replace with backquote> ;do; FSSTAT=df | grep $FS; if [ ! -n "$FSSTAT" ]; ... do somehting; else; ... do something else; fi; done
i am sorry, dealing with formatting gave a bad looking code