Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LVM locking issues #1021

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading