Skip to content

Commit

Permalink
storage: Conserve LV state (from Incus) (#13986)
Browse files Browse the repository at this point in the history
Reactivates an already-active volume after its size has been increased.
If a volume was active before being grown, it would be deactivated by
`SetVolumeQuota`; this retains the state of the LV after a size
adjustment.

Contains cherry-picks from lxc/incus#1021 and
lxc/incus#1136

incus#1021 is only partially applied as its other commit depends on
lxc/incus#512; I've made a note of this in the
tracking sheet.
  • Loading branch information
tomponline authored Aug 27, 2024
2 parents 431d38c + bd22bc0 commit f0eb9a8
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions lxd/storage/drivers/driver_lvm_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,16 +449,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 @@ -474,13 +464,32 @@ 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.
activated, err := d.activateVolume(vol)
if err != nil {
return err
}

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

// 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 for resizing.
_, err = d.deactivateVolume(vol)
if err != nil {
return err
}
Expand All @@ -499,6 +508,18 @@ func (d *lvm) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op
return err
}

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

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

// Grow the filesystem to fill block device.
err = growFileSystem(fsType, volDevPath, vol)
if err != nil {
Expand Down Expand Up @@ -528,6 +549,18 @@ func (d *lvm) SetVolumeQuota(vol Volume, size string, allowUnsafeResize bool, op
// 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 {
// Activate the volume for resizing.
activated, err := d.activateVolume(vol)
if err != nil {
return err
}

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

err = d.moveGPTAltHeader(volDevPath)
if err != nil {
return err
Expand Down

0 comments on commit f0eb9a8

Please sign in to comment.