-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial disk wipe support to vogelkop (#84)
This PR is mainly to add initial disk wipe support to vogelkop. Only nvme based devices are supported/attempted at the moment, more will follow. I've also tweaked the partition & format commands so they are noun-verb and with subcommands to match what raid is doing. It seems silly to have a `partition` subcommand right now but I think it makes sense. Someone might want to modify or grow partitions, maybe even wipe them. I don't see myself having the need but if anyone else does the setup is ready for them. The old non-subcommand verb-noun are still around, albeit hidden, so no existing scripts should break (for now).
- Loading branch information
Showing
7 changed files
with
189 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var diskCommand = &cobra.Command{ | ||
Use: "disk", | ||
Short: "Modifies disks", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(diskCommand) | ||
} |
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,94 @@ | ||
package cmd | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"errors" | ||
"os" | ||
|
||
"github.com/bmc-toolbox/common" | ||
"github.com/metal-toolbox/ironlib" | ||
"github.com/metal-toolbox/ironlib/actions" | ||
"github.com/metal-toolbox/ironlib/utils" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
cmd := &cobra.Command{ | ||
Use: "wipe /dev/disk", | ||
Short: "Wipes all data from a disk", | ||
Args: func(_ *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("requires at least one arg") // nolint:goerr113 | ||
} | ||
|
||
_, err := os.Open(args[0]) | ||
return err | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
timeout, err := cmd.Flags().GetDuration("timeout") | ||
if err != nil { | ||
logger.With("error", err).Fatal("--timeout argument is invalid") | ||
} | ||
|
||
verbose, err := cmd.Flags().GetBool("verbose") | ||
if err != nil { | ||
logger.With("error", err).Fatal("--verbose argument is invalid") | ||
} | ||
|
||
driveName := args[0] | ||
|
||
logger := logrus.New() | ||
logger.Formatter = new(logrus.TextFormatter) | ||
if verbose { | ||
logger.SetLevel(logrus.TraceLevel) | ||
} | ||
l := logger.WithField("drive", driveName) | ||
|
||
ctx := cmp.Or(cmd.Context(), context.Background()) | ||
ctx, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
collector, err := ironlib.New(logger) | ||
if err != nil { | ||
l.WithError(err).Fatal("exiting") | ||
} | ||
|
||
inventory, err := collector.GetInventory(ctx, actions.WithDynamicCollection()) | ||
if err != nil { | ||
l.WithError(err).Fatal("exiting") | ||
} | ||
|
||
var drive *common.Drive | ||
for _, d := range inventory.Drives { | ||
if d.LogicalName == driveName { | ||
drive = d | ||
break | ||
} | ||
} | ||
if drive == nil { | ||
l.Fatal("unable to find disk") | ||
} | ||
|
||
// Pick the most appropriate wipe based on the disk type and/or features supported | ||
var wiper actions.DriveWiper | ||
// nolint:gocritic // will have more cases soon, remove nolint then | ||
switch drive.Protocol { | ||
case "nvme": | ||
wiper = utils.NewNvmeCmd(verbose) | ||
} | ||
|
||
if wiper == nil { | ||
l.Fatal("failed find appropriate wiper drive") | ||
} | ||
|
||
err = wiper.WipeDrive(ctx, logger, drive) | ||
if err != nil { | ||
l.Fatal("failed to wipe drive") | ||
} | ||
}, | ||
} | ||
|
||
diskCommand.AddCommand(cmd) | ||
} |
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,14 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var partitionCommand = &cobra.Command{ | ||
Use: "partition", | ||
Short: "Modifies partitions", | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(partitionCommand) | ||
} |
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
Oops, something went wrong.