How can I check a bunch of systems to find any filesystem that are mounted read-only? Possibly via a script?
-
2What systems? What issue?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2011-10-12 07:09:34 +00:00Commented Oct 12, 2011 at 7:09
-
Your Question is not clear. Please rephrase it.SHW– SHW2011-10-12 07:13:30 +00:00Commented 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– user114962011-10-12 07:38:43 +00:00Commented Oct 12, 2011 at 7:38
8 Answers
awk '$4~/(^|,)ro($|,)/' /proc/mounts
-
I understand the awk, but not the 'nosuid' part. Can you please explain a bit more?user11496– user114962011-10-12 09:30:57 +00:00Commented Oct 12, 2011 at 9:30
-
@user11496: I meant "ro" - I mis-pasted my test code. I fixed my answer.Teddy– Teddy2011-10-12 11:45:37 +00:00Commented Oct 12, 2011 at 11:45
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'
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.
-
2This 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– Tim2014-11-13 09:18:35 +00:00Commented 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– Willem2015-01-07 14:19:19 +00:00Commented Jan 7, 2015 at 14:19
cat /proc/mounts|sort|awk '{print $1 "\011" toupper(substr($4,0,2))}'
Produces tab delimited output with mount name and mode.
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
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.
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}"
-
/tmp could be on a different block device than / ... even in ram (tmpfs)Massimo– Massimo2021-09-07 20:41:39 +00:00Commented Sep 7, 2021 at 20:41
Just look for the ro mount option.
-
3That miscatches any line with "ro" in it, including anything with the "errors=remount-ro" option.Teddy– Teddy2011-10-12 08:16:49 +00:00Commented Oct 12, 2011 at 8:16
-
1So then use a more sophisticated regex.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2011-10-12 08:19:12 +00:00Commented Oct 12, 2011 at 8:19