-
Notifications
You must be signed in to change notification settings - Fork 22
/
files.go
223 lines (185 loc) · 5.41 KB
/
files.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
package sandbox
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/KarpelesLab/reflink"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
)
const (
BuildIDName = "build_id"
RootfsName = "rootfs.ext4"
SnapfileName = "snapfile"
MemfileName = "memfile"
envsDisk = "/mnt/disks/fc-envs/v1"
BuildDirName = "builds"
EnvInstancesDirName = "env-instances"
socketWaitTimeout = 2 * time.Second
)
type SandboxFiles struct {
UFFDSocketPath *string
EnvPath string
BuildDirPath string
EnvInstancePath string
SocketPath string
KernelDirPath string
KernelMountDirPath string
FirecrackerBinaryPath string
UFFDBinaryPath string
}
// waitForSocket waits for the given file to exist
func waitForSocket(socketPath string, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ticker := time.NewTicker(10 * time.Millisecond)
defer func() {
cancel()
ticker.Stop()
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
if _, err := os.Stat(socketPath); err != nil {
continue
}
// TODO: Send test HTTP request to make sure socket is available
return nil
}
}
}
func newSandboxFiles(
ctx context.Context,
tracer trace.Tracer,
slot *IPSlot,
envID,
kernelVersion,
kernelsDir,
kernelMountDir,
kernelName,
firecrackerBinaryPath,
uffdBinaryPath string,
hugePages bool,
) (*SandboxFiles, error) {
childCtx, childSpan := tracer.Start(ctx, "create-env-instance",
trace.WithAttributes(
attribute.String("env.id", envID),
attribute.String("envs_disk", envsDisk),
),
)
defer childSpan.End()
envPath := filepath.Join(envsDisk, envID)
envInstancePath := filepath.Join(envPath, EnvInstancesDirName, slot.InstanceID)
// Mount overlay
buildIDPath := filepath.Join(envPath, BuildIDName)
data, err := os.ReadFile(buildIDPath)
if err != nil {
return nil, fmt.Errorf("failed reading build id for the env %s: %w", envID, err)
}
buildID := string(data)
buildDirPath := filepath.Join(envPath, BuildDirName, buildID)
// Assemble socket path
socketPath, sockErr := getSocketPath(slot.InstanceID)
if sockErr != nil {
errMsg := fmt.Errorf("error getting socket path: %w", sockErr)
telemetry.ReportCriticalError(childCtx, errMsg)
return nil, errMsg
}
// Assemble UFFD socket path
var uffdSocketPath *string
if hugePages {
socketName := fmt.Sprintf("uffd-%s", slot.InstanceID)
socket, sockErr := getSocketPath(socketName)
if sockErr != nil {
errMsg := fmt.Errorf("error getting UFFD socket path: %w", sockErr)
telemetry.ReportCriticalError(childCtx, errMsg)
return nil, errMsg
}
uffdSocketPath = &socket
}
// Create kernel path
kernelPath := filepath.Join(kernelsDir, kernelVersion)
childSpan.SetAttributes(
attribute.String("instance.env_instance_path", envInstancePath),
attribute.String("instance.build.dir_path", buildDirPath),
attribute.String("instance.env_path", envPath),
attribute.String("instance.kernel.mount_path", filepath.Join(kernelMountDir, kernelName)),
attribute.String("instance.kernel.path", filepath.Join(kernelPath, kernelName)),
attribute.String("instance.firecracker.path", firecrackerBinaryPath),
)
return &SandboxFiles{
EnvInstancePath: envInstancePath,
BuildDirPath: buildDirPath,
EnvPath: envPath,
SocketPath: socketPath,
KernelDirPath: kernelPath,
KernelMountDirPath: kernelMountDir,
FirecrackerBinaryPath: firecrackerBinaryPath,
UFFDSocketPath: uffdSocketPath,
UFFDBinaryPath: uffdBinaryPath,
}, nil
}
func (env *SandboxFiles) Ensure(ctx context.Context) error {
err := os.MkdirAll(env.EnvInstancePath, 0o777)
if err != nil {
telemetry.ReportError(ctx, err)
}
mkdirErr := os.MkdirAll(env.BuildDirPath, 0o777)
if mkdirErr != nil {
telemetry.ReportError(ctx, err)
}
err = reflink.Always(
filepath.Join(env.EnvPath, RootfsName),
filepath.Join(env.EnvInstancePath, RootfsName),
)
if err != nil {
errMsg := fmt.Errorf("error creating reflinked rootfs: %w", err)
telemetry.ReportCriticalError(ctx, errMsg)
return errMsg
}
return nil
}
func (env *SandboxFiles) Cleanup(
ctx context.Context,
tracer trace.Tracer,
) error {
childCtx, childSpan := tracer.Start(ctx, "cleanup-env-instance",
trace.WithAttributes(
attribute.String("instance.env_instance_path", env.EnvInstancePath),
attribute.String("instance.build_dir_path", env.BuildDirPath),
attribute.String("instance.env_path", env.EnvPath),
),
)
defer childSpan.End()
err := os.RemoveAll(env.EnvInstancePath)
if err != nil {
errMsg := fmt.Errorf("error deleting env instance files: %w", err)
telemetry.ReportCriticalError(childCtx, errMsg)
} else {
// TODO: Check the socket?
telemetry.ReportEvent(childCtx, "removed all env instance files")
}
// Remove socket
err = os.Remove(env.SocketPath)
if err != nil {
errMsg := fmt.Errorf("error deleting socket: %w", err)
telemetry.ReportCriticalError(childCtx, errMsg)
} else {
telemetry.ReportEvent(childCtx, "removed socket")
}
// Remove UFFD socket
if env.UFFDSocketPath != nil {
err = os.Remove(*env.UFFDSocketPath)
if err != nil {
errMsg := fmt.Errorf("error deleting socket for UFFD: %w", err)
telemetry.ReportCriticalError(childCtx, errMsg)
} else {
telemetry.ReportEvent(childCtx, "removed UFFD socket")
}
}
return nil
}