How to Get The File System Type for Syscall.Mount() Programmatically

Remount filesystem with mount() without knowing the filesystem type or source device

Use getmntent() to iterate over all the mounted filesystems:

NAME

getmntent, setmntent, addmntent, endmntent, hasmntopt, getmntent_r -
get filesystem descriptor file entry

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

Find the filesystem mounted at /, and get it's device from the struct mntent returned.

autodetect filesystem on mount()

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 )

golang no such device in syscall.Mount

You didn't specify your OS but I think the problem is the same on many implementations.

On Linux syscall.Mount just wraps mount(2) which doesn't itself support the concept of an "auto" fstype.

The reason the mount(8) command works with "auto" is because it does its own magic:

If no -t option is given, or if the auto type is specified,
mount will try to guess the desired type. Mount uses the
blkid library for guessing the filesystem type
; if that does
not turn up anything that looks familiar, mount will try to
read the file /etc/filesystems, or, if that does not exist,
/proc/filesystems.

How do I programmatically get the free disk space for a directory in Linux

check man statvfs(2)

I believe you can calculate 'free space' as f_bsize * f_bfree.

NAME
statvfs, fstatvfs - get file system statistics

SYNOPSIS
#include <sys/statvfs.h>

int statvfs(const char *path, struct statvfs *buf);
int fstatvfs(int fd, struct statvfs *buf);

DESCRIPTION
The function statvfs() returns information about a mounted file system.
path is the pathname of any file within the mounted file system. buf
is a pointer to a statvfs structure defined approximately as follows:

struct statvfs {
unsigned long f_bsize; /* file system block size */
unsigned long f_frsize; /* fragment size */
fsblkcnt_t f_blocks; /* size of fs in f_frsize units */
fsblkcnt_t f_bfree; /* # free blocks */
fsblkcnt_t f_bavail; /* # free blocks for unprivileged users */
fsfilcnt_t f_files; /* # inodes */
fsfilcnt_t f_ffree; /* # free inodes */
fsfilcnt_t f_favail; /* # free inodes for unprivileged users */
unsigned long f_fsid; /* file system ID */
unsigned long f_flag; /* mount flags */
unsigned long f_namemax; /* maximum filename length */
};

Mount NTFS device in C++ on linux?

mount.2 is just a kernel call. mount.8 is a complete external tool which is extended beyond what kernel does.

I think you may be looking for libmount which is a library implementing the whole mounting magic done by mount.8. Newer mount versions use it as well. It's provided in util-linux.



Related Topics



Leave a reply



Submit