0

The question

  1. I have a Linux program that can be run from different directories. It needs to detect the device from where it is started.

  2. The program need to detect where some block device is mounted.

How these tasks can be done?

The language is not important, only the algorithm. It is better external libraries not to be used.

Linux system tools like "df" are not acceptable as the work must to be done in compilable language and the program must to be self-sufficient.

The whole story

It is a part of portable application that need to work on every Linux distribution. The program has to be started from a USB pendrive with two partitions, for example /dev/sdb1 and /dev/sdb2. Linux will automatically mount these partitions on an arbitrary place depending on the Linux distribution.

The user runs the program from the /dev/sdb1 mounting point and the program need to have access to some files on /dev/sdb2. (notice, that the device name varies - sdb, sdc, etc.) In order to read these files, the program must detect what is the device where it is located and then to increase the number by 1 and to find where this device is mounted. Of course, if there is no such device, or the program is located not on removable drive, it will end with error message.

johnfound
  • 6,857
  • 4
  • 31
  • 60
  • Either you're using a big, heavy library which will have to be included with your application with all the fun that involves, or it will use `df(1)`, found in every distro everywhere. Your call. – Ignacio Vazquez-Abrams Oct 14 '14 at 17:33
  • 1
    Everything in Linux can be done using system calls and without calling big heavy libraries or external executables like "df". – johnfound Oct 14 '14 at 17:37
  • Unfortunately, someone locked the question and even if someone knows the answer, it will remain a secret. – johnfound Oct 14 '14 at 17:38
  • 1
    You could use [statvfs(2)](http://man7.org/linux/man-pages/man2/statvfs.2.html) on `"."` – Basile Starynkevitch Oct 14 '14 at 17:51
  • @BasileStarynkevitch - thanks it is a good direction. Then by the f_fsid field (file system ID) I have to search for the file system device. Well, I am reading forth... – johnfound Oct 14 '14 at 18:16

0 Answers0