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 entrySYNOPSIS
#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
Bash Script to Install Postgresql - Not Working
Version 'Glibc_2.32' Not Found Error When Running Petalinux-Build
Session Permission Denied on Aws After Setting Up Cake PHP.
Reading Microphone Data by Polling Using Alsa [Or V4L2]
Ffmpeg Combining Images to Video and Streaming in One Command Line
Chef-Server Stuck Ruby_Block[Supervise_Rabbitmq_Sleep] Action Run on Docker Container
Please Help Me "Binary Operator Expected in Cygwin"
Error When Compiling Linux Kernel 3.2 for Arm
How to Use Xdotool to Enter a Web Console Command
Ftrace: System Crash When Changing Current_Tracer from Function_Graph via Echo
Linux Allocates Memory at Specific Physical Address
Perl and Bash Variable Substitution, with Hexadecimal Characters and Repeat Count
Redirecting Output of a C Program to Another C Program with a Bash Script Under Linux
Why This Shell Won't Work If It's Called from Rc.Local But Ssh
Extract/See Content of a Specific File Inside a .War File
How to Use 'Catdoc' to Display Dock File Encoded in Utf-8
What Is The Right Place for Findxxx.Cmake Files for Locally Compiled Libs