|
| 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 | +displayPidbool |
| 16 | +newCommandstring |
| 17 | +usrNetbool |
| 18 | +) |
| 19 | + |
| 20 | +funcinit() { |
| 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 | +funcexec(container*libcontainer.Container) error { |
| 28 | +var ( |
| 29 | +netFile*os.File |
| 30 | +errerror |
| 31 | + ) |
| 32 | +container.NetNsFd=0 |
| 33 | + |
| 34 | +ifusrNet { |
| 35 | +netFile, err=os.Open("/root/nsroot/test") |
| 36 | +iferr!=nil { |
| 37 | +returnerr |
| 38 | + } |
| 39 | +container.NetNsFd=netFile.Fd() |
| 40 | + } |
| 41 | + |
| 42 | +pid, err:=namespaces.Exec(container) |
| 43 | +iferr!=nil { |
| 44 | +returnfmt.Errorf("error exec container %s", err) |
| 45 | + } |
| 46 | + |
| 47 | +ifdisplayPid { |
| 48 | +fmt.Println(pid) |
| 49 | + } |
| 50 | + |
| 51 | +exitcode, err:=utils.WaitOnPid(pid) |
| 52 | +iferr!=nil { |
| 53 | +returnfmt.Errorf("error waiting on child %s", err) |
| 54 | + } |
| 55 | +fmt.Println(exitcode) |
| 56 | +ifusrNet { |
| 57 | +netFile.Close() |
| 58 | +iferr:=network.DeleteNetworkNamespace("/root/nsroot/test"); err!=nil { |
| 59 | +returnerr |
| 60 | + } |
| 61 | + } |
| 62 | +os.Exit(exitcode) |
| 63 | +returnnil |
| 64 | +} |
| 65 | + |
| 66 | +funcexecIn(container*libcontainer.Container) error { |
| 67 | +f, err:=os.Open("/root/nsroot/test") |
| 68 | +iferr!=nil { |
| 69 | +returnerr |
| 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 | +iferr!=nil { |
| 79 | +returnfmt.Errorf("error exexin container %s", err) |
| 80 | + } |
| 81 | +exitcode, err:=utils.WaitOnPid(pid) |
| 82 | +iferr!=nil { |
| 83 | +returnfmt.Errorf("error waiting on child %s", err) |
| 84 | + } |
| 85 | +os.Exit(exitcode) |
| 86 | +returnnil |
| 87 | +} |
| 88 | + |
| 89 | +funccreateNet(config*libcontainer.Network) error { |
| 90 | +root:="/root/nsroot" |
| 91 | +iferr:=network.SetupNamespaceMountDir(root); err!=nil { |
| 92 | +returnerr |
| 93 | + } |
| 94 | + |
| 95 | +nspath:=root+"/test" |
| 96 | +iferr:=network.CreateNetworkNamespace(nspath); err!=nil { |
| 97 | +returnnil |
| 98 | + } |
| 99 | +iferr:=network.CreateVethPair("veth0", config.TempVethName); err!=nil { |
| 100 | +returnerr |
| 101 | + } |
| 102 | +iferr:=network.SetInterfaceMaster("veth0", config.Bridge); err!=nil { |
| 103 | +returnerr |
| 104 | + } |
| 105 | +iferr:=network.InterfaceUp("veth0"); err!=nil { |
| 106 | +returnerr |
| 107 | + } |
| 108 | + |
| 109 | +f, err:=os.Open(nspath) |
| 110 | +iferr!=nil { |
| 111 | +returnerr |
| 112 | + } |
| 113 | +deferf.Close() |
| 114 | + |
| 115 | +iferr:=network.SetInterfaceInNamespaceFd("veth1", int(f.Fd())); err!=nil { |
| 116 | +returnerr |
| 117 | + } |
| 118 | + |
| 119 | +/* |
| 120 | + if err := network.SetupVethInsideNamespace(f.Fd(), config); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + */ |
| 124 | +returnnil |
| 125 | +} |
| 126 | + |
| 127 | +funcprintErr(errerror) { |
| 128 | +fmt.Fprintln(os.Stderr, err) |
| 129 | +os.Exit(1) |
| 130 | +} |
| 131 | + |
| 132 | +funcmain() { |
| 133 | +var ( |
| 134 | +errerror |
| 135 | +cliCmd=flag.Arg(0) |
| 136 | +config=flag.Arg(1) |
| 137 | + ) |
| 138 | +f, err:=os.Open(config) |
| 139 | +iferr!=nil { |
| 140 | +printErr(err) |
| 141 | + } |
| 142 | + |
| 143 | +dec:=json.NewDecoder(f) |
| 144 | +varcontainer*libcontainer.Container |
| 145 | + |
| 146 | +iferr:=dec.Decode(&container); err!=nil { |
| 147 | +printErr(err) |
| 148 | + } |
| 149 | +f.Close() |
| 150 | + |
| 151 | +switchcliCmd { |
| 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 | +iferr!=nil { |
| 169 | +printErr(err) |
| 170 | + } |
| 171 | +} |
0 commit comments