Repository navigation
Expand file tree
/
Copy pathmain_linux.go
More file actions
202 lines (168 loc) · 5.12 KB
/
Copy pathmain_linux.go
File metadata and controls
202 lines (168 loc) · 5.12 KB
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
// 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 (
"os"
"runtime/debug"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/vishvananda/netlink"
"github.com/vmware/vic/lib/tether"
viclog "github.com/vmware/vic/pkg/log"
"github.com/vmware/vic/pkg/logmgr"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/vsphere/extraconfig"
"github.com/vmware/vic/pkg/vsphere/toolbox"
)
var (
tthr tether.Tether
config ExecutorConfig
debugLevel int
)
func init() {
// use the same logger as the log files
trace.Logger = log.StandardLogger()
log.SetFormatter(viclog.NewTextFormatter())
}
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, 0644)
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.Error(err)
return
}
extraconfig.Decode(src, &config)
debugLevel = config.Diagnostics.DebugLevel
if debugLevel > 2 {
enableShell()
}
setLogLevels()
sink, err := extraconfig.GuestInfoSinkWithPrefix("init")
if err != nil {
log.Error(err)
return
}
// 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", logmgr.Daily, maxLogSizeBytes, 2, true)
logrotate.AddLogRotate("/var/log/vic/init.log", logmgr.Daily, maxLogSizeBytes, 2, true)
logrotate.AddLogRotate("/var/log/vic/docker-personality.log", logmgr.Daily, maxLogSizeBytes, 2, true)
logrotate.AddLogRotate("/var/log/vic/vicadmin.log", logmgr.Daily, maxLogSizeBytes, 2, true)
tthr.Register("logrotate", logrotate)
} else {
log.Error(err)
}
err = tthr.Start()
if err != nil {
log.Error(err)
return
}
log.Info("Clean exit from init")
}
func setLogLevels() {
if debugLevel > 0 {
log.SetLevel(log.DebugLevel)
}
}
// exit cleanly shuts down the system
func halt() {
log.Infof("Powering off the system")
if debugLevel > 0 {
log.Info("Squashing power off for debug init")
return
}
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF)
}
func reboot() {
log.Infof("Rebooting the system")
if debugLevel > 0 {
log.Info("Squashing reboot for debug init")
return
}
syscall.Sync()
syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART)
}
func configureToolbox(t *tether.Toolbox) *tether.Toolbox {
vix := t.Service.VixCommand
vix.ProcessStartCommand = startCommand
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()
}