github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/cmd/run.go (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "context" 19 20 "github.com/google/subcommands" 21 "golang.org/x/sys/unix" 22 "github.com/SagerNet/gvisor/runsc/config" 23 "github.com/SagerNet/gvisor/runsc/container" 24 "github.com/SagerNet/gvisor/runsc/flag" 25 "github.com/SagerNet/gvisor/runsc/specutils" 26 ) 27 28 // Run implements subcommands.Command for the "run" command. 29 type Run struct { 30 // Run flags are a super-set of those for Create. 31 Create 32 33 // detach indicates that runsc has to start a process and exit without waiting it. 34 detach bool 35 } 36 37 // Name implements subcommands.Command.Name. 38 func (*Run) Name() string { 39 return "run" 40 } 41 42 // Synopsis implements subcommands.Command.Synopsis. 43 func (*Run) Synopsis() string { 44 return "create and run a secure container" 45 } 46 47 // Usage implements subcommands.Command.Usage. 48 func (*Run) Usage() string { 49 return `run [flags] <container id> - create and run a secure container. 50 ` 51 } 52 53 // SetFlags implements subcommands.Command.SetFlags. 54 func (r *Run) SetFlags(f *flag.FlagSet) { 55 f.BoolVar(&r.detach, "detach", false, "detach from the container's process") 56 r.Create.SetFlags(f) 57 } 58 59 // Execute implements subcommands.Command.Execute. 60 func (r *Run) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { 61 if f.NArg() != 1 { 62 f.Usage() 63 return subcommands.ExitUsageError 64 } 65 66 id := f.Arg(0) 67 conf := args[0].(*config.Config) 68 waitStatus := args[1].(*unix.WaitStatus) 69 70 if conf.Rootless { 71 return Errorf("Rootless mode not supported with %q", r.Name()) 72 } 73 74 bundleDir := r.bundleDir 75 if bundleDir == "" { 76 bundleDir = getwdOrDie() 77 } 78 spec, err := specutils.ReadSpec(bundleDir, conf) 79 if err != nil { 80 return Errorf("reading spec: %v", err) 81 } 82 specutils.LogSpec(spec) 83 84 runArgs := container.Args{ 85 ID: id, 86 Spec: spec, 87 BundleDir: bundleDir, 88 ConsoleSocket: r.consoleSocket, 89 PIDFile: r.pidFile, 90 UserLog: r.userLog, 91 Attached: !r.detach, 92 } 93 ws, err := container.Run(conf, runArgs) 94 if err != nil { 95 return Errorf("running container: %v", err) 96 } 97 98 *waitStatus = ws 99 return subcommands.ExitSuccess 100 }