Instantly share code, notes, and snippets.
julz/main.go
Created November 20, 2015 12:39- Star 310 (310) You must be signed in to star a gist
- Fork 58 (58) You must be signed in to fork a gist
-
Select an option
No results found
Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/julz/c0017fa7a40de0543001.js"></script> - Save julz/c0017fa7a40de0543001 to your computer and use it in GitHub Desktop.
Select an option
No results found
Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/julz/c0017fa7a40de0543001.js"></script> Save julz/c0017fa7a40de0543001 to your computer and use it in GitHub Desktop. Download ZIP containersched minicontainer Raw main.go This file contains hidden or 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 Show hidden characters| package main | |
| import ( | |
| "fmt" | |
| "os" | |
| "os/exec" | |
| "syscall" | |
| ) | |
| func main() { | |
| switch os.Args[1] { | |
| case "run": | |
| parent() | |
| case "child": | |
| child() | |
| default: | |
| panic("wat should I do") | |
| } | |
| } | |
| 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) | |
| } | |
| } |
trysh commented Mar 16, 2019
Copy link Copy Markdownthanks
azankorejo commented Feb 17, 2021
Copy link Copy Markdownthank you so much !!!
WhyTimePass commented Sep 10, 2021
Copy link Copy Markdowngood example!
KEINOS commented Nov 22, 2021
Copy link Copy MarkdownCame from: "Building a container from scratch in Go" | Container Camp @ Youtube
So neat and clean example and explanation!! Love it. Thanks!
zeufack commented Mar 9, 2024
Copy link Copy Markdownthanks
mmjsmohit commented Jul 26, 2026
Copy link Copy MarkdownCame from: "Building a container from scratch in Go" | Container Camp @ Youtube So neat and clean example and explanation!! Love it. Thanks!
Same!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment