Files
kvm/boot_storage.go
luckfox-eng29 d5bfaffd86 Update App version to 0.1.2
Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
2026-03-23 12:04:54 +08:00

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
}