diff --git a/plugin.go b/plugin.go index 5af6519..febf61a 100644 --- a/plugin.go +++ b/plugin.go @@ -226,15 +226,17 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) { // // Waiting for device appearance - dev := fmt.Sprintf("/dev/disk/by-id/virtio-%.20s", vol.ID) - logger.WithField("dev", dev).Debug("Waiting for device to appear...") - err = waitForDevice(dev) + logger.Debug("Waiting for device to appear...") + + dev, err := findDeviceWithTimeout(vol.ID) if err != nil { - logger.WithError(err).Error("Expected block device not found") - return nil, fmt.Errorf("Block device not found: %s", dev) + logger.WithError(err).Error("Block device not found") + return nil, err } + logger.WithField("dev", dev).Debug("Found device") + // // Check filesystem and format if necessary diff --git a/util.go b/util.go index d40bf54..f5be405 100644 --- a/util.go +++ b/util.go @@ -32,29 +32,27 @@ func formatFilesystem(dev string, label string) error { return nil } -func waitForDevice(dev string) error { - _, err := os.Stat(dev) - if err != nil { - if !os.IsNotExist(err) { - return err - } - } else { - return nil +func findDeviceWithTimeout(volId string) (string, error) { + devicePaths := []string{ + "/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_%s", + "/dev/disk/by-id/virtio-%.20s", } for i := 1; i <= 10; i++ { time.Sleep(500 * time.Millisecond) - - if _, err = os.Stat(dev); err != nil { - if !os.IsNotExist(err) { - return err + for _, devicePath := range devicePaths { + dev := fmt.Sprintf(devicePath, volId) + if _, err := os.Stat(dev); err != nil { + if !os.IsNotExist(err) { + return "", err + } + } else { + return dev, nil } - } else { - return nil } } - return fmt.Errorf("Timeout waiting for file: %s", dev) + return "", fmt.Errorf("Block device not found") } func isDirectoryPresent(path string) (bool, error) {