UEFI PXE through signed shim and GRUB (Secure Boot)
UEFI machines were given syslinux.efi, which is unsigned: with Secure
Boot on (the default on much server hardware) the firmware refuses it
("Access Denied", seen on an OVMF lab VM), so such a machine could not
PXE-boot at all. They now get Debian's signed shim (bootx64.efi), which
loads the signed network GRUB (grubx64.efi); GRUB reads
grub/grub.cfg-<mac>, else grub/grub.cfg-default, and loads kernels
only as shim verifies them. Rack controllers write the GRUB menus from
the pxelinux menus they already get (same entries: kernel and initrd,
local disk via exit, power off via halt), so both always agree; BIOS
machines keep pxelinux. install.sh, install-rack.sh and bootstrap.sh
install shim-signed and grub-efi-amd64-signed and lay out the files.
Secure Boot covers the kernel; the initrd and the squashfs it fetches
are not covered by it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ab20c277b2
commit
a0df7295ea
10 files changed
+222
-9
No files matched your search
@@ -0,0 +1,126 @@
|
||||
package main
|
||||
|
||||
// UEFI machines boot through shim and GRUB, both signed (Debian's
|
||||
// shim-signed and grub-efi-amd64-signed), so a machine with Secure Boot on
|
||||
// PXE-boots at all (syslinux.efi is unsigned: the firmware refuses it) and
|
||||
// GRUB loads only kernels that shim verifies. BIOS machines keep pxelinux.
|
||||
//
|
||||
// TFTP root:
|
||||
//
|
||||
// bootx64.efi shim (shimx64.efi.signed)
|
||||
// grubx64.efi GRUB for network boot (grubnetx64.efi.signed)
|
||||
// grub/grub.cfg picks grub.cfg-<mac>, else grub.cfg-default
|
||||
// grub/grub.cfg-<mac> per machine, made from its pxelinux file
|
||||
//
|
||||
// The rack controller writes the GRUB files from the pxelinux ones it gets,
|
||||
// so both menus always say the same. Secure Boot checks the kernel; the
|
||||
// initrd and the squashfs it fetches are not covered by it.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const grubDispatch = `# generated by rollcall: the machine's own menu, else the one for unknown machines
|
||||
set timeout=8
|
||||
configfile $prefix/grub.cfg-$net_default_mac
|
||||
configfile $prefix/grub.cfg-default
|
||||
`
|
||||
|
||||
var grubNameRe = regexp.MustCompile(`^grub\.cfg-([0-9a-f]{2}:){5}[0-9a-f]{2}$`)
|
||||
|
||||
// grubName is the GRUB file for a pxelinux one: 01-aa-bb-cc-dd-ee-ff -> grub.cfg-aa:bb:cc:dd:ee:ff.
|
||||
func grubName(pxe string) string {
|
||||
if !strings.HasPrefix(pxe, "01-") {
|
||||
return ""
|
||||
}
|
||||
n := "grub.cfg-" + strings.ReplaceAll(strings.TrimPrefix(pxe, "01-"), "-", ":")
|
||||
if !grubNameRe.MatchString(n) {
|
||||
return ""
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
type pxeEntry struct {
|
||||
label, kernel, initrd, append string
|
||||
local, poweroff bool
|
||||
}
|
||||
|
||||
// pxelinuxToGrub turns one of rollcall's pxelinux menus into the same GRUB menu.
|
||||
func pxelinuxToGrub(body string) string {
|
||||
var entries []*pxeEntry
|
||||
def, timeout := 0, 5
|
||||
var cur *pxeEntry
|
||||
for _, raw := range strings.Split(body, "\n") {
|
||||
line := strings.TrimSpace(raw)
|
||||
f := strings.Fields(line)
|
||||
if len(f) == 0 || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
key := strings.ToUpper(f[0])
|
||||
rest := strings.TrimSpace(line[len(f[0]):])
|
||||
switch {
|
||||
case key == "TIMEOUT":
|
||||
if n, err := strconv.Atoi(rest); err == nil { // tenths of a second
|
||||
timeout = (n + 9) / 10
|
||||
}
|
||||
case key == "LABEL":
|
||||
cur = &pxeEntry{label: rest}
|
||||
entries = append(entries, cur)
|
||||
case cur == nil:
|
||||
case key == "MENU" && len(f) > 1 && strings.EqualFold(f[1], "LABEL"):
|
||||
cur.label = strings.TrimSpace(rest[len(f[1]):])
|
||||
case key == "MENU" && len(f) > 1 && strings.EqualFold(f[1], "DEFAULT"):
|
||||
def = len(entries) - 1
|
||||
case key == "KERNEL":
|
||||
if strings.HasSuffix(rest, "poweroff.c32") {
|
||||
cur.poweroff = true
|
||||
} else {
|
||||
cur.kernel = rest
|
||||
}
|
||||
case key == "INITRD":
|
||||
cur.initrd = rest
|
||||
case key == "APPEND":
|
||||
cur.append = rest
|
||||
case key == "LOCALBOOT":
|
||||
cur.local = true
|
||||
}
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("# generated by rollcall from the pxelinux menu\n")
|
||||
fmt.Fprintf(&b, "set default=%d\nset timeout=%d\n", def, timeout)
|
||||
for _, e := range entries {
|
||||
fmt.Fprintf(&b, "menuentry %s {\n", grubQuote(e.label))
|
||||
switch {
|
||||
case e.poweroff:
|
||||
b.WriteString(" halt\n")
|
||||
case e.local:
|
||||
b.WriteString(" exit 1\n") // back to the firmware: its next boot option is the disk
|
||||
case e.kernel != "":
|
||||
fmt.Fprintf(&b, " linux %s", grubQuote("/"+strings.TrimPrefix(e.kernel, "/")))
|
||||
for _, w := range strings.Fields(e.append) {
|
||||
b.WriteString(" " + grubQuote(w))
|
||||
}
|
||||
b.WriteString("\n")
|
||||
if e.initrd != "" {
|
||||
fmt.Fprintf(&b, " initrd %s\n", grubQuote("/"+strings.TrimPrefix(e.initrd, "/")))
|
||||
}
|
||||
default:
|
||||
b.WriteString(" exit 1\n")
|
||||
}
|
||||
b.WriteString("}\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
var grubPlain = regexp.MustCompile(`^[A-Za-z0-9_./:=,+@%-]+$`)
|
||||
|
||||
// grubQuote makes a word safe in GRUB's script language (no $ expansion, no ;).
|
||||
func grubQuote(s string) string {
|
||||
if grubPlain.MatchString(s) {
|
||||
return s
|
||||
}
|
||||
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
|
||||
}
|
||||
Reference in new issue
Block a user