diff options
| author | Jeff Carr <[email protected]> | 2025-08-17 17:43:34 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-08-17 22:57:06 -0500 |
| commit | 145244f63d2589203b1450af61987f16fb87aff3 (patch) | |
| tree | 3b7a481b7a793f51a58a6633f3260ef512a3deca /hasPartitionTable.go | |
| parent | 25c604aeee7cd821d27dfd4c09bf0626e9312535 (diff) | |
more sanity checks
Diffstat (limited to 'hasPartitionTable.go')
| -rw-r--r-- | hasPartitionTable.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/hasPartitionTable.go b/hasPartitionTable.go new file mode 100644 index 0000000..96ed786 --- /dev/null +++ b/hasPartitionTable.go @@ -0,0 +1,34 @@ +package main + +import ( + "bytes" + "fmt" + "os/exec" + "strings" +) + +// HasPartitionTable runs `parted <device> print` and returns: +// - true if the disk has a recognized partition table +// - false if it's unrecognized or empty +func hasPartitionTable(dev string) (bool, error) { + cmd := exec.Command("parted", "-s", dev, "print") + var out bytes.Buffer + cmd.Stdout = &out + cmd.Stderr = &out + + err := cmd.Run() + output := out.String() + + // Check for known "no label" pattern + if strings.Contains(output, "unrecognised disk label") || + strings.Contains(output, "unrecognized disk label") || + strings.Contains(output, "Error") { + return false, nil + } + + if err != nil { + return false, fmt.Errorf("parted failed: %w: %s", err, output) + } + + return true, nil +} |
