简体   繁体   English

是否有Linux C API调用来查询已挂载的文件系统以查看它是否为只读?

[英]Is there a Linux C API call to query a mounted filesystem to see if it is read-only?

First, a little background information to provide some motivation for this question: I've got a program that runs on a headless Linux server and reads/writes files on several removable external hard drives, each of which is formatted with ext4 filesystem. 首先,提供一些背景信息,为这个问题提供一些动力:我有一个程序在无头Linux服务器上运行,并在几个可移动外部硬盘驱动器上读/写文件,每个硬盘驱动器都使用ext4文件系统格式化。 Very occasionally, the filesystem metadata on one of these drives gets corrupted for whatever reason (ext4 journalling notwithstanding), which can cause the ext4 filesystem drive to detect a problem and remount the partition as read-only, presumably as a precaution against cascading errors corrupting the drive further. 偶尔,其中一个驱动器上的文件系统元数据因任何原因(尽管有ext4 journalling)都会损坏,这可能导致ext4文件系统驱动器检测到问题并将分区重新安装为只读,可能是为了防止级联错误损坏进一步的驱动器。

Okay, fair enough; 好的,够公平的; but what I'd like to do now is add a function to my program that can detect when the drive is in this remounted-read-only state, so that it can pro-actively notify the user that his drive is in trouble. 但我现在要做的是为我的程序添加一个函数,该函数可以检测驱动器何时处于这种重新安装的只读状态,这样它就可以主动通知用户他的驱动器有问题。

My question is, what is an elegant/supported way to query a filesystem to find out if it is mounted read-only? 我的问题是,查询文件系统以查明它是否以只读方式安装的优雅/支持方式是什么?

Attempting to write a file to the filesystem isn't good enough, because that could fail for other reasons, and also because I don't want to write to the filesystem if I don't have to. 尝试将文件写入文件系统是不够的,因为这可能由于其他原因而失败,并且因为我不想写文件系统,如果我不需要。

My program could fopen("/proc/mounts", "r") and parse the lines of text that it generates (grepping for the "rw," token on the line corresponding to my partition), and I will if I have to, but that solution seems a bit hacky (too much like screen-scraping, liable to break if the text format ever changes). 我的程序可以fopen("/proc/mounts", "r")并解析它生成的文本行(在我的分区对应的行上为“rw”标记),如果必须,我会,但这个解决方案看起来有点像hacky(太像屏幕抓取,如果文本格式发生变化,可能会破坏)。

So, is there some lightweight/purpose-built Linux system call that I could use that would tell me whether a given filesystem mount point (eg "/dev/sda1") is currently mounted read-only? 那么,是否有一些我可以使用的轻量级/专用Linux系统调用,它会告诉我给定的文件系统挂载点(例如“/ dev / sda1”)当前是否以只读方式挂载? It seems like stat() might be able to do it, but I can't see how. 似乎stat()可能会这样做,但我看不出如何。

The getmntent() family should meet your needs. getmntent()系列应该满足您的需求。

NAME 名称

getmntent, setmntent, addmntent, endmntent, hasmntopt, getmntent_r - get filesystem descriptor file entry getmntent,setmntent,addmntent,endmntent,hasmntopt,getmntent_r - 获取文件系统描述符文件条目

SYNOPSIS 概要

  #include <stdio.h> #include <mntent.h> FILE *setmntent(const char *filename, const char *type); struct mntent *getmntent(FILE *stream); int addmntent(FILE *stream, const struct mntent *mnt); int endmntent(FILE *streamp); char *hasmntopt(const struct mntent *mnt, const char *opt); /* GNU extension */ #include <mntent.h> struct mntent *getmntent_r(FILE *streamp, struct mntent *mntbuf, char *buf, int buflen); 

DESCRIPTION 描述

These routines are used to access the filesystem description file /etc/fstab and the mounted filesystem description file /etc/mtab. 这些例程用于访问文件系统描述文件/ etc / fstab和挂载的文件系统描述文件/ etc / mtab。

The setmntent() function opens the filesystem description file filename and returns a file pointer which can be used by getmntent(). setmntent()函数打开文件系统描述文件filename并返回一个可由getmntent()使用的文件指针。 The argument type is the type of access required and can take the same values as the mode argument of fopen(3). 参数类型是所需的访问类型,可以采用与fopen(3)的mode参数相同的值。

The getmntent() function reads the next line of the filesystem description file from stream and returns a pointer to a structure containing the broken out fields from a line in the file. getmntent()函数从流中读取文件系统描述文件的下一行,并返回一个指向结构的指针,该结构包含文件中一行的分解字段。 The pointer points to a static area of memory which is overwritten by subsequent calls to getmntent(). 指针指向内存的静态区域,后续调用getmntent()会覆盖该区域。

The addmntent() function adds the mntent structure mnt to the end of the open stream. addmntent()函数将mntent结构mnt添加到打开流的末尾。

The endmntent() function closes the stream associated with the filesystem description file. endmntent()函数关闭与文件系统描述文件关联的流。

The hasmntopt() function scans the mnt_opts field (see below) of the mntent structure mnt for a substring that matches opt. hasmntopt()函数扫描mntent结构mnt的mnt_opts字段(见下文),查找与opt匹配的子字符串。 See and mount(8) for valid mount options. 请参阅并装入(8)以获取有效的挂载选项。

The reentrant getmntent_r() function is similar to getmntent(), but stores the struct mount in the provided *mntbuf and stores the strings pointed to by the entries in that struct in the provided array buf of size buflen. 可重入的getmntent_r()函数类似于getmntent(),但是将struct mount存储在提供的* mntbuf中,并将该结构中的条目指向的字符串存储在提供的大小为buflen的数组buf中。

The mntent structure is defined in as follows: mntent结构定义如下:

  struct mntent { char *mnt_fsname; /* name of mounted filesystem */ char *mnt_dir; /* filesystem path prefix */ char *mnt_type; /* mount type (see mntent.h) */ char *mnt_opts; /* mount options (see mntent.h) */ int mnt_freq; /* dump frequency in days */ int mnt_passno; /* pass number on parallel fsck */ }; 

... ...

The easiest way to check that the filesystem of an open file for writing has become mounted read-only is to check the errno variable for EROFS error. 检查用于写入的打开文件的文件系统是否以只读方式安装的最简单方法是检查errno变量是否存在EROFS错误。

If you don't have the possibility of having a writable directory or file in that filesystem, you cannot get a portable way of checking if the filesystem has become read only (more if it has become so due to device errors) 如果您不可能在该文件系统中具有可写目录或文件,则无法通过可移植方式检查文件系统是否已变为只读(如果由于设备错误而变为更多,则更多)

Another way is to ask the administrator to check, or try to read the /proc/mounts file yourself. 另一种方法是要求管理员检查,或尝试自己读取/proc/mounts文件。 But this is linux specific only. 但这只是linux特有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM