2022-03-02 Update
在 Linux 文件系统中,rm /home/john/example.log 并不会立即删除这个文件。文件系统将等待这个文件的连接数降为 1,确保没有任何进程打开这个文件,才会执行删除动作释放空间。因此删除程序的日志文件很可能不会立即产生效果。
要找出哪个进程在使用文件,可使用 lsof(LiSt Open Files)。
When
+Lis specified without a following number, all link counts will be listed. When-Lis specified (the default), no link counts will be listed.When
+Lis followed by a number, only files having a link count less than that number will be listed. (No number may follow-L.) A specification of the form+L1will select open files that have been unlinked. A specification of the form+aL1 <file_system>will select unlinked open files on the specified file system.
输出摘要为下(略去无关内容)
可以看到 ID 为 4344 的进程正在占用该文件,使用 kill -2 终止该进程即可。
在一次项目更新操作中,备份原 jar 包的脚本返回 cp: error writing 'xxx.bak': No space left on device,磁盘空间已耗尽。
查看磁盘剩余空间,/dev/vda1 只有 60GB。磁盘 /vda 应该是 ECS 的系统盘,此时已达到 100% 占用率。
查看其他磁盘,有一个 536.9GB 的磁盘未被使用过,应该是 ECS 的数据盘。
使用 fdisk 为未使用的磁盘分区 fdisk /dev/vdb 将在磁盘 vdb 上创建分区 /dev/vdb1。
为了和系统盘使用的文件系统保持一致,数据盘也使用 mkfs -t ext3 /dev/vdb1 格式化为 ext3 格式。此过程中需要交互式地选择一些参数,具体内容需要参考 mkfs 的相关资料。
通过 mount /dev/vdb1 /opt 挂载新分区到 /opt。再次查询可用空间,发现增加了 467GB。
编辑 /etc/fstab 添加一条记录以使系统重新启动时自动挂载设备,格式为 UUID=[UUID] [mount-point] [fstype] defaults 1 2。磁盘的 UUID、挂载点、文件系统类型均可通过 lsblk -f 查询,其他参数的配置参考 fstab 文档。本例中使用如下配置:
接下来清理系统盘,根据对业务的了解,估计是系统运行中产生的日志文件和中间文件耗尽了磁盘空间。项目的可执行文件位于 /usr/local/ 目录,从该目录开始查找。
du 命令可以输出指定目录下文件夹的大小。可以看到 /usr/local/picture 目录占用了 56GB 空间。
尝试把 /usr/local/picture/ 移动到 /opt/smart-claims/picture/。由于这是两个磁盘间的复制操作,整个过程耗时将长达数小时,建议在 tmux 创建的 session 中执行,防止堡垒机长时间未检测到用户操作断开连接。
目标机器没有安装 tmux,且磁盘已经没有空闲空间可以安装软件包了,故使用 nohup mv /usr/local/picture /opt/smart-claims/ & 命令在后台运行作业。可用通过 jobs 等作业相关命令查看执行情况。
通过查证源代码确定了应用程序使用绝对路径保存文件,使用 ln -s /opt/smart-claims/picture /usr/local/picture 在原目录位置创建指向新路径的软链接。
ll /usr/local/picture 输出 /usr/local/picture -> /opt/smart-claims/picture,此时访问 /usrlocal/picture/README 实际上是在访问位于 /opt/smart-claims/picture/ 的 README 文件,因此程序的执行基本不会受到影响。
查看可用磁盘空间,问题得到了解决。