Skip to content

Commit

Permalink
Merge pull request #49 from SLH335/block-device-paths
Browse files Browse the repository at this point in the history
fix: add alternative block device path
  • Loading branch information
jgraichen authored Dec 12, 2024
2 parents 93cab39 + 378af3d commit 168ffcd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
12 changes: 7 additions & 5 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 13 additions & 15 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 168ffcd

Please sign in to comment.