Skip to content

Commit c4e7342

Browse files
committed
internal/kconfig: move Find to package linux
Move Find to package linux, since it depends on deriving a file name from the current linux kernel version. This means that package kconfig only implements parsing of the kconfig format. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent c2282df commit c4e7342

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

collection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ func resolveKconfig(m *MapSpec) error {
652652

653653
// We only parse kconfig file if a CONFIG_* variable was found.
654654
if len(configs) > 0 {
655-
f, err := kconfig.Find()
655+
f, err := linux.FindKConfig()
656656
if err != nil {
657657
return fmt.Errorf("cannot find a kconfig file: %w", err)
658658
}

internal/kconfig/kconfig.go

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package kconfig implements a parser for the format of Linux's .config file.
12
package kconfig
23

34
import (
@@ -7,39 +8,13 @@ import (
78
"fmt"
89
"io"
910
"math"
10-
"os"
1111
"strconv"
1212
"strings"
1313

1414
"github.com/cilium/ebpf/btf"
1515
"github.com/cilium/ebpf/internal"
16-
"github.com/cilium/ebpf/internal/linux"
1716
)
1817

19-
// Find find a kconfig file on the host.
20-
// It first reads from /boot/config- of the current running kernel and tries
21-
// /proc/config.gz if nothing was found in /boot.
22-
// If none of the file provide a kconfig, it returns an error.
23-
func Find() (*os.File, error) {
24-
kernelRelease, err := linux.KernelRelease()
25-
if err != nil {
26-
return nil, fmt.Errorf("cannot get kernel release: %w", err)
27-
}
28-
29-
path := "/boot/config-" + kernelRelease
30-
f, err := os.Open(path)
31-
if err == nil {
32-
return f, nil
33-
}
34-
35-
f, err = os.Open("/proc/config.gz")
36-
if err == nil {
37-
return f, nil
38-
}
39-
40-
return nil, fmt.Errorf("neither %s nor /proc/config.gz provide a kconfig", path)
41-
}
42-
4318
// Parse parses the kconfig file for which a reader is given.
4419
// All the CONFIG_* which are in filter and which are set set will be
4520
// put in the returned map as key with their corresponding value as map value.

internal/linux/kconfig.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package linux
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// FindKConfig searches for a kconfig file on the host.
9+
//
10+
// It first reads from /boot/config- of the current running kernel and tries
11+
// /proc/config.gz if nothing was found in /boot.
12+
// If none of the file provide a kconfig, it returns an error.
13+
func FindKConfig() (*os.File, error) {
14+
kernelRelease, err := KernelRelease()
15+
if err != nil {
16+
return nil, fmt.Errorf("cannot get kernel release: %w", err)
17+
}
18+
19+
path := "/boot/config-" + kernelRelease
20+
f, err := os.Open(path)
21+
if err == nil {
22+
return f, nil
23+
}
24+
25+
f, err = os.Open("/proc/config.gz")
26+
if err == nil {
27+
return f, nil
28+
}
29+
30+
return nil, fmt.Errorf("neither %s nor /proc/config.gz provide a kconfig", path)
31+
}

0 commit comments

Comments
 (0)