-
Notifications
You must be signed in to change notification settings - Fork 1
/
ethernet.go
252 lines (239 loc) · 5.35 KB
/
ethernet.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/urfave/cli"
)
type EthernetState struct {
Enabled bool `json:"enabled"`
Configg *Ethernet `json:"config"`
}
func EthernetCMD(ctx *cli.Context) error {
if ctx.IsSet(enableFlag) {
return EnableEthernet(ctx)
}
if ctx.IsSet(disableFlag) {
return DisableEthernet(ctx)
}
if ctx.IsSet(removeFlag) {
return RemoveEthernet(ctx)
}
if ctx.IsSet(configFlag) {
return configEthernetCMD(ctx)
}
return nil
}
// Enable ethernet enables ethernet network in the host machine. This relies on
// systemd to be the init system of the host machine.
//
// If the config flag is set, ethernet will be configured before enabling it.
// Ommit the config flag if ethernet is already configured using this tool.
func EnableEthernet(ctx *cli.Context) error {
if ctx.IsSet(configFlag) {
err := configEthernetCMD(ctx)
if err != nil {
return err
}
}
i := getInterface(ctx)
e, err := ethernetState(i)
if err != nil {
return err
}
unit := filepath.Join(networkBase,
fmt.Sprintf(ethernetService, e.Configg.Interface))
_, err = os.Stat(unit)
if os.IsNotExist(err) {
err = CreateSystemdFile(e.Configg, unit, 0644)
if err != nil {
return err
}
}
_, err = exec.Command("ip", "link", "set", "up", e.Configg.Interface).Output()
if err != nil {
return err
}
err = restartService("systemd-networkd")
if err != nil {
return err
}
e.Enabled = true
data, err := json.Marshal(e)
if err != nil {
return err
}
return keepState(
fmt.Sprintf(defaultEthernetConfig, i), data)
}
// gives the current state of the ethernet configuration. This will return an
// error if the system hast been configured yet.
//
// Configuration state files are written in $FCONF_CONFIGDIR directory.
func ethernetState(i string) (*EthernetState, error) {
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
b, err := ioutil.ReadFile(filepath.Join(dir,
fmt.Sprintf(defaultEthernetConfig, i)))
if err != nil {
return nil, err
}
e := &EthernetState{}
err = json.Unmarshal(b, e)
if err != nil {
return nil, err
}
if e.Configg == nil {
return nil, ErrWrongStateFile
}
if e.Configg.Interface == "" {
e.Configg.Interface = "eth0"
}
return e, nil
}
func configEthernetCMD(ctx *cli.Context) error {
base := ctx.String("dir")
name := ctx.String("name")
src := ctx.String("config")
if src == "" {
return errors.New("fconf: missing configuration source file")
}
var b []byte
var err error
if src == "stdin" {
b, err = ReadFromStdin()
if err != nil {
return err
}
} else {
b, err = ioutil.ReadFile(src)
if err != nil {
return err
}
}
e := Ethernet{}
err = json.Unmarshal(b, &e)
if err != nil {
return err
}
err = checkDir(base)
if err != nil {
return err
}
if e.Interface == "" {
e.Interface = "eth0"
}
if strings.Contains(name, "%s") {
name = fmt.Sprintf(name, e.Interface)
}
filename := filepath.Join(base, name)
err = CreateSystemdFile(e, filename, 0644)
if err != nil {
return err
}
setInterface(ctx, e.Interface)
fmt.Printf("successful written ethernet configuration to %s \n", filename)
state := &EthernetState{Configg: &e}
es, err := ethernetState(e.Interface)
if err == nil {
state.Enabled = es.Enabled
}
b, _ = json.Marshal(state)
return keepState(
fmt.Sprintf(defaultEthernetConfig, e.Interface), b)
}
func keepState(filename string, src []byte) error {
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
err := checkDir(dir)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dir, filename), src, 0644)
}
//DisableEthernet disables ethernet temporaly.
func DisableEthernet(ctx *cli.Context) error {
if ctx.IsSet(configFlag) {
fmt.Println("WARN: config flag will be ignored when diable flag is used")
}
i := getInterface(ctx)
if i == "" {
return errors.New("missing interface, you must specify interface")
}
e, err := ethernetState(i)
if err != nil {
return err
}
err = FlushInterface(e.Configg.Interface)
if err != nil {
return fmt.Errorf("ERROR: running ip addr flush dev %s %v",
e.Configg.Interface, err,
)
}
//remove unit file
unit := filepath.Join(networkBase,
fmt.Sprintf(ethernetService, e.Configg.Interface))
err = removeFile(unit)
if err != nil {
return err
}
err = restartService("systemd-networkd")
if err != nil {
return err
}
fmt.Println("successfully disabled ethernet")
e.Enabled = false
data, err := json.Marshal(e)
if err != nil {
return err
}
return keepState(
fmt.Sprintf(defaultEthernetConfig, i), data)
}
//RemoveEthernet removes ethernet service.
func RemoveEthernet(ctx *cli.Context) error {
i := getInterface(ctx)
if i == "" {
return errors.New("missing interface, you must specify interface")
}
e, err := ethernetState(i)
if err != nil {
return err
}
if e.Enabled {
err = DisableEthernet(ctx)
if err != nil {
return err
}
}
// removestate file
stateFile := filepath.Join(stateDir(),
fmt.Sprintf(defaultEthernetConfig, i))
return removeFile(stateFile)
}
func removeFile(name string) error {
fmt.Printf("removing %s ...", name)
err := os.Remove(name)
if err != nil {
fmt.Println(" error")
return err
}
fmt.Println(" done without error")
return nil
}
func stateDir() string {
dir := os.Getenv("FCONF_CONFIGDIR")
if dir == "" {
dir = fconfConfigDir
}
return dir
}