-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Suleyman Akbas <[email protected]>
- Loading branch information
1 parent
f9da4df
commit 5561eae
Showing
17 changed files
with
494 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package vgmanager | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/openshift/lvm-operator/pkg/wipefs" | ||
|
||
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" | ||
"github.com/openshift/lvm-operator/pkg/lsblk" | ||
"github.com/openshift/lvm-operator/pkg/lvm" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
func (r *VGReconciler) wipeDevicesIfNecessary(ctx context.Context, volumeGroup *lvmv1alpha1.LVMVolumeGroup, vgs []lvm.VolumeGroup, blockDevices []lsblk.BlockDevice) (bool, error) { | ||
if volumeGroup.Spec.DeviceSelector == nil || !volumeGroup.Spec.DeviceSelector.ForceWipeDevicesAndDestroyAllData { | ||
return false, nil | ||
} | ||
|
||
allPaths := append(volumeGroup.Spec.DeviceSelector.Paths, volumeGroup.Spec.DeviceSelector.OptionalPaths...) | ||
wiped := false | ||
for _, path := range allPaths { | ||
if isDeviceAlreadyPartOfVG(vgs, path, volumeGroup) { | ||
continue | ||
} | ||
deviceWiped, err := r.wipeDevice(ctx, path, blockDevices) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to wipe device %s: %w", path, err) | ||
} | ||
if deviceWiped { | ||
wiped = true | ||
} | ||
} | ||
|
||
return wiped, nil | ||
} | ||
|
||
func (r *VGReconciler) wipeDevice(ctx context.Context, deviceName string, blockDevices []lsblk.BlockDevice) (bool, error) { | ||
logger := log.FromContext(ctx).WithValues("deviceName", deviceName) | ||
|
||
wiped := false | ||
for _, device := range blockDevices { | ||
if device.KName == deviceName { | ||
if err := r.Wipefs.Wipe(device.KName); err != nil { | ||
return false, err | ||
} | ||
wiped = true | ||
logger.Info("device wiped successfully") | ||
for _, child := range device.Children { | ||
// If the device was used as a Physical Volume before, wipefs does not remove the child LVs. | ||
// So, a device-mapper reference removal is necessary to further remove the child LV references. | ||
r.removeMapperReference(ctx, child) | ||
} | ||
} else if device.HasChildren() { | ||
childWiped, err := r.wipeDevice(ctx, deviceName, device.Children) | ||
if err != nil { | ||
return false, err | ||
} | ||
if childWiped { | ||
wiped = true | ||
} | ||
} | ||
} | ||
return wiped, nil | ||
} | ||
|
||
// removeMapperReference remove the device-mapper reference of the device starting from the most inner child | ||
func (r *VGReconciler) removeMapperReference(ctx context.Context, device lsblk.BlockDevice) { | ||
logger := log.FromContext(ctx).WithValues("deviceName", device.KName) | ||
if device.HasChildren() { | ||
for _, child := range device.Children { | ||
r.removeMapperReference(ctx, child) | ||
} | ||
} | ||
if err := r.Wipefs.RemoveMapperReference(device.KName); err != nil { | ||
if errors.Is(err, wipefs.ErrReferenceNotFound) { | ||
logger.Info("skipping the removal of device-mapper reference as the reference does not exist", "childName", device.KName) | ||
} else { | ||
logger.Info("failed to remove device-mapper reference", "childName", device.KName, "error", err) | ||
} | ||
} else { | ||
logger.Info("device-mapper reference removed successfully", "childName", device.KName) | ||
} | ||
} |
Oops, something went wrong.