summaryrefslogtreecommitdiff
path: root/hasPartitionTable.go
blob: 96ed786ef99161be5729ff3f9d4d763cdfbc0e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
}