2014-07-02 03:51:02 +08:00
|
|
|
package nsinit
|
2014-06-06 05:28:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-08-07 09:44:41 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/namespaces"
|
2014-08-07 09:44:41 +08:00
|
|
|
"github.com/docker/libcontainer/syncpipe"
|
2014-06-06 05:28:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var nsenterCommand = cli.Command{
|
|
|
|
Name: "nsenter",
|
|
|
|
Usage: "init process for entering an existing namespace",
|
|
|
|
Action: nsenterAction,
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
// this expects that we already have our namespaces setup by the C initializer
|
|
|
|
// we are expected to finalize the namespace and exec the user's application
|
2014-06-06 05:28:09 +08:00
|
|
|
func nsenterAction(context *cli.Context) {
|
2014-08-07 09:44:41 +08:00
|
|
|
syncPipe, err := syncpipe.NewSyncPipeFromFd(0, 3)
|
2014-06-06 05:28:09 +08:00
|
|
|
if err != nil {
|
2014-08-07 09:44:41 +08:00
|
|
|
log.Fatalf("unable to create sync pipe: %s", err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
var config *libcontainer.Config
|
|
|
|
if err := syncPipe.ReadFromParent(&config); err != nil {
|
|
|
|
log.Fatalf("reading container config from parent: %s", err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
if err := namespaces.FinalizeSetns(config, context.Args()); err != nil {
|
2014-06-06 05:28:09 +08:00
|
|
|
log.Fatalf("failed to nsenter: %s", err)
|
|
|
|
}
|
|
|
|
}
|