mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-18 08:16:39 +00:00
[client] Set up networkd to ignore ip rules (#4730)
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/kardianos/service"
|
"github.com/kardianos/service"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@@ -81,6 +83,10 @@ func configurePlatformSpecificSettings(svcConfig *service.Config) error {
|
|||||||
svcConfig.Option["LogDirectory"] = dir
|
svcConfig.Option["LogDirectory"] = dir
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := configureSystemdNetworkd(); err != nil {
|
||||||
|
log.Warnf("failed to configure systemd-networkd: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
@@ -160,6 +166,12 @@ var uninstallCmd = &cobra.Command{
|
|||||||
return fmt.Errorf("uninstall service: %w", err)
|
return fmt.Errorf("uninstall service: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if runtime.GOOS == "linux" {
|
||||||
|
if err := cleanupSystemdNetworkd(); err != nil {
|
||||||
|
log.Warnf("failed to cleanup systemd-networkd configuration: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmd.Println("NetBird service has been uninstalled")
|
cmd.Println("NetBird service has been uninstalled")
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@@ -245,3 +257,45 @@ func isServiceRunning() (bool, error) {
|
|||||||
|
|
||||||
return status == service.StatusRunning, nil
|
return status == service.StatusRunning, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
networkdConfDir = "/etc/systemd/networkd.conf.d"
|
||||||
|
networkdConfFile = "/etc/systemd/networkd.conf.d/99-netbird.conf"
|
||||||
|
networkdConfContent = `# Created by NetBird to prevent systemd-networkd from removing
|
||||||
|
# routes and policy rules managed by NetBird.
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
ManageForeignRoutes=no
|
||||||
|
ManageForeignRoutingPolicyRules=no
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
// configureSystemdNetworkd creates a drop-in configuration file to prevent
|
||||||
|
// systemd-networkd from removing NetBird's routes and policy rules.
|
||||||
|
func configureSystemdNetworkd() error {
|
||||||
|
parentDir := filepath.Dir(networkdConfDir)
|
||||||
|
if _, err := os.Stat(parentDir); os.IsNotExist(err) {
|
||||||
|
log.Debug("systemd networkd.conf.d parent directory does not exist, skipping configuration")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// nolint:gosec // standard networkd permissions
|
||||||
|
if err := os.WriteFile(networkdConfFile, []byte(networkdConfContent), 0644); err != nil {
|
||||||
|
return fmt.Errorf("write networkd configuration: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanupSystemdNetworkd removes the NetBird systemd-networkd configuration file.
|
||||||
|
func cleanupSystemdNetworkd() error {
|
||||||
|
if _, err := os.Stat(networkdConfFile); os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.Remove(networkdConfFile); err != nil {
|
||||||
|
return fmt.Errorf("remove networkd configuration: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user