Files
bearnet/BUILD.sh
iceBear67 bf67b8b95f refactor
2026-07-05 13:49:09 +08:00

68 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
set -eo pipefail
if [ "$UID" != "0" ]; then
echo "This script must be run in root."
exit 2
fi
if ! command -v "ssh-keygen"; then
echo "ssh-keygen is required for guest setup."
exit 1
fi
PATH="$PWD/scripts:$PATH"
validate.sh
if [[ "$?" != "0" ]]; then
echo "env validation failed"
exit 1
fi
IMAGE_TAG=$(git rev-parse --short HEAD)
IMAGE_NAME=${IMAGE_NAME:-bearcloud}
echo "Image tag: $IMAGE_NAME:$IMAGE_TAG and $IMAGE_NAME:latest"
echo "Additional arguments for VM image: $VM_OPTS"
echo "Additional arguments for Hypervisor Image: $HY_OPTS"
echo "Missing secret files like ssh host key will be automatically created."
echo "Continue?"
read
declare -A PRIVATE_KEYS=(["ssh_host_ecdsa_key"]="ecdsa"
["ssh_host_ed25519_key"]="ed25519"
["ssh_host_rsa_key"]="rsa")
for item in "${!PRIVATE_KEYS[@]}"; do
subject="secret/$item"
if [[ ! -f $subject ]]; then
echo "Creating missing secret $subject"
ssh-keygen -t "${PRIVATE_KEYS[$item]}" -f "$subject" \
-C "automatically generated bearcloud ssh key" \
-N ""
ssh-keygen -y -f "$subject" > "${subject}_pub"
fi
done
BUILDERS=$(docker buildx ls)
if ! (echo $BUILDERS | grep -q "bearcloud"); then
docker buildx create --name bearcloud --buildkitd-flags '--allow-insecure-entitlement security.insecure'
fi
echo "BUILDING VM DISK IMAGE"
docker build \
--builder bearcloud \
--allow security.insecure \
-f vm.Dockerfile \
--build-context host-modules=/lib/modules \
--target export \
--output type=local,dest=./data \
$VM_OPTS .
fallocate -d ./data/vm.raw
echo "BUILDING HYPERVISOR IMAGE"
docker build -t "$IMAGE_NAME:$IMAGE_TAG" -t "$IMAGE_NAME:latest" \
-f hypervisor.Dockerfile $HY_OPTS .