feat: Initial commit

This commit is contained in:
ptrcnull 2022-01-18 02:01:40 +01:00
commit a1121f8260
3 changed files with 57 additions and 0 deletions

5
README.md Normal file
View file

@ -0,0 +1,5 @@
# miniroot
> docker but simpler
Usage: `miniroot -root "/opt/something" -workdir "/usr/src/app" -init "/usr/loacal/bin/npm start"`

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module git.ddd.rip/ptrcnull/miniroot
go 1.17

49
main.go Normal file
View file

@ -0,0 +1,49 @@
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
)
var rootPath = flag.String("root", "", "path to root directory")
var initCmd = flag.String("init", "/sbin/init", "init command")
var workdir = flag.String("workdir", "/", "work directory")
func main() {
flag.Parse()
if *rootPath == "" {
fmt.Println("root directory not set")
os.Exit(1)
}
initParts := strings.Split(*initCmd, " ")
cmd := exec.Command(initParts[0], initParts[1:]...)
cmd.Dir = *workdir
cmd.SysProcAttr = &syscall.SysProcAttr{
Chroot: *rootPath,
Cloneflags: syscall.CLONE_NEWUSER|syscall.CLONE_NEWPID,
UidMappings: []syscall.SysProcIDMap{
{ContainerID: 0, HostID: os.Getuid(), Size: 1},
},
GidMappings: []syscall.SysProcIDMap{
{ContainerID: 0, HostID: os.Getgid(), Size: 1},
},
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
fmt.Printf("failed to start: %s\n", err)
}
err = cmd.Wait()
if err != nil {
fmt.Printf("process exited with error: %s\n", err)
}
}