-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevice_linux.go
More file actions
88 lines (74 loc) · 1.73 KB
/
device_linux.go
File metadata and controls
88 lines (74 loc) · 1.73 KB
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
package osmgr
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
func ScanDisks() []*Disk {
d := make([]*Disk, 0)
var lastDisk *Disk
// Use the "lsblk" command to list all block devices
out, err := exec.Command("lsblk", "-o", "NAME,PTTYPE,PTUUID,TYPE,SIZE,MODEL,PARTUUID,FSTYPE,MOUNTPOINT", "-P").Output()
if err != nil {
panic(fmt.Sprintf("Error executing lsblk: %v", err))
}
// Split the output into lines
lines := strings.Split(string(out), "\n")
// Loop through each line
for _, line := range lines {
if line == "" {
continue
}
// Split the line into key-value pairs
pairs := strings.Split(line, "\"")
if len(pairs) < 6 {
continue
}
block := pairs[1]
blockTable := pairs[3]
blockType := pairs[7]
blockSize := pairs[9]
blockModel := pairs[11]
diskID := strings.Replace(pairs[5], "-", "", -1)
diskIDnum, _ := strconv.ParseInt(diskID, 16, 64)
diskID = fmt.Sprintf("%d", diskIDnum)
if blockType == "disk" {
if lastDisk != nil {
d = append(d, lastDisk)
}
lastDisk = &Disk{
Table: blockTable,
Model: blockModel,
Size: blockSize,
Block: block,
ID: diskID,
}
} else if blockType == "part" {
partID := pairs[13]
partFS := pairs[15]
partMount := pairs[17]
if blockTable == "dos" {
partIDnum, err := strconv.ParseInt(partID, 16, 64)
if err == nil {
partID = fmt.Sprintf("%d", partIDnum*512)
}
}
if lastDisk.Partitions == nil {
lastDisk.Partitions = make([]*Partition, 0)
}
lastDisk.Partitions = append(lastDisk.Partitions, &Partition{
Disk: lastDisk,
FS: partFS,
Size: blockSize,
Block: block,
Mount: partMount,
ID: partID,
})
}
}
if lastDisk != nil {
d = append(d, lastDisk)
}
return d
}