forked from luckfox-eng29/kvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw.go
More file actions
99 lines (86 loc) · 2.06 KB
/
hw.go
File metadata and controls
99 lines (86 loc) · 2.06 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
89
90
91
92
93
94
95
96
97
98
99
package kvm
import (
"fmt"
"os"
"regexp"
"strings"
"sync"
"time"
)
func extractSerialNumber() (string, error) {
content, err := os.ReadFile("/proc/cpuinfo")
if err != nil {
return "", err
}
r, err := regexp.Compile(`Serial\s*:\s*(\S+)`)
if err != nil {
return "", fmt.Errorf("failed to compile regex: %w", err)
}
matches := r.FindStringSubmatch(string(content))
if len(matches) < 2 {
return "", fmt.Errorf("no serial found")
}
return matches[1], nil
}
func readOtpEntropy() ([]byte, error) { //nolint:unused
content, err := os.ReadFile("/sys/bus/nvmem/devices/rockchip-otp0/nvmem")
if err != nil {
return nil, err
}
return content[0x17:0x1C], nil
}
var deviceID string
var deviceIDOnce sync.Once
func GetDeviceID() string {
deviceIDOnce.Do(func() {
serial, err := extractSerialNumber()
if err != nil {
logger.Warn().Msg("unknown serial number, the program likely not running on RV1106")
deviceID = "unknown_device_id"
} else {
deviceID = serial
}
})
return deviceID
}
func GetDefaultHostname() string {
//deviceId := GetDeviceID()
//if deviceId == "unknown_device_id" {
// return "kvm"
//}
//return fmt.Sprintf("kvm-%s", strings.ToLower(deviceId))
return "picokvm"
}
func GetHostname() string {
content, err := os.ReadFile("/etc/hostname")
if err != nil {
return GetDefaultHostname()
}
return strings.TrimSpace(string(content))
}
func runWatchdog() {
file, err := os.OpenFile("/dev/watchdog", os.O_WRONLY, 0)
if err != nil {
watchdogLogger.Warn().Err(err).Msg("unable to open /dev/watchdog, skipping watchdog reset")
return
}
defer file.Close()
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
_, err = file.Write([]byte{0})
if err != nil {
watchdogLogger.Warn().Err(err).Msg("error writing to /dev/watchdog, system may reboot")
}
case <-appCtx.Done():
//disarm watchdog with magic value
_, err := file.Write([]byte("V"))
if err != nil {
watchdogLogger.Warn().Err(err).Msg("failed to disarm watchdog, system may reboot")
}
return
}
}
}