Skip to content

Commit

Permalink
Add initial disk wipe support to vogelkop (#84)
Browse files Browse the repository at this point in the history
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
mmlb authored Jun 12, 2024
2 parents bcb52f4 + 77fbf91 commit 4f13d6f
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 146 deletions.
14 changes: 14 additions & 0 deletions cmd/disk.go
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)
}
23 changes: 15 additions & 8 deletions cmd/partition_disk.go → cmd/disk_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"github.com/spf13/cobra"
)

var partitionDiskCmd = &cobra.Command{
Use: "partition-disk",
Short: "Partitions a block device",
Long: "Partitions a block device with a GPT table",
var diskPartitionCommand = &cobra.Command{
Use: "partition",
Short: "Partitions a disk with a GPT table",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, _ []string) {
ctx := context.Background()
Expand All @@ -37,8 +36,16 @@ var partitionDiskCmd = &cobra.Command{
}

func init() {
partitionDiskCmd.PersistentFlags().String("device", "/dev/sda", "Device to be partitioned")
markFlagAsRequired(partitionDiskCmd, "device")
partitionDiskCmd.PersistentFlags().StringSlice("partitions", []string{}, "Partition Definitions Name:Position:Size:Type")
rootCmd.AddCommand(partitionDiskCmd)
diskPartitionCommand.PersistentFlags().String("device", "/dev/sda", "Device to be partitioned")
markFlagAsRequired(diskPartitionCommand, "device")

diskPartitionCommand.PersistentFlags().StringSlice("partitions", []string{}, "Partition Definitions Name:Position:Size:Type")

diskCommand.AddCommand(diskPartitionCommand)

rootCmd.AddCommand(&cobra.Command{
Use: "partition-disk",
Deprecated: "use \"disk partition\"",
Run: diskPartitionCommand.Run,
})
}
94 changes: 94 additions & 0 deletions cmd/disk_wipe.go
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)
}
14 changes: 14 additions & 0 deletions cmd/partition.go
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)
}
26 changes: 16 additions & 10 deletions cmd/format_partition.go → cmd/partition_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/spf13/cobra"
)

var formatPartitionCmd = &cobra.Command{
Use: "format-partition",
var partitionFormatPartitionCommand = &cobra.Command{
Use: "format",
Short: "Formats a partition",
Long: "Formats a partition with your choice of filesystem",
Run: func(cmd *cobra.Command, _ []string) {
Expand Down Expand Up @@ -49,15 +49,21 @@ var formatPartitionCmd = &cobra.Command{
}

func init() {
formatPartitionCmd.PersistentFlags().String("device", "", "Block device")
formatPartitionCmd.PersistentFlags().String("filesystem-device", "", "Filesystem Block device")
partitionFormatPartitionCommand.PersistentFlags().String("device", "", "Block device")
partitionFormatPartitionCommand.PersistentFlags().String("filesystem-device", "", "Filesystem Block device")

formatPartitionCmd.PersistentFlags().Uint("partition", 0, "Partition number")
partitionFormatPartitionCommand.PersistentFlags().Uint("partition", 0, "Partition number")

formatPartitionCmd.PersistentFlags().String("format", "ext4", "Filesystem to be applied to the partition")
markFlagAsRequired(formatPartitionCmd, "format")
partitionFormatPartitionCommand.PersistentFlags().String("format", "ext4", "Filesystem to be applied to the partition")
markFlagAsRequired(partitionFormatPartitionCommand, "format")

formatPartitionCmd.PersistentFlags().String("mount-point", "/", "Filesystem mount point")
formatPartitionCmd.PersistentFlags().StringSlice("options", []string{}, "Filesystem creation options")
rootCmd.AddCommand(formatPartitionCmd)
partitionFormatPartitionCommand.PersistentFlags().String("mount-point", "/", "Filesystem mount point")
partitionFormatPartitionCommand.PersistentFlags().StringSlice("options", []string{}, "Filesystem creation options")
partitionCommand.AddCommand(partitionFormatPartitionCommand)

rootCmd.AddCommand(&cobra.Command{
Use: "format-partition",
Deprecated: "use \"partition format\"",
Run: partitionFormatPartitionCommand.Run,
})
}
26 changes: 10 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,38 @@ go 1.22

require (
github.com/Sytten/logrus-zap-hook v0.1.0
github.com/bmc-toolbox/common v0.0.0-20230717121556-5eb9915a8a5a
github.com/bmc-toolbox/common v0.0.0-20240510143200-3db7cecbb5a6
github.com/freddierice/go-losetup/v2 v2.0.1
github.com/metal-toolbox/ironlib v0.2.17
github.com/metal-toolbox/ironlib v0.2.18-0.20240611133518-3514176030a4
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
go.uber.org/zap v1.26.0
)

require (
github.com/beevik/etree v1.1.0 // indirect
github.com/beevik/etree v1.4.0 // indirect
github.com/dselans/dmidecode v0.0.0-20180814053009-65c3f9d81910 // indirect
github.com/elliotwutingfeng/asciiset v0.0.0-20230602022725-51bbb787efab // indirect
github.com/frankban/quicktest v1.14.3 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/xattr v0.4.9 // indirect
github.com/r3labs/diff/v2 v2.15.1 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/r3labs/diff/v3 v3.0.1 // indirect
github.com/tidwall/gjson v1.17.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/djherbis/times.v1 v1.3.0 // indirect
)

require (
github.com/diskfs/go-diskfs v1.4.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
)
Loading

0 comments on commit 4f13d6f

Please sign in to comment.