From 7516a0a2b36eec1da8c6890d68dea49303aad088 Mon Sep 17 00:00:00 2001 From: Shirong Lu <73147033+happysnaker@users.noreply.github.com> Date: Thu, 2 Jul 2026 14:07:26 +0800 Subject: [PATCH] fix(sysfs): accept delayed mdraid sync_completed state Signed-off-by: Shirong Lu <73147033+happysnaker@users.noreply.github.com> --- sysfs/mdraid.go | 2 +- sysfs/mdraid_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/sysfs/mdraid.go b/sysfs/mdraid.go index d706d216..9b288da1 100644 --- a/sysfs/mdraid.go +++ b/sysfs/mdraid.go @@ -166,7 +166,7 @@ func (fs FS) Mdraids() ([]Mdraid, error) { } if val, err := util.SysReadFile(filepath.Join(path, "sync_completed")); err == nil { - if val != "none" { + if val != "none" && val != "delayed" { var a, b uint64 // File contains two values representing the fraction of number of completed diff --git a/sysfs/mdraid_test.go b/sysfs/mdraid_test.go index ea5b8457..7f8897af 100644 --- a/sysfs/mdraid_test.go +++ b/sysfs/mdraid_test.go @@ -16,6 +16,8 @@ package sysfs import ( + "os" + "path/filepath" "testing" "github.com/google/go-cmp/cmp" @@ -155,3 +157,74 @@ func TestMdraidStats(t *testing.T) { t.Fatalf("unexpected Mdraid (-want +got):\n%s", diff) } } + +func TestMdraidsIgnoresDelayedSyncCompleted(t *testing.T) { + root := t.TempDir() + + writeFile := func(rel, content string) { + t.Helper() + path := filepath.Join(root, rel) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + + symlink := func(target, rel string) { + t.Helper() + path := filepath.Join(root, rel) + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(target, path); err != nil { + t.Fatal(err) + } + } + + writeFile("block/md1/md/level", "raid1\n") + writeFile("block/md1/md/array_state", "clean\n") + writeFile("block/md1/md/metadata_version", "1.2\n") + writeFile("block/md1/md/raid_disks", "2\n") + writeFile("block/md1/md/uuid", "test-md1-uuid\n") + writeFile("block/md1/md/degraded", "0\n") + writeFile("block/md1/md/sync_action", "resync\n") + writeFile("block/md1/md/sync_completed", "delayed\n") + writeFile("block/md1/md/dev-sda/state", "in_sync\n") + writeFile("block/md1/md/dev-sdb/state", "in_sync\n") + symlink("dev-sda", "block/md1/md/rd0") + symlink("dev-sdb", "block/md1/md/rd1") + + fs, err := NewFS(root) + if err != nil { + t.Fatal(err) + } + + got, err := fs.Mdraids() + if err != nil { + t.Fatal(err) + } + + want := []Mdraid{ + { + Device: "md1", + Level: "raid1", + ArrayState: "clean", + MetadataVersion: "1.2", + Disks: func(v uint64) *uint64 { return &v }(2), + Components: []MdraidComponent{ + {Device: "sda", State: "in_sync"}, + {Device: "sdb", State: "in_sync"}, + }, + UUID: "test-md1-uuid", + DegradedDisks: 0, + SyncAction: "resync", + SyncCompleted: 0, + }, + } + + if diff := cmp.Diff(want, got); diff != "" { + t.Fatalf("unexpected Mdraid (-want +got):\n%s", diff) + } +}