-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathzvrboot_utils.go
377 lines (332 loc) · 11.8 KB
/
zvrboot_utils.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"zstack-vyos/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var (
nicsMap map[string]*utils.NicInfo = make(map[string]*utils.NicInfo)
mgmtNic *utils.NicInfo = &utils.NicInfo{}
)
func renameNic() {
log.Debugf("[configure: rename nics]")
type deviceName struct {
expected string
actual string
swap string
}
devNames := make([]*deviceName, 0)
for _, nic := range nicsMap {
nicname, err := utils.GetNicNameByMac(nic.Mac)
utils.PanicOnError(err)
if nicname != nic.Name {
devNames = append(devNames, &deviceName{
expected: nic.Name,
actual: nicname,
})
}
}
if len(devNames) == 0 {
return
}
for i, devname := range devNames {
devnum := 1000 + i
devname.swap = fmt.Sprintf("eth%v", devnum)
err := utils.IpLinkSetDown(devname.actual)
utils.Assertf(err == nil, "IpLinkSetDown[%s] error: %s", devname.actual, err)
err = utils.IpLinkSetName(devname.actual, devname.swap)
utils.Assertf(err == nil, "IpLinkSetName[%s, %s] error: %s", devname.actual, devname.swap, err)
}
for _, devname := range devNames {
err := utils.IpLinkSetName(devname.swap, devname.expected)
utils.Assertf(err == nil, "IpLinkSetName[%s, %s] error: %s", devname.swap, devname.expected, err)
err = utils.IpLinkSetUp(devname.expected)
utils.Assertf(err == nil, "IpLinkSetUp[%s] error: %s", devname.actual, err)
}
}
func parseNicInfo(targetNic map[string]interface{}) *utils.NicInfo {
var ok bool
nicInfo := &utils.NicInfo{}
nicInfo.Name, ok = targetNic["deviceName"].(string)
utils.PanicIfError(ok, fmt.Errorf("cannot find 'deviceName' field for the nic"))
nicInfo.Mac, ok = targetNic["mac"].(string)
utils.PanicIfError(ok, errors.New("cannot find 'mac' field for the nic"))
ip, ok := targetNic["ip"].(string)
ip6, ok6 := targetNic["ip6"].(string)
utils.PanicIfError(ok || ok6, fmt.Errorf("cannot find 'ip' field for the nic[name:%s]", nicInfo.Name))
if ip != "" {
nicInfo.Ip = ip
nicInfo.Netmask, ok = targetNic["netmask"].(string)
utils.PanicIfError(ok, fmt.Errorf("cannot find 'netmask' field for the nic[name:%s]", nicInfo.Name))
nicInfo.Gateway = targetNic["gateway"].(string)
}
if ip6 != "" {
nicInfo.Ip6 = ip6
prefixLength, ok := targetNic["prefixLength"].(float64)
utils.PanicIfError(ok, fmt.Errorf("cannot find 'prefixLength' field for the nic[name:%s]", nicInfo.Name))
nicInfo.PrefixLength = int(prefixLength)
nicInfo.Gateway6 = targetNic["gateway6"].(string)
nicInfo.AddressMode, ok = targetNic["addressMode"].(string)
utils.PanicIfError(ok, fmt.Errorf("cannot find 'addressMode' field for the nic[name:%s]", nicInfo.Name))
}
nicInfo.IsDefault = targetNic["isDefaultRoute"].(bool)
if mtuFloat, ok := targetNic["mtu"].(float64); ok {
nicInfo.Mtu = int(mtuFloat)
}
if targetNic["l2type"] != nil {
nicInfo.L2Type = targetNic["l2type"].(string)
nicInfo.Category = targetNic["category"].(string)
}
if targetNic["vni"] != nil {
nicInfo.Vni = int(targetNic["vni"].(float64))
}
if targetNic["physicalInterface"] != nil {
nicInfo.PhysicalInterface = targetNic["physicalInterface"].(string)
}
return nicInfo
}
func configureSshServer() {
log.Debugf("[configure: SSH Server and publicKey]")
sshkey := utils.BootstrapInfo["publicKey"].(string)
utils.Assert(sshkey != "", "cannot find 'publicKey' in bootstrap info")
sshport := utils.BootstrapInfo["sshPort"].(float64)
address := mgmtNic.Ip
utils.Assert(address != "", "cannot find eth0 ip address in bootstrap info")
sshInfo := utils.NewSshServer().SetListen(address).SetPorts(int(sshport)).SetKeys(sshkey)
err := sshInfo.ConfigService()
utils.Assertf(err == nil, "configure SSH Server error: %s", err)
}
func configureRadvdServer() {
log.Debugf("[configure: radvd service]")
radvdMap := make(utils.RadvdAttrsMap)
if utils.IsSLB() {
_ = radvdMap.StopService()
log.Debugf("skip configure radvd service with SLB")
} else {
for _, nic := range nicsMap {
if nic.Ip6 != "" && nic.PrefixLength > 0 && nic.Category == "Private" {
radvdAttr := utils.NewRadvdAttrs().SetNicName(nic.Name).SetIp6(nic.Ip6, nic.PrefixLength).SetMode(nic.AddressMode)
radvdMap[nic.Name] = radvdAttr
}
}
if err := radvdMap.ConfigService(); err != nil {
log.Debugf("configure radvd service error: %+v", err)
}
}
}
func configurePassword() {
log.Debugf("[configure: vyos password]")
password, found := utils.BootstrapInfo["vyosPassword"]
log.Debugf("[configure: %s", password)
utils.Assert(found && password != "", "vyosPassword cannot be empty")
if !isOnVMwareHypervisor() {
log.Debugf("not vmware")
err := utils.SetUserPasswd(utils.GetZvrUser(), fmt.Sprintf("%s", password))
utils.Assertf(err == nil, "configure vpc password error: %s", err)
}
}
func configureTimeZone() {
log.Debugf("[configure: system configuration]")
if err := utils.SetTimeZone("Asia/Shanghai"); err != nil {
log.Debugf("configure time zone error: %+v", err)
}
}
func configureSshMonitor() {
if utils.IsEuler2203() {
/* use systemd to restart sshd */
return
}
log.Debugf("[configure: create sshd monitor]")
cronJobMap := make(utils.CronjobMap)
newJob := utils.NewCronjob().SetId(1).SetCommand(utils.GetCronjobFileSsh()).SetMinute("*/1")
cronJobMap[1] = newJob
err := cronJobMap.ConfigService()
utils.Assertf(err == nil, "configure ssh monitor error: %s", err)
}
func configureNicInfo(nic *utils.NicInfo) {
var err error
// TODO ....
//setNicTree.SetNicSmpAffinity(nic.name, "auto")
//set kernel arguments for specific nic
err = os.WriteFile(fmt.Sprintf("/proc/sys/net/ipv6/conf/%s/keep_addr_on_down", nic.Name), []byte("1"), 0644)
if err != nil {
log.Warningf("enable nic %s keep_addr_on_down failed: %s!", nic.Name, err)
}
err = os.WriteFile(fmt.Sprintf("/proc/sys/net/ipv6/conf/%s/accept_dad", nic.Name), []byte("0"), 0644)
if err != nil {
log.Warningf("disable nic %s accept_dad failed: %s!", nic.Name, err)
}
utils.SetNicOption(nic.Name)
err = utils.IpLinkSetUp(nic.Name)
utils.Assertf(err == nil, "IpLinkSetUp[%s] error: %s", nic.Name, err)
if nic.Ip != "" {
log.Debugf("[flush-nic]")
err := utils.Ip4AddrFlush(nic.Name)
utils.Assertf(err == nil, "IpAddr4Flush[%s] error: %+v", nic.Name, err)
log.Debugf("[flush-route]")
err = utils.FlushNicRoute(nic.Name)
utils.Assertf(err == nil, "Flush nic[%s] route error: %+v", nic.Name, err)
log.Debugf("[getcidr]")
cidr, err := utils.NetmaskToCIDR(nic.Netmask)
utils.PanicOnError(err)
ipString := fmt.Sprintf("%v/%v", nic.Ip, cidr)
log.Debugf("[addip]")
log.Debugf("nic [%s] add ipv4 address %s", nic.Name, ipString)
err = utils.IpAddrAdd(nic.Name, ipString)
utils.Assertf(err == nil, "IpAddrAdd[%s, %s] error: %+v", nic.Name, ipString, err)
}
if nic.Ip6 != "" {
err := utils.Ip6AddrFlush(nic.Name)
utils.Assertf(err == nil, "IpAddr6Flush[%s] error: %+v", nic.Name, err)
ip6String := fmt.Sprintf("%s/%d", nic.Ip6, nic.PrefixLength)
log.Debugf("nic [%s] add ipv6 address %s", nic.Name, ip6String)
err = utils.IpAddrAdd(nic.Name, ip6String)
utils.Assertf(err == nil, "IpAddrAdd[%s, %s] error: %+v", nic.Name, ip6String, err)
}
if nic.Mtu != 0 {
if err := utils.IpLinkSetMTU(nic.Name, nic.Mtu); err != nil {
log.Debugf("IpLinkSetMTU[%s, %d] error: %+v", nic.Name, nic.Mtu, err)
}
}
if nic.IsDefault {
if nic.Gateway != "" {
routeEntry := utils.NewIpRoute().SetGW(nic.Gateway).SetDev(nic.Name).SetTable(utils.RT_TABLES_MAIN).SetProto(utils.RT_PROTOS_STATIC)
err := utils.IpRouteAdd(routeEntry)
utils.Assertf(err == nil, "IpRouteAdd[%+v] error: %+v", routeEntry, err)
}
if nic.Gateway6 != "" {
route6Entry := utils.NewIpRoute().SetGW(nic.Gateway).SetDev(nic.Name).SetTable(utils.RT_TABLES_MAIN).SetProto(utils.RT_PROTOS_STATIC)
err := utils.IpRouteAdd(route6Entry)
utils.Assertf(err == nil, "IpRouteAdd[%+v] error: %+v", route6Entry, err)
}
}
if nic.L2Type != "" {
err := utils.IpLinkSetAlias(nic.Name, utils.MakeIfaceAlias(nic))
utils.Assertf(err == nil, "IpLinkSetAlias[%s] error: %+v", nic.Name, err)
}
if utils.GetHaStatus() != utils.NOHA && nic.Name != "eth0" && !utils.IsSLB() {
err := utils.IpLinkSetDown(nic.Name)
utils.Assertf(err == nil, "IpLinkSetDown[%s] error: %+v", nic.Name, err)
}
if nic.Name == "eth0" {
mgmtNodeCidr := utils.BootstrapInfo["managementNodeCidr"]
if mgmtNodeCidr != nil {
mgmtNodeCidrStr := mgmtNodeCidr.(string)
nexthop, _ := utils.IpRouteGet(mgmtNodeCidrStr)
if nexthop != nic.Gateway {
defaultRoute := utils.NewIpRoute().SetDst(mgmtNodeCidrStr).SetGW(nic.Gateway)
if err = utils.IpRouteAdd(defaultRoute); err != nil {
log.Debugf("IpRouteAdd route entry[Dst:%s gateway:%s] error: %+v", mgmtNodeCidrStr, nic.Gateway, err)
}
utils.AddRoute(mgmtNodeCidrStr, nic.Gateway)
}
}
}
if nic.Category == "Private" {
err = utils.InitNicFirewall(nic.Name, nic.Ip, false, utils.IPTABLES_ACTION_REJECT)
} else {
err = utils.InitNicFirewall(nic.Name, nic.Ip, true, utils.IPTABLES_ACTION_REJECT)
}
if err != nil {
log.Debugf("InitNicFirewall for nic: %s failed", err.Error())
}
}
func configureMgmtNic() {
log.Debugf("[configure: interfaces[%s] ... ", mgmtNic.Name)
configureNicInfo(mgmtNic)
}
func configureAdditionNic() {
for name, nic := range nicsMap {
if name != mgmtNic.Name {
log.Debugf("[configure: interfaces[%s] ... ", name)
configureNicInfo(nic)
}
}
}
func parseNicFromBootstrap() {
log.Debugf("[configure: parse managenent nic info]")
nicInfo := utils.BootstrapInfo["managementNic"].(map[string]interface{})
if nicInfo == nil {
panic(errors.New("no field 'managementNic' in bootstrap info"))
}
mgmtNic = parseNicInfo(nicInfo)
nicsMap[mgmtNic.Name] = mgmtNic
log.Debugf("[configure: parse additional nic info]")
otherNics := utils.BootstrapInfo["additionalNics"].([]interface{})
if otherNics != nil {
for _, o := range otherNics {
onic := o.(map[string]interface{})
n := parseNicInfo(onic)
nicsMap[n.Name] = n
}
}
}
func printBootStrapInfo() {
SkipVyosIptables := utils.IsSkipVyosIptables()
utils.Assert(SkipVyosIptables == true, "disable vyos cli has been set, but SkipVyosIptables is disable")
applianceTypeTmp, found := utils.BootstrapInfo["applianceVmSubType"]
if !found {
applianceTypeTmp = "None"
}
applianceType := applianceTypeTmp.(string)
log.Debugf("bootstrapInfo: %+v", utils.BootstrapInfo)
log.Debugf("EnableVyosCli: %+v", utils.IsEnableVyosCmd())
log.Debugf("SkipVyosIptables: %+v", SkipVyosIptables)
log.Debugf("applianceType: %+v", applianceType)
}
func checkNicAddress() {
log.Debugf("[configure: check nic's ip address]")
haStatus := utils.NOHA
if v, ok := utils.BootstrapInfo["haStatus"]; ok {
haStatus = v.(string)
}
if strings.EqualFold(haStatus, utils.NOHA) {
dupinfo := ""
for _, nic := range nicsMap {
if nic.Ip != "" && utils.CheckIpDuplicate(nic.Name, nic.Ip) {
dupinfo = fmt.Sprintf("%s duplicate ip %s in nic %s\n", dupinfo, nic.Ip, nic.Mac)
}
}
if !strings.EqualFold(dupinfo, "") {
log.Error(dupinfo)
err := utils.MkdirForFile(getNetworkHealthStatusPath(), 0755)
utils.PanicOnError(err)
err = ioutil.WriteFile(getNetworkHealthStatusPath(), []byte(dupinfo), 0755)
utils.PanicOnError(err)
}
}
for _, nic := range nicsMap {
utils.Arping(nic.Name, nic.Ip, nic.Gateway)
}
}
func configureHaScript() {
log.Debugf("[configure: write default Ha script]")
for _, nic := range nicsMap {
if nic.IsDefault {
defaultNic := utils.Nic{Name: nic.Name, Mac: nic.Mac, Ip: nic.Ip, Ip6: nic.Ip6,
Gateway: nic.Gateway, Gateway6: nic.Gateway6}
utils.WriteDefaultHaScript(&defaultNic)
}
}
}
func configureSystem() {
if utils.IsVYOS() {
resetVyos()
}
printBootStrapInfo()
configureTimeZone()
parseNicFromBootstrap()
renameNic()
configureMgmtNic()
configurePassword()
configureSshServer()
configureAdditionNic()
configureRadvdServer()
configureSshMonitor()
checkNicAddress()
configureHaScript()
}