From 49939e8ac1d6fa3152f607c2b1d422eedea1bce1 Mon Sep 17 00:00:00 2001 From: Chris Chiu Date: Tue, 30 Apr 2024 14:11:31 +0800 Subject: [PATCH] enhancement: check default route during installation RKE2 requires a default route to work which is required during installation. The gateway setting will be checked in Static IP mode, but it could be absent in DHCP mode. Check the existence of the default route after applying network settings and stop the installation if no default route found. The check will be applied in both ISO and iPXE installation. Link: https://github.com/harvester/harvester/issues/5675 Signed-off-by: Chris Chiu --- pkg/console/install_panels.go | 22 ++++++++++++++++++++++ pkg/console/network.go | 19 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/pkg/console/install_panels.go b/pkg/console/install_panels.go index 958c7250b..2c75e121c 100644 --- a/pkg/console/install_panels.go +++ b/pkg/console/install_panels.go @@ -42,6 +42,7 @@ const ( ErrMsgVLANShouldBeANumberInRange string = "VLAN ID should be a number 1 ~ 4094." ErrMsgMTUShouldBeANumber string = "MTU should be a number." NtpSettingName string = "ntp-servers" + ErrMsgNoDefaultRoute string = "No default route found. Please check the router setting on the DHCP server." ) var ( @@ -1388,6 +1389,15 @@ func addNetworkPanel(c *Console) error { mgmtNetwork.Gateway = "" mgmtNetwork.MTU = 0 } + + isDefaultRouteExist, err := checkDefaultRoute() + if err != nil { + return fmt.Sprintf("Failed to check default route: %s.", err.Error()), nil + } + if !isDefaultRouteExist { + return ErrMsgNoDefaultRoute, nil + } + return "", nil } @@ -2103,6 +2113,18 @@ func addInstallPanel(c *Console) error { } } + isDefaultRouteExist, err := checkDefaultRoute() + if err != nil { + logrus.Error(err) + printToPanel(c.Gui, "Failed to check default route.", installPanel) + return + } + if !isDefaultRouteExist { + logrus.Error(ErrMsgNoDefaultRoute) + printToPanel(c.Gui, ErrMsgNoDefaultRoute, installPanel) + return + } + // We need ForceGPT because cOS only supports ForceGPT (--force-gpt) flag, not ForceMBR! c.config.ForceGPT = !c.config.ForceMBR diff --git a/pkg/console/network.go b/pkg/console/network.go index b6e3be4ad..1e84f1645 100644 --- a/pkg/console/network.go +++ b/pkg/console/network.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "os/exec" + "syscall" yipSchema "github.com/mudler/yip/pkg/schema" "github.com/sirupsen/logrus" @@ -16,6 +17,24 @@ import ( "github.com/harvester/harvester-installer/pkg/config" ) +func checkDefaultRoute() (bool, error) { + routes, err := netlink.RouteList(nil, syscall.AF_INET) + if err != nil { + logrus.Errorf("Failed to list routes: %s", err.Error()) + return false, err + } + + defaultRouteExists := false + for _, route := range routes { + if route.Dst == nil { + defaultRouteExists = true + break + } + } + + return defaultRouteExists, nil +} + func applyNetworks(network config.Network, hostname string) ([]byte, error) { if err := config.RestoreOriginalNetworkConfig(); err != nil { return nil, err