Skip to content

Commit

Permalink
Merge pull request #1021 from stgraber/main
Browse files Browse the repository at this point in the history
Fix LVM locking issues
  • Loading branch information
hallyn authored Jul 19, 2024
2 parents 0739fff + 6496641 commit 9b49e78
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
17 changes: 17 additions & 0 deletions internal/server/storage/drivers/driver_lvm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,23 @@ func (d *lvm) createLogicalVolumeSnapshot(vgName string, srcVol Volume, snapVol
revert := revert.New()
defer revert.Fail()

// If clustered, we need to acquire exclusive write access for this operation.
if d.clustered && !makeThinLv {
parent, _, _ := api.GetParentAndSnapshotName(srcVol.Name())
volDevPath := d.lvmDevPath(d.config["lvm.vg_name"], srcVol.volType, srcVol.contentType, parent)

if util.PathExists(volDevPath) {
_, err := subprocess.RunCommand("lvchange", "--activate", "ey", "--ignoreactivationskip", volDevPath)
if err != nil {
return "", fmt.Errorf("Failed to acquire exclusive lock on LVM logical volume %q: %w", volDevPath, err)
}

defer func() {
_, _ = subprocess.RunCommand("lvchange", "--activate", "sy", "--ignoreactivationskip", volDevPath)
}()
}
}

_, err = subprocess.TryRunCommand("lvcreate", args...)
if err != nil {
return "", err
Expand Down
43 changes: 33 additions & 10 deletions internal/server/storage/drivers/driver_lvm_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,6 @@ func (d *lvm) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op

l := d.logger.AddContext(logger.Ctx{"dev": volDevPath, "size": fmt.Sprintf("%db", sizeBytes)})

// Activate volume if needed.
activated, err := d.activateVolume(vol)
if err != nil {
return err
}

if activated {
defer func() { _, _ = d.deactivateVolume(vol) }()
}

inUse := vol.MountInUse()

// Resize filesystem if needed.
Expand All @@ -467,13 +457,26 @@ func (d *lvm) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op
return ErrInUse // We don't allow online shrinking of filesytem volumes.
}

// Activate volume if needed.
_, err := d.activateVolume(vol)
if err != nil {
return err
}

// Shrink filesystem first.
// Pass allowUnsafeResize to allow disabling of filesystem resize safety checks.
// We do this as a separate step rather than passing -r to lvresize in resizeLogicalVolume
// so that we can have more control over when we trigger unsafe filesystem resize mode,
// otherwise by passing -f to lvresize (required for other reasons) this would then pass
// -f onto resize2fs as well.
err = shrinkFileSystem(fsType, volDevPath, vol, sizeBytes, allowUnsafeResize)
if err != nil {
_, _ = d.deactivateVolume(vol)
return err
}

// Deactivate the volume if needed.
_, err = d.deactivateVolume(vol)
if err != nil {
return err
}
Expand All @@ -492,6 +495,16 @@ func (d *lvm) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op
return err
}

// Activate the volume if needed.
_, err := d.activateVolume(vol)
if err != nil {
return err
}

defer func() {
_, _ = d.deactivateVolume(vol)
}()

// Grow the filesystem to fill block device.
err = growFileSystem(fsType, volDevPath, vol)
if err != nil {
Expand All @@ -518,6 +531,16 @@ func (d *lvm) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op
return err
}

// Activate the volume if needed.
_, err := d.activateVolume(vol)
if err != nil {
return err
}

defer func() {
_, _ = d.deactivateVolume(vol)
}()

// Move the VM GPT alt header to end of disk if needed (not needed in unsafe resize mode as it is
// expected the caller will do all necessary post resize actions themselves).
if vol.IsVMBlock() && !allowUnsafeResize {
Expand Down

0 comments on commit 9b49e78

Please sign in to comment.