2014-07-02 03:51:02 +08:00
|
|
|
package nsinit
|
2014-06-06 05:28:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2014-08-05 06:05:50 +08:00
|
|
|
"strconv"
|
2014-06-06 05:28:09 +08:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/namespaces"
|
2014-06-06 05:28:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var nsenterCommand = cli.Command{
|
|
|
|
Name: "nsenter",
|
|
|
|
Usage: "init process for entering an existing namespace",
|
|
|
|
Action: nsenterAction,
|
|
|
|
}
|
|
|
|
|
|
|
|
func nsenterAction(context *cli.Context) {
|
|
|
|
args := context.Args()
|
|
|
|
|
2014-06-17 06:28:28 +08:00
|
|
|
if len(args) == 0 {
|
|
|
|
args = []string{"/bin/bash"}
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 06:05:50 +08:00
|
|
|
container, err := loadContainerFromJson(context.GlobalString("containerjson"))
|
2014-06-06 05:28:09 +08:00
|
|
|
if err != nil {
|
2014-06-17 06:28:28 +08:00
|
|
|
log.Fatalf("unable to load container: %s", err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 06:05:50 +08:00
|
|
|
nspid, err := strconv.Atoi(context.GlobalString("nspid"))
|
|
|
|
if nspid <= 0 || err != nil {
|
|
|
|
log.Fatalf("cannot enter into namespaces without valid pid: %q - %s", nspid, err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-07-14 05:03:29 +08:00
|
|
|
if err := namespaces.NsEnter(container, args); err != nil {
|
2014-06-06 05:28:09 +08:00
|
|
|
log.Fatalf("failed to nsenter: %s", err)
|
|
|
|
}
|
|
|
|
}
|