Skip to content

Commit

Permalink
Add basic support for blkdiscard (#159)
Browse files Browse the repository at this point in the history
blkdiscard is a pretty basic command, it simply gets run against a block
device, and returns an exit code. We always include the --force flag.

### How has this been tested?

On an Equinix Metal server with several spare disks, I've modified the
`examples/blkdiscard/main.go` to point to one of these disks and
confirmed that:

* The command succeeds in the happy path, and wipes the disk device
which I had previously created a filesystem on
* The command is correctly using the --force flag, and will discard
blocks even on a mounted disk device
* The command fails when it tries to run against a non-existent disk
device
* The command fails when I set the context timeout to too low of a value
(e.g, 1 second)

### Description for changelog/release notes

Add support for blkdiscard command
  • Loading branch information
ScottGarman authored May 30, 2024
2 parents 2883c00 + efd29a0 commit 41022f4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Ironlib identifies the hardware and executes tooling respective to the hardware/

The list of tools that ironlib wraps around, in no particular order,

- blkdiscard
- dell racadm
- dmidecode
- dell dsu
Expand Down Expand Up @@ -93,6 +94,7 @@ IRONLIB_UTIL_ASRR_BIOSCONTROL
IRONLIB_UTIL_RACADM7
IRONLIB_UTIL_DNF
IRONLIB_UTIL_DSU
IRONLIB_UTIL_BLKDISCARD
IRONLIB_UTIL_HDPARM
IRONLIB_UTIL_LSBLK
IRONLIB_UTIL_LSHW
Expand Down
40 changes: 40 additions & 0 deletions examples/blkdiscard/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"context"
"flag"
"time"

"github.com/metal-toolbox/ironlib/utils"
"github.com/sirupsen/logrus"
)

var (
device = flag.String("device", "/dev/sdZZZ", "disk to wipe using blkdiscard")
timeout = flag.String("timeout", (2 * time.Minute).String(), "time to wait for command to complete")
)

// This example invokes ironlib and runs blkdiscard on the disk /dev/sdZZZ
func main() {
flag.Parse()

logger := logrus.New()
logger.Formatter = new(logrus.TextFormatter)
logger.SetLevel(logrus.TraceLevel)

timeout, err := time.ParseDuration(*timeout)
if err != nil {
logger.WithError(err).Fatal("failed to parse timeout duration")
}

blkdiscard := utils.NewBlkdiscardCmd()

ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

logger.Info("running blkdiscard on ", *device)
err = blkdiscard.Discard(ctx, *device)
if err != nil {
logger.WithError(err).Fatal("exiting")
}
}
45 changes: 45 additions & 0 deletions utils/blkdiscard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package utils

import (
"cmp"
"context"
"os"
)

const (
EnvBlkdiscardUtility = "IRONLIB_UTIL_BLKDISCARD"
)

type Blkdiscard struct {
Executor Executor
}

// Return a new blkdiscard executor
func NewBlkdiscardCmd() *Blkdiscard {
// lookup env var for util
utility := cmp.Or(os.Getenv(EnvBlkdiscardUtility), "blkdiscard")

e := NewExecutor(utility)
e.SetEnv([]string{"LC_ALL=C.UTF-8"})

return &Blkdiscard{Executor: e}
}

// Discard runs blkdiscard on the given device (--force is always used)
func (b *Blkdiscard) Discard(ctx context.Context, device string) error {
b.Executor.SetArgs("--force", device)

_, err := b.Executor.Exec(ctx)
if err != nil {
return err
}

return nil
}

// NewFakeBlkdiscard returns a mock implementation of the Blkdiscard interface for use in tests.
func NewFakeBlkdiscard() *Blkdiscard {
return &Blkdiscard{
Executor: NewFakeExecutor("blkdiscard"),
}
}
13 changes: 13 additions & 0 deletions utils/blkdiscard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_blkdiscard(t *testing.T) {
err := NewFakeBlkdiscard().Discard(context.TODO(), "/dev/sdZZZ")
assert.NoError(t, err)
}

0 comments on commit 41022f4

Please sign in to comment.