Skip to content

Instantly share code, notes, and snippets.

@Kr328
Last active June 28, 2022 12:08
Show Gist options
  • Save Kr328/7aba8c27d2743d658d1d0047aaf87666 to your computer and use it in GitHub Desktop.
Save Kr328/7aba8c27d2743d658d1d0047aaf87666 to your computer and use it in GitHub Desktop.
Sync systemd boot next boot with current entry.
package main
import (
"fmt"
"io"
"os"
"runtime"
"syscall"
"unsafe"
)
const (
GUID = "4a67b082-0a4c-41cf-b6c7-440b29bb8c4f" // Put your systemd-boot GUID here
FS_IOC_SETFLAGS = 0x40086602
)
func deleteExisted() error {
fileName := fmt.Sprintf("/sys/firmware/efi/efivars/LoaderEntryOneShot-%s", GUID)
file, err := os.OpenFile(fileName, os.O_RDONLY, 0)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer file.Close()
sysconn, err := file.SyscallConn()
if err != nil {
return err
}
var innerErr error
err = sysconn.Control(func(fd uintptr) {
flags := uint32(0)
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, FS_IOC_SETFLAGS, uintptr(unsafe.Pointer(&flags)))
if err != 0 {
innerErr = err
}
runtime.KeepAlive(flags)
})
if err != nil {
return err
}
if innerErr != nil {
return innerErr
}
return os.Remove(fileName)
}
func main() {
err := deleteExisted()
if err != nil {
panic(err.Error())
}
current, err := os.OpenFile(fmt.Sprintf("/sys/firmware/efi/efivars/LoaderEntrySelected-%s", GUID), os.O_RDONLY, 0)
if err != nil {
panic(err.Error())
}
defer current.Close()
content, err := io.ReadAll(current)
if err != nil {
panic(err.Error())
}
content[0] = 0x07
oneshot, err := os.OpenFile(fmt.Sprintf("/sys/firmware/efi/efivars/LoaderEntryOneShot-%s", GUID), os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err.Error())
}
n, err := oneshot.Write(content)
println(n, "written")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment