mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
9c56596f24
A couple minor changes to error handling in startup: 1. Don't dump full help/usage text when the only problem is `runc` wasn't started under root privileges 2. Check for rootfs and make error clear to user when it doesn't exist 3. Change fatal to logrus.Fatal to get nicer output with simple error message Docker-DCO-1.1-Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com> (github: estesp)
107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/codegangsta/cli"
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
|
)
|
|
|
|
const (
|
|
version = "0.1"
|
|
usage = `open container runtime
|
|
|
|
runc integrates well with existing process supervisors to provide a production container runtime environment for
|
|
applications. It can be used with your existing process monitoring tools and the container will be spawned as direct
|
|
child of the process supervisor. nsinit can be used to manage the lifetime of a single container.
|
|
|
|
Execute a simple container in your shell by running:
|
|
|
|
cd /mycontainer
|
|
runc
|
|
`
|
|
)
|
|
|
|
func init() {
|
|
if len(os.Args) > 1 && os.Args[1] == "init" {
|
|
runtime.GOMAXPROCS(1)
|
|
runtime.LockOSThread()
|
|
factory, _ := libcontainer.New("")
|
|
if err := factory.StartInitialization(); err != nil {
|
|
fatal(err)
|
|
}
|
|
panic("--this line should never been executed, congradulations--")
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
app := cli.NewApp()
|
|
app.Name = "runc"
|
|
app.Usage = usage
|
|
app.Version = version
|
|
app.Flags = []cli.Flag{
|
|
cli.StringFlag{
|
|
Name: "id",
|
|
Value: getDefaultID(),
|
|
Usage: "specify the ID to be used for the container",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "debug",
|
|
Usage: "enable debug output for logging",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "root",
|
|
Value: "/var/run/ocf",
|
|
Usage: "root directory for storage of container state (this should be located in tmpfs)",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "criu",
|
|
Value: "criu",
|
|
Usage: "path to the criu binary used for checkpoint and restore",
|
|
},
|
|
}
|
|
app.Commands = []cli.Command{
|
|
checkpointCommand,
|
|
eventsCommand,
|
|
restoreCommand,
|
|
specCommand,
|
|
}
|
|
app.Before = func(context *cli.Context) error {
|
|
if context.GlobalBool("debug") {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
}
|
|
return nil
|
|
}
|
|
// default action is to execute a container
|
|
app.Action = func(context *cli.Context) {
|
|
if os.Geteuid() != 0 {
|
|
logrus.Fatal("runc should be run as root")
|
|
}
|
|
spec, err := loadSpec(context.Args().First())
|
|
if err != nil {
|
|
fatal(err)
|
|
}
|
|
status, err := execContainer(context, spec)
|
|
if err != nil {
|
|
logrus.Fatalf("Container start failed: %v", err)
|
|
}
|
|
// exit with the container's exit status so any external supervisor is
|
|
// notified of the exit with the correct exit status.
|
|
os.Exit(status)
|
|
}
|
|
|
|
//allow for relative path for the runC binary
|
|
if absPath, err := filepath.Abs(os.Args[0]); err != nil {
|
|
logrus.Fatal("Cannot convert runc path to absolute: %v", err)
|
|
} else {
|
|
os.Args[0] = absPath
|
|
}
|
|
if err := app.Run(os.Args); err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
}
|