Detect read-only partition in Linux

In some sort of problems, ext3 goes read-only to protect the filesystem against further damages. What could cause such a readonly problem? It’s difficult to get an exact answer, but I’m inclined to take it as a file-system issue, though some hardware problems will trigger this error as well.

This issue is common on Linux server which has high disk I/O. As busy I/O retry error would mark low level disk calls as failed, this will force ext3 to go into read-only mode.

We get a question here: Is there a way to detect read-only disk partition in Linux?
Yes, sure we have. Admon has this feature integrated when it was came out some years ago. Here we’ll show two smart ways on detecting read-only partitions.

Detecting Read-Only partition in C
You can copy the follow code into a file, and save it as fs_readonly.c

#include <stdio.h>
#include <sys/statvfs.h>

int main(){
        char *path;
        struct statvfs fs_stat;

        path="/home";
        statvfs(*path, &fs_stat);
        printf("Filesystem: %s, status: %Xn", path,fs_stat.f_flag);
}

After compiled the code, you can run is in normal user mode like this:

$ ./fs_readonly
Filesystem: /home, status: 1

Here the status code 1 means it’s a Read-only partition. More details about statvfs is available here at linux.die.net. It tells us that we can check a readonly partition in normal user mode.

The second way is even easier.

Detecting Read-only partition in Shell/perl

Basically you just need to run this command:

$ LANG=C touch /home 2>&1 | grep Read-only
touch: setting times of `/home': Read-only file system

The 2>&1 means all standard error message is redirected to standard output so that grep can catch the error. The Read-only in output is a typical string which can be used to monitor read-only file systems. Note that this error message is slightly different from normal ones:

$ LANG=C touch /usr
touch: setting times of `/usr': Permission denied

Which the help of this method, you can now monitor your disk parition status more efficiently.

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *