Skip to content

Instantly share code, notes, and snippets.

@17twenty
Created May 3, 2016 00:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 17twenty/f16cfc461043220aa6e8f96c9c6cbcfd to your computer and use it in GitHub Desktop.
Save 17twenty/f16cfc461043220aa6e8f96c9c6cbcfd to your computer and use it in GitHub Desktop.
Containerisation in 100 Lines of Go Code
package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)
func main() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "run":
parent()
case "child":
child()
}
}
fmt.Printf("Usage\n\t%s <run | child> executable\n", os.Args[0])
os.Exit(1)
}
func parent() {
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWNS,
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Println("ERROR", err)
os.Exit(1)
}
}
func child() {
must(syscall.Mount("rootfs", "rootfs", "", syscall.MS_BIND, ""))
must(os.MkdirAll("rootfs/oldrootfs", 0700))
must(syscall.PivotRoot("rootfs", "rootfs/oldrootfs"))
must(os.Chdir("/"))
cmd := exec.Command(os.Args[2], os.Args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Println("ERROR", err)
os.Exit(1)
}
}
func must(err error) {
if err != nil {
panic(err)
}
}
@agners
Copy link

agners commented Mar 12, 2017

For some reason, on a Linux 4.10/systemd based Arch Linux system pivot_root always returns invalid argument. I successfully used must(syscall.Chroot("rootfs/")) instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment