Coder Social home page Coder Social logo

s8sg / firecracker-go-template Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 2.0 14 KB

Builds firecracker filesystem with provided Go application

License: Apache License 2.0

Makefile 7.37% Shell 71.92% Dockerfile 17.63% Go 3.08%
unikernel firecracker firecracker-microvms firecracker-go golang

firecracker-go-template's Introduction

firecracker-go-template

Builds firecracker filesystem with provided Go application

This repository is a direct copy and edit on UNIK's firecracker compiler
For more details on UNIK click here

Getting Started

Prerequisites

docker
firecracker
firectl

Get firecracker kernel

curl -fsSL -o /tmp/hello-vmlinux.bin https://s3.amazonaws.com/spec.ccfc.min/img/hello/kernel/hello-vmlinux.bin

Setup tap interface - Optional for network

sudo ip tuntap add tap0 mode tap # user $(id -u) group $(id -g)
sudo ip addr add 172.20.0.1/24 dev tap0
sudo ip link set tap0 up

Set your main interface device. If you have different name check it with ifconfig command

DEVICE_NAME=eth0

Provide iptables rules to enable packet forwarding

sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
sudo iptables -t nat -A POSTROUTING -o $DEVICE_NAME -j MASQUERADE
sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i tap0 -o $DEVICE_NAME -j ACCEPT

Build docker image

make

Write your simple golang application

1. Write your demo go application
mkdir demo
cat > demo/main.go <<EOF
package main

import (
  "fmt"
  "os/exec"
  "time"
)

func main() {
  for {
    fmt.Println("Hello from firecracker (run by unik from solo.io)")
    out, _ := exec.Command("uname", "-a").CombinedOutput()
    fmt.Printf("OS Version: %s\n", string(out))
    time.Sleep(10 * time.Second)
  }
}
EOF
2. Build root filesystem using build script
./build ./demo my_root_fs
3. Use firectl to run firecracker with your rootfs
ROOTFS="$(readlink -f my_root_fs)"
sudo firectl \
   --kernel=/tmp/hello-vmlinux.bin \
   --root-drive=$ROOTFS \ 
   --kernel-opts="console=ttyS0 noapic reboot=k panic=1 pci=off nomodules rw" 

Write your golang Http Server (need tap device setup)

1. Write your demo go application
mkdir demo
cat > demo/main.go <<EOF
package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}
EOF
2. Build root filesystem using build script
./build ./demo my_root_fs 172.20.0.2/24 172.20.0.1
3. Use firectl to run firecracker with your rootfs
MAC="$(cat /sys/class/net/tap0/address)"
ROOTFS="$(readlink -f my_root_fs)"
sudo firectl \
   --kernel=/tmp/hello-vmlinux.bin \
   --root-drive=$ROOTFS \ 
   --kernel-opts="console=ttyS0 noapic reboot=k panic=1 pci=off nomodules rw" \ 
   --tap-device=tap0/$MAC
4. Test your application server
curl http://172.20.0.2:8080/firecracker

firecracker-go-template's People

Contributors

s8sg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.