在不知道文件系统类型或源设备的情况下,使用mount()重新挂载文件系统

时间:2018-08-25 13:22:31

标签: c linux filesystems mount

我需要在Linux上运行的C上以只读方式重新安装/。到目前为止,我已经提出了以下代码:

mount("/dev/sda1", "/", "ext4", MS_REMOUNT | MS_RDONLY, NULL);

但是我想知道是否有一种方法可以不指定源设备(/ dev / sda1)或文件系统类型(ext4),例如命令mount -o remount,ro /。可以仅将NULL替换为syscall的那些部分吗?

1 个答案:

答案 0 :(得分:1)

使用getmntent() to iterate over all the mounted filesystems

  

NAME

     

getmntent,setmntent,addmntent,endmntent,hasmntopt,getmntent_r-   获取文件系统描述符文件条目

     

简介

   #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);

找到挂载在/上的文件系统,然后从返回的struct mntent获取它的设备。