Skip to content

Commit

Permalink
lxd/storage/drivers/zfs: Check for non /dev/zvol/* paths
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <din.music@canonical.com>
  • Loading branch information
MusicDin committed Jun 25, 2024
1 parent 9728e82 commit b394f90
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lxd/storage/drivers/driver_zfs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,13 @@ func (d *zfs) CreateVolume(vol Volume, filler *VolumeFiller, op *operations.Oper
ctx, cancel := context.WithTimeout(d.state.ShutdownCtx, 30*time.Second)
defer cancel()

if !tryExists(ctx, filepath.Join("/dev/zvol", d.dataset(vol, false))) {
return fmt.Errorf("Failed to activate created volume %q", vol.name)
}

zfsFilesystem := vol.ConfigBlockFilesystem()

devPath, err := d.GetVolumeDiskPath(vol)
devPath, err := d.tryGetVolumeDiskPathFromDataset(ctx, d.dataset(vol, false))
if err != nil {
return err
}

zfsFilesystem := vol.ConfigBlockFilesystem()

_, err = makeFSType(devPath, zfsFilesystem, nil)
if err != nil {
return err
Expand Down Expand Up @@ -1912,6 +1908,23 @@ func (d *zfs) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op
return nil
}

// tryGetVolumeDiskPathFromDataset attempts to find the disk path for the given ZFS dataset,
// retrying until the context is canceled or expires.
func (d *zfs) tryGetVolumeDiskPathFromDataset(ctx context.Context, dataset string) (string, error) {
for {
if ctx.Err() != nil {
return "", fmt.Errorf("Failed to locate zvol for %q: %w", dataset, ctx.Err())
}

diskPath, err := d.getVolumeDiskPathFromDataset(dataset)
if err == nil {
return diskPath, nil
}

time.Sleep(500 * time.Millisecond)
}
}

func (d *zfs) getVolumeDiskPathFromDataset(dataset string) (string, error) {
// Shortcut for udev.
if shared.PathExists(filepath.Join("/dev/zvol", dataset)) {
Expand Down Expand Up @@ -2116,8 +2129,9 @@ func (d *zfs) activateVolume(vol Volume) (bool, error) {
ctx, cancel := context.WithTimeout(d.state.ShutdownCtx, 30*time.Second)
defer cancel()

if !tryExists(ctx, filepath.Join("/dev/zvol", dataset)) {
return false, fmt.Errorf("Failed to activate volume %q", vol.name)
_, err := d.tryGetVolumeDiskPathFromDataset(ctx, dataset)
if err != nil {
return false, fmt.Errorf("Failed to activate volume: %v", err)
}

d.logger.Debug("Activated ZFS volume", logger.Ctx{"volName": vol.Name(), "dev": dataset})
Expand Down

0 comments on commit b394f90

Please sign in to comment.