Created
November 30, 2017 13:12
-
-
Save owulveryck/b12c60d04de3db69603b599a2234987e to your computer and use it in GitHub Desktop.
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" | |
"os" | |
"os/exec" | |
"syscall" | |
) | |
func main() { | |
switch os.Args[1] { | |
case "run": | |
run() | |
case "child": | |
child() | |
default: | |
fmt.Println("help") | |
} | |
} | |
func run() { | |
fmt.Printf("Running %v as PID %v\n", os.Args[2:], os.Getpid()) | |
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...) | |
cmd.Stdin = os.Stdin | |
cmd.Stderr = os.Stderr | |
cmd.Stdout = os.Stdout | |
cmd.SysProcAttr = &syscall.SysProcAttr{ | |
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID, | |
} | |
must(cmd.Run()) | |
} | |
func child() { | |
fmt.Printf("Running %v as PID %v\n", os.Args[2:], os.Getpid()) | |
cmd := exec.Command(os.Args[2], os.Args[3:]...) | |
cmd.Stdin = os.Stdin | |
cmd.Stderr = os.Stderr | |
cmd.Stdout = os.Stdout | |
must(syscall.Sethostname([]byte("container"))) | |
must(syscall.Chroot("/home/ubuntu/GOPROJECTS/src/havre/ubuntufs")) | |
must(os.Chdir("/")) | |
must(syscall.Mount("proc", "proc", "proc", 0, "")) | |
must(cmd.Run()) | |
must(syscall.Unmount("proc", 0)) | |
} | |
func must(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment