十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Linux 磁盘空间显示为负?深度解析 df -h 负值背后的文件系统元数据损坏与修复实战

Linux 磁盘空间显示为负?深度解析 df -h 负值背后的文件系统元数据损坏与修复实战 一、背景修机师傅运气不错有时候还能收到些“奇奇怪怪”的问题话说df -h返回值为负是啥情况上个图检查挂载点下也没有特别异常的文件而且业务还运行正常。检查内核日志发下xfs有报错重启故障虚拟机也还是有这个现象 。在关机状态下克隆了一台做测试以下操作基于克隆的虚拟机。二、修复与分析1、进入救援模式查看挂载点显示为负值2、执行xfs_repair /dev/mapper/centos-root然后再次挂载正常从xfs_repair修复的结果看有识别到一些异常并且成功修复了而修复之后再次挂载空间使用也正常。结合日志和操作分析是xfs文件系统异常导致后面在redhat kb库也找到了类似案例同步分享下。3、进一步分析根因Root CauseIn this case, the command the strace of the df command provided a value of f_bfree, 37345930, which is larger than the value of f_blocks, 34216732. Based on statfs(2) man page, we see the second parameter is a struct which the kernel populates, with the following member definitions:Rawstruct statfs { long f_type; /* type of file system (see below) */ long f_bsize; /* optimal transfer block size */ long f_blocks; /* total data blocks in file system */ long f_bfree; /* free blocks in fs */ long f_bavail; /* free blocks avail to non-superuser */ long f_files; /* total file nodes in file system */ long f_ffree; /* free file nodes in fs */ fsid_t f_fsid; /* file system id */ long f_namelen; /* maximum length of filenames */ };statfs is part of the linux VFS, so each file system will have its own implementation. In this case, the file system was ext3, which populates the two struct fields of interest in fs/ext3/super.c via:Rawfbuf-f_blocks le32_to_cpu(es-s_blocks_count) - overhead; buf-f_bfree percpu_counter_sum(sbi-s_freeblocks_counter);Since both values are maintained by the file system, some form of filesystem corruption seemed likely.Diagnostic StepsRun strace to determine if the kernel is handing back bad data or df mishandling itIn particular, look at the values of f_bfree and f_blocksIf b_free excedes b_blocks, the kernel is providing bad dataRaw# strace -e tracestatfs,statfs64 df /dev/cciss/c0d0p1 /dev/null statfs(/, {f_typeEXT2_SUPER_MAGIC, f_bsize4096, f_blocks34216732, f_bfreebr /37345930, f_bavail35579785, f_files35323904, f_ffree35180816, f_fsid{0, 0}, f_namelen255, f_frsize4096}) 0参考上述kb当文件系统的f_free大于f_blocks时会出现负值我这边的环境这个值出现异常也就是前面xfs报错相关。三、总结df -h返回负值是因为文件系统元数据f_free大于f_blocks导致解决方案主要也是通过文件系统修复来处理1、卸载并检查文件系统对于ext3/ext4文件系统使用e2fsck -fvy /dev/centos/root来修复对xfs文件系统使用xfs_repair命令2、如果无法卸载文件则进救援模式并参考上面步骤处理。3、如果文件系统仍显示负值则考虑是否空间不足尝试进行文件系统扩容。
返回列表