|
| 1 | +#!/bin/bash -e |
| 2 | +SB_ID="${1:-0}" # Default to sb_id=0 |
| 3 | + |
| 4 | +START_TS=`date +%s%N | cut -b1-13` |
| 5 | + |
| 6 | +#RO_DRIVE="$PWD/rootfs.ext4" |
| 7 | +RO_DRIVE="$PWD/xenial.rootfs.ext4" |
| 8 | +#RW_DRIVE="$PWD/rw-drives/fc${SB_ID}.ext4" |
| 9 | +RW_DRIVE="$PWD/fc-rw.ext4" |
| 10 | + |
| 11 | +# TODO: Boot vmlinuz/bzImage when supported, https://sim.amazon.com/issues/P12329852 |
| 12 | +KERNEL="$PWD/vmlinux" |
| 13 | +TAP_DEV="fc-sb${SB_ID}-tap0" |
| 14 | + |
| 15 | +# Enable if using bootchart |
| 16 | +INIT="/init" |
| 17 | +#R_INIT="init=$INIT" |
| 18 | +#BOOTCHART_ARGS="initcall_debug printk.time=y quiet init=/sbin/bootchartd bootchart_init=$INIT" |
| 19 | + |
| 20 | +KERNEL_BOOT_ARGS="panic=1 pci=off reboot=k tsc=reliable quiet 8250.nr_uarts=0 ipv6.disable=1 $R_INIT" |
| 21 | +#KERNEL_BOOT_ARGS="console=ttyS0 reboot=k panic=1 pci=off nomodules ipv6.disable=1 $R_INIT" |
| 22 | + |
| 23 | +API_SOCKET="/tmp/firecracker-sb${SB_ID}.sock" |
| 24 | +CURL=(curl --silent --show-error --header Content-Type:application/json --unix-socket "${API_SOCKET}" --write-out "HTTP %{http_code}") |
| 25 | + |
| 26 | +curl_put() { |
| 27 | + local URL_PATH="$1" |
| 28 | + local OUTPUT RC |
| 29 | + OUTPUT="$("${CURL[@]}" -X PUT --data @- "http://localhost/${URL_PATH#/}" 2>&1)" |
| 30 | + RC="$?" |
| 31 | + if [ "$RC" -ne 0 ]; then |
| 32 | + echo "Error: curl PUT ${URL_PATH} failed with exit code $RC, output:" |
| 33 | + echo "$OUTPUT" |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + # Error if output doesn't end with "HTTP 2xx" |
| 37 | + if [[ "$OUTPUT" != *HTTP\ 2[0-9][0-9] ]]; then |
| 38 | + echo "Error: curl PUT ${URL_PATH} failed with non-2xx HTTP status code, output:" |
| 39 | + echo "$OUTPUT" |
| 40 | + return 1 |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +logfile="$PWD/output/fc-sb${SB_ID}-log" |
| 45 | +metricsfile="$PWD/output/fc-sb${SB_ID}-metrics" |
| 46 | +#metricsfile="/dev/null" |
| 47 | + |
| 48 | +touch $logfile |
| 49 | +touch $metricsfile |
| 50 | + |
| 51 | + |
| 52 | +# Setup TAP device that uses proxy ARP |
| 53 | +MASK_LONG="255.255.255.252" |
| 54 | +MASK_SHORT="/30" |
| 55 | +FC_IP="$(printf '169.254.%s.%s' $(((4 * SB_ID + 1) / 256)) $(((4 * SB_ID + 1) % 256)))" |
| 56 | +TAP_IP="$(printf '169.254.%s.%s' $(((4 * SB_ID + 2) / 256)) $(((4 * SB_ID + 2) % 256)))" |
| 57 | +FC_MAC="$(printf '02:FC:00:00:%02X:%02X' $((SB_ID / 256)) $((SB_ID % 256)))" |
| 58 | +ip link del "$TAP_DEV" 2> /dev/null || true |
| 59 | +ip tuntap add dev "$TAP_DEV" mode tap |
| 60 | +sysctl -w net.ipv4.conf.${TAP_DEV}.proxy_arp=1 > /dev/null |
| 61 | +sysctl -w net.ipv6.conf.${TAP_DEV}.disable_ipv6=1 > /dev/null |
| 62 | +ip addr add "${TAP_IP}${MASK_SHORT}" dev "$TAP_DEV" |
| 63 | +ip link set dev "$TAP_DEV" up |
| 64 | + |
| 65 | +KERNEL_BOOT_ARGS="${KERNEL_BOOT_ARGS} ip=${FC_IP}::${TAP_IP}:${MASK_LONG}::eth0:off" |
| 66 | + |
| 67 | +# Start Firecracker API server |
| 68 | +rm -f "$API_SOCKET" |
| 69 | + |
| 70 | +INSTANTIATE_TS=`date +%s%N | cut -b1-13` |
| 71 | +SETUP_DELTA=$(($INSTANTIATE_TS - $START_TS)) |
| 72 | + |
| 73 | +#if (($SETUP_DELTA > 99)) ; then |
| 74 | +# if [ ! -e perf.pid ] ; then |
| 75 | +# perf record -a -o perf-data/pdata${SB_ID} --call-graph dwarf sleep 5 > /dev/null 2>&1 & |
| 76 | +# echo $! > perf.pid |
| 77 | +# fi |
| 78 | +#else |
| 79 | +# if [ -e perf.pid ] ; then |
| 80 | +# kill `cat perf.pid` || true |
| 81 | +# rm -f perf.pid || true |
| 82 | +# fi |
| 83 | +#fi |
| 84 | + |
| 85 | +echo "Bash setup overhead $SETUP_DELTA ms" |
| 86 | + |
| 87 | +#screen -dmLS "fc-sb${SB_ID}" \ |
| 88 | +#/usr/bin/env "$PWD/firecracker" \ |
| 89 | +# --api-sock "$API_SOCKET" |
| 90 | + |
| 91 | +./firecracker --api-sock "$API_SOCKET" --context '{"id": "fc-'${SB_ID}'", "jailed": false, "seccomp_level": 0, "start_time_us": 0, "start_time_cpu_us": 0}' & |
| 92 | +#echo trying $CMD |
| 93 | +#$($CMD) |
| 94 | + |
| 95 | +sleep 0.005s |
| 96 | + |
| 97 | +START_TS=`date +%s%N | cut -b1-13` |
| 98 | + |
| 99 | +# Wait for API server to start |
| 100 | +while [ ! -e "$API_SOCKET" ]; do |
| 101 | + echo "FC $SB_ID still not ready..." |
| 102 | + sleep 0.01s |
| 103 | +done |
| 104 | + |
| 105 | +curl_put '/logger' <<EOF |
| 106 | +{ |
| 107 | + "log_fifo": "$logfile", |
| 108 | + "metrics_fifo": "$metricsfile", |
| 109 | + "level": "Warning", |
| 110 | + "show_level": false, |
| 111 | + "show_log_origin": false |
| 112 | +} |
| 113 | +EOF |
| 114 | + |
| 115 | +curl_put '/machine-config' <<EOF |
| 116 | +{ |
| 117 | + "vcpu_count": 1, |
| 118 | + "mem_size_mib": 128 |
| 119 | +} |
| 120 | +EOF |
| 121 | + |
| 122 | +curl_put '/boot-source' <<EOF |
| 123 | +{ |
| 124 | + "kernel_image_path": "$KERNEL", |
| 125 | + "boot_args": "$KERNEL_BOOT_ARGS" |
| 126 | +} |
| 127 | +EOF |
| 128 | + |
| 129 | +curl_put '/drives/1' <<EOF |
| 130 | +{ |
| 131 | + "drive_id": "1", |
| 132 | + "path_on_host": "$RO_DRIVE", |
| 133 | + "is_root_device": true, |
| 134 | + "is_read_only": true, |
| 135 | + "rate_limiter": { |
| 136 | + "bandwidth": { |
| 137 | + "size": 104857600, |
| 138 | + "refill_time": 100 |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | +EOF |
| 143 | + |
| 144 | +curl_put '/drives/2' <<EOF |
| 145 | +{ |
| 146 | + "drive_id": "2", |
| 147 | + "path_on_host": "$RW_DRIVE", |
| 148 | + "is_root_device": false, |
| 149 | + "is_read_only": false |
| 150 | +} |
| 151 | +EOF |
| 152 | + |
| 153 | +curl_put '/network-interfaces/1' <<EOF |
| 154 | +{ |
| 155 | + "iface_id": "1", |
| 156 | + "guest_mac": "$FC_MAC", |
| 157 | + "host_dev_name": "$TAP_DEV", |
| 158 | + "state": "Attached" |
| 159 | +} |
| 160 | +EOF |
| 161 | + |
| 162 | +curl_put '/actions' <<EOF |
| 163 | +{ |
| 164 | + "action_type": "InstanceStart" |
| 165 | +} |
| 166 | +EOF |
| 167 | + |
| 168 | +BOOTSTART_TS=`date +%s%N | cut -b1-13` |
| 169 | +#let CURL_DELTA=$BOOTSTART_TS - $START_TS |
| 170 | +CURL_DELTA=$(($BOOTSTART_TS - $START_TS)) |
| 171 | + |
| 172 | +echo "Curl overhead: $CURL_DELTA ms" |
| 173 | + |
| 174 | +#exec screen -r "fc-sb${SB_ID}" |
0 commit comments