Sunday, July 12, 2009

Mounted Filesystem Information

For getting information of a partition (filesystem) which is mounted on a directory on Linux System, I have created a C program. Its listing is given as below:





/*
* =====================================================================================
*
* Filename: file_system_stat.c
*
* Description: Get file system statistics of mounted partition
*
* Version: 1.0
* Created: Thursday 09 July 2009 10:48:02 IST IST
* Revision: none
* Compiler: gcc
*
* Author: Mitesh Singh Jat (mitesh)
*
* =====================================================================================
*/

#include <stdio.h>
#include <sys/statfs.h>

int file_system_stats(const char *pathp)
{
struct statfs sfs;
int retval;
if (pathp == NULL)
{
fprintf(stderr, "file_system_stats: cannot find stats of NULL path\n");
return (-1);
}
retval = statfs(pathp, &sfs);
if (retval < 0)
{
fprintf(stderr, "file_system_stats: cannot find file system\n");
fprintf(stderr, " stats of partition %s\n", pathp);
return (-1);
}
printf("\n=== Filesystem Stats of '%s' ===\n", pathp);
printf("Optimal Transfer Block Size = %ld\n", sfs.f_bsize);
printf("Total data blocks = %ld\n", sfs.f_blocks);
printf("Total free data blocks = %ld\n", sfs.f_bfree);
printf("Total file nodes = %ld\n", sfs.f_files);
printf("Total free file nodes = %ld\n", sfs.f_ffree);
printf("=================================================\n\n");
return (retval);
}

int main(int argc, char *argv[])
{
int i;
int retval = 0;
if (argc <= 1)
{
fprintf(stderr, "Usage: %s <path> [more_paths...]\n", argv[0]);
retval = file_system_stats("./");
return (retval);
}

for (i = 1; i < argc && retval >= 0; ++i)
{
retval = file_system_stats(argv[i]);
}
return (retval);
}



Compilation:
$ gcc -Wall -o file_system_stat file_system_stat.c

Sample Runs:
--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--
--(0 : 505)
$ ./file_system_stat
Usage: ./file_system_stat <path> [more_paths...]


=== Filesystem Stats of './' ===
Optimal Transfer Block Size = 4096
Total data blocks = 9690330

Total free data blocks = 3956097

Total file nodes = 2449408

Total free file nodes = 2315870

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--

--(0 : 506)
$ ./file_system_stat /boot

=== Filesystem Stats of '/boot' ===

Optimal Transfer Block Size = 4096

Total data blocks = 12017122

Total free data blocks = 5616343

Total file nodes = 3055616

Total free file nodes = 2830533

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--
--(0 : 507)
$ ./file_system_stat /boot /mnt/extra

=== Filesystem Stats of '/boot' ===

Optimal Transfer Block Size = 4096

Total data blocks = 12017122

Total free data blocks = 5616343

Total file nodes = 3055616

Total free file nodes = 2830533

=================================================



=== Filesystem Stats of '/mnt/extra' ===

Optimal Transfer Block Size = 4096

Total data blocks = 21235766

Total free data blocks = 7269970

Total file nodes = 5357568

Total free file nodes = 5357279

=================================================


--(mitesh@roundduck-lm)-(~/Programming/C/Usp)--

--(1 : 508)
$ ./file_system_stat /this_is_not_a_partition
file_system_stats: cannot find file system

stats of partition /this_is_not_a_partition





Note: By the way, the block size of a filesystem can be found using commands given at here or here.

2 comments:

Nikesh Srivastava said...

Hi Mitesh,
Beautiful Contribution for Linux user. Please keep posting such blog some where some one may utilize these concept to enhance his/her skill sets using your blog. Good job.......

mitesh.singh.jat said...

Hi Nikesh,

Many thanks! for these encouraging words. :)

Regards,
Mitesh