| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "math/rand" | |
| "os" | |
| "cos.googlesource.com/cos/tools.git/src/pkg/containers" | |
| "github.com/opencontainers/runtime-spec/specs-go" | |
| ) | |
| func main() { | |
| imageName := envOrDefault("IMAGE_NAME", "gcr.io/cos-cloud/cos-dkms:latest") | |
| containerName := envOrDefault("CONTAINER_NAME", fmt.Sprintf("cos-dkms-%d", rand.Int())) | |
| containerNamespace := envOrDefault("CONTAINER_NAMESPACE", "cos-dkms") | |
| containerdSocket := envOrDefault("CONTAINERD_SOCK", "/run/containerd/containerd.sock") | |
| dirMounts := []specs.Mount{ | |
| { | |
| Source: envOrDefault("HOST_DKMS_TREE", "/var/lib/google/dkms/dkms"), | |
| Destination: envOrDefault("GUEST_DKMS_TREE", "/var/lib/dkms"), | |
| Options: []string{"rw", "rbind"}, | |
| }, | |
| { | |
| Source: envOrDefault("HOST_SOURCE_TREE", "/var/lib/google/dkms/source"), | |
| Destination: envOrDefault("GUEST_SOURCE_TREE", "/usr/src"), | |
| Options: []string{"rw", "rbind"}, | |
| }, | |
| { | |
| Source: envOrDefault("HOST_KERNEL_TREE", "/var/lib/google/dkms/kernel"), | |
| Destination: envOrDefault("GUEST_KERNEL_TREE", "/lib/modules"), | |
| Options: []string{"rw", "rbind"}, | |
| }, | |
| // Mount this so we can use the host's modules as dependencies. | |
| { | |
| Source: envOrDefault("HOST_KERNEL_MODULES_TREE", "/lib/modules"), | |
| Destination: envOrDefault("GUEST_KERNEL_MODULES_TREE", "/host_modules"), | |
| Options: []string{"ro", "rbind"}, | |
| }, | |
| } | |
| fileMounts := []specs.Mount { | |
| // Mount this so we can read OS information. There is sensitive information | |
| // in /etc, so we don't want to mount the full directory. | |
| { | |
| Source: envOrDefault("HOST_LSB_RELEASE_PATH", "/etc/lsb-release"), | |
| Destination: envOrDefault("GUEST_LSB_RELEASE_PATH", "/etc/lsb-release"), | |
| Options: []string{"ro", "bind"}, | |
| }, | |
| } | |
| args := []string{ | |
| "--gcs-bucket=cos-default", | |
| "--kernelmodulestree=/host_modules", | |
| "--logtostderr", | |
| "--download-workers=0", | |
| } | |
| // We prepend the default args first in case a user wants to override them. | |
| args = append(args, os.Args[1:]...) | |
| config := containers.ContainerConfig{ | |
| Name: containerName, | |
| Command: "/usr/bin/cos-dkms", | |
| Args: args, | |
| Cwd: "/", | |
| DirMounts: dirMounts, | |
| FileMounts: fileMounts, | |
| Privileged: true, | |
| NetHost: true, | |
| } | |
| ctx := context.Background() | |
| exitCode, err := containers.Run(ctx, imageName, config, containerNamespace, containerdSocket) | |
| if exitCode != 0 || err != nil { | |
| fmt.Errorf("%v\n", err) | |
| os.Exit(int(exitCode)) | |
| } | |
| } | |
| // envOrDefault returns the value of the environment variable with the given | |
| // name, or a default value if the environment variable is unset. | |
| func envOrDefault(name, defaultValue string) string { | |
| envValue := os.Getenv(name) | |
| if envValue == "" { | |
| return defaultValue | |
| } else { | |
| return envValue | |
| } | |
| } |