Created
October 13, 2018 10:51
-
-
Save planetA/64b032d186329c95721cb3ad867e12ab to your computer and use it in GitHub Desktop.
test-runc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/sys/unix" | |
"log" | |
"os" | |
"runtime" | |
"github.com/opencontainers/runc/libcontainer" | |
"github.com/opencontainers/runc/libcontainer/configs" | |
_ "github.com/opencontainers/runc/libcontainer/nsenter" | |
) | |
func init() { | |
runtime.GOMAXPROCS(1) | |
runtime.LockOSThread() | |
} | |
func main() { | |
if len(os.Args) > 1 && os.Args[1] == "init" { | |
factory, _ := libcontainer.New("") | |
if err := factory.StartInitialization(); err != nil { | |
log.Fatal(err) | |
} | |
panic("--this line should have never been executed, congratulations--") | |
} | |
if err := RunLC(0, os.Args[1:]); err != nil { | |
log.Fatal(err) | |
} | |
} | |
var ( | |
defaultMountFlags = unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV | |
config = &configs.Config{ | |
Root: { | |
Path: "/", | |
}, | |
// NoPivotRoot: true, | |
Capabilities: &configs.Capabilities{ | |
Bounding: []string{ | |
"CAP_CHOWN", | |
"CAP_DAC_OVERRIDE", | |
"CAP_FSETID", | |
"CAP_FOWNER", | |
"CAP_MKNOD", | |
"CAP_NET_RAW", | |
"CAP_SETGID", | |
"CAP_SETUID", | |
"CAP_SETFCAP", | |
"CAP_SETPCAP", | |
"CAP_NET_BIND_SERVICE", | |
"CAP_SYS_CHROOT", | |
"CAP_KILL", | |
"CAP_AUDIT_WRITE", | |
}, | |
Effective: []string{ | |
"CAP_CHOWN", | |
"CAP_DAC_OVERRIDE", | |
"CAP_FSETID", | |
"CAP_FOWNER", | |
"CAP_MKNOD", | |
"CAP_NET_RAW", | |
"CAP_SETGID", | |
"CAP_SETUID", | |
"CAP_SETFCAP", | |
"CAP_SETPCAP", | |
"CAP_NET_BIND_SERVICE", | |
"CAP_SYS_CHROOT", | |
"CAP_KILL", | |
"CAP_AUDIT_WRITE", | |
}, | |
Inheritable: []string{ | |
"CAP_CHOWN", | |
"CAP_DAC_OVERRIDE", | |
"CAP_FSETID", | |
"CAP_FOWNER", | |
"CAP_MKNOD", | |
"CAP_NET_RAW", | |
"CAP_SETGID", | |
"CAP_SETUID", | |
"CAP_SETFCAP", | |
"CAP_SETPCAP", | |
"CAP_NET_BIND_SERVICE", | |
"CAP_SYS_CHROOT", | |
"CAP_KILL", | |
"CAP_AUDIT_WRITE", | |
}, | |
Permitted: []string{ | |
"CAP_CHOWN", | |
"CAP_DAC_OVERRIDE", | |
"CAP_FSETID", | |
"CAP_FOWNER", | |
"CAP_MKNOD", | |
"CAP_NET_RAW", | |
"CAP_SETGID", | |
"CAP_SETUID", | |
"CAP_SETFCAP", | |
"CAP_SETPCAP", | |
"CAP_NET_BIND_SERVICE", | |
"CAP_SYS_CHROOT", | |
"CAP_KILL", | |
"CAP_AUDIT_WRITE", | |
}, | |
Ambient: []string{ | |
"CAP_CHOWN", | |
"CAP_DAC_OVERRIDE", | |
"CAP_FSETID", | |
"CAP_FOWNER", | |
"CAP_MKNOD", | |
"CAP_NET_RAW", | |
"CAP_SETGID", | |
"CAP_SETUID", | |
"CAP_SETFCAP", | |
"CAP_SETPCAP", | |
"CAP_NET_BIND_SERVICE", | |
"CAP_SYS_CHROOT", | |
"CAP_KILL", | |
"CAP_AUDIT_WRITE", | |
}, | |
}, | |
Namespaces: configs.Namespaces([]configs.Namespace{ | |
{Type: configs.NEWNS}, | |
{Type: configs.NEWUTS}, | |
{Type: configs.NEWIPC}, | |
{Type: configs.NEWPID}, | |
{Type: configs.NEWUSER}, | |
{Type: configs.NEWNET}, | |
}), | |
MaskPaths: []string{ | |
"/proc/kcore", | |
"/sys/firmware", | |
}, | |
ReadonlyPaths: []string{ | |
"/proc/sys", "/proc/sysrq-trigger", "/proc/irq", "/proc/bus", | |
}, | |
Devices: configs.DefaultAutoCreatedDevices, | |
Hostname: "testing", | |
Mounts: []*configs.Mount{ | |
{ | |
Source: "proc", | |
Destination: "/proc", | |
Device: "proc", | |
Flags: defaultMountFlags, | |
}, | |
{ | |
Source: "tmpfs", | |
Destination: "/dev", | |
Device: "tmpfs", | |
Flags: unix.MS_NOSUID | unix.MS_STRICTATIME, | |
Data: "mode=755", | |
}, | |
{ | |
Source: "devpts", | |
Destination: "/dev/pts", | |
Device: "devpts", | |
Flags: unix.MS_NOSUID | unix.MS_NOEXEC, | |
Data: "newinstance,ptmxmode=0666,mode=0620,gid=5", | |
}, | |
{ | |
Device: "tmpfs", | |
Source: "shm", | |
Destination: "/dev/shm", | |
Data: "mode=1777,size=65536k", | |
Flags: defaultMountFlags, | |
}, | |
{ | |
Source: "mqueue", | |
Destination: "/dev/mqueue", | |
Device: "mqueue", | |
Flags: defaultMountFlags, | |
}, | |
{ | |
Source: "sysfs", | |
Destination: "/sys", | |
Device: "sysfs", | |
Flags: defaultMountFlags | unix.MS_RDONLY, | |
}, | |
}, | |
UidMappings: []configs.IDMap{ | |
{ | |
ContainerID: 0, | |
HostID: 1000, | |
Size: 65536, | |
}, | |
}, | |
GidMappings: []configs.IDMap{ | |
{ | |
ContainerID: 0, | |
HostID: 1000, | |
Size: 65536, | |
}, | |
}, | |
Networks: []*configs.Network{ | |
{ | |
Type: "loopback", | |
Address: "127.0.0.1/0", | |
Gateway: "localhost", | |
}, | |
}, | |
Cgroups: &configs.Cgroup{ | |
Name: "test-container", | |
Parent: "system", | |
Resources: &configs.Resources{ | |
MemorySwappiness: nil, | |
AllowAllDevices: nil, | |
AllowedDevices: configs.DefaultAllowedDevices, | |
}, | |
}, | |
} | |
) | |
func RunLC(id int, args []string) error { | |
factory, err := libcontainer.New("/tmp/test", libcontainer.Cgroupfs) | |
if err != nil { | |
return fmt.Errorf("Failed to create container factory: %v", err) | |
} | |
container, err := factory.Create(fmt.Sprintf("test%v", id), config) | |
if err != nil { | |
return fmt.Errorf("Failed to create a new container: %v", err) | |
} | |
defer container.Destroy() | |
process := &libcontainer.Process{ | |
Args: args, | |
Env: os.Environ(), | |
Stdin: os.Stdin, | |
Stdout: os.Stdout, | |
Stderr: os.Stderr, | |
Init: true, | |
} | |
err = container.Run(process) | |
if err != nil { | |
return fmt.Errorf("Failed to launch the container: %v", err) | |
} | |
log.Println(args) | |
processes, _ := container.Processes() | |
stats, _ := container.Stats() | |
status, _ := container.Status() | |
log.Printf("%+v %v %v\n", processes, stats.Interfaces, status) | |
_, err = process.Wait() | |
if err != nil { | |
return fmt.Errorf("Process failed: %v", err) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment