Reduce the number of bootenv vars
This commit is contained in:
@@ -3,6 +3,7 @@ package osupgrade
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/klog/v2"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"example.com/monok8s/pkg/catalog"
|
||||
"example.com/monok8s/pkg/controller/osimage"
|
||||
"example.com/monok8s/pkg/kube"
|
||||
"example.com/monok8s/pkg/node/uboot"
|
||||
)
|
||||
|
||||
func HandleOSUpgrade(ctx context.Context, clients *kube.Clients,
|
||||
@@ -89,7 +91,7 @@ func HandleOSUpgrade(ctx context.Context, clients *kube.Clients,
|
||||
|
||||
imageOptions := osimage.ApplyOptions{
|
||||
URL: first.URL,
|
||||
TargetPath: "./out/flash.img",
|
||||
TargetPath: "/dev/sda?",
|
||||
ExpectedRawSHA256: imageSHA,
|
||||
ExpectedRawSize: first.Size,
|
||||
BufferSize: 6 * 1024 * 1024,
|
||||
@@ -128,10 +130,9 @@ func HandleOSUpgrade(ctx context.Context, clients *kube.Clients,
|
||||
}
|
||||
|
||||
klog.Info(result)
|
||||
if err := SetNextBootEnv(ctx, NextBootConfig{
|
||||
Key: "boot_part",
|
||||
Value: "B",
|
||||
}); err != nil {
|
||||
|
||||
cfgPath := os.Getenv("FW_ENV_CONFIG_FILE")
|
||||
if err := uboot.ConfigureNextBoot(ctx, cfgPath); err != nil {
|
||||
return failProgress(ctx, clients, osup, "set boot env", err)
|
||||
}
|
||||
|
||||
@@ -145,5 +146,8 @@ func HandleOSUpgrade(ctx context.Context, clients *kube.Clients,
|
||||
return fmt.Errorf("update progress status: %w", err)
|
||||
}
|
||||
|
||||
// TODO: Drain the node here
|
||||
// TODO: Issue Reboot
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
12
clitools/pkg/node/uboot/bootcmd_test.go
Normal file
12
clitools/pkg/node/uboot/bootcmd_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package uboot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBootCmd(_ *testing.T) {
|
||||
|
||||
cfg := BootEnvConfig{}
|
||||
fmt.Println(cfg.bootCmdOrDefault())
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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};
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user