-
Notifications
You must be signed in to change notification settings - Fork 81
/
main.go
457 lines (446 loc) · 12.3 KB
/
main.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"syscall"
"github.com/containerd/containerd/archive"
"github.com/containerd/containerd/archive/compression"
ctdcontainers "github.com/containerd/containerd/containers"
"github.com/containerd/containerd/images"
ctdnamespaces "github.com/containerd/containerd/namespaces"
ctdoci "github.com/containerd/containerd/oci"
"github.com/containerd/containerd/platforms"
inittype "github.com/ktock/container2wasm/cmd/init/types"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
spec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/runc/libcontainer/user"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
const (
// runtimeRootfsPath is the rootfs path in the VM used by runc
runtimeRootfsPath = "/run/rootfs"
// runtimeBundlePath is the OCI filesystem bundle path in the VM used by runc
runtimeBundlePath = "/run/bundle"
)
func main() {
var (
debug = flag.Bool("debug", false, "enable debug print on boot")
debugInit = flag.Bool("debug-init", false, "enable debug print during init")
imageConfigPath = flag.String("image-config-path", "/oci/image.json", "path to image config used by init during runtime")
runtimeConfigPath = flag.String("runtime-config-path", "/oci/spec.json", "path to runtime spec config used by init during runtime")
imageRootfsPath = flag.String("rootfs-path", "/oci/rootfs", "path to rootfs used as overlayfs lowerdir of container rootfs")
noVmtouch = flag.Bool("no-vmtouch", false, "do not perform vmtouch")
)
flag.Parse()
args := flag.Args()
imgDir := args[0]
platform := args[1]
rootfs := args[2]
p, err := platforms.Parse(platform)
if err != nil {
panic(err)
}
cfg, err := unpack(context.TODO(), imgDir, &p, rootfs)
if err != nil {
panic(err)
}
cfgD, err := io.ReadAll(cfg)
if err != nil {
panic(err)
}
if err := os.WriteFile("image.json", cfgD, 0600); err != nil {
panic(err)
}
if err := createSpec(bytes.NewReader(cfgD), rootfs, *debug, *debugInit, *imageConfigPath, *runtimeConfigPath, *imageRootfsPath, *noVmtouch); err != nil {
panic(err)
}
}
func unpack(ctx context.Context, imgDir string, platform *spec.Platform, rootfs string) (io.Reader, error) {
fmt.Println("Trying to unpack image as an OCI image")
if rootfs == "" {
return nil, fmt.Errorf("specify rootfs")
}
idxR, err := os.Open(filepath.Join(imgDir, "index.json"))
if err != nil {
fmt.Println("Failed to unpack the image as an OCI image:", err)
return unpackDocker(ctx, imgDir, platform, rootfs)
}
var idx ocispec.Index
if err := json.NewDecoder(idxR).Decode(&idx); err != nil {
return nil, err
}
var platformMC platforms.MatchComparer
if platformMC != nil {
platformMC = platforms.Only(*platform)
}
for _, desc := range idx.Manifests {
switch desc.MediaType {
case ocispec.MediaTypeImageManifest, images.MediaTypeDockerSchema2Manifest:
if desc.Platform != nil && platformMC != nil && !platformMC.Match(*desc.Platform) {
continue
}
mfstD, err := os.ReadFile(filepath.Join(imgDir, "/blobs/sha256", desc.Digest.Encoded()))
if err != nil {
return nil, err
}
var manifest ocispec.Manifest
if err := json.Unmarshal(mfstD, &manifest); err != nil {
return nil, err
}
configD, err := os.ReadFile(filepath.Join(imgDir, "/blobs/sha256", manifest.Config.Digest.Encoded()))
if err != nil {
return nil, err
}
var image ocispec.Image
if err := json.Unmarshal(configD, &image); err != nil {
return nil, err
}
if platformMC != nil && !platformMC.Match(platforms.Normalize(ocispec.Platform{OS: image.OS, Architecture: image.Architecture})) {
continue
}
for _, layerDesc := range manifest.Layers {
if err := func() error {
layerR, err := os.Open(filepath.Join(imgDir, "/blobs/sha256", layerDesc.Digest.Encoded()))
if err != nil {
return err
}
defer layerR.Close()
r, err := compression.DecompressStream(layerR)
if err != nil {
return err
}
var opts []archive.ApplyOpt
if os.Getenv("_NO_SAME_OWNER") == "1" {
opts = append(opts, archive.WithNoSameOwner())
}
if _, err := archive.Apply(ctx, rootfs, r, opts...); err != nil {
return err
}
return nil
}(); err != nil {
return nil, err
}
}
return bytes.NewReader(configD), nil
default:
// TODO: support nested manifest lists?
return nil, fmt.Errorf("unsupported mediatype %v", desc.MediaType)
}
}
return nil, fmt.Errorf("target config not found")
}
func unpackDocker(ctx context.Context, imgDir string, platform *spec.Platform, rootfs string) (io.Reader, error) {
fmt.Println("Trying to unpack image as a docker image")
if rootfs == "" {
return nil, fmt.Errorf("specify rootfs")
}
mfstsR, err := os.Open(filepath.Join(imgDir, "manifest.json"))
if err != nil {
return nil, fmt.Errorf("unrecognized image format")
}
var mfsts []struct {
Config string
Layers []string
}
if err := json.NewDecoder(mfstsR).Decode(&mfsts); err != nil {
return nil, err
}
fmt.Printf("%+v\n", mfsts)
var platformMC platforms.MatchComparer
if platformMC != nil {
platformMC = platforms.Only(*platform)
}
for _, mfst := range mfsts {
configD, err := os.ReadFile(filepath.Join(imgDir, mfst.Config))
if err != nil {
return nil, err
}
var image ocispec.Image
if err := json.Unmarshal(configD, &image); err != nil {
return nil, err
}
if platformMC != nil && !platformMC.Match(platforms.Normalize(ocispec.Platform{OS: image.OS, Architecture: image.Architecture})) {
// TODO: allow unspecified platform?
continue
}
for _, l := range mfst.Layers {
if err := func() error {
layerR, err := os.Open(filepath.Join(imgDir, l))
if err != nil {
return err
}
defer layerR.Close()
r, err := compression.DecompressStream(layerR)
if err != nil {
return err
}
var opts []archive.ApplyOpt
if os.Getenv("_NO_SAME_OWNER") == "1" {
opts = append(opts, archive.WithNoSameOwner())
}
if _, err := archive.Apply(ctx, rootfs, r, opts...); err != nil {
return err
}
return nil
}(); err != nil {
return nil, err
}
}
return bytes.NewReader(configD), nil
}
return nil, fmt.Errorf("target config not found")
}
func createSpec(r io.Reader, rootfs string, debug bool, debugInit bool, imageConfigPath, runtimeConfigPath, imageRootfsPath string, noVmtouch bool) error {
if rootfs == "" {
return fmt.Errorf("rootfs path must be specified")
}
var config spec.Image
if err := json.NewDecoder(r).Decode(&config); err != nil {
return err
}
s, err := generateSpec(config, rootfs)
if err != nil {
return err
}
bootConfig, err := generateBootConfig(config, debug, debugInit, imageConfigPath, runtimeConfigPath, imageRootfsPath, noVmtouch)
if err != nil {
return err
}
sd, err := json.Marshal(s)
if err != nil {
return err
}
if err := os.WriteFile("spec.json", sd, 0600); err != nil {
return err
}
bd, err := json.Marshal(bootConfig)
if err != nil {
return err
}
if err := os.WriteFile("initconfig.json", bd, 0600); err != nil {
return err
}
return nil
}
func generateSpec(config spec.Image, rootfs string) (_ *specs.Spec, err error) {
ic := config.Config
ctdCtx := ctdnamespaces.WithNamespace(context.TODO(), "default")
p := "linux/riscv64"
if config.Architecture == "amd64" {
p = "linux/amd64"
}
s, err := ctdoci.GenerateSpecWithPlatform(ctdCtx, nil, p, &ctdcontainers.Container{})
if err != nil {
return nil, fmt.Errorf("failed to generate spec: %w", err)
}
if username := ic.User; username != "" {
passwdPath, err := user.GetPasswdPath()
if err != nil {
return nil, fmt.Errorf("failed to get passwd file path: %w", err)
}
groupPath, err := user.GetGroupPath()
if err != nil {
return nil, fmt.Errorf("failed to get group file path: %w", err)
}
passwdR, err := os.Open(filepath.Join(rootfs, passwdPath))
if err != nil {
return nil, err
}
defer passwdR.Close()
groupR, err := os.Open(filepath.Join(rootfs, groupPath))
if err != nil {
return nil, err
}
defer groupR.Close()
execUser, err := user.GetExecUser(username, nil, passwdR, groupR)
if err != nil {
return nil, fmt.Errorf("failed to resolve username %q: %w", username, err)
}
s.Process.User.UID = uint32(execUser.Uid)
s.Process.User.GID = uint32(execUser.Gid)
for _, g := range execUser.Sgids {
s.Process.User.AdditionalGids = append(s.Process.User.AdditionalGids, uint32(g))
}
}
s.Process.Env = ic.Env
args := ic.Entrypoint
if len(ic.Cmd) > 0 {
args = append(args, ic.Cmd...)
}
if len(args) > 0 {
s.Process.Args = args
}
if ic.WorkingDir != "" {
s.Process.Cwd = ic.WorkingDir
}
s.Process.Terminal = true // TODO: make it configurable
// TODO: support seccomp and apparmor
s.Linux.Seccomp = nil
s.Root = &specs.Root{
Path: runtimeRootfsPath,
}
// TODO: ports
return s, nil
}
func generateBootConfig(config spec.Image, debug, debugInit bool, imageConfigPath, runtimeConfigPath, imageRootfsPath string, noVmtouch bool) (*inittype.BootConfig, error) {
runcArgs := []string{"-b", runtimeBundlePath, "foo"}
if debug {
runcArgs = append([]string{"--debug"}, runcArgs...)
}
var cmdPreRun [][]string
if !noVmtouch {
cmdPreRun = [][]string{
[]string{"vmtouch", "-tv", "/sbin/runc", "/sbin/init", "/sbin/start-runc"},
}
}
bootConfig := &inittype.BootConfig{
Debug: debug,
DebugInit: debugInit,
Cmd: [][]string{
append([]string{"/sbin/start-runc"}, runcArgs...),
},
CmdPreRun: cmdPreRun,
Container: inittype.ContainerInfo{
BundlePath: runtimeBundlePath,
ImageConfigPath: imageConfigPath,
RuntimeConfigPath: runtimeConfigPath,
},
Mounts: []inittype.MountInfo{
{
FSType: "proc",
Src: "proc",
Dst: "/proc",
},
{
FSType: "tmpfs",
Src: "tmpfs",
Dst: "/run",
},
{
FSType: "tmpfs",
Src: "tmpfs",
Dst: "/sys",
},
{
FSType: "tmpfs",
Src: "tmpfs",
Dst: "/tmp",
},
{
FSType: "tmpfs",
Src: "tmpfs",
Dst: "/var",
},
{
FSType: "tmpfs",
Src: "tmpfs",
Dst: "/mnt",
},
{
FSType: "cgroup2",
Src: "none",
Dst: "/sys/fs/cgroup",
Dir: []inittype.DirInfo{
{
Path: "/sys/fs/cgroup",
Mode: 0666, // TODO: better mode
},
},
},
{
// FSType: "bind",
Src: "/run/etc/hosts",
Dst: "/etc/hosts",
Data: "bind",
Flags: syscall.MS_BIND,
Dir: []inittype.DirInfo{
{
Path: "/run/etc",
Mode: 0666, // TODO: better mode
},
},
File: []inittype.FileInfo{
{
Path: "/run/etc/hosts",
Mode: 0666,
Contents: "127.0.0.1 localhost\n",
},
},
},
{
// FSType: "bind",
Src: "/run/etc/resolv.conf",
Dst: "/etc/resolv.conf",
Data: "bind",
Flags: syscall.MS_BIND,
File: []inittype.FileInfo{
{
Path: "/run/etc/resolv.conf",
Mode: 0666,
Contents: "",
},
},
},
{
FSType: "tmpfs",
Src: "tmpfs",
Dst: runtimeBundlePath,
Dir: []inittype.DirInfo{
{
Path: runtimeBundlePath,
Mode: 0666,
},
},
},
{
FSType: "overlay",
Src: "overlay",
Data: fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", imageRootfsPath, "/run/rootfs-upper", "/run/rootfs-work"),
Dst: runtimeRootfsPath,
Dir: []inittype.DirInfo{
{
Path: runtimeRootfsPath,
Mode: 0666,
},
{
Path: "/run/rootfs-upper",
Mode: 0666,
},
{
Path: "/run/rootfs-work",
Mode: 0666,
},
},
},
},
}
if arch := config.Architecture; arch != "riscv64" && arch != "amd64" {
procfsPos, found := 0, false
for i, m := range bootConfig.Mounts {
if m.FSType == "proc" {
procfsPos, found = i, true
break
}
}
if !found {
return nil, fmt.Errorf("binfmt_misc configuration failed: procfs must be mounted")
}
var newMountInfo []inittype.MountInfo
newMountInfo = append(newMountInfo, bootConfig.Mounts[:procfsPos+1]...)
newMountInfo = append(newMountInfo, inittype.MountInfo{
FSType: "binfmt_misc",
Src: "binfmt_misc",
Dst: "/proc/sys/fs/binfmt_misc",
Cmd: []string{"binfmt", "--install", arch},
})
newMountInfo = append(newMountInfo, bootConfig.Mounts[procfsPos+1:]...)
bootConfig.Mounts = newMountInfo
}
return bootConfig, nil
}