Search code examples
ckernelmountsystem-calls

using the syscall mount() to remount ext4 filesystem read-only


I want to write a simple C program with hardcoded options, which does nothing else than remount root filesystem to read-only

I see, the mount() syscall takes following parameters:

mount(const char *spec, const char *node, const char *type, int flags, void *data)

I have following C code:

#include <stdio.h>
#include <errno.h>
#include <sys/mount.h>

int main(int argc, char *argv[]) {
    return mount ("/dev/sda1", "/", "ext4", "MS_RDONLY", NULL);
}

I know, in place of MS_RDONLY I should use a type int. But where do I find the value corresponding to MS_RDONLY (or which ever option I need to use) ?


Solution

  • MS_RDONLY should be defined in mount.h, that you already included in your code. Changing "MS_RDONLY" to MS_RDONLY should do the trick.