|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/dotcloud/docker/pkg/libcontainer" |
| 8 | + "github.com/dotcloud/docker/pkg/libcontainer/namespaces" |
| 9 | + "github.com/dotcloud/docker/pkg/libcontainer/network" |
| 10 | + "github.com/dotcloud/docker/pkg/libcontainer/utils" |
| 11 | + "os" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + displayPid bool |
| 16 | + newCommand string |
| 17 | + usrNet bool |
| 18 | +) |
| 19 | + |
| 20 | +func init() { |
| 21 | + flag.BoolVar(&displayPid, "pid", false, "display the pid before waiting") |
| 22 | + flag.StringVar(&newCommand, "cmd", "/bin/bash", "command to run in the existing namespace") |
| 23 | + flag.BoolVar(&usrNet, "net", false, "user a net namespace") |
| 24 | + flag.Parse() |
| 25 | +} |
| 26 | + |
| 27 | +func exec(container *libcontainer.Container) error { |
| 28 | + var ( |
| 29 | + netFile *os.File |
| 30 | + err error |
| 31 | + ) |
| 32 | + container.NetNsFd = 0 |
| 33 | + |
| 34 | + if usrNet { |
| 35 | + netFile, err = os.Open("/root/nsroot/test") |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + container.NetNsFd = netFile.Fd() |
| 40 | + } |
| 41 | + |
| 42 | + pid, err := namespaces.Exec(container) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("error exec container %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + if displayPid { |
| 48 | + fmt.Println(pid) |
| 49 | + } |
| 50 | + |
| 51 | + exitcode, err := utils.WaitOnPid(pid) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("error waiting on child %s", err) |
| 54 | + } |
| 55 | + fmt.Println(exitcode) |
| 56 | + if usrNet { |
| 57 | + netFile.Close() |
| 58 | + if err := network.DeleteNetworkNamespace("/root/nsroot/test"); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + } |
| 62 | + os.Exit(exitcode) |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func execIn(container *libcontainer.Container) error { |
| 67 | + f, err := os.Open("/root/nsroot/test") |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + container.NetNsFd = f.Fd() |
| 72 | + pid, err := namespaces.ExecIn(container, &libcontainer.Command{ |
| 73 | + Env: container.Command.Env, |
| 74 | + Args: []string{ |
| 75 | + newCommand, |
| 76 | + }, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("error exexin container %s", err) |
| 80 | + } |
| 81 | + exitcode, err := utils.WaitOnPid(pid) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("error waiting on child %s", err) |
| 84 | + } |
| 85 | + os.Exit(exitcode) |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func createNet(config *libcontainer.Network) error { |
| 90 | + root := "/root/nsroot" |
| 91 | + if err := network.SetupNamespaceMountDir(root); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + nspath := root + "/test" |
| 96 | + if err := network.CreateNetworkNamespace(nspath); err != nil { |
| 97 | + return nil |
| 98 | + } |
| 99 | + if err := network.CreateVethPair("veth0", config.TempVethName); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if err := network.SetInterfaceMaster("veth0", config.Bridge); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + if err := network.InterfaceUp("veth0"); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + f, err := os.Open(nspath) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + defer f.Close() |
| 114 | + |
| 115 | + if err := network.SetInterfaceInNamespaceFd("veth1", int(f.Fd())); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + if err := network.SetupVethInsideNamespace(f.Fd(), config); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + */ |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func printErr(err error) { |
| 128 | + fmt.Fprintln(os.Stderr, err) |
| 129 | + os.Exit(1) |
| 130 | +} |
| 131 | + |
| 132 | +func main() { |
| 133 | + var ( |
| 134 | + err error |
| 135 | + cliCmd = flag.Arg(0) |
| 136 | + config = flag.Arg(1) |
| 137 | + ) |
| 138 | + f, err := os.Open(config) |
| 139 | + if err != nil { |
| 140 | + printErr(err) |
| 141 | + } |
| 142 | + |
| 143 | + dec := json.NewDecoder(f) |
| 144 | + var container *libcontainer.Container |
| 145 | + |
| 146 | + if err := dec.Decode(&container); err != nil { |
| 147 | + printErr(err) |
| 148 | + } |
| 149 | + f.Close() |
| 150 | + |
| 151 | + switch cliCmd { |
| 152 | + case "exec": |
| 153 | + err = exec(container) |
| 154 | + case "execin": |
| 155 | + err = execIn(container) |
| 156 | + case "net": |
| 157 | + err = createNet(&libcontainer.Network{ |
| 158 | + TempVethName: "veth1", |
| 159 | + IP: "172.17.0.100/16", |
| 160 | + Gateway: "172.17.42.1", |
| 161 | + Mtu: 1500, |
| 162 | + Bridge: "docker0", |
| 163 | + }) |
| 164 | + default: |
| 165 | + err = fmt.Errorf("command not supported: %s", cliCmd) |
| 166 | + } |
| 167 | + |
| 168 | + if err != nil { |
| 169 | + printErr(err) |
| 170 | + } |
| 171 | +} |
0 commit comments