Skip to content

Commit 898cec5

Browse files
committed
check if device exists before flushing it
1 parent 7b2d8ee commit 898cec5

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

iscsi/iscsi.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -548,22 +548,26 @@ func RemoveSCSIDevices(devices ...Device) error {
548548
var errs []error
549549
for _, device := range devices {
550550
debug.Printf("Flush SCSI device %v.\n", device.Name)
551-
out, err := exec.Command("blockdev", "--flushbufs", device.GetPath()).CombinedOutput()
552-
if err != nil {
553-
debug.Printf("Command 'blockdev --flushbufs %v' did not succeed to flush the device: %v\n", device.Name, err)
554-
return errors.New(string(out))
551+
if err := device.Exists(); err == nil {
552+
out, err := exec.Command("blockdev", "--flushbufs", device.GetPath()).CombinedOutput()
553+
if err != nil {
554+
debug.Printf("Command 'blockdev --flushbufs %s' did not succeed to flush the device: %v\n", device.GetPath(), err)
555+
return errors.New(string(out))
556+
}
557+
} else if !os.IsNotExist(err) {
558+
return err
555559
}
556560

557-
debug.Printf("Put SCSI device %v offline.\n", device.Name)
558-
err = device.Shutdown()
561+
debug.Printf("Put SCSI device %q offline.\n", device.Name)
562+
err := device.Shutdown()
559563
if err != nil {
560564
if !os.IsNotExist(err) { // Ignore device already removed
561565
errs = append(errs, err)
562566
}
563567
continue
564568
}
565569

566-
debug.Printf("Delete SCSI device %v.\n", device.Name)
570+
debug.Printf("Delete SCSI device %q.\n", device.Name)
567571
err = device.Delete()
568572
if err != nil {
569573
if !os.IsNotExist(err) { // Ignore device already removed
@@ -611,6 +615,12 @@ func GetConnectorFromFile(filePath string) (*Connector, error) {
611615
return &data, nil
612616
}
613617

618+
// Exists check if the device exists at its path and returns an error otherwise
619+
func (d *Device) Exists() error {
620+
_, err := os.Stat(d.GetPath())
621+
return err
622+
}
623+
614624
// GetPath returns the path of a device
615625
func (d *Device) GetPath() string {
616626
if d.Type == "mpath" {

0 commit comments

Comments
 (0)