-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wipe devices before using if enabled
Signed-off-by: Suleyman Akbas <sakbas@redhat.com>
- Loading branch information
1 parent
b1f83fe
commit 84eefba
Showing
15 changed files
with
542 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package dmsetup | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/openshift/lvm-operator/pkg/internal/exec" | ||
) | ||
|
||
var ( | ||
DefaultDMSetup = "/usr/sbin/dmsetup" | ||
ErrReferenceNotFound = fmt.Errorf("device-mapper reference not found") | ||
) | ||
|
||
type Dmsetup interface { | ||
Remove(deviceName string) error | ||
} | ||
|
||
type HostDmsetup struct { | ||
exec.Executor | ||
dmsetup string | ||
} | ||
|
||
func NewDefaultHostDmsetup() *HostDmsetup { | ||
return NewHostDmsetup(&exec.CommandExecutor{}, DefaultDMSetup) | ||
} | ||
|
||
func NewHostDmsetup(executor exec.Executor, dmsetup string) *HostDmsetup { | ||
return &HostDmsetup{ | ||
Executor: executor, | ||
dmsetup: dmsetup, | ||
} | ||
} | ||
|
||
// Remove removes the device's reference from the device-mapper | ||
func (dmsetup *HostDmsetup) Remove(deviceName string) error { | ||
if len(deviceName) == 0 { | ||
return fmt.Errorf("failed to remove device-mapper reference. Device name is empty") | ||
} | ||
|
||
args := []string{"remove"} | ||
args = append(args, deviceName) | ||
output, err := dmsetup.ExecuteCommandWithOutputAsHost(dmsetup.dmsetup, args...) | ||
if err != nil { | ||
if strings.Contains(output, "not found") { | ||
return ErrReferenceNotFound | ||
} | ||
return fmt.Errorf("failed to remove the reference from device-mapper %q: %v", deviceName, err) | ||
} | ||
|
||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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" | ||
|
||
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" | ||
"github.com/openshift/lvm-operator/pkg/dmsetup" | ||
"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.Dmsetup.Remove(device.KName); err != nil { | ||
if errors.Is(err, dmsetup.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.