github.com/demonoid81/containerd@v1.3.4/cmd/ctr/commands/run/run_windows.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package run 18 19 import ( 20 gocontext "context" 21 22 "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" 23 "github.com/containerd/console" 24 "github.com/containerd/containerd" 25 "github.com/containerd/containerd/cmd/ctr/commands" 26 "github.com/containerd/containerd/oci" 27 specs "github.com/opencontainers/runtime-spec/specs-go" 28 "github.com/sirupsen/logrus" 29 "github.com/urfave/cli" 30 ) 31 32 var platformRunFlags = []cli.Flag{ 33 cli.BoolFlag{ 34 Name: "isolated", 35 Usage: "run the container with vm isolation", 36 }, 37 } 38 39 // NewContainer creates a new container 40 func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) { 41 var ( 42 id string 43 opts []oci.SpecOpts 44 cOpts []containerd.NewContainerOpts 45 spec containerd.NewContainerOpts 46 47 config = context.IsSet("config") 48 ) 49 50 if config { 51 id = context.Args().First() 52 opts = append(opts, oci.WithSpecFromFile(context.String("config"))) 53 } else { 54 var ( 55 ref = context.Args().First() 56 args = context.Args()[2:] 57 ) 58 59 id = context.Args().Get(1) 60 snapshotter := context.String("snapshotter") 61 if snapshotter == "windows-lcow" { 62 opts = append(opts, oci.WithDefaultSpecForPlatform("linux/amd64")) 63 // Clear the rootfs section. 64 opts = append(opts, oci.WithRootFSPath("")) 65 } else { 66 opts = append(opts, oci.WithDefaultSpec()) 67 opts = append(opts, oci.WithWindowNetworksAllowUnqualifiedDNSQuery()) 68 opts = append(opts, oci.WithWindowsIgnoreFlushesDuringBoot()) 69 } 70 if ef := context.String("env-file"); ef != "" { 71 opts = append(opts, oci.WithEnvFile(ef)) 72 } 73 opts = append(opts, oci.WithEnv(context.StringSlice("env"))) 74 opts = append(opts, withMounts(context)) 75 76 image, err := client.GetImage(ctx, ref) 77 if err != nil { 78 return nil, err 79 } 80 unpacked, err := image.IsUnpacked(ctx, snapshotter) 81 if err != nil { 82 return nil, err 83 } 84 if !unpacked { 85 if err := image.Unpack(ctx, snapshotter); err != nil { 86 return nil, err 87 } 88 } 89 opts = append(opts, oci.WithImageConfig(image)) 90 cOpts = append(cOpts, containerd.WithImage(image)) 91 cOpts = append(cOpts, containerd.WithSnapshotter(snapshotter)) 92 cOpts = append(cOpts, containerd.WithNewSnapshot(id, image)) 93 94 if len(args) > 0 { 95 opts = append(opts, oci.WithProcessArgs(args...)) 96 } 97 if cwd := context.String("cwd"); cwd != "" { 98 opts = append(opts, oci.WithProcessCwd(cwd)) 99 } 100 if context.Bool("tty") { 101 opts = append(opts, oci.WithTTY) 102 103 con := console.Current() 104 size, err := con.Size() 105 if err != nil { 106 logrus.WithError(err).Error("console size") 107 } 108 opts = append(opts, oci.WithTTYSize(int(size.Width), int(size.Height))) 109 } 110 if context.Bool("isolated") { 111 opts = append(opts, oci.WithWindowsHyperV) 112 } 113 limit := context.Uint64("memory-limit") 114 if limit != 0 { 115 opts = append(opts, oci.WithMemoryLimit(limit)) 116 } 117 ccount := context.Uint64("cpu-count") 118 if ccount != 0 { 119 opts = append(opts, oci.WithWindowsCPUCount(ccount)) 120 } 121 } 122 123 cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(context.StringSlice("label")))) 124 runtime := context.String("runtime") 125 var runtimeOpts interface{} 126 if runtime == "io.containerd.runhcs.v1" { 127 runtimeOpts = &options.Options{ 128 Debug: context.GlobalBool("debug"), 129 } 130 } 131 cOpts = append(cOpts, containerd.WithRuntime(runtime, runtimeOpts)) 132 133 var s specs.Spec 134 spec = containerd.WithSpec(&s, opts...) 135 136 cOpts = append(cOpts, spec) 137 138 return client.NewContainer(ctx, id, cOpts...) 139 } 140 141 func getNewTaskOpts(_ *cli.Context) []containerd.NewTaskOpts { 142 return nil 143 }