2

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.

2
  • What it sounds like you are trying to do sounds pretty dangerous. Shouldn't you just choose to never mount a file-system from some other system other then the one that is supposed to be mounted on?
    – Zoredache
    May 20, 2010 at 18:52
  • @Zoredache, I don't actually want to mount it - I just want to check from the host system whether a virtual machine has mounted a filesystem.
    – Brian
    May 20, 2010 at 18:56

5 Answers 5

1

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.

4
  • Is there a way to see if the filesystem is dirty then?
    – Brian
    May 20, 2010 at 18:57
  • I don't know Linux very well at all, so the only thing I would know how to do is either try mounting it or use fsck. Maybe someone else knows.
    – Chris S
    May 20, 2010 at 19:05
  • 2
    tune2fs -l -- "Filesystem state"
    – Warner
    May 20, 2010 at 19:35
  • @Warner, nice - I looked through the man page for tune2fs but only saw "set" options; must've skipped right over -l
    – Brian
    May 20, 2010 at 21:03
1

The filesystems at boot in most distributions use mount -a. It mounts all filesystems in fstab with auto specified, which is part of the default 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.

1

Rethink your architecture. If you need to access a file system from multiple nodes, wrap it in LVM. You then have a couple options.

  1. You can import a volume when you need it and export it when finished.
  2. Or, if you're a bit more strict, you can set ownership tags which restrict where it cam be activated.

It doesn't then matter what ext3 provides; you can restrict access a layer lower.

1
  • That's a good suggestion - I've been meaning to look into LVM to make database backups less disruptive as well
    – Brian
    May 20, 2010 at 21:05
0

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.

0

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 result

you 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .