Skip to content

disk: use POSIX df output for Linux root fallback#240

Open
pexcn wants to merge 2 commits intonezhahq:mainfrom
pexcn:fix-disk-usage
Open

disk: use POSIX df output for Linux root fallback#240
pexcn wants to merge 2 commits intonezhahq:mainfrom
pexcn:fix-disk-usage

Conversation

@pexcn
Copy link
Copy Markdown

@pexcn pexcn commented Apr 23, 2026

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

修复前:
image

修复后:
image

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 df to df -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.

Comment thread pkg/monitor/disk/disk.go Outdated
// Fallback 到这个方法,仅统计根路径,适用于OpenVZ之类的.
if runtime.GOOS == "linux" && used == 0 {
cmd := exec.Command("df")
cmd := exec.Command("df", "-P", "-k", "/")
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
cmd := exec.Command("df", "-P", "-k", "/")
cmd := exec.CommandContext(ctx, "df", "-P", "-k", "/")

Copilot uses AI. Check for mistakes.
Comment thread pkg/monitor/disk/disk.go Outdated
Comment on lines +81 to +85
if len(info) == 6 && info[5] == "/" {
used, _ = strconv.ParseUint(info[2], 10, 64)
// 默认获取的是1K块为单位的.
used = used * 1024
break
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread pkg/monitor/disk/disk.go Outdated
Comment on lines 40 to 41
cmd := exec.Command("df", "-P", "-k", "/")
out, err := cmd.CombinedOutput()
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
cmd := exec.Command("df", "-P", "-k", "/")
out, err := cmd.CombinedOutput()
out, err := exec.CommandContext(ctx, "df", "-P", "-k", "/").CombinedOutput()

Copilot uses AI. Check for mistakes.
Comment thread pkg/monitor/disk/disk.go Outdated
Comment on lines +46 to +50
if len(info) == 6 && info[5] == "/" {
total, _ = strconv.ParseUint(info[1], 10, 64)
// 默认获取的是1K块为单位的.
total = total * 1024
break
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@pexcn
Copy link
Copy Markdown
Author

pexcn commented Apr 23, 2026

已更新

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@uubulb
Copy link
Copy Markdown
Contributor

uubulb commented Apr 26, 2026

可以给一下测试机器 dfdf -P -k / 的样例输出么?

@pexcn
Copy link
Copy Markdown
Author

pexcn commented Apr 27, 2026

image 已附上截图

@uubulb
Copy link
Copy Markdown
Contributor

uubulb commented Apr 27, 2026

应该没什么问题,可以辛苦把两段重复逻辑写成一个工具函数么?

@pexcn
Copy link
Copy Markdown
Author

pexcn commented Apr 27, 2026

这两天比较忙哈,这两天抽空再更新下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants