Linux系统 df 命令显示异常、分区丢失问题解决
<article><h2 style="box-sizing: border-box; font-family: inherit; line-height: 1.1; color: rgb(255, 102, 0); margin: 26px 0px 0.6em; font-size: 26px; border-bottom: 1px solid;">一、根目录丢失</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">前些日子,同事在 RTX 群里问大家,有台服务器执行 df -h 看不到根目录,该如何解决?</p><pre class="lang:sh decode:true hljs sql" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.207.126.12:~]# df -hFilesystem Size Used Avail Use% Mounted onudev 8.0G 168K 8.0G 1% /dev/dev/sda3 20G 1.7G 18G 9% /usr/local/dev/sda4 103G 63G 35G 65% /data</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">于是我帮忙解决了一把,看了下 /etc/fstab 内容,根目录挂载信息是正常的:</p><pre class="lang:sh decode:true hljs properties" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.207.126.12:~]# cat /etc/fstab /dev/sda1 / ext3 noatime,acl,user_xattr 1 1/dev/sda2 swap swap defaults 0 0/dev/sda3 /usr/local ext3 noatime,acl,user_xattr 1 2/dev/sda4 /data ext3 noatime,acl,user_xattr 1 2proc /proc proc defaults 0 0sysfs /sys sysfs noauto 0 0debugfs /sys/kernel/debug debugfs noauto 0 0devpts /dev/pts devpts mode=0620,gid=5 0 0</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">接着,看了下 /etc/mtab 文件内容,发现根目录缺失:</p><pre class="lang:sh decode:true hljs properties" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.207.126.12:~]# cat /etc/mtab proc /proc proc rw 0 0udev /dev tmpfs rw 0 0devpts /dev/pts devpts rw,mode=0620,gid=5 0 0/dev/sda3 /usr/local ext3 rw,noatime,acl,user_xattr 0 0/dev/sda4 /data ext3 rw,noatime,acl,user_xattr 0 0nfsd /proc/fs/nfsd nfsd rw 0 0</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">执行 grep -v rootfs /proc/mounts 命令进行修复:</p><pre class="lang:sh decode:true hljs ruby" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.207.126.12:~]# grep -v rootfs /proc/mounts > /etc/mtab[root@10.207.126.12:~]# cat /etc/mtab/dev/root / ext3 rw,data=ordered 0 0proc /proc proc rw 0 0sysfs /sys sysfs rw 0 0udev /dev tmpfs rw 0 0devpts /dev/pts devpts rw 0 0/dev/sda3 /usr/local ext3 rw,noatime,data=ordered 0 0/dev/sda4 /data ext3 rw,noatime,data=ordered 0 0nfsd /proc/fs/nfsd nfsd rw 0 0</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">可以看到,根目录已经出现了,再执行 df -h 就正常了:</p><pre class="lang:sh decode:true hljs sql" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.207.126.12:~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/root 9.9G 5.0G 4.4G 54% /udev 8.0G 168K 8.0G 1% /dev/dev/sda3 20G 1.7G 18G 9% /usr/local/dev/sda4 103G 63G 35G 65% /data</pre><h2 style="box-sizing: border-box; font-family: inherit; line-height: 1.1; color: rgb(255, 102, 0); margin: 26px 0px 0.6em; font-size: 26px; border-bottom: 1px solid;">二、df 命令报错</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">帮同事解决问题后,不巧自己负责的服务器也出现类似问题,执行 df 命令报如下错误:</p><blockquote style="box-sizing: border-box; padding: 10px 20px; margin: 0px 0px 20px; font-size: 17.5px; border-left: 5px solid rgb(238, 238, 238);"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">df: cannot read table of mounted file systems: No such file or directory</p></blockquote><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">想着应该可以上述问题原因一样,所以直接执行修复命令,发现报错:</p><pre class="lang:sh decode:true hljs ruby" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.12.208.117:/var/spool/postfix]# grep -v rootfs /proc/mounts > /etc/mtabgrep: write error: No space left on device</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">看来是空间不足,找了下发现是 maildrop 目录把根目录撑爆了:</p><pre class="lang:sh decode:true hljs css" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.12.208.117:/var/spool/postfix]# du -sh *4.0K active4.0K bounce4.0K corrupt4.0K defer4.0K deferred4.0K flush4.0K hold4.0K incoming9.4G maildrop8.0K pid4.0K private4.0K public4.0K saved4.0K trace</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">直接清空,在执行 grep -v rootfs /proc/mounts >/etc/mtab 命令进行修复:</p><pre class="lang:sh decode:true hljs sql" style="box-sizing: border-box; font-size: 1em; font-family: "Lucida Console", Monaco, "Courier New"; overflow-x: auto; padding: 0.5em 0px; color: rgb(171, 178, 191); background-image: linear-gradient(rgb(41, 56, 64) 50%, rgb(34, 50, 58) 0px); background-position: 0% 0%; background-size: auto 36px; background-attachment: local; background-origin: content-box; background-clip: content-box; background-color: rgb(34, 50, 58); border-radius: 10px; box-shadow: rgba(31, 45, 62, 0.38) 0px 10px 13px;">[root@10.12.208.117:/var/spool/postfix]# grep -v rootfs /proc/mounts > /etc/mtab[root@10.12.208.117:/var/spool/postfix]# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda1 20G 10.5G 9.5G 53% //dev/sda2 20G 1.7G 18G 9% /usr/local/dev/sda3 103G 63G 35G 65% /data</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">已经正常了,maildrop 爆满的问题一般是 crontab 未屏蔽错误造成的,于是顺手将 crontab 里面的条目都带上了 2>&1 屏蔽了,下次应该不会出现因为目录爆满导致 mtab 异常的情况了。</p><h2 style="box-sizing: border-box; font-family: inherit; line-height: 1.1; color: rgb(255, 102, 0); margin: 26px 0px 0.6em; font-size: 26px; border-bottom: 1px solid;">三、区别与联系</h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">继续记录一下/etc/fstab 和/etc/mtab 的区别和联系。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">/etc/fstab 文件记录了服务器上硬盘分区信息,启动 Linux 的时候,检查分区的 fsck 命令和挂载分区的 mount 命令都需要 fstab 中的信息,来检查和挂载分区。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">/etc/mtab 文件记载的是现在系统已经装载的文件系统,包括操作系统建立的虚拟文件等,每当 mount 挂载分区、umount 卸载分区,都会动态更新 mtab,mtab 总是保持着当前系统中已挂载的分区信息,fdisk、df 这类程序,必须要读取 mtab 文件,才能获得当前系统中的分区挂载情况。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 20px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9;">当然我们自己还可以通过读取/proc/mount 也可以来获取当前挂载信息(即使用文章中用到的修复命令 grep -v rootfs /proc/mounts)。</p><p style="box-sizing: border-box; margin-top: 0px; z-index: 2; font-size: 16px; position: relative; line-height: 1.9; margin-bottom: 0px !important;">当 /etc/mtab 因为磁盘满或文件系统异常,导致该文件内缺失常或直接为空,就会出现上文记录的问题了。</p></article><div class="share-modal" style="box-sizing: border-box; margin-left: -60px; margin-right: -60px; padding-left: 30px; padding-right: 30px; height: 78px; background: rgb(249, 250, 252); border-top: 1px solid rgb(240, 242, 247); margin-top: 40px; text-align: right; color: rgb(101, 116, 135); font-family: Play, 微软雅黑, "Microsoft YaHei UI", "Microsoft YaHei", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; white-space: normal;"><span class="fab fa-weibo" style="box-sizing: border-box; -webkit-font-smoothing: antialiased; display: inline-block; font-variant-numeric: normal; font-variant-east-asian: normal; text-rendering: auto; line-height: 34px; font-family: "Font Awesome 5 Brands"; background: radial-gradient(circle at 75% 25%, rgb(255, 122, 96) 0px, rgb(230, 22, 45) 100%); transition: all 0.5s ease 0s; color: rgb(255, 255, 255); width: 32px; height: 32px; border-radius: 50%; margin-right: 4px; font-size: 16px; text-align: center; box-shadow: rgba(255, 73, 73, 0.6) 0px 10px 10px -2px;"></span></div><p><br/></p>