55

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
  • 9,133

9 Answers9

76

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,903
16

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.

9

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

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
  • 52,423
Wayne
  • 51
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,903
3

Here is my solution:

if findmnt -n -o OPTIONS ${YOUR_MOUNT_POINT} | egrep "^ro,|,ro,|,ro$"; then
  echo "Read only!"
fi
1

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.
  • 121
0

Ubuntu has moved to snap packages which returns a lot of /snap/$package results. So my updated command is the following:

grep "[[:space:]]ro[[:space:],]" /proc/mounts | grep -v /snap | grep -v tmpfs
kolydart
  • 1
  • 1
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)}'