Is there an easy way to determine if a mounted filesystem is mounted as Read-Only or Read-Write? I was thinking just to pipe mount but I thought there might be an easier way.

link|improve this question

74% accept rate
feedback

3 Answers

up vote 3 down vote accepted

If the file system is mounted, I'd cd to a temporary directory and attempt to create a file. The return code will tell you if the file system is Read-Only or Read-Write.

link|improve this answer
If you are just checking to see how a filesytem is mounted, getting the output from mount should be enough. But I have to agree, this is a more exhaustive way to check. There are occasions mount can report that it is mounted read/write, but is actually read-only. A common example of this is a large number of SCSI errors on a device causing it to protect itself by going read-only. Creating a file will verify read+write/read-only without a doubt. – Alex Oct 22 '10 at 20:33
this would be tidy: touch afile && { rm afile; echo "read-write"; } || echo "read-only" – glenn jackman Jun 6 '11 at 14:59
The scriptlet as written has a race condition. I would use FILE=mktemp -p /filesystem/of/interest/ instead of just using 'afile' to generate the file and filename. best – Rik Schneider Jun 6 '11 at 21:22
feedback

This little one-liner will pop-out something if a ro file system exists.

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

Assuming you don't usually have a ro file system like a CD in the drive, it is sufficient for some basic monitoring type stuff and doesn't require changing the file system to find the current state. It also doesn't assume your file system type. Pipe it into grep -v iso9660 if you want to keep your CDs out of the record.

link|improve this answer
feedback

For example, to check if the root partition is in Read-Only mode:

if [[ ! -z `mount | grep "on / type ext3 (ro,"` ]]
then
   echo "It's in read-only mode"
fi
link|improve this answer
This doesn't catch all cases. /sbin/mount will look at /etc/mtab for the cached version of the currently mounted filesystems (and their current options). If / manages to remount ro for some reason, mtab may not be updated correctly, so / may appear rw still. /proc/mounts should always show the correct value though. – Travis Campbell Jan 11 at 18:15
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.