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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/Sirupsen/logrus"
    10  	"github.com/opencontainers/runtime-spec/specs-go"
    11  	"github.com/urfave/cli"
    12  )
    13  
    14  // version will be populated by the Makefile, read from
    15  // VERSION file of the source code.
    16  var version = ""
    17  
    18  // gitCommit will be the hash that the binary was built from
    19  // and will be populated by the Makefile
    20  var gitCommit = ""
    21  
    22  const (
    23  	specConfig = "config.json"
    24  	usage      = `Open Container Initiative runtime
    25  
    26  runc is a command line client for running applications packaged according to
    27  the Open Container Initiative (OCI) format and is a compliant implementation of the
    28  Open Container Initiative specification.
    29  
    30  runc integrates well with existing process supervisors to provide a production
    31  container runtime environment for applications. It can be used with your
    32  existing process monitoring tools and the container will be spawned as a
    33  direct child of the process supervisor.
    34  
    35  Containers are configured using bundles. A bundle for a container is a directory
    36  that includes a specification file named "` + specConfig + `" and a root filesystem.
    37  The root filesystem contains the contents of the container.
    38  
    39  To start a new instance of a container:
    40  
    41      # runc run [ -b bundle ] <container-id>
    42  
    43  Where "<container-id>" is your name for the instance of the container that you
    44  are starting. The name you provide for the container instance must be unique on
    45  your host. Providing the bundle directory using "-b" is optional. The default
    46  value for "bundle" is the current directory.`
    47  )
    48  
    49  func main() {
    50  	app := cli.NewApp()
    51  	app.Name = "runc"
    52  	app.Usage = usage
    53  
    54  	var v []string
    55  	if version != "" {
    56  		v = append(v, version)
    57  	}
    58  	if gitCommit != "" {
    59  		v = append(v, fmt.Sprintf("commit: %s", gitCommit))
    60  	}
    61  	v = append(v, fmt.Sprintf("spec: %s", specs.Version))
    62  	app.Version = strings.Join(v, "\n")
    63  	app.Flags = []cli.Flag{
    64  		cli.BoolFlag{
    65  			Name:  "debug",
    66  			Usage: "enable debug output for logging",
    67  		},
    68  		cli.StringFlag{
    69  			Name:  "log",
    70  			Value: "/dev/null",
    71  			Usage: "set the log file path where internal debug information is written",
    72  		},
    73  		cli.StringFlag{
    74  			Name:  "log-format",
    75  			Value: "text",
    76  			Usage: "set the format used by logs ('text' (default), or 'json')",
    77  		},
    78  		cli.StringFlag{
    79  			Name:  "root",
    80  			Value: "/run/runc",
    81  			Usage: "root directory for storage of container state (this should be located in tmpfs)",
    82  		},
    83  		cli.StringFlag{
    84  			Name:  "criu",
    85  			Value: "criu",
    86  			Usage: "path to the criu binary used for checkpoint and restore",
    87  		},
    88  		cli.BoolFlag{
    89  			Name:  "systemd-cgroup",
    90  			Usage: "enable systemd cgroup support, expects cgroupsPath to be of form \"slice:prefix:name\" for e.g. \"system.slice:runc:434234\"",
    91  		},
    92  	}
    93  	app.Commands = []cli.Command{
    94  		checkpointCommand,
    95  		createCommand,
    96  		deleteCommand,
    97  		eventsCommand,
    98  		execCommand,
    99  		initCommand,
   100  		killCommand,
   101  		listCommand,
   102  		pauseCommand,
   103  		psCommand,
   104  		restoreCommand,
   105  		resumeCommand,
   106  		runCommand,
   107  		specCommand,
   108  		startCommand,
   109  		stateCommand,
   110  		updateCommand,
   111  	}
   112  	app.Before = func(context *cli.Context) error {
   113  		if context.GlobalBool("debug") {
   114  			logrus.SetLevel(logrus.DebugLevel)
   115  		}
   116  		if path := context.GlobalString("log"); path != "" {
   117  			f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0666)
   118  			if err != nil {
   119  				return err
   120  			}
   121  			logrus.SetOutput(f)
   122  		}
   123  		switch context.GlobalString("log-format") {
   124  		case "text":
   125  			// retain logrus's default.
   126  		case "json":
   127  			logrus.SetFormatter(new(logrus.JSONFormatter))
   128  		default:
   129  			return fmt.Errorf("unknown log-format %q", context.GlobalString("log-format"))
   130  		}
   131  		return nil
   132  	}
   133  	// If the command returns an error, cli takes upon itself to print
   134  	// the error on cli.ErrWriter and exit.
   135  	// Use our own writer here to ensure the log gets sent to the right location.
   136  	cli.ErrWriter = &FatalWriter{cli.ErrWriter}
   137  	if err := app.Run(os.Args); err != nil {
   138  		fatal(err)
   139  	}
   140  }
   141  
   142  type FatalWriter struct {
   143  	cliErrWriter io.Writer
   144  }
   145  
   146  func (f *FatalWriter) Write(p []byte) (n int, err error) {
   147  	logrus.Error(string(p))
   148  	return f.cliErrWriter.Write(p)
   149  }