9

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

3
  • 2
    What systems? What issue? Oct 12, 2011 at 7:09
  • Your Question is not clear. Please rephrase it.
    – SHW
    Oct 12, 2011 at 7:13
  • 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.
    – user11496
    Oct 12, 2011 at 7:38

8 Answers 8

13
awk '$4~/(^|,)ro($|,)/' /proc/mounts
2
  • I understand the awk, but not the 'nosuid' part. Can you please explain a bit more?
    – user11496
    Oct 12, 2011 at 9:30
  • @user11496: I meant "ro" - I mis-pasted my test code. I fixed my answer.
    – Teddy
    Oct 12, 2011 at 11:45
4

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'
2

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.

2
  • 2
    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.
    – Tim
    Nov 13, 2014 at 9:18
  • 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.
    – Willem
    Jan 7, 2015 at 14:19
2
cat /proc/mounts|sort|awk '{print $1 "\011" toupper(substr($4,0,2))}'

Produces tab delimited output with mount name and mode.

2

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
1

An approach that works even for remote file systems (which can be mounted rw even if they're exported ro, resulting in a read only file system) is to simply create a test file on each file system and test the return code of that command.

But if this is something that happens regularly you need to look into the cause rather than trying to keep up fixing the symptoms.

1

I'm a little late but this is how I handle it.

#!/bin/bash

FILE=/tmp/test.txt

# Check health of LXD host machine
touch ${FILE}

if [ -f ${FILE} ];
then
   rm ${FILE}
   echo "File System is Good!"
else
   message="File System is R/O !"
   echo ${message} >> /var/log/lxd/health.log >&2
   send_twilio "${TWILIO_ACCOUNT_SID}" "${TWILIO_ACCOUNT_TOKEN}" "${TWILIO_NUMBER}" "${to_twilio}" "${HOST}" "${message}"
1
  • /tmp could be on a different block device than / ... even in ram (tmpfs)
    – Massimo
    Sep 7, 2021 at 20:41
-3

Just look for the ro mount option.

2
  • 3
    That miscatches any line with "ro" in it, including anything with the "errors=remount-ro" option.
    – Teddy
    Oct 12, 2011 at 8:16
  • 1
    So then use a more sophisticated regex. Oct 12, 2011 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .