-
Notifications
You must be signed in to change notification settings - Fork 8
/
nspawn.go
172 lines (139 loc) · 3.13 KB
/
nspawn.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 (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"syscall"
"github.com/reconquest/executil-go"
"github.com/reconquest/ser-go"
)
func nspawn(
storageEngine storage,
containerName string,
bridge string,
networkAddress string, bridgeAddress string,
ephemeral bool, keepFailed bool, quiet bool,
commandLine []string,
) (err error) {
defer storageEngine.DeInitContainer(containerName)
if err != nil {
return ser.Errorf(
err,
"storage can't create rootfs for nspawn",
)
}
if err != nil {
return ser.Errorf(
err,
"can't setup overlayfs for '%s'", containerName,
)
}
if ephemeral {
defer func() {
if err != nil && keepFailed {
return
}
removeErr := storageEngine.DestroyContainer(containerName)
if removeErr != nil {
err = removeErr
fmt.Fprintln(
os.Stderr,
ser.Errorf(
err, "can't remove container '%s'", containerName,
).HierarchicalError(),
)
}
}()
}
bootstrapper := "/.hastur.exec"
err = installBootstrapExecutable(
storageEngine.GetContainerRoot(containerName),
bootstrapper,
)
if err != nil {
return err
}
controlPipeName := bootstrapper + ".control"
controlPipePath := filepath.Join(
storageEngine.GetContainerRoot(containerName),
controlPipeName,
)
err = syscall.Mknod(controlPipePath, syscall.S_IFIFO|0644, 0)
if err != nil {
return ser.Errorf(
err,
"can't create control pipe for bootstrapper",
)
}
defer os.Remove(controlPipePath)
// we ignore error there because interface may not exist
_ = umountNetorkNamespace(containerName)
_ = cleanupNetworkInterface(containerName)
defer cleanupNetworkInterface(containerName)
err = addPostroutingMasquarading(bridge)
if err != nil {
return ser.Errorf(
err,
"can't add masquarading rules on the '%s'",
bridge,
)
}
defer removePostroutingMasquarading(bridge)
command := exec.Command(
"systemd-machine-id-setup",
"--root", storageEngine.GetContainerRoot(containerName),
)
_, _, err = executil.Run(command)
if err != nil {
return err
}
args := []string{
"--pipe",
"-M", containerName + containerSuffix,
"-D", storageEngine.GetContainerRoot(containerName),
}
args = append(args, "-n", "--network-bridge", bridge)
if quiet {
args = append(args, "-q")
}
args = append(args, bootstrapper, controlPipeName)
command = exec.Command(
"systemd-nspawn",
append(args, commandLine...)...,
)
command.Env = []string{}
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err = command.Start()
if err != nil {
return err
}
defer command.Process.Kill()
_, err = ioutil.ReadFile(controlPipePath)
if err != nil {
return err
}
pid, err := getContainerLeaderPID(containerName)
if err != nil {
return err
}
err = mountNetworkNamespace(pid, containerName)
if err != nil {
return err
}
defer umountNetorkNamespace(containerName)
err = setupNetwork(containerName, networkAddress, bridgeAddress)
if err != nil {
return err
}
err = ioutil.WriteFile(controlPipePath, []byte{}, 0)
if err != nil {
return ser.Errorf(
err, "can't write to control pipe")
}
err = command.Wait()
return err
}