Script to check for read only filesystem

45,976

Solution 1

awk '$4~/(^|,)ro($|,)/' /proc/mounts

Solution 2

I've used the following in the past

grep ' ro ' /proc/mounts

In some instances you may want to skip any remote mounts which may be RO by design

grep ' ro ' /proc/mounts | grep -v ':'

You also may want to skip things that are mounted via automount

grep ' ro ' /proc/mounts | egrep -v 'automount|autofs'

Solution 3

If you intent is to find filesystems with problems (i.e. the mounting status has been changed to READ-ONLY due to a filesystem error), then I'd do the following (assumming ext* filesystems):

 tune2fs -e panic [raw-disk-partition-name]

EX:

 tune2fs -e panic /dev/sda1

What this does is panic the system, thus rebooting the server, possibly invoking a fsck on the problem filesystem to fix it. Thus a serious filesystem problem is handled by having the system fix it automatically, instead of dumping it into Read-ONLY mode which I have not found very helpful. Besides I'd rather panic a problem filesystem, fixing it than attempting to run with it damaged which as time goes on might cause more damage.

Solution 4

cat /proc/mounts|sort|awk '{print $1 "\011" toupper(substr($4,0,2))}'

Produces tab delimited output with mount name and mode.

Solution 5

As other answers suggest, you can parse /proc/mounts with grep or awk, for example you can list the read-only mounts:

$ grep "\sro[\s,]" /proc/mounts

or

$ awk '$4~/(^|,)ro($|,)/' /proc/mounts

An alternative to parsing the content of /proc/mounts that you could try is

$  grep '^ro$' /proc/fs/*/<device>/options

where <device> is the filesystem's device node name under /dev. For example

$ grep '^ro$' /proc/fs/*/sdc1/options

would return ro if /dev/sdc1 is mounted read-only.

If you want to check for a read-only block device (instead of a mounted filesystem) you can use

$ cat /sys/block/<device>/ro

which returns 1 if the filesystem is read-only or 0 if read-write.

Note that <device> above refers to the real device node. If you want to check a symlink device (such as those created by device-mapper or by-uuid references) then you can use basename and readlink to get the device node name. Like these examples:

$ grep '^ro$' /proc/fs/*/$(basename $(readlink -f /dev/mapper/foo)/options
$ cat /sys/block/$(basename $(readlink -f /dev/mapper/foo)/ro
Share:
45,976

Related videos on Youtube

Josef.B
Author by

Josef.B

Updated on September 18, 2022

Comments

  • Josef.B
    Josef.B over 1 year

    How can I check a bunch of systems to find any filesystem that are mounted read-only? Possibly via a script?

    • Admin
      Admin over 12 years
      What systems? What issue?
    • Admin
      Admin over 12 years
      Your Question is not clear. Please rephrase it.
    • Admin
      Admin over 12 years
      They are RHEL4 and RHEL5 systems. The filesystem (eg /var, /tmp , /) are mounted as read only. I want to find/list which sytems have such read only issue.
  • Teddy
    Teddy over 12 years
    That miscatches any line with "ro" in it, including anything with the "errors=remount-ro" option.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams over 12 years
    So then use a more sophisticated regex.
  • Josef.B
    Josef.B over 12 years
    I understand the awk, but not the 'nosuid' part. Can you please explain a bit more?
  • Teddy
    Teddy over 12 years
    @user11496: I meant "ro" - I mis-pasted my test code. I fixed my answer.
  • SARUAV
    SARUAV over 9 years
    This doesn't address the question. The poster isn't asking how to fix file systems, they are asking how to find read-only file systems. Additionally, making the system panic is a really bad way to go about fixing a corrupt file system.
  • Willem
    Willem over 9 years
    Unfortunately, read-only filesystems due to panic, are not marked as "ro" by the mount utility. So this is at least a valuable pointer in a helpful direction.
  • Adam
    Adam over 2 years
    /tmp could be on a different block device than / ... even in ram (tmpfs)