diff --git a/pkg/util/hardware/hardware_info.go b/pkg/util/hardware/hardware_info.go index 38fb6e220ea7e..877edd505c1e3 100644 --- a/pkg/util/hardware/hardware_info.go +++ b/pkg/util/hardware/hardware_info.go @@ -17,6 +17,8 @@ import ( "runtime" "sync" + "github.com/cockroachdb/errors" + "github.com/cockroachdb/errors/oserror" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/mem" @@ -107,6 +109,10 @@ func GetFreeMemoryCount() uint64 { func GetDiskUsage(path string) (float64, float64, error) { diskStats, err := disk.Usage(path) if err != nil { + // If the path does not exist, ignore the error and return 0. + if errors.Is(err, oserror.ErrNotExist) { + return 0, 0, nil + } return 0, 0, err } usedGB := float64(diskStats.Used) / 1e9 diff --git a/pkg/util/hardware/hardware_info_test.go b/pkg/util/hardware/hardware_info_test.go index 037e1c3a58cb5..7bb79de04d935 100644 --- a/pkg/util/hardware/hardware_info_test.go +++ b/pkg/util/hardware/hardware_info_test.go @@ -47,6 +47,11 @@ func TestGetDiskUsage(t *testing.T) { assert.NoError(t, err) assert.GreaterOrEqual(t, used, 0.0) assert.GreaterOrEqual(t, total, 0.0) + + used, total, err = GetDiskUsage("/dir_not_exist") + assert.NoError(t, err) + assert.Equal(t, 0.0, used) + assert.Equal(t, 0.0, total) } func TestGetIOWait(t *testing.T) {