disk: use POSIX df output for Linux root fallback#240
disk: use POSIX df output for Linux root fallback#240pexcn wants to merge 2 commits intonezhahq:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the Linux disk-size fallback logic to use POSIX df output for the root filesystem, addressing incorrect disk capacity reporting on some LXC/OpenVZ-like environments when gopsutil-based aggregation returns 0.
Changes:
- Switch fallback command from
dftodf -P -k /to ensure stable, single-line, 1K-block output for/. - Simplify parsing by matching the
/mount row directly and exiting early after it’s found. - Parse block counts explicitly as base-10 before converting to bytes.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Fallback 到这个方法,仅统计根路径,适用于OpenVZ之类的. | ||
| if runtime.GOOS == "linux" && used == 0 { | ||
| cmd := exec.Command("df") | ||
| cmd := exec.Command("df", "-P", "-k", "/") |
There was a problem hiding this comment.
Consider using exec.CommandContext(ctx, ...) for the df fallback so the command respects cancellation/deadlines from the caller context (avoids a potentially stuck df process).
| cmd := exec.Command("df", "-P", "-k", "/") | |
| cmd := exec.CommandContext(ctx, "df", "-P", "-k", "/") |
| if len(info) == 6 && info[5] == "/" { | ||
| used, _ = strconv.ParseUint(info[2], 10, 64) | ||
| // 默认获取的是1K块为单位的. | ||
| used = used * 1024 | ||
| break |
There was a problem hiding this comment.
Because this branch breaks after the first "/" entry, ParseUint errors should be handled explicitly; otherwise a parse failure will keep used at 0 and still break, preventing any further scanning/logging. Only break after a successful parse.
| cmd := exec.Command("df", "-P", "-k", "/") | ||
| out, err := cmd.CombinedOutput() |
There was a problem hiding this comment.
Consider using exec.CommandContext(ctx, ...) for the df fallback so the command respects cancellation/deadlines from the caller context (avoids a potentially stuck df process).
| cmd := exec.Command("df", "-P", "-k", "/") | |
| out, err := cmd.CombinedOutput() | |
| out, err := exec.CommandContext(ctx, "df", "-P", "-k", "/").CombinedOutput() |
| if len(info) == 6 && info[5] == "/" { | ||
| total, _ = strconv.ParseUint(info[1], 10, 64) | ||
| // 默认获取的是1K块为单位的. | ||
| total = total * 1024 | ||
| break |
There was a problem hiding this comment.
Because this branch breaks after the first "/" entry, ParseUint errors should be handled explicitly; otherwise a parse failure will keep total at 0 and still break, preventing any further scanning/logging. Only break after a successful parse.
|
已更新 |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
可以给一下测试机器 |
|
应该没什么问题,可以辛苦把两段重复逻辑写成一个工具函数么? |
|
这两天比较忙哈,这两天抽空再更新下 |

修复部分LXC VPS磁盘容量显示不正确的问题。
修复前:

修复后:
