-
Notifications
You must be signed in to change notification settings - Fork 11
/
conf.go
206 lines (182 loc) · 5.25 KB
/
conf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package kernels
import (
"encoding/json"
"fmt"
"os"
"path"
"time"
"github.com/sirupsen/logrus"
)
// ConfigOption are switches passed to scripts/config in a kernel dir
type ConfigOption []string
// KernelConf is the configuration of a kernel (to build from source)
type KernelConf struct {
Name string `json:"name"`
// URL of the kernel source
URL string `json:"url"`
// config options
Opts []ConfigOption `json:"opts,omitempty"`
// Extra make args
ExtraMakeArgs []string `json:"extra_make_args,omitempty"`
}
type Conf struct {
Kernels []KernelConf `json:"kernels"`
CommonOpts []ConfigOption `json:"common_opts,omitempty"`
}
func confAddGroups(opts []ConfigOption, gs ...string) ([]ConfigOption, error) {
newOpts := make([]ConfigOption, 0)
for _, g := range gs {
nopts, ok := ConfigOptGroups[g]
if !ok {
return nil, fmt.Errorf("unknown group %s", g)
}
for _, opt := range nopts {
newOpts = append(newOpts, opt)
}
}
for _, opt := range newOpts {
opts = append(opts, opt)
}
return opts, nil
}
var ConfigOptGroups = map[string][]ConfigOption{
"basic": []ConfigOption{
{"--enable", "CONFIG_LOCALVERSION_AUTO"},
{"--enable", "CONFIG_DEBUG_INFO"},
{"--enable", "CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT"},
{"--disable", "CONFIG_WERROR"},
},
"minimize": []ConfigOption{
{"--disable", "CONFIG_DRM"},
{"--disable", "CONFIG_GPU"},
// NB: is not disabled from the final config
// {"--disable", "CONFIG_CDROM"},
{"--disable", "CONFIG_ISO9669_FS"},
{"--disable", "CONFIG_CFG80211"},
{"--disable", "CONFIG_WIRELESS"},
{"--disable", "CONFIG_RFKILL"},
{"--disable", "CONFIG_MACINTOSH_DRIVERS"},
{"--disable", "CONFIG_SOUND"},
{"--disable", "CONFIG_AGP"},
{"--disable", "CONFIG_USB_SUPPORT"},
{"--disable", "CONFIG_USB"},
{"--disable", "CONFIG_WLAN"},
{"--disable", "CONFIG_HID"},
{"--disable", "CONFIG_I2C"},
{"--disable", "CONFIG_PCMCIA"},
{"--disable", "CONFIG_MD"},
{"--disable", "CONFIG_DMADEVICES"},
{"--disable", "CONFIG_THERMAL"},
},
"bpf": []ConfigOption{
{"--enable", "CONFIG_BPF"},
{"--enable", "CONFIG_BPF_SYSCALL"},
// {"--enable", "CONFIG_NETFILTER"},
// {"--enable", "CONFIG_NETFILTER_XT_MATCH_BPF"},
{"--enable", "CONFIG_NET_CLS_BPF"},
{"--enable", "CONFIG_NET_ACT_BPF"},
{"--enable", "CONFIG_BPF_JIT"},
{"--enable", "CONFIG_BPF_JIT_DEFAULT_ON"},
{"--enable", "CONFIG_BPF_EVENTS"},
{"--enable", "CONFIG_BPF_STREAM_PARSER"},
//{"--enable", "CONFIG_LWTUNNEL"},
//{"--enable", "CONFIG_LWTUNNEL_BPF"},
{"--enable", "CONFIG_DEBUG_INFO_BTF"},
{"--enable", "CONFIG_DEBUG_INFO_BTF_MODULES"},
{"--enable", "CONFIG_BPF_LSM"},
{"--enable", "CONFIG_CGROUP_BPF"},
{"--enable", "CONFIG_FTRACE_SYSCALLS"},
//{"--enable", "CONFIG_BPF_PRELOAD"},
{"--enable", "CONFIG_SKB_EXTENSIONS"},
{"--enable", "CONFIG_NET_TC_SKB_EXT"},
},
"virtio": []ConfigOption{
{"--enable", "CONFIG_VIRTIO"},
{"--enable", "CONFIG_VIRTIO_MENU"},
{"--enable", "CONFIG_VIRTIO_PCI_LIB"},
{"--enable", "CONFIG_VIRTIO_PCI"},
{"--enable", "CONFIG_VIRTIO_NET"},
{"--enable", "CONFIG_NET_9P"},
{"--enable", "CONFIG_9P_FS"},
{"--enable", "CONFIG_NET_9P_VIRTIO"},
{"--enable", "CONFIG_VIRTIO_BLK"},
},
"namespaces": []ConfigOption{
{"--enable", "CONFIG_NAMESPACES"},
{"--enable", "CONFIG_UTS_NS"},
{"--enable", "CONFIG_TIME_NS"},
{"--enable", "CONFIG_IPC_NS"},
{"--enable", "CONFIG_USER_NS"},
{"--enable", "CONFIG_PID_NS"},
{"--enable", "CONFIG_NET_NS"},
},
}
var DefaultConfigGroups = []string{"basic", "bpf", "virtio", "minimize", "namespaces"}
func GetConfigGroupNames() []string {
ret := make([]string, 0, len(ConfigOptGroups))
for k := range ConfigOptGroups {
ret = append(ret, k)
}
return ret
}
func (cnf *Conf) SaveTo(log logrus.FieldLogger, dir string, backup bool) error {
fname := path.Join(dir, ConfigFname)
confb, err := json.MarshalIndent(cnf, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal config: %w", err)
}
if backup {
// rename configuration file if it exists
if ok, _ := regularFileExists(fname); ok {
dateStr := time.Now().Format("20060102.150405000000")
fnameOld := fmt.Sprintf("%s.%s", fname, dateStr)
err := os.Rename(fname, fnameOld)
if err != nil {
log.Infof("failed to rename %s to %s", fname, fnameOld)
} else {
log.Infof("renamed %s to %s", fname, fnameOld)
}
}
}
err = os.WriteFile(fname, confb, 0666)
if err != nil {
return fmt.Errorf("error writing configuration: %w", err)
}
return nil
}
func (kc *KernelConf) Validate() error {
_, err := ParseURL(kc.URL)
return err
}
func (kc *KernelConf) AddGroupsOpts(gs ...string) error {
opts, err := confAddGroups(kc.Opts, gs...)
if err != nil {
return err
}
kc.Opts = opts
return nil
}
func (c *Conf) AddGroupsCommonOpts(gs ...string) error {
opts, err := confAddGroups(c.CommonOpts, gs...)
if err != nil {
return err
}
c.CommonOpts = opts
return nil
}
func (cnf *Conf) getOptions(kc *KernelConf) []ConfigOption {
ret := make([]ConfigOption, 0, len(cnf.CommonOpts)+len(kc.Opts))
// common options first
for _, opts := range cnf.CommonOpts {
ret = append(ret, opts)
}
// then kernel-specific options
if kc != nil {
for _, opts := range kc.Opts {
ret = append(ret, opts)
}
}
return ret
}