Install NTFS device in C ++ on Linux?

I am trying to connect an external drive in my C ++ application. At first I tried to use mount (2), but this failed:

int ret = mount(deviceName.c_str(), mountPoint.c_str(), fsType.c_str(), 0, NULL); 

errno - 19, ENODEV (file system type not configured in the kernel)

However, if I switch to using mount (8), it works fine:

 std::string cmd = "mount -t " + fsType + " " + deviceName + " " + mountPoint; int ret = system(cmd.c_str()); 

Does mount (2) have a different list of valid file system types? This is an ntfs device, so I used ntfs-3g as fstype. I checked / proc / filesystems and saw that it was not on the list, so I tried fuseblk , but it just changed the error to 22, EINVAL .

What is the correct way to connect NTFS devices using mount (2)?

+4
source share
2 answers

mount.2 is just a kernel call. mount.8 is a complete external tool that extends beyond what the kernel does.

I think you can look for libmount , which is a library that implements all the mount magic performed by mount.8 . Newer mount versions also use it. It is introduced in util-linux .

+4
source

Have you tried running mount(8) with the strace command? It will print the system calls made by the program, including mount(2) . When I do such a mount, it runs mount.ntfs (which is NTFS-3g), which then runs mount for fuseblk and then rotates in the background to support that mount point.

FUSE-based file systems are handled differently because the user space daemon must be running. Installing with fuseblk does not provide sufficient information that the kernel starts the daemon (and the kernel does not even have information to start the daemon). For ntfs-3g, you can usually do something like ntfs-3g /dev/sda1 /mnt/windows (from the help). There is no software way to tell the kernel to do this because it is happening in user space.

+2
source

Source: https://habr.com/ru/post/1432699/


All Articles