Skip to content

Commit

Permalink
wipe out devices
Browse files Browse the repository at this point in the history
Signed-off-by: Suleyman Akbas <[email protected]>
  • Loading branch information
suleymanakbas91 committed Sep 14, 2023
1 parent f9da4df commit 5561eae
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ packages:
github.com/openshift/lvm-operator/pkg/lvm:
interfaces:
LVM:
github.com/openshift/lvm-operator/pkg/wipefs:
interfaces:
Wipefs:
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: 2 additions & 2 deletions bundle/manifests/lvms-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.name
image: quay.io/lvms_dev/lvms-operator:latest
image: quay.io/sakbas/lvms-operator:11.09
livenessProbe:
httpGet:
path: /healthz
Expand Down Expand Up @@ -661,7 +661,7 @@ spec:
memory: 20Mi
- command:
- /metricsexporter
image: quay.io/lvms_dev/lvms-operator:latest
image: quay.io/sakbas/lvms-operator:11.09
name: metricsexporter
resources:
requests:
Expand Down
2 changes: 2 additions & 0 deletions cmd/vgmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"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"
"sigs.k8s.io/controller-runtime/pkg/cache"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -97,6 +98,7 @@ func main() {
LVMD: lvmd.DefaultConfigurator(),
Scheme: mgr.GetScheme(),
LSBLK: lsblk.NewDefaultHostLSBLK(),
Wipefs: wipefs.NewDefaultHostWipefs(),
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
4 changes: 2 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: quay.io/lvms_dev/lvms-operator
newTag: latest
newName: quay.io/sakbas/lvms-operator
newTag: "11.09"
namePrefix: lvms-
4 changes: 2 additions & 2 deletions olm-deploy/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ configurations:
# replace catalogsource image
images:
- name: catalog-img
newName: quay.io/lvms_dev/lvms-operator-catalog
newTag: latest
newName: quay.io/sakbas/lvms-operator-catalog
newTag: "11.09"

patches:
- patch: |-
Expand Down
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
28 changes: 13 additions & 15 deletions pkg/vgmanager/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,21 @@ func (r *VGReconciler) filterMatchingDevices(ctx context.Context, blockDevices [
}

// If Paths is specified, treat it as required paths
if len(volumeGroup.Spec.DeviceSelector.Paths) > 0 {
for _, path := range volumeGroup.Spec.DeviceSelector.Paths {
blockDevice, err := getValidDevice(path, blockDevices, vgs, volumeGroup)
if err != nil {
// An error for required devices is critical
return nil, fmt.Errorf("unable to validate device %s: %v", path, err)
}

// Check if we should skip this device
if blockDevice.DevicePath == "" {
logger.Info(fmt.Sprintf("skipping required device that is already part of volume group %s: %s", volumeGroup.Name, path))
devicesAlreadyInVG = true
continue
}
for _, path := range volumeGroup.Spec.DeviceSelector.Paths {
blockDevice, err := getValidDevice(path, blockDevices, vgs, volumeGroup)
if err != nil {
// An error for required devices is critical
return nil, fmt.Errorf("unable to validate device %s: %v", path, err)
}

filteredBlockDevices = append(filteredBlockDevices, blockDevice)
// Check if we should skip this device
if blockDevice.DevicePath == "" {
logger.Info(fmt.Sprintf("skipping required device that is already part of volume group %s: %s", volumeGroup.Name, path))
devicesAlreadyInVG = true
continue
}

filteredBlockDevices = append(filteredBlockDevices, blockDevice)
}

// Check for any optional paths
Expand Down
18 changes: 15 additions & 3 deletions pkg/vgmanager/vgmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package vgmanager
import (
"context"
"fmt"
"github.com/openshift/lvm-operator/pkg/wipefs"
"strconv"
"time"

Expand All @@ -30,7 +31,6 @@ import (
"github.com/openshift/lvm-operator/pkg/lsblk"
"github.com/openshift/lvm-operator/pkg/lvm"
"github.com/openshift/lvm-operator/pkg/lvmd"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -88,6 +88,7 @@ type VGReconciler struct {
LVMD lvmd.Configurator
lvm.LVM
lsblk.LSBLK
wipefs.Wipefs
NodeName string
Namespace string
Filters func(lvm.LVM, lsblk.LSBLK) filter.Filters
Expand Down Expand Up @@ -174,13 +175,24 @@ func (r *VGReconciler) reconcile(ctx context.Context, volumeGroup *lvmv1alpha1.L
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
availableDevices, err := r.getAvailableDevicesForVG(ctx, blockDevices, vgs, volumeGroup)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get block devices for volumegroup %s: %w", volumeGroup.GetName(), err)
return ctrl.Result{}, fmt.Errorf("failed to get available block devices for volumegroup %s: %w", volumeGroup.GetName(), err)
}

logger.Info("listing available and delayed devices", "availableDevices", availableDevices)
logger.Info("listing available devices", "availableDevices", availableDevices)

// If there are no available devices, that could mean either
// - There is no available devices to attach to the volume group
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"
"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)
}
}
Loading

0 comments on commit 5561eae

Please sign in to comment.