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.
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.
-
1I had to use
grep -P "\sro[\s,]" /proc/mounts
orgrep " ro[ ,]" /proc/mounts
– WhiteKnight Apr 7 '15 at 10:11 -
4
-
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 checkingmount
you don't know if that remount has occurred, hence if it is in fact currently inro
. – Walf Apr 28 '16 at 1:58 -
-
3I 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
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.
-
3That seems to test filesystem permission, but not mount status. – Robert Calhoun Oct 29 '14 at 14:13
-
1
-
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
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).
-
1If 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
-
1this 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 -
1
-
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.
-
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;
Here is my solution:
if findmnt -n -o OPTIONS ${YOUR_MOUNT_POINT} | egrep "^ro,|,ro,|,ro$"; then
echo "Read only!"
fi
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
-
2This 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
-
1I 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
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 needcat
if you usegrep
(orsed
), and no needgrep
when you useawk
– Zelnes Jun 10 '20 at 16:06
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 usedstat -f
, but that command shows everything except flags. – kasperd Apr 13 '16 at 6:59mount
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