Linux – Remount a busy disk to read-only mode

filesystemslinuxmountreadonly

I want to force a disk partition to read only mode and keep it read-only for more than 30 minutes.

What I have tried:

  1. mount -o remount,ro (partition-identifier) (mount-point) -t (filesystem)

    Issue: This gave device busy error as some processes were using the partition. I don't want to kill the processes using the disk. I want to simulate the disk suddenly going read-only when the processes are still using it.

  2. Used magic sysrq key, like below

    echo u > /proc/sysrq-trigger
    

    Issue: This will make all the disk partitions read-only (although device is busy). But after 20-30 minutes the machine is rebooting itself. Some machines are rebooting immediately once this command is executed. Not sure what is causing this reboot yet. I don't want the machine to reboot itself and need to keep the disk in read-only mode for 30+ minutes.

Question:
Is there any better way I can force a single disk partition to read-only and sustain it in that state for half an hour and bring it back to read-write mode without causing any reboot in the process?

Best Answer

You normally can't remount a filesystem as read-only if processes have a file on it that's open for writing, or if it contains a file that's deleted but still open. Similarly, you can't unmount a filesystem that has any file open (or similar uses of files such as a process having its current directory there, a running executable, etc.).

You can use umount -l to release the mount point and prevent the opening of further files, but keep the filesystem mounted and keep processes that already have files open running normally.

I can't think of a generic way to force a filesystem to be remounted read-only when it shouldn't be. However, if the filesystem is backed by a block device, you can make the block device read-only, e.g.

echo 1 >/sys/block/dm-4/ro
echo 1 >/sys/block/sda/sda2/ro

echo u > /proc/sysrq-trigger is a rather extreme way to force remounting as read-only, because it affects all filesystems. It's meant as a last-ditch method to leave the filesystem in a clean state just before rebooting.

Remounting a filesystem as read-only does not cause a reboot. Whatever is causing the reboot is not directly related to remounting the partition as read-only. Maybe it's completely unrelated, or maybe this triggers a bug in the application which causes it to spin and make the processor overheat and your processor is defective or overclocked and eventually reboots. You need to track down the cause of the reboot.

Related Question