github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/start.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/opencontainers/runc/libcontainer"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  var startCommand = cli.Command{
    12  	Name:  "start",
    13  	Usage: "executes the user defined process in a created container",
    14  	ArgsUsage: `<container-id> [container-id...]
    15  
    16  Where "<container-id>" is your name for the instance of the container that you
    17  are starting. The name you provide for the container instance must be unique on
    18  your host.`,
    19  	Description: `The start command executes the user defined process in a created container .`,
    20  	Action: func(context *cli.Context) error {
    21  		hasError := false
    22  		if !context.Args().Present() {
    23  			return fmt.Errorf("runc: \"start\" requires a minimum of 1 argument")
    24  		}
    25  
    26  		factory, err := loadFactory(context)
    27  		if err != nil {
    28  			return err
    29  		}
    30  
    31  		for _, id := range context.Args() {
    32  			container, err := factory.Load(id)
    33  			if err != nil {
    34  				fmt.Fprintf(os.Stderr, "container %s does not exist\n", id)
    35  				hasError = true
    36  				continue
    37  			}
    38  			status, err := container.Status()
    39  			if err != nil {
    40  				fmt.Fprintf(os.Stderr, "status for %s: %v\n", id, err)
    41  				hasError = true
    42  				continue
    43  			}
    44  			switch status {
    45  			case libcontainer.Created:
    46  				if err := container.Exec(); err != nil {
    47  					fmt.Fprintf(os.Stderr, "start for %s failed: %v\n", id, err)
    48  					hasError = true
    49  				}
    50  			case libcontainer.Stopped:
    51  				fmt.Fprintln(os.Stderr, "cannot start a container that has run and stopped")
    52  				hasError = true
    53  			case libcontainer.Running:
    54  				fmt.Fprintln(os.Stderr, "cannot start an already running container")
    55  				hasError = true
    56  			default:
    57  				fmt.Fprintf(os.Stderr, "cannot start a container in the %s state\n", status)
    58  				hasError = true
    59  			}
    60  		}
    61  
    62  		if hasError {
    63  			return fmt.Errorf("one or more of container start failed")
    64  		}
    65  		return nil
    66  	},
    67  }