From 22ede1c2b73e6994f94eb6002dd7211d800a63cb Mon Sep 17 00:00:00 2001 From: Christophe de Carvalho Date: Tue, 24 Mar 2020 15:38:19 +0100 Subject: [PATCH] handle error when smartcl returns an empty response fixes #608 --- pkg/capacity/capacity.go | 6 ++++++ pkg/capacity/smartctl/smartctl.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/pkg/capacity/capacity.go b/pkg/capacity/capacity.go index 848282fc6..4e6dfa50e 100644 --- a/pkg/capacity/capacity.go +++ b/pkg/capacity/capacity.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/pkg/errors" + "github.com/rs/zerolog/log" "github.com/shirou/gopsutil/host" "github.com/threefoldtech/zos/pkg/capacity/dmi" "github.com/threefoldtech/zos/pkg/capacity/smartctl" @@ -78,6 +79,11 @@ type Disks struct { // Disks list and parse the hardware information using smartctl func (r *ResourceOracle) Disks() (d Disks, err error) { devices, err := smartctl.ListDevices() + if errors.Is(err, smartctl.ErrEmpty) { + // TODO: for now we allow to not have the smartctl dump of all the disks + log.Warn().Err(err).Msg("smartctl did not found any disk on the system") + return d, nil + } if err != nil { return } diff --git a/pkg/capacity/smartctl/smartctl.go b/pkg/capacity/smartctl/smartctl.go index f6010c102..1097827d7 100644 --- a/pkg/capacity/smartctl/smartctl.go +++ b/pkg/capacity/smartctl/smartctl.go @@ -1,6 +1,7 @@ package smartctl import ( + "errors" "fmt" "os/exec" "regexp" @@ -11,6 +12,9 @@ var reScan = regexp.MustCompile(`(?m)^([^\s]+)\s+-d\s+([^\s]+)\s+#`) var reHeader = regexp.MustCompile(`(?m)([^\[]+)\[([^\[]+)\]`) var reInfo = regexp.MustCompile(`(?m)([^:]+):\s+(.+)`) +// ErrEmpty is return when smatctl doesn't find any device +var ErrEmpty = errors.New("smartctl returned an empty response") + // Device represents a device as returned by "smartctl --scan" type Device struct { Type string @@ -25,6 +29,10 @@ func ListDevices() ([]Device, error) { return nil, err } + if len(output) == 0 { + return nil, ErrEmpty + } + return parseScan(output) }