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