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 11, 2023
1 parent d147fcb commit 7391494
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 24 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/lvmcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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
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 @@ -27,7 +27,7 @@ var (
losetupPath = "/usr/sbin/losetup"
)

// 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
55 changes: 40 additions & 15 deletions pkg/vgmanager/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/openshift/lvm-operator/pkg/wipefs"
"path/filepath"

lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1"
Expand Down Expand Up @@ -74,12 +75,38 @@ func (r *VGReconciler) addDevicesToVG(ctx context.Context, vgs []lvm.VolumeGroup

// getAvailableDevicesForVG determines the available devices that can be used to create a volume group.
func (r *VGReconciler) getAvailableDevicesForVG(ctx context.Context, blockDevices []lsblk.BlockDevice, vgs []lvm.VolumeGroup, volumeGroup *lvmv1alpha1.LVMVolumeGroup) ([]lsblk.BlockDevice, error) {
logger := log.FromContext(ctx)

// filter devices based on DeviceSelector.Paths if specified
availableDevices, err := r.filterMatchingDevices(ctx, blockDevices, vgs, volumeGroup)
if err != nil {
return nil, fmt.Errorf("failed to filter matching devices for volume group %s: %w", volumeGroup.GetName(), err)
}

for _, device := range availableDevices {
wipe := false
for _, path := range volumeGroup.Spec.DeviceSelector.Paths {
if device.KName == path && volumeGroup.Spec.DeviceSelector.ForceWipeDevicesAndDestroyAllData {
wipe = true
}
}

if wipe {
if err := wipefs.Wipe(r.executor, device.KName); err != nil {
logger.Error(err, "failed to wipe the device", "deviceName", device.KName)
} else {
logger.Info("device wiped successfully", "deviceName", device.KName)
}
for _, child := range device.Children {
if err := wipefs.RemoveMapperReference(r.executor, child.KName); err != nil {
logger.Error(err, "failed to remove device-mapper reference", "deviceName", device.KName, "childName", child.KName)
} else {
logger.Info("device-mapper reference removed successfully", "deviceName", device.KName, "childName", child.KName)
}
}
}
}

return r.filterAvailableDevices(ctx, availableDevices), nil
}

Expand Down Expand Up @@ -129,23 +156,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
3 changes: 1 addition & 2 deletions pkg/vgmanager/vgmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 @@ -182,7 +181,7 @@ func (r *VGReconciler) reconcile(ctx context.Context, volumeGroup *lvmv1alpha1.L
return ctrl.Result{}, fmt.Errorf("failed to get 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
44 changes: 44 additions & 0 deletions pkg/wipefs/wipefs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package wipefs

import (
"fmt"

"github.com/openshift/lvm-operator/pkg/internal/exec"
)

const (
wipefsCmd = "/usr/sbin/wipefs"
dmsetupCmd = "/usr/sbin/dmsetup"
)

// Wipe wipes the device only if force delete flag is set
func Wipe(exec exec.Executor, deviceName string) error {
if len(deviceName) == 0 {
return fmt.Errorf("failed to wipe the device. Device name is empty")
}

args := []string{"--all", "--force"}
args = append(args, deviceName)
_, err := exec.ExecuteCommandWithOutputAsHost(wipefsCmd, args...)
if err != nil {
return fmt.Errorf("failed to wipe the device %q. %v", deviceName, err)
}

return nil
}

// RemoveMapperReference removes the device's reference from the device-mapper
func RemoveMapperReference(exec exec.Executor, 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)
_, err := exec.ExecuteCommandWithOutputAsHost(dmsetupCmd, args...)
if err != nil {
return fmt.Errorf("failed to remove the reference from device-mapper %q. %v", deviceName, err)
}

return nil
}

0 comments on commit 7391494

Please sign in to comment.