Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for blkdiscard #159

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
ScottGarman marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Loading