Repository: firecracker-microvm/firecracker
File: resources/rebuild.sh (Shell)

Code before:
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# fail if we encounter an error, uninitialized variable or a pipe breaks
set -eu -o pipefail

PS4='+\t '

cd $(dirname $0)
ARCH=$(uname -m)
OUTPUT_DIR=$PWD/$ARCH

GIT_ROOT_DIR=$(git rev-parse --show-toplevel)
source "$GIT_ROOT_DIR/tools/functions"

# Make sure we have all the needed tools
function install_dependencies {
    apt update
    apt install -y bc flex bison gcc make libelf-dev libssl-dev squashfs-tools busybox-static tree cpio curl patch docker.io

    # Install Go
    case $ARCH in
        x86_64) arch=amd64 ;;
        aarch64) arch=arm64 ;;
    esac
    version=$(curl -s https://go.dev/VERSION?m=text | head -n 1)
    archive="${version}.linux-${arch}.tar.gz"
    curl -LO http://go.dev/dl/${archive}
    tar -C /usr/local -xzf $archive
    export PATH=$PATH:/usr/local/go/bin
    go version
    rm $archive
}

function prepare_docker {
    nohup /usr/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 &

    # Wait for Docker socket to be created
    timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
}

function compile_and_install {
    local C_FILE=$1
    local BIN_FILE=$2
    local OUTPUT_DIR=$(dirname $BIN_FILE)
    mkdir -pv $OUTPUT_DIR
    gcc -Wall -o $BIN_FILE $C_FILE
}

# Build a rootfs
function build_rootfs {

Review comment (suggestion):
nit: you're only using this variable once, so could just inline it, and instead have a `case` statement when defining `$version`. Would avoid having two variables named `$arch` that are slightly different.

Code after:
# Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# fail if we encounter an error, uninitialized variable or a pipe breaks
set -eu -o pipefail

PS4='+\t '

cd $(dirname $0)
ARCH=$(uname -m)
OUTPUT_DIR=$PWD/$ARCH

GIT_ROOT_DIR=$(git rev-parse --show-toplevel)
source "$GIT_ROOT_DIR/tools/functions"

# Make sure we have all the needed tools
function install_dependencies {
    apt update
    apt install -y bc flex bison gcc make libelf-dev libssl-dev squashfs-tools busybox-static tree cpio curl patch docker.io

    # Install Go
    version=$(curl -s https://go.dev/VERSION?m=text | head -n 1)
    case $ARCH in
        x86_64) archive="${version}.linux-amd64.tar.gz" ;;
        aarch64) archive="${version}.linux-arm64.tar.gz" ;;
    esac
    curl -LO http://go.dev/dl/${archive}
    tar -C /usr/local -xzf $archive
    export PATH=$PATH:/usr/local/go/bin
    go version
    rm $archive
}

function prepare_docker {
    nohup /usr/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 &

    # Wait for Docker socket to be created
    timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
}

function compile_and_install {
    local SRC=$1
    local BIN="${SRC%.*}"
    if [[ $SRC == *.c ]]; then
        gcc -Wall -o $BIN $SRC
    elif [[ $SRC == *.go ]]; then
        pushd $SRC
        local MOD=$(basename $BIN)
        go mod init $MOD
        go mod tidy
        go build -o ../$MOD