Jay Taylor's notes

back to listing index

Determine if filesystem or partition is mounted RO or RW via Bash Script?

[web search]
Original source (serverfault.com)
Tags: linux filesystem command-line read-only serverfault.com
Clipped on: 2019-04-24

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 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. – mwfearnley May 23 '16 at 11:31
  • 45

    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.

    I had to use grep -P "\sro[\s,]" /proc/mounts or grep " ro[ ,]" /proc/mounts – WhiteKnight Apr 7 '15 at 10:11
  • 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 1:58
  • That has not been my experience. – flickerfly Apr 28 '16 at 2:05
  • 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
  • 11

    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.

    answered Jan 31 '13 at 4:18
    That seems to test filesystem permission, but not mount status. – Robert Calhoun Oct 29 '14 at 14:13
  • 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 9:21
  • 7

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

    answered Oct 22 '10 at 19:59
    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
  • This will incorrectly report a full filesystem as read-only. – Willem Jan 7 '15 at 14:20
  • @David, This seems like a stopgap hack instead of a true solution. – Pacerier Apr 23 '15 at 16:08
  • 2

    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.

    This will not work if 'ro' is the only mount option – Willem Jun 15 '15 at 11:20
    2

    Based on a flickerdfly'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;
    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
  • 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 4:46
  • 0

    Here is my solution:

    if findmnt ${YOUR_MOUNT_POINT} | awk '{print $4}' | grep "ro,"; then
      echo "Read only!"
    fi
    answered Sep 18 '18 at 7:27
    Image (Asset 7/12) alt= question feed