Skip to content

Commit

Permalink
handle error when smartcl returns an empty response
Browse files Browse the repository at this point in the history
fixes #608
  • Loading branch information
zaibon committed Apr 3, 2020
1 parent f3c9c05 commit 22ede1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/capacity/capacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/capacity/smartctl/smartctl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package smartctl

import (
"errors"
"fmt"
"os/exec"
"regexp"
Expand All @@ -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
Expand All @@ -25,6 +29,10 @@ func ListDevices() ([]Device, error) {
return nil, err
}

if len(output) == 0 {
return nil, ErrEmpty
}

return parseScan(output)
}

Expand Down

0 comments on commit 22ede1c

Please sign in to comment.