-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathmain_linux.go
284 lines (234 loc) · 7.42 KB
/
main_linux.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
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"errors"
"os"
"os/signal"
"runtime/debug"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/vmware/govmomi/toolbox"
"github.com/vmware/vic/lib/tether"
"github.com/vmware/vic/lib/tether/shared"
viclog "github.com/vmware/vic/pkg/log"
"github.com/vmware/vic/pkg/log/syslog"
"github.com/vmware/vic/pkg/logmgr"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
)
var (
tthr tether.Tether
config ExecutorConfig
debugLevel int
)
func main() {
defer func() {
if r := recover(); r != nil {
log.Errorf("run time panic: %s : %s", r, debug.Stack())
}
reboot()
}()
// ensure that panics and error output are persisted
logFile, err := os.OpenFile("/dev/ttyS0", os.O_WRONLY|os.O_SYNC, 0)
if err != nil {
log.Errorf("Could not redirect outputs to serial for debugging info, some debug info may be lost! Error reported was %s", err)
}
err = syscall.Dup3(int(logFile.Fd()), int(os.Stderr.Fd()), 0)
if err != nil {
log.Errorf("Could not pipe standard error to logfile: %s", err)
}
_, err = os.Stderr.WriteString("all stderr redirected to debug log")
if err != nil {
log.Errorf("Could not write to Stderr due to error %s", err)
}
err = syscall.Dup3(int(logFile.Fd()), int(os.Stdout.Fd()), 0)
if err != nil {
log.Errorf("Could not pipe standard out to logfile: %s", err)
}
_, err = os.Stderr.WriteString("all stdout redirected to debug log")
if err != nil {
log.Errorf("Could not write to stdout due to error %s", err)
}
src, err := extraconfig.GuestInfoSourceWithPrefix("init")
if err != nil {
log.Fatal(err)
}
extraconfig.Decode(src, &config)
debugLevel = config.Diagnostics.DebugLevel
startSignalHandler()
logcfg := viclog.NewLoggingConfig()
if debugLevel > 0 {
logcfg.Level = log.DebugLevel
trace.Logger.Level = log.DebugLevel
syslog.Logger.Level = log.DebugLevel
}
if config.Diagnostics.SysLogConfig != nil {
logcfg.Syslog = &viclog.SyslogConfig{
Network: config.Diagnostics.SysLogConfig.Network,
RAddr: config.Diagnostics.SysLogConfig.RAddr,
Priority: syslog.Info | syslog.Daemon,
}
}
viclog.Init(logcfg)
if debugLevel > 2 {
enableShell()
}
sink, err := extraconfig.GuestInfoSinkWithPrefix("init")
if err != nil {
log.Fatal(err)
}
// create the tether
tthr = tether.New(src, sink, &operations{})
// register the toolbox extension and configure for appliance
toolbox := configureToolbox(tether.NewToolbox())
toolbox.PrimaryIP = externalIP
tthr.Register("Toolbox", toolbox)
// Check logs every 5 minutes and rotate them if their size exceeds 20MB.
// The history size we keep is 2 previous files in a compressed form.
// TODO: Check available memory to tune log size and history length for log files.
logrotate, err := logmgr.NewLogManager(time.Second * 300)
const maxLogSizeBytes = 20 * 1024 * 1024
if err == nil {
logrotate.AddLogRotate("/var/log/vic/port-layer.log", maxLogSizeBytes, 2, true)
logrotate.AddLogRotate("/var/log/vic/init.log", maxLogSizeBytes, 2, true)
logrotate.AddLogRotate("/var/log/vic/docker-personality.log", maxLogSizeBytes, 2, true)
logrotate.AddLogRotate("/var/log/vic/vicadmin.log", maxLogSizeBytes, 2, true)
tthr.Register("logrotate", logrotate)
} else {
log.Error(err)
}
err = tthr.Start()
if err != nil {
log.Fatal(err)
}
log.Info("Clean exit from init")
}
// exitTether signals the current process, which triggers tether.Stop and the killing of its children.
// NOTE: I don't like having this here and it really needs to be moved into an interface that
// can be provided to toolbox for system callbacks. While this could be part of the Operations
// interface I think I'd rather have a separate one specifically for the possible toolbox interactions.
func exitTether() error {
defer trace.End(trace.Begin(""))
p, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
if err = p.Signal(syscall.SIGUSR2); err != nil {
return err
}
return err
}
// exit cleanly shuts down the system
func halt() error {
log.Infof("Powering off the system")
err := exitTether()
if err != nil {
log.Warn(err)
}
if debugLevel > 2 {
log.Info("Squashing power off for debug init")
return errors.New("debug config suppresses shutdown")
}
timeout, cancel := context.WithTimeout(context.Background(), shared.GuestShutdownTimeout)
err = tthr.Wait(timeout)
cancel()
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
return err
}
func reboot() error {
log.Infof("Rebooting the system")
err := exitTether()
if err != nil {
log.Warn(err)
}
if debugLevel > 2 {
log.Info("Squashing reboot for debug init")
return errors.New("debug config suppresses reboot")
}
timeout, cancel := context.WithTimeout(context.Background(), shared.GuestRebootTimeout)
err = tthr.Wait(timeout)
cancel()
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
return err
}
func configureToolbox(t *tether.Toolbox) *tether.Toolbox {
cmd := t.Service.Command
cmd.ProcessStartCommand = startCommand
t.Power.Halt.Handler = halt
t.Power.Reboot.Handler = reboot
t.Power.Suspend.Handler = exitTether
return t
}
// externalIP attempts to find an external IP to be reported as the guest IP
func externalIP() string {
l, err := netlink.LinkByName("client")
if err != nil {
log.Debugf("error looking up client interface by name: %s", err)
l, err = netlink.LinkByAlias("client")
if err != nil {
log.Errorf("error looking up client interface by alias: %s", err)
return ""
}
}
addrs, err := netlink.AddrList(l, netlink.FAMILY_V4)
if err != nil {
log.Errorf("error getting address list for client interface: %s", err)
return ""
}
if len(addrs) == 0 {
log.Warnf("no addresses set on client interface")
return ""
}
return addrs[0].IP.String()
}
// defaultIP tries externalIP, falling back to toolbox.DefaultIP()
func defaultIP() string {
ip := externalIP()
if ip != "" {
return ip
}
return toolbox.DefaultIP()
}
// This code is mirrored in cmd/tether/main_linux.go and should be de-duped
func startSignalHandler() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGPWR, syscall.SIGTERM, syscall.SIGINT)
go func() {
for s := range sigs {
switch s {
case syscall.SIGHUP:
log.Infof("Reloading tether configuration")
tthr.Reload()
case syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGPWR:
log.Infof("Stopping tether via signal %s", s.String())
tthr.Stop()
return
case syscall.SIGTERM, syscall.SIGINT:
log.Infof("Stopping system in lieu of restart handling via signal %s", s.String())
// TODO: update this to adjust power off handling for reboot
// this should be in guest reboot rather than power cycle
tthr.Stop()
return
default:
log.Infof("%s signal not defined", s.String())
}
}
}()
}