2014-08-13 01:46:43 +08:00
|
|
|
package main
|
2014-06-06 05:28:09 +08:00
|
|
|
|
|
|
|
import (
|
2014-08-13 02:52:33 +08:00
|
|
|
"fmt"
|
2014-06-06 05:28:09 +08:00
|
|
|
"log"
|
2014-08-13 02:52:33 +08:00
|
|
|
"net"
|
|
|
|
"os"
|
2014-08-13 02:43:12 +08:00
|
|
|
"strconv"
|
2014-08-13 02:52:33 +08:00
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
2014-06-06 05:28:09 +08:00
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-08-13 02:43:12 +08:00
|
|
|
"github.com/docker/libcontainer/devices"
|
|
|
|
"github.com/docker/libcontainer/mount/nodes"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/namespaces"
|
2014-08-09 01:43:18 +08:00
|
|
|
_ "github.com/docker/libcontainer/namespaces/nsenter"
|
2014-06-06 05:28:09 +08:00
|
|
|
)
|
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
// nsenterExec exec's a process inside an existing container
|
|
|
|
func nsenterExec(config *libcontainer.Config, args []string) {
|
|
|
|
if err := namespaces.FinalizeSetns(config, args); err != nil {
|
|
|
|
log.Fatalf("failed to nsenter: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2014-08-09 02:16:56 +08:00
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
// nsenterMknod runs mknod inside an existing container
|
|
|
|
//
|
|
|
|
// mknod <path> <type> <major> <minor>
|
|
|
|
func nsenterMknod(config *libcontainer.Config, args []string) {
|
|
|
|
if len(args) != 4 {
|
|
|
|
log.Fatalf("expected mknod to have 4 arguments not %d", len(args))
|
2014-08-09 02:16:56 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
t := rune(args[1][0])
|
2014-06-06 05:28:09 +08:00
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
major, err := strconv.Atoi(args[2])
|
2014-06-06 05:28:09 +08:00
|
|
|
if err != nil {
|
2014-08-13 02:43:12 +08:00
|
|
|
log.Fatal(err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
minor, err := strconv.Atoi(args[3])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
n := &devices.Device{
|
|
|
|
Path: args[0],
|
|
|
|
Type: t,
|
|
|
|
MajorNumber: int64(major),
|
|
|
|
MinorNumber: int64(minor),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodes.CreateDeviceNode("/", n); err != nil {
|
|
|
|
log.Fatal(err)
|
2014-06-06 05:28:09 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-13 02:52:33 +08:00
|
|
|
|
|
|
|
// nsenterIp displays the network interfaces inside a container's net namespace
|
|
|
|
func nsenterIp(config *libcontainer.Config, args []string) {
|
|
|
|
interfaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
|
|
|
|
fmt.Fprint(w, "NAME\tMTU\tMAC\tFLAG\tADDRS\n")
|
|
|
|
|
|
|
|
for _, iface := range interfaces {
|
|
|
|
addrs, err := iface.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
o := []string{}
|
|
|
|
|
|
|
|
for _, a := range addrs {
|
|
|
|
o = append(o, a.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s\t%d\t%s\t%s\t%s\n", iface.Name, iface.MTU, iface.HardwareAddr, iface.Flags, strings.Join(o, ","))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
}
|