Monday, December 5, 2011

Monitor a list of currently mounted filesystems

You know that /proc/mounts and /proc/self/mountinfo contain list of currently mounted filesystems. These files is possible to monitor by poll(2) or select(2) functions. The util findmnt(8) exports this functionality to command line.

Examples:
session A (monitor):                                          
findmnt --poll
session B (event):
mount /home/fs-images/ext2.img /mnt/test

session A (findmnt output after event):
ACTION TARGET SOURCE FSTYPE OPTIONS
mount /mnt/test /dev/loop0 ext2 rw,relatime,user_xattr,acl,barrier=1
The another examples; wait until /mnt/test is unmounted:
   findmnt --poll=umount --first-only /mnt/test
Inform me about all ext2, ext3, and ext4 remounts to read-only mode:
   findmnt --poll=remount --types ext2,ext3,ext4 --options ro
You can also define output columns, for example if you want to know more about "mount --move" operations:
   # findmnt --poll=move -o OLD-TARGET,TARGET,SOURCE
OLD-TARGET TARGET SOURCE
/mnt/test /mnt/foo /dev/loop0
the event in this example was generated by "mount --move /mnt/test /mnt/foo" command.

And for example if you want info about old and new options after remount:
   # findmnt --poll=remount -o TARGET,OLD-OPTIONS,OPTIONS
TARGET OLD-OPTIONS OPTIONS
/mnt/foo ro,relatime,user_xattr,acl,barrier=1 rw,user_xattr,acl,barrier=1
this event was generated by "mount -o remount /mnt/foo -o rw,strictatime" command.

All this is available in util-linux 2.20 (e.g. Fedora 16).