github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/cmd/start.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 "github.com/SagerNet/gvisor/runsc/config" 22 "github.com/SagerNet/gvisor/runsc/container" 23 "github.com/SagerNet/gvisor/runsc/flag" 24 "github.com/SagerNet/gvisor/runsc/specutils" 25 ) 26 27 // Start implements subcommands.Command for the "start" command. 28 type Start struct{} 29 30 // Name implements subcommands.Command.Name. 31 func (*Start) Name() string { 32 return "start" 33 } 34 35 // Synopsis implements subcommands.Command.Synopsis. 36 func (*Start) Synopsis() string { 37 return "start a secure container" 38 } 39 40 // Usage implements subcommands.Command.Usage. 41 func (*Start) Usage() string { 42 return `start <container id> - start a secure container.` 43 } 44 45 // SetFlags implements subcommands.Command.SetFlags. 46 func (*Start) SetFlags(f *flag.FlagSet) {} 47 48 // Execute implements subcommands.Command.Execute. 49 func (*Start) Execute(_ context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus { 50 if f.NArg() != 1 { 51 f.Usage() 52 return subcommands.ExitUsageError 53 } 54 55 id := f.Arg(0) 56 conf := args[0].(*config.Config) 57 58 c, err := container.Load(conf.RootDir, container.FullID{ContainerID: id}, container.LoadOpts{}) 59 if err != nil { 60 Fatalf("loading container: %v", err) 61 } 62 // Read the spec again here to ensure flag annotations from the spec are 63 // applied to "conf". 64 if _, err := specutils.ReadSpec(c.BundleDir, conf); err != nil { 65 Fatalf("reading spec: %v", err) 66 } 67 68 if err := c.Start(conf); err != nil { 69 Fatalf("starting container: %v", err) 70 } 71 return subcommands.ExitSuccess 72 }