Advertisement
errc

libcontainer-experiment-main.go

Feb 23rd, 2020
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.17 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "github.com/opencontainers/runc/libcontainer"
  6.     "github.com/opencontainers/runc/libcontainer/configs"
  7.     _ "github.com/opencontainers/runc/libcontainer/nsenter"
  8.     "github.com/sirupsen/logrus"
  9.     unix "golang.org/x/sys/unix"
  10.     "os"
  11.     "runtime"
  12. )
  13.  
  14. func init() {
  15.     if len(os.Args) > 1 && os.Args[1] == "init" {
  16.         runtime.GOMAXPROCS(1)
  17.         runtime.LockOSThread()
  18.         factory, _ := libcontainer.New("")
  19.         if err := factory.StartInitialization(); err != nil {
  20.             logrus.Fatal(err)
  21.         }
  22.         panic("--this line should never have been executed, congratulations--")
  23.     }
  24. }
  25.  
  26. /* Ways to interact with a running container
  27. // return all the pids for all processes running inside the container
  28. processes, err := container.Processes()
  29.  
  30. // get detailed cpu, memory, io, and network statistics for the container and
  31. // it's processes
  32. stats, err := container.Stats()
  33.  
  34. // pause all processes inside the container,
  35. container.Pause()
  36.  
  37. // resume all paused processes.
  38. container.Resume()
  39.  
  40. // send signal to container's init process.
  41. container.Signal(signal)
  42.  
  43. // update container resource constraints
  44. container.Set(config)
  45.  
  46. // get current status of the container
  47. status, err := container.Status()
  48.  
  49. // get current container's state information
  50. state, err := container.State()
  51. */
  52. func main() {
  53.     fmt.Printf("Running container for netrun-test\n")
  54.  
  55.     factory, err := libcontainer.New(
  56.         "/var/lib/container",
  57.         libcontainer.Cgroupfs,
  58.         libcontainer.InitArgs(os.Args[0], "init"),
  59.     )
  60.     if err != nil {
  61.         logrus.Fatal(err)
  62.         return
  63.     }
  64.  
  65.     defaultMountFlags := unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV
  66.     config := &configs.Config{
  67.         Rootfs: "/home/errc/i/containers/netrun-test/archlinux/rootfs",
  68.         Capabilities: &configs.Capabilities{
  69.             Bounding: []string{
  70.                 "CAP_CHOWN",
  71.                 "CAP_DAC_OVERRIDE",
  72.                 "CAP_FSETID",
  73.                 "CAP_FOWNER",
  74.                 "CAP_MKNOD",
  75.                 "CAP_NET_RAW",
  76.                 "CAP_SETGID",
  77.                 "CAP_SETUID",
  78.                 "CAP_SETFCAP",
  79.                 "CAP_SETPCAP",
  80.                 "CAP_NET_BIND_SERVICE",
  81.                 "CAP_SYS_CHROOT",
  82.                 "CAP_KILL",
  83.                 "CAP_AUDIT_WRITE",
  84.             },
  85.             Effective: []string{
  86.                 "CAP_CHOWN",
  87.                 "CAP_DAC_OVERRIDE",
  88.                 "CAP_FSETID",
  89.                 "CAP_FOWNER",
  90.                 "CAP_MKNOD",
  91.                 "CAP_NET_RAW",
  92.                 "CAP_SETGID",
  93.                 "CAP_SETUID",
  94.                 "CAP_SETFCAP",
  95.                 "CAP_SETPCAP",
  96.                 "CAP_NET_BIND_SERVICE",
  97.                 "CAP_SYS_CHROOT",
  98.                 "CAP_KILL",
  99.                 "CAP_AUDIT_WRITE",
  100.             },
  101.             Inheritable: []string{
  102.                 "CAP_CHOWN",
  103.                 "CAP_DAC_OVERRIDE",
  104.                 "CAP_FSETID",
  105.                 "CAP_FOWNER",
  106.                 "CAP_MKNOD",
  107.                 "CAP_NET_RAW",
  108.                 "CAP_SETGID",
  109.                 "CAP_SETUID",
  110.                 "CAP_SETFCAP",
  111.                 "CAP_SETPCAP",
  112.                 "CAP_NET_BIND_SERVICE",
  113.                 "CAP_SYS_CHROOT",
  114.                 "CAP_KILL",
  115.                 "CAP_AUDIT_WRITE",
  116.             },
  117.             Permitted: []string{
  118.                 "CAP_CHOWN",
  119.                 "CAP_DAC_OVERRIDE",
  120.                 "CAP_FSETID",
  121.                 "CAP_FOWNER",
  122.                 "CAP_MKNOD",
  123.                 "CAP_NET_RAW",
  124.                 "CAP_SETGID",
  125.                 "CAP_SETUID",
  126.                 "CAP_SETFCAP",
  127.                 "CAP_SETPCAP",
  128.                 "CAP_NET_BIND_SERVICE",
  129.                 "CAP_SYS_CHROOT",
  130.                 "CAP_KILL",
  131.                 "CAP_AUDIT_WRITE",
  132.             },
  133.             Ambient: []string{
  134.                 "CAP_CHOWN",
  135.                 "CAP_DAC_OVERRIDE",
  136.                 "CAP_FSETID",
  137.                 "CAP_FOWNER",
  138.                 "CAP_MKNOD",
  139.                 "CAP_NET_RAW",
  140.                 "CAP_SETGID",
  141.                 "CAP_SETUID",
  142.                 "CAP_SETFCAP",
  143.                 "CAP_SETPCAP",
  144.                 "CAP_NET_BIND_SERVICE",
  145.                 "CAP_SYS_CHROOT",
  146.                 "CAP_KILL",
  147.                 "CAP_AUDIT_WRITE",
  148.             },
  149.         },
  150.         Namespaces: configs.Namespaces([]configs.Namespace{
  151.             {Type: configs.NEWNS},
  152.             {Type: configs.NEWUTS},
  153.             {Type: configs.NEWIPC},
  154.             {Type: configs.NEWPID},
  155.             {Type: configs.NEWUSER},
  156.             {Type: configs.NEWNET},
  157.             {Type: configs.NEWCGROUP},
  158.         }),
  159.         Cgroups: &configs.Cgroup{
  160.             Name:   "netrun-test-container",
  161.             Parent: "system",
  162.             Resources: &configs.Resources{
  163.                 MemorySwappiness: nil,
  164.                 AllowAllDevices:  nil,
  165.                 AllowedDevices:   configs.DefaultAllowedDevices,
  166.             },
  167.         },
  168.         MaskPaths: []string{
  169.             "/proc/kcore",
  170.             "/sys/firmware",
  171.         },
  172.         ReadonlyPaths: []string{
  173.             "/proc/sys", "/proc/sysrq-trigger", "/proc/irq", "/proc/bus",
  174.         },
  175.         Devices:  configs.DefaultAutoCreatedDevices,
  176.         Hostname: "netrun-test",
  177.         Mounts: []*configs.Mount{
  178.             {
  179.                 Source:      "proc",
  180.                 Destination: "/proc",
  181.                 Device:      "proc",
  182.                 Flags:       defaultMountFlags,
  183.             },
  184.             {
  185.                 Source:      "tmpfs",
  186.                 Destination: "/dev",
  187.                 Device:      "tmpfs",
  188.                 Flags:       unix.MS_NOSUID | unix.MS_STRICTATIME,
  189.                 Data:        "mode=755",
  190.             },
  191.             {
  192.                 Source:      "devpts",
  193.                 Destination: "/dev/pts",
  194.                 Device:      "devpts",
  195.                 Flags:       unix.MS_NOSUID | unix.MS_NOEXEC,
  196.                 Data:        "newinstance,ptmxmode=0666,mode=0620,gid=5",
  197.             },
  198.             {
  199.                 Source:      "shm",
  200.                 Destination: "/dev/shm",
  201.                 Device:      "tmpfs",
  202.                 Flags:       defaultMountFlags,
  203.                 Data:        "mode=1777,size=65536k",
  204.             },
  205.             {
  206.                 Source:      "mqueue",
  207.                 Destination: "/dev/mqueue",
  208.                 Device:      "mqueue",
  209.                 Flags:       defaultMountFlags,
  210.             },
  211.             {
  212.                 Source:      "sysfs",
  213.                 Destination: "/sys",
  214.                 Device:      "sysfs",
  215.                 Flags:       defaultMountFlags | unix.MS_RDONLY,
  216.             },
  217.         },
  218.         UidMappings: []configs.IDMap{
  219.             {
  220.                 ContainerID: 0,
  221.                 HostID:      1000,
  222.                 Size:        65536,
  223.             },
  224.         },
  225.         GidMappings: []configs.IDMap{
  226.             {
  227.                 ContainerID: 0,
  228.                 HostID:      1000,
  229.                 Size:        65536,
  230.             },
  231.         },
  232.         Networks: []*configs.Network{
  233.             {
  234.                 Type:    "loopback",
  235.                 Address: "127.0.0.1/0",
  236.                 Gateway: "localhost",
  237.             },
  238.         },
  239.         Rlimits: []configs.Rlimit{
  240.             {
  241.                 Type: unix.RLIMIT_NOFILE,
  242.                 Hard: uint64(1025),
  243.                 Soft: uint64(1025),
  244.             },
  245.         },
  246.     }
  247.  
  248.     container, err := factory.Create("test-container-id", config)
  249.     if err != nil {
  250.         logrus.Fatal(err)
  251.         return
  252.     }
  253.  
  254.     process := &libcontainer.Process{
  255.         Args:   []string{"/bin/bash"},
  256.         Env:    []string{"PATH=/bin", "TERM=" + os.Getenv("TERM")},
  257.         User:   "daemon",
  258.         Stdin:  os.Stdin,
  259.         Stdout: os.Stdout,
  260.         Stderr: os.Stderr,
  261.         Init:   true,
  262.     }
  263.  
  264.     err = container.Run(process)
  265.     if err != nil {
  266.         container.Destroy()
  267.         logrus.Fatal(err)
  268.         return
  269.     }
  270.  
  271.     // wait for the process to finish
  272.     _, err = process.Wait()
  273.     if err != nil {
  274.         logrus.Fatal(err)
  275.     }
  276.  
  277.     // destroy the container
  278.     container.Destroy()
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement