Skip to content

Instantly share code, notes, and snippets.

@bzub
Created March 8, 2020 17:33
Show Gist options
  • Save bzub/821190de5a58cf2e93e9cc01def89831 to your computer and use it in GitHub Desktop.
Save bzub/821190de5a58cf2e93e9cc01def89831 to your computer and use it in GitHub Desktop.
Flatcar Linux via Flatcar
#!/usr/bin/env sh
set -e
command="${1}"
usage="${0} start|stop"
if [ -z "${command}" ]; then
echo "ERROR: command argument required. [start|stop]"
echo "${usage}"
exit 1
fi
bin="${BIN_DIR}"
if [ -z "${bin}" ]; then
bin="/usr/local/bin"
fi
flatcar_assets="${FLATCAR_ASSETS_DIR}"
if [ -z "${flatcar_assets}" ]; then
flatcar_assets="/tmp/flatcar"
fi
mkdir -p "${flatcar_assets}"
tap_if="${TAP_IF}"
if [ -z "${tap_if}" ]; then
tap_if="tap0"
fi
channel="${FLATCAR_CHANNEL}"
if [ -z "${channel}" ]; then
channel="stable"
fi
if [ ! -f "${bin}/firecracker" ]; then
curl -L -o "${bin}/firecracker" \
"https://github.com/firecracker-microvm/firecracker/releases/download/v0.21.0/firecracker-v0.21.0-x86_64"
chmod +x "${bin}/firecracker"
fi
if [ ! -f "${bin}/extract-vmlinux" ]; then
curl -L -o "${bin}/extract-vmlinux" "https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux"
chmod +x "${bin}/extract-vmlinux"
fi
if [ ! -f "${flatcar_assets}/vmlinux" ]; then
curl -L -o "${flatcar_assets}/vmlinuz" "https://${channel}.release.flatcar-linux.net/amd64-usr/current/flatcar_production_pxe.vmlinuz"
/usr/local/bin/extract-vmlinux "${flatcar_assets}/vmlinuz" > "${flatcar_assets}/vmlinux"
fi
if [ ! -f "${flatcar_assets}/initramfs" ]; then
curl -L "https://${channel}.release.flatcar-linux.net/amd64-usr/current/flatcar_production_pxe_image.cpio.gz" |\
gunzip > "${flatcar_assets}/initramfs"
fi
echo "[INFO] Determining public facing network interface."
default_route="$(ip route|grep default|awk '{print $3}')"
host_if="$(cat /proc/net/arp|grep -F "${default_route}"|awk '{print $6}'|head -n1)"
if [ -z "${host_if}" ]; then
echo "ERROR: Unable to determine public facing network interface."
exit 1
fi
echo "[INFO] Using \"${host_if}\"."
if [ "${command}" == "start" ]; then
vm_config="${VM_CONFIG}"
if [ -z "${vm_config}" ]; then
echo "[ERROR] VM_CONFIG firecracker json config file not provided."
exit 1
fi
echo "[INFO] Using firecracker VM config \"${vm_config}\"."
vm_mac="$(cat "${vm_config}" | jq '.["network-interfaces"]|.[]|.guest_mac' -r)"
echo "[INFO] VM Mac: \"${vm_mac}\"."
echo "[INFO] Creating macvtap network interface \"${tap_if}\" from interface \"${host_if}\"."
ip link add link "${host_if}" name "${tap_if}" type macvtap mode bridge
ip link set tap0 address "${vm_mac}" up
"${bin}/firecracker" --no-api --config-file "${vm_config}"
elif [ "${command}" == "stop" ]; then
set +e
echo "[INFO] Deleting macvtap network interface \"${tap_if}\""
ip link del dev "${tap_if}"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment