start.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // +build linux
  2. package runc
  3. import (
  4. "os"
  5. "runtime"
  6. "github.com/codegangsta/cli"
  7. "github.com/coreos/go-systemd/activation"
  8. "github.com/opencontainers/runc/libcontainer"
  9. "github.com/opencontainers/runtime-spec/specs-go"
  10. )
  11. // default action is to start a container
  12. var startCommand = cli.Command{
  13. Name: "start",
  14. Usage: "create and run a container",
  15. ArgsUsage: `<container-id>
  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 creates an instance of a container for a bundle. The bundle
  20. is a directory with a specification file named "` + specConfig + `" and a root
  21. filesystem.
  22. The specification file includes an args parameter. The args parameter is used
  23. to specify command(s) that get run when the container is started. To change the
  24. command(s) that get executed on start, edit the args parameter of the spec. See
  25. "runc spec --help" for more explanation.`,
  26. Flags: []cli.Flag{
  27. cli.StringFlag{
  28. Name: "bundle, b",
  29. Value: "",
  30. Usage: `path to the root of the bundle directory, defaults to the current directory`,
  31. },
  32. cli.StringFlag{
  33. Name: "console",
  34. Value: "",
  35. Usage: "specify the pty slave path for use with the container",
  36. },
  37. cli.BoolFlag{
  38. Name: "detach,d",
  39. Usage: "detach from the container's process",
  40. },
  41. cli.StringFlag{
  42. Name: "pid-file",
  43. Value: "",
  44. Usage: "specify the file to write the process id to",
  45. },
  46. cli.BoolFlag{
  47. Name: "no-subreaper",
  48. Usage: "disable the use of the subreaper used to reap reparented processes",
  49. },
  50. cli.BoolFlag{
  51. Name: "no-pivot",
  52. Usage: "do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk",
  53. },
  54. },
  55. Action: func(context *cli.Context) {
  56. bundle := context.String("bundle")
  57. if bundle != "" {
  58. if err := os.Chdir(bundle); err != nil {
  59. fatal(err)
  60. }
  61. }
  62. spec, err := loadSpec(specConfig)
  63. if err != nil {
  64. fatal(err)
  65. }
  66. notifySocket := os.Getenv("NOTIFY_SOCKET")
  67. if notifySocket != "" {
  68. setupSdNotify(spec, notifySocket)
  69. }
  70. if os.Geteuid() != 0 {
  71. fatalf("runc should be run as root")
  72. }
  73. status, err := startContainer(context, spec)
  74. if err != nil {
  75. fatal(err)
  76. }
  77. // exit with the container's exit status so any external supervisor is
  78. // notified of the exit with the correct exit status.
  79. os.Exit(status)
  80. },
  81. }
  82. func init() {
  83. if len(os.Args) > 1 && os.Args[1] == "init" {
  84. runtime.GOMAXPROCS(1)
  85. runtime.LockOSThread()
  86. }
  87. }
  88. var initCommand = cli.Command{
  89. Name: "init",
  90. Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
  91. Action: func(context *cli.Context) {
  92. factory, _ := libcontainer.New("")
  93. if err := factory.StartInitialization(); err != nil {
  94. // as the error is sent back to the parent there is no need to log
  95. // or write it to stderr because the parent process will handle this
  96. os.Exit(1)
  97. }
  98. panic("libcontainer: container init failed to exec")
  99. },
  100. }
  101. func startContainer(context *cli.Context, spec *specs.Spec) (int, error) {
  102. id := context.Args().First()
  103. if id == "" {
  104. return -1, errEmptyID
  105. }
  106. container, err := createContainer(context, id, spec)
  107. if err != nil {
  108. return -1, err
  109. }
  110. detach := context.Bool("detach")
  111. // Support on-demand socket activation by passing file descriptors into the container init process.
  112. listenFDs := []*os.File{}
  113. if os.Getenv("LISTEN_FDS") != "" {
  114. listenFDs = activation.Files(false)
  115. }
  116. r := &runner{
  117. enableSubreaper: !context.Bool("no-subreaper"),
  118. shouldDestroy: true,
  119. container: container,
  120. listenFDs: listenFDs,
  121. console: context.String("console"),
  122. detach: detach,
  123. pidFile: context.String("pid-file"),
  124. }
  125. return r.run(&spec.Process)
  126. }