Skip to content

Commit 8f2b4d0

Browse files
committed
improve error reporting on command execution
1 parent efce096 commit 8f2b4d0

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

iscsi/iscsi.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package iscsi
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"io/ioutil"
@@ -482,10 +483,10 @@ func GetISCSIDevices(devicePaths []string) (devices []Device, err error) {
482483

483484
// lsblk execute the lsblk commands
484485
func lsblk(flags string, devicePaths []string) ([]byte, error) {
485-
out, err := exec.Command("lsblk", append([]string{flags}, devicePaths...)...).Output()
486+
out, err := exec.Command("lsblk", append([]string{flags}, devicePaths...)...).CombinedOutput()
486487
debug.Printf("lsblk %s %s", flags, strings.Join(devicePaths, " "))
487488
if err != nil {
488-
return nil, fmt.Errorf("lsblk: %v", err)
489+
return nil, fmt.Errorf("lsblk: %s", string(out))
489490
}
490491

491492
return out, nil
@@ -518,10 +519,10 @@ func RemoveSCSIDevices(devices ...Device) error {
518519
var errs []error
519520
for _, device := range devices {
520521
debug.Printf("Flush SCSI device %v.\n", device.Name)
521-
err := exec.Command("blockdev", "--flushbufs", device.GetPath()).Run()
522+
out, err := exec.Command("blockdev", "--flushbufs", device.GetPath()).CombinedOutput()
522523
if err != nil {
523524
debug.Printf("Command 'blockdev --flushbufs %v' did not succeed to flush the device: %v\n", device.Name, err)
524-
return err
525+
return errors.New(string(out))
525526
}
526527

527528
debug.Printf("Put SCSI device %v offline.\n", device.Name)

iscsi/multipath.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package iscsi
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"os"
78
"os/exec"
9+
"strings"
810
"time"
911
)
1012

@@ -50,7 +52,10 @@ func ExecWithTimeout(command string, args []string, timeout time.Duration) ([]by
5052
}
5153

5254
if err != nil {
53-
debug.Printf("Non-zero exit code: %s\n", err)
55+
if ee, ok := err.(*exec.ExitError); ok {
56+
debug.Printf("Non-zero exit code: %s\n", err)
57+
err = fmt.Errorf("%s", ee.Stderr)
58+
}
5459
}
5560

5661
debug.Println("Finished executing command.")
@@ -61,10 +66,10 @@ func getMultipathMap(device string) (*multipathDeviceMap, error) {
6166
debug.Printf("Getting multipath map for device %s.\n", device)
6267

6368
cmd := exec.Command("multipathd", "show", "map", device[1:], "json")
64-
out, err := cmd.Output()
69+
out, err := cmd.CombinedOutput()
6570
// debug.Printf(string(out))
6671
if err != nil {
67-
debug.Printf("An error occured while looking for multipath device map: %v\n", err)
72+
debug.Printf("An error occured while looking for multipath device map: %s\n", out)
6873
return nil, err
6974
}
7075

@@ -100,6 +105,9 @@ func FlushMultipathDevice(device *Device) error {
100105
if _, e := os.Stat(devicePath); os.IsNotExist(e) {
101106
debug.Printf("Multipath device %v has been removed.\n", devicePath)
102107
} else {
108+
if strings.Contains(err.Error(), "map in use") {
109+
err = fmt.Errorf("device is probably still in use somewhere else: %v", err)
110+
}
103111
debug.Printf("Command 'multipath -f %v' did not succeed to delete the device: %v\n", devicePath, err)
104112
return err
105113
}

0 commit comments

Comments
 (0)