Products > Computers

how to check if root is rw mounted? GNU Linux, kernel >=v5

(1/6) > >>

DiTBho:

--- Code: ---#/bin/bash

function linea_arg2_get()
{
   #return
   ans="$2"
}

function do_fs_mode_get()
{
    local name="$1"
    local test

    test="`cat /proc/mounts | grep $name`"
    test="`echo $test | awk '{print $1 "\011" tolower(substr($4,0,2))}'`"
    linea_arg2_get $test
    # return ans
}

--- End code ---

This bash script works fine but is slow.

You won't notice its slowness on a superfast GHz CPU, but you will definitely feel it on a 400 MHz one.
In my case, the rb532a is mips32r2@400Mhz.

The problems are:

* the check is part of a procedure that must protect { disk-cache-flush, synchronize, microdrive-park } the read-only filesystem before shutting down the node in case of emergency, e.g. power loss, small battery system
* the check is repeated multiple times by other (bash) scriptsI will probably end up rewriting this check in pure C, just wondering: is there a better way to check if /dev/root is mounted in rw mode?


--- Code: ---uc-rb532 ~ # cat /proc/mounts
/dev/root / xfs rw,noatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0
none /proc proc rw,relatime 0 0
none /sys sysfs rw,relatime 0 0
mpme /dev/pts devpts rw,relatime,mode=600,ptmxmode=000 0 0

--- End code ---

DiTBho:
awk is a nice slug  :-//

DiTBho:

--- Code: ---    test="`echo $test | awk '{print $1 "\011" tolower(substr($4,0,2))}'`"

--- End code ---

just this line takes

--- Code: ---real    0m1.073s
user    0m0.085s
sys     0m0.090s

--- End code ---

 :-//

DiTBho:
I am also thinking about hacking the Linux kernel to export something in /proc to directly show the { root, others } mount status { ro, rw }.
A file for each mounted things, so you don't need to process by { awk, grep, egrep, ... , sed, ... whatever } a file to extract the information you need.

This simple check can't take 1 second on rb532a
it must take no more than 1/4 of a second!

brucehoult:
q&d test .. wfm. Tried on x86 and on RISC-V (VisionFive 2)


--- Code: ---#include <stdio.h>
#include <stdlib.h>
#include <sys/statvfs.h>

int is_mount_rw(const char *path) {
    struct statvfs stat;

    // Get filesystem stats
    if (statvfs(path, &stat) != 0) {
        perror("statvfs");
        return -1; // Error occurred
    }

    // Check if the filesystem is read-only
    if (stat.f_flag & ST_RDONLY) {
        return 0; // Mount is read-only
    }
    return 1; // Mount is read-write
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <mount-point>\n", argv[0]);
        return EXIT_FAILURE;
    }

    const char *mount_point = argv[1];
    int rw_status = is_mount_rw(mount_point);

    if (rw_status == -1) {
        fprintf(stderr, "Failed to determine the status of %s\n", mount_point);
        return EXIT_FAILURE;
    }

    if (rw_status) {
        printf("The mount point %s is read-write.\n", mount_point);
    } else {
        printf("The mount point %s is read-only.\n", mount_point);
    }

    return EXIT_SUCCESS;
}

--- End code ---

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod