From 2870d470c69e1adc1bf5919ef0315feefc37464b Mon Sep 17 00:00:00 2001 From: iceBear67 Date: Mon, 13 Jul 2026 17:22:24 +0800 Subject: [PATCH] replace script with makefile --- BUILD.sh | 69 ---------------------------------------------------- Makefile | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 +- 3 files changed, 74 insertions(+), 70 deletions(-) delete mode 100755 BUILD.sh create mode 100644 Makefile diff --git a/BUILD.sh b/BUILD.sh deleted file mode 100755 index 3f04b5d..0000000 --- a/BUILD.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/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} - -HY_OPTS=${HY_OPTS:-"--no-cache"} - -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 . diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9b4e044 --- /dev/null +++ b/Makefile @@ -0,0 +1,73 @@ +SHELL := /bin/bash +export PATH := $(PWD)/scripts:$(PATH) + +IMAGE_TAG := $(shell git rev-parse --short HEAD) +IMAGE_NAME ?= bearcloud +HY_OPTS ?= --no-cache +VM_OPTS ?= + +SECRET_KEYS := ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key +KEYTYPE_ssh_host_ecdsa_key := ecdsa +KEYTYPE_ssh_host_ed25519_key := ed25519 +KEYTYPE_ssh_host_rsa_key := rsa + +SECRET_FILES := $(addprefix secret/,$(SECRET_KEYS)) + +.PHONY: all check-root check-deps validate secrets builder vm hypervisor confirm + +all: vm hypervisor + +check-root: + @if [ "$$(id -u)" != "0" ]; then \ + echo "This script must be run in root."; \ + exit 2; \ + fi + +check-deps: + @if ! command -v ssh-keygen >/dev/null; then \ + echo "ssh-keygen is required for guest setup."; \ + exit 1; \ + fi + +validate: check-deps + @validate.sh || { echo "env validation failed"; exit 1; } + +confirm: check-root validate + @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 + +$(SECRET_FILES): + @echo "Creating missing secret $@" + ssh-keygen -t "$(KEYTYPE_$(notdir $@))" -f "$@" \ + -C "automatically generated bearcloud ssh key" \ + -N "" + ssh-keygen -y -f "$@" > "$@_pub" + +secrets: $(SECRET_FILES) + +builder: + @if ! docker buildx ls | grep -q "bearcloud"; then \ + docker buildx create --name bearcloud \ + --buildkitd-flags '--allow-insecure-entitlement security.insecure'; \ + fi + +vm: confirm secrets builder + @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 + +hypervisor: confirm secrets + @echo "BUILDING HYPERVISOR IMAGE" + docker build -t "$(IMAGE_NAME):$(IMAGE_TAG)" -t "$(IMAGE_NAME):latest" \ + -f hypervisor.Dockerfile $(HY_OPTS) . diff --git a/README.md b/README.md index c14b84e..445006b 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Edit corresponding files in [image/overlay](./image/overlay) to customize VM beh ```bash # cp .env.example .env # vim .env -# VM_OPTS="--no-cache" HY_OPTS="--no-cache" ./BUILD.sh +# VM_OPTS="--no-cache" HY_OPTS="--no-cache" make # fallocate -l 128G ./data/data.raw # sgdisk -o -n 1:0:0 -t 1:8300 ./data.raw # losetup -Pf ./data.raw