clinuxfilesystemsmount

autodetect filesystem on mount()


My app is supposed to mount and check contents of any pendrive inserted in the USB slot. The problem is, some users may be using pendrives formatted for other filesystems than standard 'vfat'. The standard

 mount /dev/sda1 /mnt/pendrive -t auto

would be sufficient for my needs - it works and any filesystem readable by the kernel will be used. The problem is I have to do it from within the application, and I'd prefer to avoid calling system() and shell commands when "natural" C solution exists.

 #include <sys/mount.h>
 ...
 result = mount("/dev/sda1", "/mnt/pendrive", "vfat" ,0, NULL);

works correctly. But if I replace "vfat" with "auto" or NULL nothing is mounted.

As I checked sources for busybox's mount, it seems to set the filesystem to NULL in the mount() call if 'auto' or no filesystem type is supplied. But that trick doesn't seem to work for me. What am I missing? Is there some relatively simple way to do this?


Solution

  • The kernel isn't able to auto-detect the file system, so you have to do it yourself.

    What busybox actually does is just loop through all the relevant filesystems, parsed from /proc/filesystems, and call mount() until it succeeds (see line 1898 )