Skip to content

Commit

Permalink
feat: added json file to simulate db
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Oct 22, 2023
1 parent 2d406b5 commit e4a6cf9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
9 changes: 9 additions & 0 deletions create-container/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Create a container from scratch


## Get started
```bash
sudo su
go build .
./jb-container run /bin/bash
```
2 changes: 1 addition & 1 deletion create-container/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module c-container
module jb-container

go 1.21.0
Binary file added create-container/jb-container
Binary file not shown.
81 changes: 70 additions & 11 deletions create-container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,84 @@ package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)

func runContainer() {
fmt.Println("Sup")
func runContainer(args []string) {
fmt.Printf("Running %v as PID %d\n", args[2:], os.Getppid())
cmd := exec.Command("/proc/self/exe", append([]string{"child"}, args[2:]...)...)

cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.SysProcAttr = &syscall.SysProcAttr{
Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID,
}

isFuncPanic(cmd.Run())
}

func main() {
var cmds [1]string
func runContainerChild(args []string) {

cmd := exec.Command(args[2], args[3:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

cmds[0] = "run"
isFuncPanic(syscall.Chroot(os.Getenv("HOME")))
isFuncPanic(os.Chdir("/"))
isFuncPanic(syscall.Mount("proc", "proc", "proc", 0, ""))
isFuncPanic(cmd.Run())

if len(os.Args) < 2 {
panic("Provide at leas 1 argument")
isFuncPanic(cmd.Run())
}

func messageHelper(cmds []string, opts []string) {
cmdsDescriptions := []string{
"you can start a container",
"run as a child",
}
optsDescriptions := []string{
"provides message helper",
}

fmt.Println("Usage: jb-docker [COMMAND] [OPTIONS]")
fmt.Println("\nAvailable commands:")
for idxCmd := range cmds {
fmt.Println(fmt.Sprintf("[%s] - %s", cmds[idxCmd], cmdsDescriptions[idxCmd]))
}
fmt.Println("\nAvailable options:")
for idxOpt := range opts {
fmt.Println(fmt.Sprintf("[%s] - %s", opts[idxOpt], optsDescriptions[idxOpt]))
}
fmt.Println(len(os.Args))
switch os.Args[1] {
}

func startContainer(args []string) {
cmds := []string{"run", "child"}
opts := []string{"--help"}

if len(args) < 2 {
panic("Provide at least 1 argument")
}
switch args[1] {
case cmds[0]:
runContainer()
runContainer(args)
case cmds[1]:
runContainerChild(args)
case opts[0]:
messageHelper(cmds, opts)
default:
panic("Provide at leas 1 argument")
fmt.Println(fmt.Sprintf("jb-docker: '%s' is not a jb-docker command.\nSee 'jb-docker --help'", args[1]))
}
}

func isFuncPanic(err error) {
if err != nil {
panic(err)
}
}

func main() {
startContainer(os.Args)
}

0 comments on commit e4a6cf9

Please sign in to comment.