6

On Linux, does mount -o remount,ro flush filesystem buffers/cache, or should I also run sync to achieve that?

Mat
  • 52,586
Totor
  • 20,040

2 Answers2

3

It should flush the cache, yes.

Arguably there is some fragility in this code path. It is not implemented in one place; it is implemented in each individual filesystem. E.g. ext2_remount() must call sync_filesystem().

Also in all the use cases that I know of, it is used together with the sync() system call. So if you rely on this, and the kernel gets it wrong, you might have the "privilege" of being the first to notice a problem.

If you want to be extra-careful, you could use sync -f /my/mounted/filesystem/. It is preferable to sync because it avoids interference with any other filesystem. It uses the Linux system call syncfs().

sourcejedi
  • 50,249
2

From Wikipedia

Buffers are also flushed when filesystems are unmounted or remounted read-only, for example prior to system shutdown.

UVV
  • 3,068
  • 2
    Thank you, but can you quote a better source than Wikipedia? – Totor Sep 03 '14 at 15:23
  • This quote is currently marked [citation needed]. The quote is also wrong, because it is not true for some non-Linux systems. "According to the standard specification (e.g., POSIX.1-2001), sync() schedules the writes, but may return before the actual writing is done." http://man7.org/linux/man-pages/man2/sync.2.html This was a problem on historical UNIX: https://unix.stackexchange.com/a/416466/29483 – sourcejedi Mar 03 '19 at 17:55