2014-02-19 00:56:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/namespaces"
|
2014-02-19 02:15:41 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/namespaces/nsinit"
|
2014-02-19 00:56:11 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/utils"
|
|
|
|
"os"
|
2014-02-19 07:13:36 +00:00
|
|
|
exec_ "os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2014-02-19 00:56:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
displayPid bool
|
|
|
|
newCommand string
|
|
|
|
usrNet bool
|
2014-02-19 02:15:41 +00:00
|
|
|
masterFd int
|
|
|
|
console string
|
2014-02-19 00:56:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&displayPid, "pid", false, "display the pid before waiting")
|
|
|
|
flag.StringVar(&newCommand, "cmd", "/bin/bash", "command to run in the existing namespace")
|
|
|
|
flag.BoolVar(&usrNet, "net", false, "user a net namespace")
|
2014-02-19 02:15:41 +00:00
|
|
|
flag.IntVar(&masterFd, "master", 0, "master fd")
|
|
|
|
flag.StringVar(&console, "console", "", "console path")
|
2014-02-19 00:56:11 +00:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2014-02-19 02:15:41 +00:00
|
|
|
func nsinitFunc(container *libcontainer.Container) error {
|
|
|
|
container.Master = uintptr(masterFd)
|
|
|
|
container.Console = console
|
2014-02-19 18:44:29 +00:00
|
|
|
container.LogFile = "/root/logs"
|
2014-02-19 02:15:41 +00:00
|
|
|
|
|
|
|
return nsinit.InitNamespace(container)
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:56:11 +00:00
|
|
|
func exec(container *libcontainer.Container) error {
|
|
|
|
var (
|
|
|
|
netFile *os.File
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
container.NetNsFd = 0
|
|
|
|
|
|
|
|
if usrNet {
|
|
|
|
netFile, err = os.Open("/root/nsroot/test")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.NetNsFd = netFile.Fd()
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:13:36 +00:00
|
|
|
self, err := exec_.LookPath(os.Args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if output, err := exec_.Command("cp", self, path.Join(container.RootFs, ".nsinit")).CombinedOutput(); err != nil {
|
|
|
|
return fmt.Errorf("Error exec cp: %s, (%s)", err, output)
|
|
|
|
} else {
|
|
|
|
println(self, container.RootFs)
|
|
|
|
fmt.Printf("-----> %s\n", output)
|
|
|
|
}
|
|
|
|
println("----")
|
|
|
|
|
2014-02-19 02:15:41 +00:00
|
|
|
pid, err := namespaces.ExecContainer(container)
|
2014-02-19 00:56:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error exec container %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if displayPid {
|
|
|
|
fmt.Println(pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
exitcode, err := utils.WaitOnPid(pid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error waiting on child %s", err)
|
|
|
|
}
|
|
|
|
fmt.Println(exitcode)
|
|
|
|
if usrNet {
|
|
|
|
netFile.Close()
|
|
|
|
if err := network.DeleteNetworkNamespace("/root/nsroot/test"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Exit(exitcode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func execIn(container *libcontainer.Container) error {
|
2014-02-19 07:13:36 +00:00
|
|
|
// f, err := os.Open("/root/nsroot/test")
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// container.NetNsFd = f.Fd()
|
|
|
|
// pid, err := namespaces.ExecIn(container, &libcontainer.Command{
|
|
|
|
// Env: container.Command.Env,
|
|
|
|
// Args: []string{
|
|
|
|
// newCommand,
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf("error exexin container %s", err)
|
|
|
|
// }
|
|
|
|
// exitcode, err := utils.WaitOnPid(pid)
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf("error waiting on child %s", err)
|
|
|
|
// }
|
|
|
|
// os.Exit(exitcode)
|
2014-02-19 00:56:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNet(config *libcontainer.Network) error {
|
2014-02-19 02:15:41 +00:00
|
|
|
/*
|
|
|
|
root := "/root/nsroot"
|
|
|
|
if err := network.SetupNamespaceMountDir(root); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 02:15:41 +00:00
|
|
|
nspath := root + "/test"
|
|
|
|
if err := network.CreateNetworkNamespace(nspath); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := network.CreateVethPair("veth0", config.TempVethName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := network.SetInterfaceMaster("veth0", config.Bridge); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := network.InterfaceUp("veth0"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 02:15:41 +00:00
|
|
|
f, err := os.Open(nspath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 02:15:41 +00:00
|
|
|
if err := network.SetInterfaceInNamespaceFd("veth1", int(f.Fd())); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-02-19 02:15:41 +00:00
|
|
|
|
|
|
|
if err := network.SetupVethInsideNamespace(f.Fd(), config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
*/
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func printErr(err error) {
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2014-02-19 07:13:36 +00:00
|
|
|
cliCmd := flag.Arg(0)
|
|
|
|
|
|
|
|
config, err := filepath.Abs(flag.Arg(1))
|
|
|
|
if err != nil {
|
|
|
|
printErr(err)
|
|
|
|
}
|
|
|
|
println("cli:", cliCmd, "config:", config)
|
2014-02-19 00:56:11 +00:00
|
|
|
f, err := os.Open(config)
|
|
|
|
if err != nil {
|
|
|
|
printErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dec := json.NewDecoder(f)
|
|
|
|
var container *libcontainer.Container
|
|
|
|
|
|
|
|
if err := dec.Decode(&container); err != nil {
|
|
|
|
printErr(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
switch cliCmd {
|
2014-02-19 02:15:41 +00:00
|
|
|
case "init":
|
|
|
|
err = nsinitFunc(container)
|
2014-02-19 00:56:11 +00:00
|
|
|
case "exec":
|
|
|
|
err = exec(container)
|
|
|
|
case "execin":
|
|
|
|
err = execIn(container)
|
|
|
|
case "net":
|
|
|
|
err = createNet(&libcontainer.Network{
|
|
|
|
TempVethName: "veth1",
|
|
|
|
IP: "172.17.0.100/16",
|
|
|
|
Gateway: "172.17.42.1",
|
|
|
|
Mtu: 1500,
|
|
|
|
Bridge: "docker0",
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("command not supported: %s", cliCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
printErr(err)
|
|
|
|
}
|
|
|
|
}
|