Recent

Author Topic: [SOLVED] Linux: how to detect whether a certain partition is mounted?  (Read 3629 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
In a console program I want to detect, whether a certain Windows-Partition is currently mounted, but without to change the mount-status.

As a workaround I detect via FindFirst() whether the mounted folder is empty or not (if it's not empty, the partition must be mounted). But this would fail, if the mounted partition is totally empty.

How can this be done? I'm using FPC 3.0.4 on Ubuntu 18.04. Thanks in advance.
« Last Edit: December 07, 2018, 07:52:06 pm by Hartmut »

vincococka

  • Full Member
  • ***
  • Posts: 101
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #1 on: December 06, 2018, 02:55:28 pm »
One possible solution is to check /proc/mounts if it contains exact path that you expected to be mounted.
---
  Guide me God and I`ll find you

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #2 on: December 06, 2018, 03:30:52 pm »
Or you could use fpStatFS() and check the result for congruence.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #3 on: December 06, 2018, 05:45:43 pm »
One possible solution is to check /proc/mounts if it contains exact path that you expected to be mounted.

I looked into file /proc/mounts and it seems to work. But I'm afraid to "disturb" the Operating System when I open that file while reading:

Code: Pascal  [Select][+][-]
  1. function isMounted: boolean;
  2.    var f1: text;
  3.        s: ansistring;
  4.    begin
  5.    assign(f1,'/proc/mounts'); reset(f1);
  6.    while not eof(f1) do
  7.       begin
  8.       readln(f1, s);  
  9.       if pos(' /media/C ', s) > 0 then begin close(f1); exit(true); end;
  10.       end;
  11.    close(f1); exit(false);
  12.    end;  

What do the experts say? Is this "dangerous" or not?


Or you could use fpStatFS() and check the result for congruence.

I tried function fpStatFS from Unit Unix:

Code: Pascal  [Select][+][-]
  1. procedure Test_fpStatFS;
  2.    var info: TStatFs;
  3.        i: longint;
  4.    begin
  5.    fillchar(info,sizeof(info),0);
  6.    i:=fpStatFS('/media/C/', @info);    
  7.    writeln('i=', i);
  8.    end;
 
Result 'i' was always 0.

if mounted =>
INFO={FSTYPE = 1702057286, BSIZE = 4096, BLOCKS = 1048233, BFREE = 106248, BAVAIL = 106248, FILES = 457760, FFREE = 438097, FSID = {0, 0}, NAMELEN = 255, FRSIZE = 4096, SPARE = {4142, 0, 0, 0, 0}}

if not mounted =>
INFO={FSTYPE = 61267, BSIZE = 4096, BLOCKS = 6417977, BFREE = 1245557, BAVAIL = 913781, FILES = 1638400, FFREE = 1111304, FSID = {-1900541924, -1966772697}, NAMELEN = 255, FRSIZE = 4096, SPARE = {4128, 0, 0, 0, 0}}

How do you think that I can distinguish mounted state and not mounted state?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #4 on: December 06, 2018, 08:12:39 pm »
Is this "dangerous" or not?

No, it's not dangerous unless you try to lock it for too much time. Think that it's there precisely to be able to do what you are doing. Although you may as well "slurp" the whole file in one pass and then search into the buffer, if just to minimize file operations.

Quote
I tried function fpStatFS from Unit Unix:
[. . .]
How do you think that I can distinguish mounted state and not mounted state?

Well, to start with: the filesystem type is different, the number of blocks is different, etc. I don't know off the top of ny head how to parse FSType but a man invocation or a search in the kernel docs should tell you that. Let me have a look, I'll be back in a jiffy... There and back again ...

OK, here is (part of) what man statfs has to say about fstype:
Code: [Select]
File system types:
              [. . .]
              EXT_SUPER_MAGIC       0x137D
              EXT2_OLD_SUPER_MAGIC  0xEF51
              EXT2_SUPER_MAGIC      0xEF53
              EXT3_SUPER_MAGIC      0xEF53
              EXT4_SUPER_MAGIC      0xEF53
              HFS_SUPER_MAGIC       0x4244
              HPFS_SUPER_MAGIC      0xF995E849
              [. . .]
              MSDOS_SUPER_MAGIC     0x4d44
              NCP_SUPER_MAGIC       0x564c
              NFS_SUPER_MAGIC       0x6969
              NTFS_SB_MAGIC         0x5346544e
              [. . .]

Just with that you can know whether the path is still in an EXTx partition or an NTFS (or many others!) one.
« Last Edit: December 06, 2018, 08:25:44 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #5 on: December 06, 2018, 09:21:37 pm »
Thanks again for your help! No I understood: when the partition is not mounted, I get FSTYPE = 61267 = 0xef53 which means:

EXT2_SUPER_MAGIC      0xef53
EXT3_SUPER_MAGIC      0xef53
EXT4_SUPER_MAGIC      0xef53
           
This is because folder '/media/C/' is not mounted and that folder is part of the etx4 filesystem of Ubuntu. If folder '/media/C/' is mounted, I get FSTYPE = 1702057286 = 0x65735546 which means:

FUSE_SUPER_MAGIC      0x65735546

Although you may as well "slurp" the whole file in one pass and then search into the buffer, if just to minimize file operations.
Do you mean: read that file in binary mode into an ansistring and then search in that ansistring with pos() like:

Code: Pascal  [Select][+][-]
  1. function readFile_string(filespec: ansistring; var s: ansistring): boolean;
  2.    {reads file 'filespec' into var 's'}
  3.    var f1: file;
  4.        size: int64;
  5.    begin
  6.    assign(f1,filespec); reset(f1,1);
  7.    size:=filesize(f1);
  8.  
  9.    try    SetLength(s,size);
  10.    except close(f1); {if not enough memory}
  11.           SetLength(s,0);
  12.           exit(false);
  13.    end;
  14.  
  15.    if size > 0 then blockread(f1, s[1],size); {0 => "Range check error"}
  16.    close(f1);
  17.    exit(true);
  18.    end;  

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #6 on: December 06, 2018, 10:37:54 pm »

Although you may as well "slurp" the whole file in one pass and then search into the buffer, if just to minimize file operations.
Do you mean: read that file in binary mode into an ansistring and then search in that ansistring with pos() like:

[... some code ...]

Well, yes, that's one way. Or use a (buffered) stream, or even a TStringList. But you got the point: since it's usually rather small (mine's never more than ~2 Kb, negligible) it's better to load it whole in memory and close the file as quickly as possible.

Incidentally, I've taken the liberty of modifying your code a tiny bit:
Code: Pascal  [Select][+][-]
  1. function readFile_string(filespec: ansistring; var s: ansistring): boolean;
  2.    {reads file 'filespec' into var 's'}
  3. var
  4.   f1: file;
  5.   size: int64;
  6. begin
  7.   assign(f1,filespec);
  8.   reset(f1,1);
  9.   try
  10.     size:=filesize(f1);
  11.     if size > 0 then begin
  12.       try
  13.         SetLength(s, size);
  14.       except
  15.         close(f1); {if not enough memory}
  16.         {               .^.
  17.          You should make *sure* whether the problem is really NEM
  18.         }
  19.         s := ''
  20.         Exit(false);
  21.       end;
  22.       blockread(f1, s[1], size); {0 => "Range check error"}
  23.   end else
  24.     s := '';
  25.   finally  
  26.     close(f1);
  27.   end;
  28.   Result := true;
  29. end;  
« Last Edit: December 06, 2018, 10:53:04 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: Linux: how to detect whether a certain partition is mounted?
« Reply #7 on: December 07, 2018, 07:51:39 pm »
The result of this Topic are 2 good solutions:
 - checking file /proc/mounts for the expected path to be mounted or
 - calling function fpStatFS() from unit Unix and checking the magic codes in field INFO.FSTYPE
 
Thanks a lot to all who helped me.

 

TinyPortal © 2005-2018