-
Notifications
You must be signed in to change notification settings - Fork 163
/
lsblk.go
96 lines (77 loc) · 2.53 KB
/
lsblk.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package blkutils
import (
"context"
"fmt"
oexec "github.com/scylladb/scylla-operator/pkg/util/exec"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/utils/exec"
)
type lsblkOutput struct {
BlockDevices []*BlockDevice `json:"blockdevices"`
}
type BlockDevice struct {
Name string `json:"name"`
Model string `json:"model"`
FSType string `json:"fstype"`
PartUUID string `json:"partuuid"`
}
var (
lsblkListBlockDevicesArgs = []string{
"--json",
"--nodeps",
"--paths",
"--fs",
"--output=NAME,MODEL,FSTYPE,PARTUUID",
}
)
func IsBlockDevice(ctx context.Context, executor exec.Interface, device string) (bool, error) {
const (
notABlockDeviceExitCode = 32
)
stdout, stderr, err := oexec.RunCommand(ctx, executor, "lsblk", device)
if err != nil {
exitErr, ok := err.(exec.ExitError)
if ok && exitErr.ExitStatus() == notABlockDeviceExitCode {
return false, nil
}
return false, fmt.Errorf("failed to run lsblk with args: %v: %w, stdout: %q, stderr: %q", device, err, stdout.String(), stderr.String())
}
return true, nil
}
func GetBlockDevice(ctx context.Context, executor exec.Interface, device string) (*BlockDevice, error) {
blockDevices, err := ListBlockDevices(ctx, executor, device)
if err != nil {
return nil, fmt.Errorf("can't list block device %q: %w", device, err)
}
if len(blockDevices) < 1 {
return nil, fmt.Errorf("device %q not found", device)
}
return blockDevices[0], nil
}
func ListBlockDevices(ctx context.Context, executor exec.Interface, devices ...string) ([]*BlockDevice, error) {
args := append(lsblkListBlockDevicesArgs, devices...)
stdout, stderr, err := oexec.RunCommand(ctx, executor, "lsblk", args...)
if err != nil {
return nil, fmt.Errorf("failed to run lsblk with args: %v: %w, stdout: %q, stderr: %q", args, err, stdout, stderr.String())
}
output := &lsblkOutput{}
err = json.Unmarshal(stdout.Bytes(), output)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal lsblk output %q: %w", stdout.String(), err)
}
return output.BlockDevices, nil
}
func GetPartitionUUID(ctx context.Context, executor exec.Interface, device string) (string, error) {
bd, err := GetBlockDevice(ctx, executor, device)
if err != nil {
return "", fmt.Errorf("can't get block device %q: %w", device, err)
}
return bd.PartUUID, nil
}
func GetFilesystemType(ctx context.Context, executor exec.Interface, device string) (string, error) {
bd, err := GetBlockDevice(ctx, executor, device)
if err != nil {
return "", fmt.Errorf("can't get block device %q: %w", device, err)
}
return bd.FSType, nil
}