forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_ipv6.go
72 lines (56 loc) · 1.74 KB
/
kernel_ipv6.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
package net
import (
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type KernelIPv6 interface {
Enable(stopCh <-chan struct{}) error
}
type KernelIPv6Impl struct {
fs boshsys.FileSystem
cmdRunner boshsys.CmdRunner
logger boshlog.Logger
}
func NewKernelIPv6Impl(fs boshsys.FileSystem, cmdRunner boshsys.CmdRunner, logger boshlog.Logger) KernelIPv6Impl {
return KernelIPv6Impl{fs: fs, cmdRunner: cmdRunner, logger: logger}
}
func (net KernelIPv6Impl) Enable(stopCh <-chan struct{}) error {
const (
grubConfPath = "/boot/grub/grub.cfg"
grubIPv6DisableOpt = "ipv6.disable=1"
)
grubConf, err := net.fs.ReadFileString(grubConfPath)
if err != nil {
return bosherr.WrapError(err, "Reading grub")
}
if strings.Contains(grubConf, grubIPv6DisableOpt) {
grubConf = strings.Replace(grubConf, grubIPv6DisableOpt, "", -1)
err = net.fs.WriteFileString(grubConfPath, grubConf)
if err != nil {
return bosherr.WrapError(err, "Writing grub.cnf")
}
net.logger.Info("net.KernelIPv6", "Rebooting to enable IPv6 in kernel")
_, _, _, err = net.cmdRunner.RunCommand("shutdown", "-r", "now")
if err != nil {
return bosherr.WrapError(err, "Rebooting for IPv6")
}
// Wait here for the OS to reboot the machine
<-stopCh
return nil
}
ipv6Sysctls := []string{
"net.ipv6.conf.all.accept_ra=1",
"net.ipv6.conf.default.accept_ra=1",
"net.ipv6.conf.all.disable_ipv6=0",
"net.ipv6.conf.default.disable_ipv6=0",
}
for _, sysctl := range ipv6Sysctls {
_, _, _, err := net.cmdRunner.RunCommand("sysctl", sysctl)
if err != nil {
return bosherr.WrapError(err, "Running IPv6 sysctl")
}
}
return nil
}