2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-06-25 02:14:46 +08:00
|
|
|
"path/filepath"
|
2015-06-22 10:31:12 +08:00
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
|
|
_ "github.com/opencontainers/runc/libcontainer/nsenter"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
version = "0.1"
|
2015-06-26 07:26:31 +08:00
|
|
|
usage = `Open Container Project runtime
|
|
|
|
|
|
|
|
runc is a command line client for running applications packaged according to the Open Container Format (OCF) and is
|
|
|
|
a compliant implementation of the Open Container Project specification.
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
runc integrates well with existing process supervisors to provide a production container runtime environment for
|
2015-06-26 07:26:31 +08:00
|
|
|
applications. It can be used with your existing process monitoring tools and the container will be spawned as a direct
|
|
|
|
child of the process supervisor.
|
2015-06-22 10:31:12 +08:00
|
|
|
|
2015-06-26 07:26:31 +08:00
|
|
|
After creating a spec for your root filesystem with runc, you can execute a simple container in your shell by running:
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-06-26 07:26:31 +08:00
|
|
|
panic("--this line should never been executed, congratulations--")
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-06-25 15:19:44 +08:00
|
|
|
logrus.Fatalf("Container start failed: %v", err)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
|
|
|
// exit with the container's exit status so any external supervisor is
|
|
|
|
// notified of the exit with the correct exit status.
|
|
|
|
os.Exit(status)
|
|
|
|
}
|
2015-06-25 02:14:46 +08:00
|
|
|
|
|
|
|
//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
|
|
|
|
}
|
2015-06-22 10:31:12 +08:00
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|