mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-04-09 10:35:51 +02:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package kvm
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
type BootStorageType string
|
|
|
|
const (
|
|
BootStorageUnknown BootStorageType = "unknown"
|
|
BootStorageEMMC BootStorageType = "emmc"
|
|
BootStorageSD BootStorageType = "sd"
|
|
)
|
|
|
|
var (
|
|
bootStorageOnce sync.Once
|
|
bootStorageType BootStorageType = BootStorageUnknown
|
|
)
|
|
|
|
func GetBootStorageType() BootStorageType {
|
|
bootStorageOnce.Do(func() {
|
|
bootStorageType = detectBootStorageType()
|
|
})
|
|
return bootStorageType
|
|
}
|
|
|
|
func IsBootFromSD() bool {
|
|
return GetBootStorageType() == BootStorageSD
|
|
}
|
|
|
|
func detectBootStorageType() BootStorageType {
|
|
cmdlineBytes, err := os.ReadFile("/proc/cmdline")
|
|
if err != nil {
|
|
return BootStorageUnknown
|
|
}
|
|
|
|
cmdline := strings.TrimSpace(string(cmdlineBytes))
|
|
for _, field := range strings.Fields(cmdline) {
|
|
if !strings.HasPrefix(field, "root=") {
|
|
continue
|
|
}
|
|
root := strings.TrimPrefix(field, "root=")
|
|
switch {
|
|
case strings.HasPrefix(root, "/dev/mmcblk0"):
|
|
return BootStorageEMMC
|
|
case strings.HasPrefix(root, "/dev/mmcblk1"):
|
|
return BootStorageSD
|
|
default:
|
|
return BootStorageUnknown
|
|
}
|
|
}
|
|
|
|
return BootStorageUnknown
|
|
}
|
|
|