Skip to content

Instantly share code, notes, and snippets.

@vitali2y
Created January 28, 2021 23:20
Show Gist options
  • Save vitali2y/f42a5c0214551539d7d0cefc9b5a8c5c to your computer and use it in GitHub Desktop.
Save vitali2y/f42a5c0214551539d7d0cefc9b5a8c5c to your computer and use it in GitHub Desktop.
First steps on Firecracker, a lightweight virtualization solution for serverless computing

First steps on Firecracker, a lightweight virtualization solution for serverless computing

Table Of Contents

General

Please follow Getting Started with Firecracker for more details.

Prerequisites

Preliminary three Firecracker-v0.23.2-x86_64.tgz, hello-vmlinux.bin, hello-rootfs.ext4 files were downloaded, and Firecracker-v0.23.2-x86_64.tgz was unpacked.

On 1st Console

Note: two consoles will be used at the beginning - so, follow On 2nd Console section for second console session after running firecracker on the first one.

➜  firecracker pwd && ls -la
/home/vit/prjs/firecracker
total 54968
drwxrwxr-x  2 vit vit     4096 Jan 28 22:49 .
drwxr-xr-x 15 vit vit     4096 Jan 28 22:45 ..
lrwxrwxrwx  1 vit vit       26 Jan 28 22:47 firecracker -> firecracker-v0.23.2-x86_64
-rw-rw-r--  1 vit vit    26507 Jan 15 18:52 firecracker_spec-v0.23.2.yaml
-rwxr-xr-x  1 vit vit  2046336 Jan 15 18:52 firecracker-v0.23.2-x86_64
-rw-rw-r--  1 vit vit 31457280 Jan 28 22:49 hello-rootfs.ext4
-rw-rw-r--  1 vit vit 21266136 Jan 28 22:48 hello-vmlinux.bin
lrwxrwxrwx  1 vit vit       21 Jan 28 22:47 jailer -> jailer-v0.23.2-x86_64
-rwxr-xr-x  1 vit vit  1457312 Jan 15 18:52 jailer-v0.23.2-x86_64
-rw-rw-r--  1 vit vit    11358 Jan 15 18:52 LICENSE
-rw-rw-r--  1 vit vit       89 Jan 15 18:52 NOTICE
-rw-rw-r--  1 vit vit     1562 Jan 15 18:52 THIRD-PARTY
➜  firecracker # after running the next command just continue from 2nd tty
➜  firecracker ./firecracker --api-sock /tmp/firecracker.socket
[    0.000000] Linux version 4.14.55-84.37.amzn2.x86_64 (mockbuild@ip-10-0-1-79) (gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)) #1 SMP Wed Jul 25 18:47:15 UTC 2018
[    0.000000] Command line: console=ttyS0 reboot=k panic=1 pci=off root=/dev/vda rw virtio_mmio.device=4K@0xd0000000:5
~...~
[    0.116064] VFS: Mounted root (ext4 filesystem) on device 254:0.
[    0.117313] devtmpfs: mounted
[    0.118587] Freeing unused kernel memory: 1268K
[    0.124111] Write protecting the kernel read-only data: 12288k
[    0.126557] Freeing unused kernel memory: 2016K
[    0.128780] Freeing unused kernel memory: 584K
OpenRC init version 0.35.5.87b1ff59c1 starting
Starting sysinit runlevel

   OpenRC 0.35.5.87b1ff59c1 is starting up Linux 4.14.55-84.37.amzn2.x86_64 (x86_64)

 * Mounting /proc ...
 [ ok ]
 * Mounting /run ...
 * /run/openrc: creating directory
 * /run/lock: creating directory
 * /run/lock: correcting owner
 * Caching service dependencies ...
Service `hwdrivers' needs non existent service `dev'
 [ ok ]
Starting boot runlevel
 * Remounting devtmpfs on /dev ...
 [ ok ]
 * Mounting /dev/mqueue ...
 [ ok ]
 * Mounting /dev/pts ...
 [ ok ]
 * Mounting /dev/shm ...
 [ ok ]
 * Setting hostname ...
 [ ok ]
 * Checking local filesystems  ...
 [ ok ]
 * Remounting filesystems ...
 [ ok[    0.307292] random: fast init done
 ]
 * Mounting local filesystems ...
 [ ok ]
 * Loading modules ...
modprobe: can't change directory to '/lib/modules': No such file or directory
modprobe: can't change directory to '/lib/modules': No such file or directory
 [ ok ]
 * Mounting misc binary format filesystem ...
 [ ok ]
 * Mounting /sys ...
 [ ok ]
 * Mounting security filesystem ...
 [ ok ]
 * Mounting debug filesystem ...
 [ ok ]
 * Mounting SELinux filesystem ...
 [ ok ]
 * Mounting persistent storage (pstore) filesystem ...
 [ ok ]
Starting default runlevel
[    1.056069] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x211399552f8, max_idle_ns: 440795292447 ns

Welcome to Alpine Linux 3.8
Kernel 4.14.55-84.37.amzn2.x86_64 on an x86_64 (ttyS0)

localhost login: root
Password:        <- root
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

login[855]: root login on 'ttyS0'
localhost:~# uname -a && ps -fe && df -h
Linux localhost 4.14.55-84.37.amzn2.x86_64 #1 SMP Wed Jul 25 18:47:15 UTC 2018 x86_64 Linux
PID   USER     TIME  COMMAND
    1 root      0:00 {openrc-init} /sbin/init
    2 root      0:00 [kthreadd]
    3 root      0:00 [kworker/0:0]
    4 root      0:00 [kworker/0:0H]
    5 root      0:00 [kworker/u2:0]
    6 root      0:00 [mm_percpu_wq]
    7 root      0:00 [ksoftirqd/0]
    8 root      0:00 [rcu_sched]
    9 root      0:00 [rcu_bh]
   10 root      0:00 [migration/0]
   11 root      0:00 [cpuhp/0]
   12 root      0:00 [kdevtmpfs]
   13 root      0:00 [netns]
   14 root      0:00 [kworker/u2:1]
   15 root      0:00 [oom_reaper]
  132 root      0:00 [kworker/0:1]
  133 root      0:00 [writeback]
  134 root      0:00 [kcompactd0]
  136 root      0:00 [ksmd]
  137 root      0:00 [crypto]
  138 root      0:00 [kintegrityd]
  140 root      0:00 [kblockd]
  247 root      0:00 [kauditd]
  252 root      0:00 [kswapd0]
  385 root      0:00 [kthrotld]
  431 root      0:00 [ipv6_addrconf]
  440 root      0:00 [kstrp]
  455 root      0:00 [kworker/0:1H]
  456 root      0:00 [jbd2/vda-8]
  457 root      0:00 [ext4-rsv-conver]
  846 root      0:00 supervise-daemon agetty.ttyS0 --start --pidfile /run/agett
  848 root      0:00 /bin/login -- root
  854 root      0:00 [kworker/u2:2]
  855 root      0:00 -ash
  857 root      0:00 ps -fe
Filesystem                Size      Used Available Use% Mounted on
/dev/root                28.0M     21.0M      4.9M  81% /
devtmpfs                 10.0M         0     10.0M   0% /dev
tmpfs                    11.2M     80.0K     11.1M   1% /run
shm                      56.1M         0     56.1M   0% /dev/shm
localhost:~# ls -la /
total 31
drwxr-xr-x   19 root     root          1024 Jan 28 21:03 .
drwxr-xr-x   19 root     root          1024 Jan 28 21:03 ..
drwxr-xr-x    2 root     root          3072 Oct 19  2018 bin
drwxr-xr-x    7 root     root          2120 Jan 28 21:03 dev
drwxr-xr-x   22 root     root          1024 Oct 19  2018 etc
drwxr-xr-x    2 root     root          1024 Sep 11  2018 home
drwxr-xr-x    6 root     root          1024 Oct 19  2018 lib
drwx------    2 root     root         12288 Oct 18  2018 lost+found
drwxr-xr-x    5 root     root          1024 Sep 11  2018 media
drwxr-xr-x    2 root     root          1024 Sep 11  2018 mnt
dr-xr-xr-x   43 root     root             0 Jan 28 21:03 proc
drwx------    3 root     root          1024 Oct 19  2018 root
drwxr-xr-x    5 root     root           140 Jan 28 21:03 run
drwxr-xr-x    2 root     root          4096 Oct 19  2018 sbin
drwxr-xr-x    2 root     root          1024 Sep 11  2018 srv
dr-xr-xr-x   12 root     root             0 Jan 28 21:03 sys
drwxrwxrwt    2 root     root          1024 Sep 11  2018 tmp
drwxr-xr-x    9 root     root          1024 Oct 19  2018 usr
drwxr-xr-x   11 root     root          1024 Oct 19  2018 var
localhost:~# cat /etc/os-release 
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.8.1
PRETTY_NAME="Alpine Linux v3.8"
HOME_URL="http://alpinelinux.org"
BUG_REPORT_URL="http://bugs.alpinelinux.org"
localhost:~# reboot 
PID1: Received "reboot" from FIFO...
localhost:~# Starting reboot runlevel
 * Unmounting loop devices
 * Unmounting filesystems
Sending the final term signal
Sending the final kill signal
[  755.990303] Unregister pv shared memory for cpu 0
[  755.991998] reboot: Restarting system
[  755.993229] reboot: machine restart
➜  firecracker   # we are back to my OS

On 2nd Console

➜  firecracker kernel_path=$(pwd)"/hello-vmlinux.bin" && curl --unix-socket /tmp/firecracker.socket -i \
      -X PUT 'http://localhost/boot-source'   \
      -H 'Accept: application/json'           \
      -H 'Content-Type: application/json'     \
      -d "{
            \"kernel_image_path\": \"${kernel_path}\",
            \"boot_args\": \"console=ttyS0 reboot=k panic=1 pci=off\"
       }"
HTTP/1.1 204 
Server: Firecracker API
Connection: keep-alive

➜  firecracker rootfs_path=$(pwd)"/hello-rootfs.ext4"
curl --unix-socket /tmp/firecracker.socket -i \
  -X PUT 'http://localhost/drives/rootfs' \
  -H 'Accept: application/json'           \
  -H 'Content-Type: application/json'     \
  -d "{
        \"drive_id\": \"rootfs\",
        \"path_on_host\": \"${rootfs_path}\",
        \"is_root_device\": true,
        \"is_read_only\": false
   }"
HTTP/1.1 204 
Server: Firecracker API
Connection: keep-alive

➜  firecracker curl --unix-socket /tmp/firecracker.socket -i \
  -X PUT 'http://localhost/actions'       \
  -H  'Accept: application/json'          \
  -H  'Content-Type: application/json'    \
  -d '{
      "action_type": "InstanceStart"
   }'
HTTP/1.1 204 
Server: Firecracker API
Connection: keep-alive

➜  firecracker # now I can login from first tty
➜  firecracker [ -r /dev/kvm ] && [ -w /dev/kvm ] && echo "OK" || echo "FAIL"
OK
➜  firecracker

Running Firecracker By Single Command

➜  firecracker cat vm_config.json
{
  "boot-source": {
    "kernel_image_path": "hello-vmlinux.bin",
    "boot_args": "console=ttyS0 reboot=k panic=1 pci=off"
  },
  "drives": [
    {
      "drive_id": "rootfs",
      "path_on_host": "hello-rootfs.ext4",
      "is_root_device": true,
      "is_read_only": false
    }
  ],
  "machine-config": {
    "vcpu_count": 2,
    "mem_size_mib": 1024,
    "ht_enabled": false
  }
}
➜  firecracker ls -la /tmp/firecracker.socket 
srwxrwxr-x 1 vit vit 0 Jan 28 22:49 /tmp/firecracker.socket
➜  firecracker rm -f /tmp/firecracker.socket 
➜  firecracker ./firecracker --api-sock /tmp/firecracker.socket --config-file vm_config.json
[    0.000000] Linux version 4.14.55-84.37.amzn2.x86_64 (mockbuild@ip-10-0-1-79) (gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)) #1 SMP Wed Jul 25 18:47:15 UTC 2018
[    0.000000] Command line: console=ttyS0 reboot=k panic=1 pci=off root=/dev/vda rw virtio_mmio.device=4K@0xd0000000:5
~...~
Starting default runlevel
[    1.084236] clocksource: tsc: mask: 0xffffffffffffffff max_cycles: 0x211399552f8, max_idle_ns: 440795292447 ns

Welcome to Alpine Linux 3.8
Kernel 4.14.55-84.37.amzn2.x86_64 on an x86_64 (ttyS0)

localhost login:

Useful Resources

@vitali2y
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment