Skip to content

Commit

Permalink
feat: wipe devices before using if enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Suleyman Akbas <sakbas@redhat.com>
  • Loading branch information
suleymanakbas91 committed Sep 26, 2023
1 parent b1f83fe commit 84eefba
Show file tree
Hide file tree
Showing 15 changed files with 542 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ packages:
github.com/openshift/lvm-operator/pkg/lvm:
interfaces:
LVM:
github.com/openshift/lvm-operator/pkg/wipefs:
interfaces:
Wipefs:
github.com/openshift/lvm-operator/pkg/dmsetup:
interfaces:
Dmsetup:
6 changes: 6 additions & 0 deletions api/v1alpha1/lvmcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ type DeviceSelector struct {
// We discourage using the device names as they can change over node restarts.
// +optional
OptionalPaths []string `json:"optionalPaths,omitempty"`

// ForceWipeDevicesAndDestroyAllData runs wipefs to wipe the devices.
// This can lead to data lose. Please use this only when you know that the disk
// does not contain any important data.
// +optional
ForceWipeDevicesAndDestroyAllData bool `json:"forceWipeDisksAndDestroyAllData,omitempty"`
}

// type DeviceClassConfig struct {
Expand Down
6 changes: 6 additions & 0 deletions bundle/manifests/lvm.topolvm.io_lvmclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ spec:
description: DeviceSelector is a set of rules that should
match for a device to be included in the LVMCluster
properties:
forceWipeDisksAndDestroyAllData:
description: ForceWipeDevicesAndDestroyAllData runs
wipefs to wipe the devices. This can lead to data
lose. Please use this only when you know that the
disk does not contain any important data.
type: boolean
optionalPaths:
description: A list of device paths which could be chosen
for creating Volume Group. For example "/dev/disk/by-path/pci-0000:04:00.0-nvme-1"
Expand Down
6 changes: 6 additions & 0 deletions bundle/manifests/lvm.topolvm.io_lvmvolumegroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ spec:
description: DeviceSelector is a set of rules that should match for
a device to be included in this TopoLVMCluster
properties:
forceWipeDisksAndDestroyAllData:
description: ForceWipeDevicesAndDestroyAllData runs wipefs to
wipe the devices. This can lead to data lose. Please use this
only when you know that the disk does not contain any important
data.
type: boolean
optionalPaths:
description: A list of device paths which could be chosen for
creating Volume Group. For example "/dev/disk/by-path/pci-0000:04:00.0-nvme-1"
Expand Down
4 changes: 4 additions & 0 deletions cmd/vgmanager/vgmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
"os"

"github.com/go-logr/logr"
"github.com/openshift/lvm-operator/pkg/dmsetup"
"github.com/openshift/lvm-operator/pkg/filter"
"github.com/openshift/lvm-operator/pkg/lsblk"
"github.com/openshift/lvm-operator/pkg/lvm"
"github.com/openshift/lvm-operator/pkg/lvmd"
"github.com/openshift/lvm-operator/pkg/vgmanager"
"github.com/openshift/lvm-operator/pkg/wipefs"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/cache"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
Expand Down Expand Up @@ -106,6 +108,8 @@ func run(_ *cobra.Command, _ []string, opts *Options) error {
LVMD: lvmd.DefaultConfigurator(),
Scheme: mgr.GetScheme(),
LSBLK: lsblk.NewDefaultHostLSBLK(),
Wipefs: wipefs.NewDefaultHostWipefs(),
Dmsetup: dmsetup.NewDefaultHostDmsetup(),
LVM: lvm.NewDefaultHostLVM(),
NodeName: os.Getenv("NODE_NAME"),
Namespace: os.Getenv("POD_NAMESPACE"),
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/lvm.topolvm.io_lvmclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ spec:
description: DeviceSelector is a set of rules that should
match for a device to be included in the LVMCluster
properties:
forceWipeDisksAndDestroyAllData:
description: ForceWipeDevicesAndDestroyAllData runs
wipefs to wipe the devices. This can lead to data
lose. Please use this only when you know that the
disk does not contain any important data.
type: boolean
optionalPaths:
description: A list of device paths which could be chosen
for creating Volume Group. For example "/dev/disk/by-path/pci-0000:04:00.0-nvme-1"
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/lvm.topolvm.io_lvmvolumegroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ spec:
description: DeviceSelector is a set of rules that should match for
a device to be included in this TopoLVMCluster
properties:
forceWipeDisksAndDestroyAllData:
description: ForceWipeDevicesAndDestroyAllData runs wipefs to
wipe the devices. This can lead to data lose. Please use this
only when you know that the disk does not contain any important
data.
type: boolean
optionalPaths:
description: A list of device paths which could be chosen for
creating Volume Group. For example "/dev/disk/by-path/pci-0000:04:00.0-nvme-1"
Expand Down
52 changes: 52 additions & 0 deletions pkg/dmsetup/dmsetup.go
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
}
74 changes: 74 additions & 0 deletions pkg/dmsetup/mocks/mock_dmsetup.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
nsenterPath = "/usr/bin/nsenter"
)

// Executor is the interface for running exec commands
// Executor is the interface for running exec commands
type Executor interface {
ExecuteCommandWithOutput(command string, arg ...string) (string, error)
ExecuteCommandWithOutputAsHost(command string, arg ...string) (string, error)
Expand Down
16 changes: 16 additions & 0 deletions pkg/vgmanager/vgmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (
"github.com/google/go-cmp/cmp"
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1"
"github.com/openshift/lvm-operator/controllers"
"github.com/openshift/lvm-operator/pkg/dmsetup"
"github.com/openshift/lvm-operator/pkg/filter"
"github.com/openshift/lvm-operator/pkg/lsblk"
"github.com/openshift/lvm-operator/pkg/lvm"
"github.com/openshift/lvm-operator/pkg/lvmd"
"github.com/openshift/lvm-operator/pkg/wipefs"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -87,6 +90,8 @@ type VGReconciler struct {
LVMD lvmd.Configurator
lvm.LVM
lsblk.LSBLK
wipefs.Wipefs
dmsetup.Dmsetup
NodeName string
Namespace string
Filters func(*lvmv1alpha1.LVMVolumeGroup, lvm.LVM, lsblk.LSBLK) filter.Filters
Expand Down Expand Up @@ -184,6 +189,17 @@ func (r *VGReconciler) reconcile(
return ctrl.Result{}, fmt.Errorf("failed to list block devices: %w", err)
}

wiped, err := r.wipeDevicesIfNecessary(ctx, volumeGroup, vgs, blockDevices)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to wipe devices: %w", err)
}
if wiped {
blockDevices, err = r.LSBLK.ListBlockDevices()
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to list block devices: %w", err)
}
}

// Get the available block devices that can be used for this volume group
// valid means that it can be used to create or extend the volume group
// devices that are already part of the volume group will not be returned
Expand Down
85 changes: 85 additions & 0 deletions pkg/vgmanager/wipe_devices.go
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)
}
}
Loading

0 comments on commit 84eefba

Please sign in to comment.