2015-02-01 11:56:27 +08:00
|
|
|
package configs
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
import (
|
2015-02-13 02:38:43 +08:00
|
|
|
"fmt"
|
2015-02-01 13:21:06 +08:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
type NamespaceType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
NEWNET NamespaceType = "NEWNET"
|
|
|
|
NEWPID NamespaceType = "NEWPID"
|
|
|
|
NEWNS NamespaceType = "NEWNS"
|
|
|
|
NEWUTS NamespaceType = "NEWUTS"
|
|
|
|
NEWIPC NamespaceType = "NEWIPC"
|
|
|
|
NEWUSER NamespaceType = "NEWUSER"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Namespace defines configuration for each namespace. It specifies an
|
|
|
|
// alternate path that is able to be joined via setns.
|
|
|
|
type Namespace struct {
|
|
|
|
Type NamespaceType `json:"type"`
|
2015-02-12 08:45:23 +08:00
|
|
|
Path string `json:"path"`
|
2015-02-01 11:56:27 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 13:21:06 +08:00
|
|
|
func (n *Namespace) Syscall() int {
|
|
|
|
return namespaceInfo[n.Type]
|
|
|
|
}
|
|
|
|
|
2015-02-13 02:38:43 +08:00
|
|
|
func (n *Namespace) GetPath(pid int) string {
|
|
|
|
if n.Path != "" {
|
|
|
|
return n.Path
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("/proc/%d/ns/%s", pid, n.file())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespace) file() string {
|
|
|
|
file := ""
|
|
|
|
switch n.Type {
|
|
|
|
case NEWNET:
|
|
|
|
file = "net"
|
|
|
|
case NEWNS:
|
|
|
|
file = "mnt"
|
|
|
|
case NEWPID:
|
|
|
|
file = "pid"
|
|
|
|
case NEWIPC:
|
|
|
|
file = "ipc"
|
|
|
|
case NEWUSER:
|
|
|
|
file = "user"
|
|
|
|
case NEWUTS:
|
|
|
|
file = "uts"
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
type Namespaces []Namespace
|
|
|
|
|
|
|
|
func (n *Namespaces) Remove(t NamespaceType) bool {
|
|
|
|
i := n.index(t)
|
|
|
|
if i == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*n = append((*n)[:i], (*n)[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) Add(t NamespaceType, path string) {
|
|
|
|
i := n.index(t)
|
|
|
|
if i == -1 {
|
|
|
|
*n = append(*n, Namespace{Type: t, Path: path})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
(*n)[i].Path = path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) index(t NamespaceType) int {
|
|
|
|
for i, ns := range *n {
|
|
|
|
if ns.Type == t {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) Contains(t NamespaceType) bool {
|
|
|
|
return n.index(t) != -1
|
|
|
|
}
|
2015-02-01 13:21:06 +08:00
|
|
|
|
|
|
|
var namespaceInfo = map[NamespaceType]int{
|
|
|
|
NEWNET: syscall.CLONE_NEWNET,
|
|
|
|
NEWNS: syscall.CLONE_NEWNS,
|
|
|
|
NEWUSER: syscall.CLONE_NEWUSER,
|
|
|
|
NEWIPC: syscall.CLONE_NEWIPC,
|
|
|
|
NEWUTS: syscall.CLONE_NEWUTS,
|
|
|
|
NEWPID: syscall.CLONE_NEWPID,
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloneFlags parses the container's Namespaces options to set the correct
|
|
|
|
// flags on clone, unshare. This functions returns flags only for new namespaces.
|
|
|
|
func (n *Namespaces) CloneFlags() uintptr {
|
|
|
|
var flag int
|
|
|
|
for _, v := range *n {
|
|
|
|
if v.Path != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
flag |= namespaceInfo[v.Type]
|
|
|
|
}
|
|
|
|
return uintptr(flag)
|
|
|
|
}
|