statvfs()对于每个挂载的文件系统(通过提供到所述挂载的文件系统上的任何文件或目录的路径来指定),函数会报告您寻找的内容。
如果系统安装了 GNU coreutils,它会有一个名为stat. 对于文件系统上文件或目录的每个路径,
stat -c '%d %c' -f /path
Run Code Online (Sandbox Code Playgroud)
报告空闲 inode 的数量和 inode 的总数,给定的每个路径一行。如果系统使用busybox,则
busybox stat -c '%d %c' -f /path
Run Code Online (Sandbox Code Playgroud)
做同样的事情。
如果您需要对输出进行更多控制,或者由于某种原因上述两种方法都不适合您,您可以轻松编写自己的实用程序来报告摘要:这是一个示例,inode-stats.c:
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
dev_t device[argc];
int devices = 0;
uint64_t total_inodes = 0;
uint64_t avail_inodes = 0; /* Free to normal users */
uint64_t free_inodes = 0; /* Free to superuser */
int arg, i;
if (argc < 2 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
printf("\n");
printf("Usage: %s [ -h | --help ]\n", argv[0]);
printf(" %s mountpoint [ mountpoint ... ]\n", argv[0]);
printf("\n");
printf("This program will report the percentage of inodes in use,\n");
printf("the number free inodes available for normal users,\n");
printf("the number of free inodes available for root,\n");
printf("and the total number of inodes,\n");
printf("in the filesystems referred to the supplied paths.\n");
printf("\n");
printf("Each mount is only counted once, even if multiple paths\n");
printf("to the same mount are given as parameters.\n");
printf("\n");
return EXIT_SUCCESS;
}
for (arg = 1; arg < argc; arg++) {
struct stat info;
struct statvfs vfsinfo;
if (stat(argv[arg], &info) == -1) {
fprintf(stderr, "%s: %s.\n", argv[arg], strerror(errno));
continue;
}
if (statvfs(argv[arg], &vfsinfo) == -1) {
fprintf(stderr, "%s: %s.\n", argv[arg], strerror(errno));
continue;
}
/* Check if device already seen. */
for (i = 0; i < devices; i++)
if (info.st_dev == device[i])
break;
if (i < devices)
continue;
/* Add to known devices. */
device[devices++] = info.st_dev;
/* Add to inode counters. */
total_inodes += (uint64_t)vfsinfo.f_files;
avail_inodes += (uint64_t)vfsinfo.f_favail;
free_inodes += (uint64_t)vfsinfo.f_ffree;
}
if (total_inodes < 0) {
fprintf(stderr, "No inodes!\n");
return EXIT_FAILURE;
}
/* Print result. */
printf("%.3f%% - %" PRIu64 " free (%" PRIu64 " for root) out of %" PRIu64 " inodes.\n",
100.0 - 100.0 * (double)avail_inodes / (double)total_inodes,
avail_inodes, free_inodes, total_inodes);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
使用例如编译它
gcc -Wall -O2 inode-stats.c -o inode-stats
Run Code Online (Sandbox Code Playgroud)
可选择使用例如安装它
sudo install -o root -g root -m 0755 inode-stats /usr/bin
Run Code Online (Sandbox Code Playgroud)
并运行它,提供您感兴趣的挂载(挂载的文件系统)中任何目录或文件的路径。例如,
inode-stats / /usr /var /home
Run Code Online (Sandbox Code Playgroud)
该程序足够智能,只计算一次挂载,即使您提供了指向其中的目录/文件的多个路径 - 与 GNU coreutils 或 busybox 不同stat。
您可以简单地更改输出报告格式,并轻松添加其他统计信息(如可用磁盘空间、(uint64_t)vfsinfo.f_bavail * (uint64_t)vfsinfo.f_bsize普通用户可用的磁盘空间量以及(uint64_t)vfsinfo.f_blocks * (uint64_t)vfsinfo.f_frsize每个文件系统的总大小)。
| 归档时间: |
|
| 查看次数: |
3051 次 |
| 最近记录: |