-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_unix.go
54 lines (50 loc) · 1017 Bytes
/
config_unix.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
// +build !windows
package thuder
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
)
func getDriveID() (string, error) {
buf := bytes.NewBuffer(nil)
cmd := exec.Command("lsblk", "--nodeps", "-o", "name,rm", "-n")
cmd.Stdout = buf
err := cmd.Run()
if err != nil {
//buf.WriteString("sda 1\nmmcblk0 0\n") //sample data
return "", err
}
var name string
for {
line, err := buf.ReadString('\n')
if err != nil {
break
}
rmi := strings.LastIndex(line, " ")
rm := strings.TrimSpace(line[rmi:])
if rm == "0" {
name = strings.TrimSpace(line[:rmi])
break
}
}
if len(name) == 0 {
return "", fmt.Errorf("no acceptable block listed")
}
serial, err := os.Open("/sys/block/" + name + "/device/serial")
if err != nil {
return "", err
}
defer serial.Close()
b := make([]byte, 12)
n, err := serial.Read(b)
if err != nil {
return "", err
}
b = bytes.TrimSpace(b[:n])
if n > 2 {
return string(b[2:]), nil
}
return "", fmt.Errorf("read serial \"%s\" too short", b)
}