How to Check Filesystem Mount Status (RO/RW) in Bash Scripting


2 views

When working with Linux systems, determining whether a filesystem is mounted as read-only (RO) or read-write (RW) is crucial for many automation scripts. This becomes particularly important when dealing with system maintenance, backup operations, or filesystem recovery scenarios.

The most straightforward method is parsing the output of the mount command:

mount | grep " / "

This will show you the mount options for the root filesystem. The output will include either ro or rw in the options list.

Here's a more robust function you can use in your scripts:

#!/bin/bash

check_mount_status() {
    local mountpoint="$1"
    if [[ -z "$mountpoint" ]]; then
        echo "Usage: $0 [mountpoint]" >&2
        return 1
    fi
    
    if mount | grep -q " on ${mountpoint} "; then
        if mount | grep " on ${mountpoint} " | grep -q "ro,"; then
            echo "Read-Only"
            return 10
        else
            echo "Read-Write"
            return 20
        fi
    else
        echo "Not mounted"
        return 30
    fi
}

# Example usage:
check_mount_status "/"
check_mount_status "/home"

For more modern systems, findmnt provides cleaner output and is easier to parse:

#!/bin/bash

mount_status=$(findmnt -no OPTIONS / | grep -o '\\|\')
case $mount_status in
    ro) echo "Read-Only";;
    rw) echo "Read-Write";;
    *) echo "Unknown status";;
esac

Sometimes, the most reliable method is to actually test writing to the filesystem:

#!/bin/bash

test_write() {
    local testfile="${1}/.mount_test_$RANDOM"
    if touch "$testfile" 2>/dev/null; then
        rm -f "$testfile"
        echo "Read-Write"
        return 0
    else
        echo "Read-Only"
        return 1
    fi
}

test_write "/tmp"

Some filesystems might report as RW but have underlying issues. Consider this enhanced version that handles more edge cases:

#!/bin/bash

check_mount_rw() {
    local mp="$1"
    [[ -z "$mp" ]] && mp="/"
    
    # Check if mounted
    if ! mountpoint -q "$mp"; then
        echo "Not mounted"
        return 3
    fi
    
    # Check mount options
    local options=$(findmnt -no OPTIONS "$mp")
    if [[ "$options" == *"ro"* ]]; then
        echo "Read-Only (by mount options)"
        return 1
    fi
    
    # Test actual write capability
    local testfile="${mp}/.mount_test_$RANDOM"
    if touch "$testfile" 2>/dev/null; then
        rm -f "$testfile"
        echo "Read-Write (verified)"
        return 0
    else
        echo "Read-Only (write test failed)"
        return 2
    fi
}

When working with Linux systems, it's often necessary to determine whether a filesystem is mounted with read-only (RO) or read-write (RW) permissions. This information is crucial for system administration tasks, automated scripts, and troubleshooting scenarios.

The most straightforward method is parsing the output of the mount command:


mount | grep "on /mount/point"

For example, to check the root filesystem:


mount | grep "on / "

This will show you the mount options including either ro or rw in the output.

For scripting purposes, we can create more reliable checks:

Method 1: Using findmnt

The findmnt command provides cleaner output for parsing:


findmnt -n -o OPTIONS /mount/point | grep -q "\brw\b" && echo "RW" || echo "RO"

Method 2: Checking /proc/mounts

You can also examine /proc/mounts directly:


grep "^/dev/sda1" /proc/mounts | grep -q "\brw\b" && echo "RW" || echo "RO"

Here's a reusable bash function to check mount status:


#!/bin/bash

is_mounted_rw() {
    local mount_point=$1
    if findmnt -n -o OPTIONS "$mount_point" | grep -q "\brw\b"; then
        return 0  # RW
    else
        return 1  # RO
    fi
}

# Usage example:
if is_mounted_rw "/"; then
    echo "Root filesystem is mounted RW"
else
    echo "Root filesystem is mounted RO"
fi

Some filesystems might show both ro and rw in their options. In such cases, the last option typically takes precedence. Here's how to handle this:


check_mount_rw() {
    local opts=$(findmnt -n -o OPTIONS "$1")
    if [[ "$opts" == *"rw"* && "$opts" != *"ro"* ]]; then
        echo "RW"
    elif [[ "$opts" == *"ro"* ]]; then
        echo "RO"
    else
        echo "Unknown"
    fi
}

For scripts that need to check multiple mount points frequently, consider caching the results or using the most efficient method (findmnt is generally faster than parsing mount output).

As a last resort, you can attempt to create a temporary file:


check_rw_by_test() {
    local test_file="$1/.rw_test_$RANDOM"
    if touch "$test_file" 2>/dev/null; then
        rm -f "$test_file"
        return 0
    else
        return 1
    fi
}

Note: This method has obvious limitations and should be used cautiously.