-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. Signed-off-by: Scott Garman <[email protected]>
- Loading branch information
1 parent
2883c00
commit efd29a0
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |