46

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.

Jake Wilson
  • 8,494
  • 29
  • 94
  • 121
  • 1
    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. – kasperd Apr 13 '16 at 06:59
  • 2
    Note: as @Travis Campbell helpfully explains in his comment on http://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. – mwfearnley May 23 '16 at 11:31

8 Answers8

66

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

grep "[[:space:]]ro[[:space:],]" /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.

flickerfly
  • 2,533
  • 3
  • 24
  • 27
  • 2
    I had to use `grep -P "\sro[\s,]" /proc/mounts` or `grep " ro[ ,]" /proc/mounts` – WhiteKnight Apr 07 '15 at 10:11
  • 4
    This is a much better answer than "try and create a file" thankyou. – Vagnerr Feb 11 '16 at 10:26
  • 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`. – Walf Apr 28 '16 at 01:58
  • That has not been my experience. – flickerfly Apr 28 '16 at 02:05
  • 3
    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` – flickerfly Apr 28 '16 at 17:22
15

Old question, but I've came across it looking for same help and seems like found even easier way without the need to create file.

    [ -w /root-rw ] && echo "rw" || echo "ro"
    rw
    [ -w /root-ro ] && echo "rw" || echo "ro"
    ro

Of course, root-ro is ro mounted fs and root-rw is rw fs.

user156888
  • 151
  • 1
  • 2
  • 4
    That seems to test filesystem permission, but not mount status. – Robert Calhoun Oct 29 '14 at 14:13
  • 2
    True, but it's very simple and can work for some cases (like mine). – Yajo Feb 25 '15 at 11:51
  • From `man dash` for the `-w` option - 'The file is not writable on a read-only file system even if this test indicates true.' AFAIK this is the same for other shells. – Graeme Jun 26 '15 at 09:21
8

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).

David Harris
  • 239
  • 1
  • 4
  • 2
    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
  • 1
    this would be tidy: `touch afile && { rm afile; echo "read-write"; } || echo "read-only"` – glenn jackman Jun 06 '11 at 14:59
  • 1
    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 06 '11 at 21:22
  • 1
    This will incorrectly report a full filesystem as read-only. – Willem Jan 07 '15 at 14:20
  • @David, This seems like a stopgap hack instead of a true solution. – Pacerier Apr 23 '15 at 16:08
5

I just had this issue and these are real pastes ...

Take a look at /proc/mounts -

egrep " ro,|,ro " /proc/mounts 
/dev/sda3 / ext4 ro,seclabel,relatime,barrier=1,data=ordered 0 0    
/dev/sda5 /var ext4 ro,seclabel,relatime,barrier=1,data=ordered 0 0

FYI - These two partitions show as being mounted rw when just using the mount command.

quanta
  • 50,327
  • 19
  • 152
  • 213
Wayne
  • 51
  • 1
  • 1
4

Based on a flickerfly's answer, influenced by a comment from WhiteKnight

Create a detector function the fly.

eval "function is_readonly () {
          $( grep -P "\sro[\s,]" /proc/mounts | awk '{print "if echo $1 | grep -q \""$2"\"; then return 0;fi"}' )
      return 1;}";    

use it to determine if a path is on a read only fs

is_readonly /path/to/file/on/read/only/fs && echo "sorry. can't delete that"

And dispose of it when done

#dump temp function
unset -f is_readonly;
flickerfly
  • 2,533
  • 3
  • 24
  • 27
  • It is a lot of fun to circle back here years later and see the iterative improvements made on a simple answer. Flickerfly -> WhiteKnight -> unsynchronized This internet thing might really take off. – flickerfly Sep 30 '21 at 14:32
3

Here is my solution:

if findmnt -n -o OPTIONS ${YOUR_MOUNT_POINT} | egrep "^ro,|,ro,|,ro$"; then
  echo "Read only!"
fi
Paul Dubuc
  • 13
  • 2
0

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
Jaime M.
  • 111
  • 4
  • 2
    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 '12 at 18:15
  • 1
    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. – Skaperen Aug 17 '12 at 04:46
0

Similar to Antonio, you can use /proc/mounts to do the same thing. Use your own drive in place of sda4.

cat /proc/mounts | grep /dev/sda4 | awk '{print substr($4,1,2)}'

  • Just so you know : `awk '$0 ~ "/dev/sda4" {print substr($4,1,2)}' /proc/mounts`, no need `cat` if you use `grep` (or `sed`), and no need `grep` when you use `awk` – Zelnes Jun 10 '20 at 16:06