I have a software that will unmount a partition programatically (if it's not busy) and mount it again after all of the work is done.

If I run this in the terminal:

sudo mount /dev/sdb1 /mnt/fat32 -o sync,dirsync,rw,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro -t vfat

I get this in /proc/self/mountinfo:

100 24 8:17 / /mnt/fat32 rw,relatime shared:61 - vfat /dev/sdb1 rw,sync,dirsync,uid=1000,gid=1000,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,showexec,utf8,flush,errors=remount-ro

So I made a mountinfo parser andto get the data to restore the mount, but I'm having with converting mount options (rw,relatime) and super options (the last part in /mountinfo: rw,sync,dirsync,...) with mountoptions and data:

int mount(const char *source, const char *target,
          const char *filesystemtype, unsigned long mountflags,
          const void *data);

My approach was to convert the mount options into the appropriate mountflags and to give special options straight to data, but then I get this error:

[62857.390803] FAT-fs (sdb1): Unrecognized mount option "rw" or missing value

So I made a function to copy the attributes in the super options thet just matches the filesystem specific data, and now it works. But I still have a problem: how can I set, for instance, sync and dirsync if they are not in the mount options flags and if they aren't for vfat specific attributes?

If it can be made through the command line mount it can also be made in the command line version, right?


Some of the parameters given to mount(8) are translated to flags specified in the mountflags parameter to mount(2):

  • sync is MS_SYNCHRONOUS;
  • dirsync is MS_DIRSYNC;
  • relatime is MS_RELATIME;
  • rw is the default, so it can't be specified; ro would be MS_RDONLY.
  • so in my case mountflags it will be generated with /mountinfo mountoptions (rw,relatime) and non-filesystem specific options from the super options (e.g. rw, sync, dirsync)? – rnunes May 18 '16 at 9:41
  • 1
    I think the general idea is to look for any options that match flags, process those, and then handle any remaining options. In your case, that would indeed cover rw, relatime, sync and dirsync. – Stephen Kitt May 18 '16 at 9:44

So I made a mountinfo parser […]

You're better off using setmntent() and getmntent() that are provided in the GNU C runtime library.

My approach was to convert the mount options into the appropriate mountflags and to give special options straight to data […]

What you need to do is take the mnt_opts given to you by getmntent() and process them into flags and data. The mount command from the util-linux package relies upon a library for this, named libmount. Some of it is table-driven, from lengthy Linux-specific tables.

Further reading

  • "The mtab file". The GNU C Library Reference Manual. 2.23. Free Software Foundation. 2016.
  • Karel Zak (2012). "Mount options". libmount Reference Manual. 2.21. Linux Kernel Archives.
  • getmntent() doesn't work (with me) with /mountinfo but only with the /proc/mounts structure, guess they just do a sscanf like I did for the parameters and /mountinfo have extra parameters. until now I didn't need any of the extra information in /mountinfo so I might change it to that – rnunes May 18 '16 at 12:57
  • also, I remember reading somewhere (can't find the source now) that it was always better to read from /mountinfo, for some reason (portability?) – rnunes May 18 '16 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.