The ideal system call for this would be statvfs which among other things returns a flag fields with a flag indicating a read only mount. Unfortunately I don't know a shell command to do this directly. I would have used stat -f, but that command shows everything except flags.
– kasperdApr 13 '16 at 6:59
Note: as @Travis Campbell helpfully explains in his comment on serverfault.com/a/277449/236916, mount doesn't always tell you what you want: to paraphrase, it reads from /etc/mtab, which is a cached version of the data, and may be outdated in some cases. The info in /proc/mounts is what you really want.
– mwfearnleyMay 23 '16 at 11:31
This little one-liner will pop-out something if a ro file system exists.
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.
Doesn't this only tell you the options that were used to mount, not the current status? E.g. an entry with the options ext4 rw,noatime,nobarrier,errors=remount-ro,data=ordered 0 0 is set to remount as read only in the event of an error, so without checking mount you don't know if that remount has occurred, hence if it is in fact currently in ro.
– WalfApr 28 '16 at 1:58
I just had one today. This is the root file-system in ro, but it was rw when it started. $ grep "\sro[\s,]" /proc/mounts Output: /dev/mapper/root / ext4 ro,relatime,errors=remount-ro,user_xattr,acl,barrier=1,data=ordered 0 0
– flickerflyApr 28 '16 at 17:22
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 provided that the file system is not full (thanks Willem).
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.
– AlexOct 22 '10 at 20:33
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 SchneiderJun 6 '11 at 21:22
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 CampbellJan 11 '12 at 18:15
I agree with the need to use /proc/mounts. I think this test should be reduced to a shell (bash since the OP asks that) function that makes sure the string being referenced is not a substring of another path.
– SkaperenAug 17 '12 at 4:46