-
Notifications
You must be signed in to change notification settings - Fork 1k
/
runc.go
172 lines (140 loc) · 4.04 KB
/
runc.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
package main
import (
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
const (
runcBinary = "/usr/bin/runc"
logDirBase = "/run/log/"
varLogDir = "/var/log"
)
func dumpFile(w io.Writer, filePath string) error {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(w, f)
return err
}
func runcInit(rootPath, serviceType string) int {
// do nothing if the path does not exist
if _, err := os.Stat(rootPath); err != nil && os.IsNotExist(err) {
return 0
}
// get files; note ReadDir already sorts them
files, err := ioutil.ReadDir(rootPath)
if err != nil {
log.Fatalf("Cannot read files in %s: %v", rootPath, err)
}
tmpdir, err := ioutil.TempDir("", filepath.Base(rootPath))
if err != nil {
log.Fatalf("Cannot create temporary directory: %v", err)
}
// need to set ourselves as a child subreaper or we cannot wait for runc as reparents to init
if err := setSubreaper(1); err != nil {
log.Fatalf("Cannot set as subreaper: %v", err)
}
status := 0
logDir := path.Join(logDirBase, serviceType)
varLogLink := path.Join(varLogDir, serviceType)
if err := os.MkdirAll(logDir, 0755); err != nil {
log.Fatalf("Cannot create log directory %s: %v", logDir, err)
}
logger := GetLog(logDir)
for _, file := range files {
name := file.Name()
path := filepath.Join(rootPath, name)
runtimeConfig := getRuntimeConfig(path)
if err := prepareFilesystem(path, runtimeConfig); err != nil {
log.Printf("Error preparing %s: %v", name, err)
status = 1
continue
}
pidfile := filepath.Join(tmpdir, name)
cmd := exec.Command(runcBinary, "create", "--bundle", path, "--pid-file", pidfile, name)
stdoutLog := serviceType + "." + name + ".out"
stdout, err := logger.Open(stdoutLog)
if err != nil {
log.Printf("Error opening stdout log connection: %v", err)
status = 1
continue
}
defer stdout.Close()
stderrLog := serviceType + "." + name
stderr, err := logger.Open(stderrLog)
if err != nil {
log.Printf("Error opening stderr log connection: %v", err)
status = 1
continue
}
defer stderr.Close()
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
log.Printf("Error creating %s: %v", name, err)
status = 1
// skip cleanup on error for debug
continue
}
pf, err := ioutil.ReadFile(pidfile)
if err != nil {
log.Printf("Cannot read pidfile: %v", err)
status = 1
continue
}
pid, err := strconv.Atoi(string(pf))
if err != nil {
log.Printf("Cannot parse pid from pidfile: %v", err)
status = 1
continue
}
if err := prepareProcess(pid, runtimeConfig); err != nil {
log.Printf("Cannot prepare process: %v", err)
status = 1
continue
}
waitFor := make(chan *os.ProcessState)
go func() {
// never errors in Unix
p, _ := os.FindProcess(pid)
state, err := p.Wait()
if err != nil {
log.Printf("Process wait error: %v", err)
}
waitFor <- state
}()
cmd = exec.Command(runcBinary, "start", name)
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
log.Printf("Error starting %s: %v", name, err)
status = 1
continue
}
_ = <-waitFor
cleanup(path)
_ = os.Remove(pidfile)
// ideally we want to use io.MultiWriter here, sending one stream to stdout/stderr, another to the log
// however, this hangs if we do, due to a runc bug, see https://github.com/opencontainers/runc/issues/1721#issuecomment-366315563
// once that is fixed, this can be cleaned up
logger.Dump(stdoutLog)
logger.Dump(stderrLog)
}
_ = os.RemoveAll(tmpdir)
// make sure the link exists from /var/log/onboot -> /run/log/onboot
logger.Symlink(varLogLink)
return status
}
// setSubreaper copied directly from https://github.com/opencontainers/runc/blob/b23315bdd99c388f5d0dd3616188729c5a97484a/libcontainer/system/linux.go#L88
// to avoid version and vendor conflict issues
func setSubreaper(i int) error {
return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
}