| 1 | package containerenv |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/host" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | const dockerInit = `set -eu |
| 20 | umount /sys/fs/cgroup |
| 21 | mount -t cgroup2 none /sys/fs/cgroup |
| 22 | exec /sbin/init |
| 23 | ` |
| 24 | |
| 25 | func (r *Runtime) createArguments() []string { |
| 26 | args := []string{"create", "--name", r.Name(), "--label", profileLabel + "=" + r.identity, "--network", "none", "--cgroupns", "private", "--cgroup-parent", r.resourceSlice(), "--ipc", "private", "--hostname", "cage", "--add-host", "cage:127.0.1.1", "--cpus", strconv.Itoa(r.config.CPUs), "--memory", strconv.FormatInt(r.config.MemoryBytes, 10), "--pids-limit", strconv.Itoa(PIDLimit), "--device", "/dev/fuse", "--device", "/dev/net/tun", "--cap-add", "SYS_ADMIN", "--cap-add", "NET_ADMIN", "--tmpfs", "/run", "--tmpfs", "/tmp", "--stop-signal", "SIGRTMIN+3", "--stop-timeout", "30"} |
| 27 | if r.config.Engine == "podman" { |
| 28 | return append(args, "--http-proxy=false", "--systemd", "always", "--pull", "never", "--entrypoint", "/sbin/init", r.config.Image) |
| 29 | } |
| 30 | return append(args, "--pull", "never", "--entrypoint", "/bin/sh", r.config.Image, "-c", dockerInit) |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | func (r *Runtime) Prepare(ctx context.Context, log io.Writer) error { |
| 38 | if err := host.EnsurePrivateDir(r.InstanceDir()); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | unlock, err := lockRuntime(ctx, filepath.Join(r.InstanceDir(), "lifecycle.lock")) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | defer unlock() |
| 46 | rootless, err := r.rootlessEngine(ctx) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | if !rootless { |
| 51 | return errors.New("Cage 只允许使用独立 rootless 容器引擎") |
| 52 | } |
| 53 | if err := r.ensureResourceSlice(ctx); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | container, err := r.inspectContainer(ctx) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | if container != nil { |
| 61 | if err := checkContainerConfiguration(container); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | if container.HostConfig.CgroupParent != r.resourceSlice() { |
| 65 | return errors.New("已有容器缺少宿主资源边界;未重建或删除原数据") |
| 66 | } |
| 67 | return r.recordStorage(ctx, container.ID) |
| 68 | } |
| 69 | for _, device := range []string{"/dev/fuse", "/dev/net/tun"} { |
| 70 | info, err := os.Stat(device) |
| 71 | if err != nil || info.Mode()&os.ModeDevice == 0 { |
| 72 | return fmt.Errorf("服务器缺少容器所需设备 %s", device) |
| 73 | } |
| 74 | } |
| 75 | image, err := r.output(ctx, "image", "inspect", "--format", "{{.Id}}", r.config.Image) |
| 76 | if err != nil { |
| 77 | return errors.New("指定的固定容器基础镜像尚未安装,请先完成服务器部署") |
| 78 | } |
| 79 | if !imageIDPattern.MatchString(strings.TrimSpace(string(image))) { |
| 80 | return errors.New("容器基础镜像没有有效 SHA-256 标识") |
| 81 | } |
| 82 | if _, err := r.output(ctx, r.createArguments()...); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | container, err = r.inspectContainer(ctx) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | if container == nil { |
| 90 | return errors.New("容器创建后未能验证身份") |
| 91 | } |
| 92 | if err := checkContainerConfiguration(container); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | if err := r.recordStorage(ctx, container.ID); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | if log != nil { |
| 99 | fmt.Fprintln(log, "Cage container prepared with a persistent filesystem and no default network.") |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | func (r *Runtime) recordStorage(ctx context.Context, containerID string) error { |
| 105 | path, err := r.StoragePath(ctx) |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | return writePrivateJSON(filepath.Join(r.InstanceDir(), "storage.json"), map[string]string{"engine": r.config.Engine, "container_id": containerID, "storage_path": path}) |
| 110 | } |