35 lines
752 B
Go
35 lines
752 B
Go
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
|
|
}
|