Shutdown procedure clarification

initshutdownsystemdsysvinit

The book "How Linux Works" says the general shutdown procedure (independent of init system) is something like this:

  1. init asks every process to shut down cleanly.
  2. If a process doesn’t respond after a while, init kills it, first trying a TERM signal.
  3. If the TERM signal doesn’t work, init uses the KILL signal on any stragglers.
  4. The system locks system files into place and makes other preparations for shutdown.
  5. The system unmounts all filesystems other than the root.
  6. The system remounts the root filesystem read-only.
  7. The system writes all buffered data out to the filesystem with the sync program.
  8. The final step is to tell the kernel to reboot or stop with the reboot(2) system call. This can be done by init or an auxiliary program such as reboot, halt, or poweroff.

How can sync write its buffers if the filesystem is read-only?

Best Answer

You're right to be surprised: that order doesn't make sense. If a book presents it that way, it's sloppy and misleading.

Unmounting a filesystem, or mounting it read-only, writes all the data to the disk. When the umount command or mount -o remount,ro returns, all the data is written to the disk and sync has nothing left to do. It's pointless to call sync before (the data will be written by the umount operation anyway) and pointless to call it after (it won't do anything).

I think this wasn't true in some ancient Unix systems, where you had to call sync before unmounting. Calling it after was still pointless.

If you look beyond filesystems, there may be cases where sync does something. For example, I think that on Linux sync ensures that the metadata of RAID arrays are written to the disk. This is useful even in the absence of any filesystem mounted read-write.

Related Question