|
1 | 1 | package configutils
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
| 5 | + "bytes" |
4 | 6 | _ "embed"
|
5 | 7 | "fmt"
|
6 | 8 | "os"
|
7 | 9 | "os/exec"
|
8 | 10 | "path/filepath"
|
| 11 | + "strings" |
9 | 12 |
|
10 | 13 | "github.com/replicatedhq/embedded-cluster/pkg/helpers"
|
| 14 | + "go.uber.org/multierr" |
11 | 15 | )
|
12 | 16 |
|
13 |
| -// sysctlConfigPath is the path to the sysctl config file that is used to configure |
14 |
| -// the embedded cluster. This could have been a constant but we want to be able to |
15 |
| -// override it for testing purposes. |
| 17 | +// sysctlConfigPath is the path to the sysctl config file that is used to configure the embedded |
| 18 | +// cluster. This could have been a constant but we want to be able to override it for testing |
| 19 | +// purposes. |
16 | 20 | var sysctlConfigPath = "/etc/sysctl.d/99-embedded-cluster.conf"
|
17 | 21 |
|
18 |
| -//go:embed static/99-embedded-cluster.conf |
19 |
| -var embeddedClusterConf []byte |
| 22 | +var modulesLoadConfigPath = "/etc/modules-load.d/99-embedded-cluster.conf" |
20 | 23 |
|
21 |
| -// ConfigureSysctl writes the sysctl config file for the embedded cluster and |
22 |
| -// reloads the sysctl configuration. This function has a distinct behavior: if |
23 |
| -// the sysctl binary does not exist it returns an error but if it fails to lay |
24 |
| -// down the sysctl config on disk it simply returns nil. |
| 24 | +//go:embed static/sysctl.d/99-embedded-cluster.conf |
| 25 | +var embeddedClusterSysctlConf []byte |
| 26 | + |
| 27 | +//go:embed static/modules-load.d/99-embedded-cluster.conf |
| 28 | +var embeddedClusterModulesConf []byte |
| 29 | + |
| 30 | +// ConfigureSysctl writes the sysctl config file for the embedded cluster and reloads the sysctl |
| 31 | +// configuration. This function has a distinct behavior: if the sysctl binary does not exist it |
| 32 | +// returns an error but if it fails to lay down the sysctl config on disk it simply returns nil. |
| 33 | +// NOTE: do not run this after the cluster has already been installed as it may revert sysctl |
| 34 | +// settings set by k0s and its extensions. |
25 | 35 | func ConfigureSysctl() error {
|
26 | 36 | if _, err := exec.LookPath("sysctl"); err != nil {
|
27 |
| - return fmt.Errorf("unable to find sysctl binary: %w", err) |
| 37 | + return fmt.Errorf("find sysctl binary: %w", err) |
28 | 38 | }
|
29 | 39 |
|
30 |
| - if err := sysctlConfig(sysctlConfigPath); err != nil { |
31 |
| - return fmt.Errorf("unable to materialize sysctl config: %w", err) |
| 40 | + if err := sysctlConfig(); err != nil { |
| 41 | + return fmt.Errorf("materialize sysctl config: %w", err) |
32 | 42 | }
|
33 | 43 |
|
34 | 44 | if _, err := helpers.RunCommand("sysctl", "--system"); err != nil {
|
35 |
| - return fmt.Errorf("unable to configure sysctl: %w", err) |
| 45 | + return fmt.Errorf("configure sysctl: %w", err) |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// sysctlConfig writes the embedded sysctl config to the /etc/sysctl.d directory. |
| 51 | +func sysctlConfig() error { |
| 52 | + if err := os.MkdirAll(filepath.Dir(sysctlConfigPath), 0755); err != nil { |
| 53 | + return fmt.Errorf("create directory: %w", err) |
| 54 | + } |
| 55 | + if err := os.WriteFile(sysctlConfigPath, embeddedClusterSysctlConf, 0644); err != nil { |
| 56 | + return fmt.Errorf("write file: %w", err) |
| 57 | + } |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// ConfigureKernelModules writes the kernel modules config file and ensures the kernel modules are |
| 62 | +// loaded that are listed in the file. |
| 63 | +func ConfigureKernelModules() error { |
| 64 | + if _, err := exec.LookPath("modprobe"); err != nil { |
| 65 | + return fmt.Errorf("find modprobe binary: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + if err := kernelModulesConfig(); err != nil { |
| 69 | + return fmt.Errorf("materialize kernel modules config: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + if err := ensureKernelModulesLoaded(); err != nil { |
| 73 | + return fmt.Errorf("ensure kernel modules are loaded: %w", err) |
36 | 74 | }
|
37 | 75 | return nil
|
38 | 76 | }
|
39 | 77 |
|
40 |
| -// SysctlConfig writes the embedded sysctl config to the /etc/sysctl.d directory. |
41 |
| -func sysctlConfig(dstpath string) error { |
42 |
| - if err := os.MkdirAll(filepath.Dir(dstpath), 0755); err != nil { |
43 |
| - return fmt.Errorf("unable to create directory: %w", err) |
| 78 | +// kernelModulesConfig writes the embedded kernel modules config to the /etc/modules-load.d |
| 79 | +// directory. |
| 80 | +func kernelModulesConfig() error { |
| 81 | + if err := os.MkdirAll(filepath.Dir(modulesLoadConfigPath), 0755); err != nil { |
| 82 | + return fmt.Errorf("create directory: %w", err) |
44 | 83 | }
|
45 |
| - if err := os.WriteFile(dstpath, embeddedClusterConf, 0644); err != nil { |
46 |
| - return fmt.Errorf("unable to write file: %w", err) |
| 84 | + if err := os.WriteFile(modulesLoadConfigPath, embeddedClusterModulesConf, 0644); err != nil { |
| 85 | + return fmt.Errorf("write file: %w", err) |
47 | 86 | }
|
48 | 87 | return nil
|
49 | 88 | }
|
| 89 | + |
| 90 | +// ensureKernelModulesLoaded ensures the kernel modules are loaded by iterating over the modules in |
| 91 | +// the config file and calling modprobe for each one. |
| 92 | +func ensureKernelModulesLoaded() (finalErr error) { |
| 93 | + scanner := bufio.NewScanner(bytes.NewReader(embeddedClusterModulesConf)) |
| 94 | + for scanner.Scan() { |
| 95 | + module := strings.TrimSpace(scanner.Text()) |
| 96 | + if module != "" && !strings.HasPrefix(module, "#") { |
| 97 | + if err := modprobe(module); err != nil { |
| 98 | + err = fmt.Errorf("modprobe %s: %w", module, err) |
| 99 | + finalErr = multierr.Append(finalErr, err) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return |
| 104 | +} |
| 105 | + |
| 106 | +func modprobe(module string) error { |
| 107 | + _, err := helpers.RunCommand("modprobe", module) |
| 108 | + return err |
| 109 | +} |
0 commit comments