mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-06-05 04:39:09 +02:00
Update App version to 0.1.2
Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
This commit is contained in:
57
boot_storage.go
Normal file
57
boot_storage.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user