Reduce the number of bootenv vars

This commit is contained in:
2026-04-06 02:20:41 +08:00
parent f8db036a5f
commit 50d9440e0a
14 changed files with 455 additions and 93 deletions

View File

@@ -118,6 +118,16 @@ func applyControlAgentClusterRole(ctx context.Context, kubeClient kubernetes.Int
Resources: []string{"osupgrades/status"},
Verbs: []string{"get", "patch", "update"},
},
{
APIGroups: []string{monov1alpha1.Group},
Resources: []string{"osupgradeprogresses"},
Verbs: []string{"get", "list", "watch", "create", "patch", "update"},
},
{
APIGroups: []string{monov1alpha1.Group},
Resources: []string{"osupgradeprogresses/status"},
Verbs: []string{"get", "list", "watch", "create", "patch", "update"},
},
{
APIGroups: []string{""},
Resources: []string{"nodes"},

View File

@@ -19,12 +19,39 @@ func ConfigureABBoot(ctx context.Context, nctx *node.NodeContext) error {
// TODO: configurable from cluster.env
return writer.EnsureBootEnv(ctx, BootEnvConfig{
BootSource: "usb",
BootPart: "A",
BootDisk: 0,
RootfsAPartNum: 2,
RootfsBPartNum: 3,
DataPartNum: 4,
LinuxRootPrefix: "/dev/sda",
BootSource: "usb",
BootPart: "A",
})
}
// This is called by the agent controller/osupgrade/handler.go
func ConfigureNextBoot(ctx context.Context, fwEnvCfgPath string) error {
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("get current executable path: %w", err)
}
writer := NewFWEnvWriter(fwEnvCfgPath, exePath)
currBootPart, err := writer.GetEnv(ctx, "boot_part")
if err != nil {
return fmt.Errorf("get boot_part: %w", err)
}
next := "A"
if currBootPart == "A" {
next = "B"
}
currBootSource, err := writer.GetEnv(ctx, "boot_source")
if err != nil {
return fmt.Errorf("get boot_source: %w", err)
}
return writer.EnsureBootEnv(ctx, BootEnvConfig{
BootSource: currBootSource,
BootPart: next,
})
}

View File

@@ -0,0 +1,12 @@
package uboot
import (
"fmt"
"testing"
)
func TestBootCmd(_ *testing.T) {
cfg := BootEnvConfig{}
fmt.Println(cfg.bootCmdOrDefault())
}

View File

@@ -18,22 +18,6 @@ func (c BootEnvConfig) Validate() error {
return fmt.Errorf("invalid boot part %q", c.BootPart)
}
if c.BootDisk < 0 {
return fmt.Errorf("invalid boot disk %d", c.BootDisk)
}
if c.RootfsAPartNum <= 0 {
return fmt.Errorf("invalid rootfs A part %d", c.RootfsAPartNum)
}
if c.RootfsBPartNum <= 0 {
return fmt.Errorf("invalid rootfs B part %d", c.RootfsBPartNum)
}
if c.DataPartNum <= 0 {
return fmt.Errorf("invalid data part %d", c.DataPartNum)
}
if strings.TrimSpace(c.LinuxRootPrefix) == "" {
return fmt.Errorf("linux root prefix is required")
}
return nil
}

View File

@@ -126,11 +126,6 @@ func (w *FWEnvWriter) EnsureBootEnv(ctx context.Context, cfg BootEnvConfig) erro
{"bootcmd", cfg.bootCmdOrDefault()},
{"boot_source", string(cfg.BootSource)},
{"boot_part", string(cfg.BootPart)},
{"boot_disk", fmt.Sprintf("%d", cfg.BootDisk)},
{"rootfs_a_partnum", fmt.Sprintf("%d", cfg.RootfsAPartNum)},
{"rootfs_b_partnum", fmt.Sprintf("%d", cfg.RootfsBPartNum)},
{"data_partnum", fmt.Sprintf("%d", cfg.DataPartNum)},
{"linux_root_prefix", cfg.LinuxRootPrefix},
}
for _, kv := range envs {

View File

@@ -3,6 +3,7 @@ package uboot
import (
"bytes"
"context"
"errors"
"fmt"
"k8s.io/klog/v2"
"os"
@@ -155,7 +156,7 @@ func ConfigureUBootCommands(ctx context.Context, n *node.NodeContext) error {
b.WriteString(trimOutput(a.output, 600))
b.WriteString("\n")
}
return fmt.Errorf(b.String())
return errors.New(b.String())
}
klog.Infof("Using: %s", best.config)

View File

@@ -12,14 +12,9 @@ const (
)
type BootEnvConfig struct {
BootSource string // usb or emmc
BootPart string // A or B
BootDisk int
RootfsAPartNum int
RootfsBPartNum int
DataPartNum int
LinuxRootPrefix string // /dev/sda or /dev/mmcblk0p
BootCmd string // optional; defaults to DefaultBootCmd
BootSource string // usb or emmc
BootPart string // A or B
BootCmd string // optional; defaults to DefaultBootCmd
}
type FWEnvWriter struct {
@@ -29,32 +24,52 @@ type FWEnvWriter struct {
}
const defaultBootCmdTemplate = `
setenv kernel_addr_r 0xa0000000;
if usb start; then
if usb dev 0; then
echo "Trying generic USB boot...";
# Prefer extlinux-style boot
if test -e usb 0:1 /boot/extlinux/extlinux.conf; then
echo "Found extlinux config on USB";
sysboot usb 0:1 any ${kernel_addr_r} /boot/extlinux/extlinux.conf;
fi;
# Fallback: U-Boot script image
if test -e usb 0:1 /boot/boot.scr; then
echo "Found boot.scr on USB";
if load usb 0:1 ${kernel_addr_r} /boot/boot.scr; then
source ${kernel_addr_r};
fi;
fi;
fi;
fi;
if test "${boot_source}" = "usb"; then
usb start || exit;
setenv boot_iface usb;
usb dev 0 || exit;
elif test "${boot_source}" = "emmc"; then
mmc dev ${boot_disk} || exit;
mmc dev 0 || exit;
setenv boot_iface mmc;
else
echo "unsupported boot_source: ${boot_source}";
exit;
fi;
setenv kernel_addr_r 0xa0000000;
if test "${boot_part}" = "A"; then
setenv rootpart ${rootfs_a_partnum};
setenv rootpart 2;
elif test "${boot_part}" = "B"; then
setenv rootpart ${rootfs_b_partnum};
setenv rootpart 3;
else
echo "unsupported boot_part: ${boot_part}";
exit;
fi;
setenv bootdev ${boot_disk}:${rootpart};
setenv rootdev ${linux_root_prefix}${rootpart};
setenv datadev ${linux_root_prefix}${data_partnum};
setenv bootdev 0:${rootpart};
setenv rootdev ${boot_source}:${rootpart};
setenv bootargs "${bootargs_console} root=${rootdev} data=${datadev} rw rootwait rootfstype=ext4";
setenv bootargs "${bootargs_console} root=${rootdev} bootpart=${boot_part} rw rootwait rootfstype=ext4";
ext4load ${boot_iface} ${bootdev} ${kernel_addr_r} /boot/kernel.itb && bootm ${kernel_addr_r};
`