This commit is contained in:
mofeng-git
2025-12-28 18:19:16 +08:00
commit d143d158e4
771 changed files with 220548 additions and 0 deletions

76
build/Dockerfile.runtime Normal file
View File

@@ -0,0 +1,76 @@
# One-KVM Runtime Image
# This Dockerfile only packages pre-compiled binaries (no compilation)
# Used after cross-compiling with `cross build`
ARG TARGETPLATFORM=linux/amd64
FROM debian:12-slim
ARG TARGETPLATFORM
# Install runtime dependencies only (matches build environment)
RUN apt-get update && apt-get install -y --no-install-recommends \
# Runtime libraries
libasound2 \
libv4l-0 \
libudev1 \
zlib1g \
libjpeg62-turbo \
libyuv0 \
# FFmpeg runtime
libavcodec59 \
libavformat59 \
libavutil57 \
libswscale6 \
libswresample4 \
# Video codecs
libx264-164 \
libx265-199 \
libvpx7 \
# Audio codec
libopus0 \
# Hardware acceleration
libva2 \
libva-drm2 \
libdrm2 \
# X11 (for VAAPI)
libx11-6 \
libxcb1 \
# Utilities
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Intel Media SDK runtime for x86_64 only
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
apt-get update && \
apt-get install -y --no-install-recommends libmfx1 && \
rm -rf /var/lib/apt/lists/*; \
fi
# Create directories
RUN mkdir -p /etc/one-kvm/ventoy
# Copy init script
COPY init.sh /init.sh
# Copy binaries (these are placed by the build script)
COPY one-kvm /usr/bin/one-kvm
COPY ttyd /usr/bin/ttyd
COPY gostc /usr/bin/gostc
COPY easytier-core /usr/bin/easytier-core
# Copy ventoy resources if they exist
COPY ventoy/ /etc/one-kvm/ventoy/
# Set permissions and verify
RUN chmod +x /init.sh /usr/bin/one-kvm /usr/bin/ttyd /usr/bin/gostc /usr/bin/easytier-core && \
/usr/bin/one-kvm --help
# Expose ports
EXPOSE 8080 8443
# Data volume
VOLUME ["/etc/one-kvm"]
# Entrypoint
CMD ["/init.sh"]

View File

@@ -0,0 +1,66 @@
# Cross-compilation image for ARM64 based on Debian 12
# Uses multiarch to install ARM64 libraries on x86_64 host
FROM debian:12
# Install Rust toolchain
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable \
&& rm -rf /var/lib/apt/lists/*
ENV PATH="/root/.cargo/bin:${PATH}"
# Add ARM64 architecture
RUN dpkg --add-architecture arm64
# Install cross-compiler and native build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
pkg-config \
cmake \
nasm \
git \
libclang-dev \
llvm \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
libc6-dev-arm64-cross \
&& rm -rf /var/lib/apt/lists/*
# Install ARM64 development libraries
RUN apt-get update && apt-get install -y --no-install-recommends \
libasound2-dev:arm64 \
libv4l-dev:arm64 \
libudev-dev:arm64 \
zlib1g-dev:arm64 \
libjpeg62-turbo-dev:arm64 \
libyuv-dev:arm64 \
libavcodec-dev:arm64 \
libavformat-dev:arm64 \
libavutil-dev:arm64 \
libswscale-dev:arm64 \
libswresample-dev:arm64 \
libx264-dev:arm64 \
libx265-dev:arm64 \
libvpx-dev:arm64 \
libopus-dev:arm64 \
libva-dev:arm64 \
libdrm-dev:arm64 \
libx11-dev:arm64 \
libxcb1-dev:arm64 \
libxau-dev:arm64 \
libxdmcp-dev:arm64 \
&& rm -rf /var/lib/apt/lists/*
# Add Rust target
RUN rustup target add aarch64-unknown-linux-gnu
# Configure environment for cross-compilation
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc \
CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc \
CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ \
AR_aarch64_unknown_linux_gnu=aarch64-linux-gnu-ar \
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig \
PKG_CONFIG_ALLOW_CROSS=1

View File

@@ -0,0 +1,66 @@
# Cross-compilation image for ARMv7 based on Debian 12
# Uses multiarch to install ARMv7 libraries on x86_64 host
FROM debian:12
# Install Rust toolchain
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable \
&& rm -rf /var/lib/apt/lists/*
ENV PATH="/root/.cargo/bin:${PATH}"
# Add armhf architecture
RUN dpkg --add-architecture armhf
# Install cross-compiler and native build tools
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
pkg-config \
cmake \
nasm \
git \
libclang-dev \
llvm \
gcc-arm-linux-gnueabihf \
g++-arm-linux-gnueabihf \
libc6-dev-armhf-cross \
&& rm -rf /var/lib/apt/lists/*
# Install ARMv7 development libraries
RUN apt-get update && apt-get install -y --no-install-recommends \
libasound2-dev:armhf \
libv4l-dev:armhf \
libudev-dev:armhf \
zlib1g-dev:armhf \
libjpeg62-turbo-dev:armhf \
libyuv-dev:armhf \
libavcodec-dev:armhf \
libavformat-dev:armhf \
libavutil-dev:armhf \
libswscale-dev:armhf \
libswresample-dev:armhf \
libx264-dev:armhf \
libx265-dev:armhf \
libvpx-dev:armhf \
libopus-dev:armhf \
libva-dev:armhf \
libdrm-dev:armhf \
libx11-dev:armhf \
libxcb1-dev:armhf \
libxau-dev:armhf \
libxdmcp-dev:armhf \
&& rm -rf /var/lib/apt/lists/*
# Add Rust target
RUN rustup target add armv7-unknown-linux-gnueabihf
# Configure environment for cross-compilation
ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=arm-linux-gnueabihf-gcc \
CC_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-gcc \
CXX_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-g++ \
AR_armv7_unknown_linux_gnueabihf=arm-linux-gnueabihf-ar \
PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig \
PKG_CONFIG_ALLOW_CROSS=1

View File

@@ -0,0 +1,57 @@
# Cross-compilation image for x86_64 based on Debian 12
# Matches the runtime environment exactly
FROM debian:12
# Install Rust toolchain
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
ca-certificates \
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable \
&& rm -rf /var/lib/apt/lists/*
ENV PATH="/root/.cargo/bin:${PATH}"
# Install build dependencies (same as runtime Debian 12)
RUN apt-get update && apt-get install -y --no-install-recommends \
# Build tools
build-essential \
pkg-config \
cmake \
nasm \
git \
libclang-dev \
llvm \
# Core system libraries
libasound2-dev \
libv4l-dev \
libudev-dev \
zlib1g-dev \
# Video/image processing
libjpeg62-turbo-dev \
libyuv-dev \
# FFmpeg and codecs
libavcodec-dev \
libavformat-dev \
libavutil-dev \
libswscale-dev \
libswresample-dev \
# Video codec libraries
libx264-dev \
libx265-dev \
libvpx-dev \
# Audio codec
libopus-dev \
# Hardware acceleration
libva-dev \
libdrm-dev \
libmfx-dev \
# X11 libraries
libx11-dev \
libxcb1-dev \
libxau-dev \
libxdmcp-dev \
&& rm -rf /var/lib/apt/lists/*
# Add Rust target
RUN rustup target add x86_64-unknown-linux-gnu

39
build/init.sh Normal file
View File

@@ -0,0 +1,39 @@
#!/bin/bash
# One-KVM initialization script
# Container entrypoint to start the one-kvm service
set -e
# Start one-kvm with default options
# Additional options can be passed via environment variables
EXTRA_ARGS=""
# Enable HTTPS if requested
if [ "${ENABLE_HTTPS:-false}" = "true" ]; then
EXTRA_ARGS="$EXTRA_ARGS --enable-https"
fi
# Custom bind address
if [ -n "$BIND_ADDRESS" ]; then
EXTRA_ARGS="$EXTRA_ARGS -a $BIND_ADDRESS"
fi
# Custom port
if [ -n "$HTTP_PORT" ]; then
EXTRA_ARGS="$EXTRA_ARGS -p $HTTP_PORT"
fi
# Verbosity level
if [ -n "$VERBOSE" ]; then
case "$VERBOSE" in
1) EXTRA_ARGS="$EXTRA_ARGS -v" ;;
2) EXTRA_ARGS="$EXTRA_ARGS -vv" ;;
3) EXTRA_ARGS="$EXTRA_ARGS -vvv" ;;
esac
fi
echo "[INFO] Starting one-kvm..."
echo "[INFO] Extra arguments: $EXTRA_ARGS"
# Execute one-kvm
exec /usr/bin/one-kvm $EXTRA_ARGS

358
build/package-docker.sh Executable file
View File

@@ -0,0 +1,358 @@
#!/bin/bash
# One-KVM Docker Image Packaging Script
# Packages pre-compiled binaries into runtime Docker images
#
# Prerequisites:
# 1. Build binaries first: cross build --release --target <target>
# 2. Docker with buildx support
#
# Usage:
# ./build-docker.sh --platform linux/amd64 --load
# ./build-docker.sh --platform linux/arm64 --load
# ./build-docker.sh --push --tag v1.0.0
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
echo_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
echo_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Configuration
REGISTRY="${REGISTRY:-}" # e.g., docker.io/username or ghcr.io/username
IMAGE_NAME="${IMAGE_NAME:-one-kvm}"
TAG="${TAG:-latest}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
STAGING_DIR="$PROJECT_ROOT/build-staging"
# Full image name with registry
get_full_image_name() {
if [ -n "$REGISTRY" ]; then
echo "$REGISTRY/$IMAGE_NAME"
else
echo "$IMAGE_NAME"
fi
}
# Detect current platform
CURRENT_ARCH=$(uname -m)
case "$CURRENT_ARCH" in
x86_64) DEFAULT_PLATFORM="linux/amd64" ;;
aarch64) DEFAULT_PLATFORM="linux/arm64" ;;
armv7l) DEFAULT_PLATFORM="linux/arm/v7" ;;
*) DEFAULT_PLATFORM="linux/amd64" ;;
esac
# Parse arguments
PLATFORMS=""
PUSH=false
LOAD=false
BUILD_BINARY=false
while [[ $# -gt 0 ]]; do
case $1 in
--platform)
PLATFORMS="$2"
shift 2
;;
--push)
PUSH=true
shift
;;
--load)
LOAD=true
shift
;;
--tag)
TAG="$2"
shift 2
;;
--registry)
REGISTRY="$2"
shift 2
;;
--build)
BUILD_BINARY=true
shift
;;
--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Package pre-compiled One-KVM binaries into Docker images."
echo ""
echo "Options:"
echo " --platform PLATFORM Target platform (linux/amd64, linux/arm64, linux/arm/v7)"
echo " Use comma to specify multiple: linux/amd64,linux/arm64"
echo " Default: $DEFAULT_PLATFORM"
echo " --registry REGISTRY Container registry (e.g., docker.io/user, ghcr.io/user)"
echo " --push Push image to registry"
echo " --load Load image to local Docker (single platform only)"
echo " --tag TAG Image tag (default: latest)"
echo " --build Also build the binary with cross (optional)"
echo " --help Show this help"
echo ""
echo "Examples:"
echo " # Build for current platform and load locally"
echo " $0 --platform linux/arm64 --load"
echo ""
echo " # Build and push single platform"
echo " $0 --platform linux/arm64 --registry docker.io/user --push"
echo ""
echo " # Build multi-arch and push (creates unified manifest)"
echo " $0 --platform linux/amd64,linux/arm64,linux/arm/v7 --registry docker.io/user --push"
exit 0
;;
*)
echo_error "Unknown option: $1"
exit 1
;;
esac
done
# Default platform
if [ -z "$PLATFORMS" ]; then
PLATFORMS="$DEFAULT_PLATFORM"
fi
# Validate single platform for --load
if [ "$LOAD" = true ] && [[ "$PLATFORMS" == *","* ]]; then
echo_error "Cannot use --load with multiple platforms"
exit 1
fi
# Map platform to Rust target
platform_to_target() {
case "$1" in
"linux/amd64") echo "x86_64-unknown-linux-gnu" ;;
"linux/arm64") echo "aarch64-unknown-linux-gnu" ;;
"linux/arm/v7") echo "armv7-unknown-linux-gnueabihf" ;;
*) echo_error "Unknown platform: $1"; exit 1 ;;
esac
}
# Map platform to tool download names
get_tool_urls() {
local platform="$1"
case "$platform" in
"linux/amd64")
TTYD_URL="https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.x86_64"
GOSTC_URL="https://github.com/SianHH/gostc-open/releases/download/v2.0.9/gostc_linux_amd64_v1.tar.gz"
EASYTIER_URL="https://github.com/EasyTier/EasyTier/releases/download/v2.4.5/easytier-linux-x86_64-v2.4.5.zip"
EASYTIER_DIR="easytier-linux-x86_64"
;;
"linux/arm64")
TTYD_URL="https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.aarch64"
GOSTC_URL="https://github.com/SianHH/gostc-open/releases/download/v2.0.9/gostc_linux_arm64_v8.0.tar.gz"
EASYTIER_URL="https://github.com/EasyTier/EasyTier/releases/download/v2.4.5/easytier-linux-aarch64-v2.4.5.zip"
EASYTIER_DIR="easytier-linux-aarch64"
;;
"linux/arm/v7")
TTYD_URL="https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.armhf"
GOSTC_URL="https://github.com/SianHH/gostc-open/releases/download/v2.0.9/gostc_linux_arm_7.tar.gz"
EASYTIER_URL="https://github.com/EasyTier/EasyTier/releases/download/v2.4.5/easytier-linux-armv7hf-v2.4.5.zip"
EASYTIER_DIR="easytier-linux-armv7hf"
;;
esac
}
# Download tools for a platform
download_tools() {
local platform="$1"
local staging="$2"
get_tool_urls "$platform"
echo_info "Downloading tools for $platform..."
# ttyd
if [ ! -f "$staging/ttyd" ]; then
curl -fsSL "$TTYD_URL" -o "$staging/ttyd"
chmod +x "$staging/ttyd"
fi
# gostc
if [ ! -f "$staging/gostc" ]; then
curl -fsSL "$GOSTC_URL" -o /tmp/gostc.tar.gz
tar -xzf /tmp/gostc.tar.gz -C "$staging"
chmod +x "$staging/gostc"
rm /tmp/gostc.tar.gz
fi
# easytier
if [ ! -f "$staging/easytier-core" ]; then
curl -fsSL "$EASYTIER_URL" -o /tmp/easytier.zip
unzip -o /tmp/easytier.zip -d /tmp/easytier
cp "/tmp/easytier/$EASYTIER_DIR/easytier-core" "$staging/easytier-core"
chmod +x "$staging/easytier-core"
rm -rf /tmp/easytier.zip /tmp/easytier
fi
}
# Build and package for a single platform
build_for_platform() {
local platform="$1"
local target=$(platform_to_target "$platform")
local staging="$STAGING_DIR/$target"
echo_info "=========================================="
echo_info "Processing: $platform ($target)"
echo_info "=========================================="
# Create staging directory
mkdir -p "$staging/ventoy"
# Build binary if requested
if [ "$BUILD_BINARY" = true ]; then
echo_info "Building binary with cross..."
cd "$PROJECT_ROOT"
cross build --release --target "$target"
fi
# Check binary exists
local binary="$PROJECT_ROOT/target/$target/release/one-kvm"
if [ ! -f "$binary" ]; then
echo_error "Binary not found: $binary"
echo_error "Build it first: cross build --release --target $target"
exit 1
fi
# Copy binary to staging
echo_info "Copying binary..."
cp "$binary" "$staging/one-kvm"
# Download tools
download_tools "$platform" "$staging"
# Copy init script
cp "$PROJECT_ROOT/build/init.sh" "$staging/init.sh"
# Copy ventoy resources if they exist
if [ -d "$PROJECT_ROOT/res/ventoy" ]; then
cp -r "$PROJECT_ROOT/res/ventoy/"* "$staging/ventoy/" 2>/dev/null || true
fi
# Ensure ventoy dir exists (even if empty) for COPY command
touch "$staging/ventoy/.keep"
# Copy Dockerfile
cp "$PROJECT_ROOT/build/Dockerfile.runtime" "$staging/Dockerfile"
# Build Docker image
echo_info "Building Docker image..."
local full_image=$(get_full_image_name)
local arch_tag="${target//_/-}"
local build_cmd="docker buildx build --platform $platform"
build_cmd="$build_cmd --build-arg TARGETPLATFORM=$platform"
if [ "$PUSH" = true ]; then
build_cmd="$build_cmd --push"
elif [ "$LOAD" = true ]; then
build_cmd="$build_cmd --load"
fi
# For multi-platform push, only tag with arch-specific name
# The unified tag will be created via manifest later
if [ "$PUSH" = true ] && [[ "$PLATFORMS" == *","* ]]; then
build_cmd="$build_cmd -t $full_image:$TAG-$arch_tag"
else
build_cmd="$build_cmd -t $full_image:$TAG"
build_cmd="$build_cmd -t $full_image:$TAG-$arch_tag"
fi
build_cmd="$build_cmd $staging"
echo_info "Running: $build_cmd"
eval "$build_cmd"
echo_info "Done: $full_image:$TAG-$arch_tag"
}
# Main
main() {
local full_image=$(get_full_image_name)
echo_info "One-KVM Docker Image Builder"
echo_info "Image: $full_image:$TAG"
echo_info "Platforms: $PLATFORMS"
if [ -n "$REGISTRY" ]; then
echo_info "Registry: $REGISTRY"
fi
echo ""
# Validate: push requires registry for multi-arch
if [ "$PUSH" = true ] && [ -z "$REGISTRY" ]; then
echo_warn "No registry specified. Images will be pushed to Docker Hub default."
echo_warn "Consider using --registry to specify a registry."
fi
# Process each platform
IFS=',' read -ra PLATFORM_ARRAY <<< "$PLATFORMS"
for platform in "${PLATFORM_ARRAY[@]}"; do
build_for_platform "$platform"
echo ""
done
# Create multi-arch manifest if pushing multiple platforms
if [ "$PUSH" = true ] && [ ${#PLATFORM_ARRAY[@]} -gt 1 ]; then
echo_info "Creating multi-arch manifest..."
local manifest_images=""
for platform in "${PLATFORM_ARRAY[@]}"; do
local target=$(platform_to_target "$platform")
local arch_tag="${target//_/-}"
manifest_images="$manifest_images $full_image:$TAG-$arch_tag"
done
# Remove existing manifest if any
docker manifest rm "$full_image:$TAG" 2>/dev/null || true
# Create and push manifest
echo_info "Creating manifest: $full_image:$TAG"
echo_info "Source images:$manifest_images"
if docker manifest create "$full_image:$TAG" $manifest_images; then
echo_info "Pushing manifest..."
docker manifest push "$full_image:$TAG"
echo_info "Multi-arch manifest pushed: $full_image:$TAG"
else
echo_warn "docker manifest failed, trying buildx imagetools..."
docker buildx imagetools create -t "$full_image:$TAG" $manifest_images
fi
fi
echo_info "=========================================="
echo_info "Build completed successfully!"
echo_info "=========================================="
if [ "$LOAD" = true ]; then
echo ""
echo "Run the container:"
echo " docker run -d --privileged \\"
echo " -p 8080:8080 \\"
echo " -v /dev:/dev \\"
echo " $full_image:$TAG"
fi
if [ "$PUSH" = true ] && [ ${#PLATFORM_ARRAY[@]} -gt 1 ]; then
echo ""
echo "Multi-arch image available:"
echo " docker pull $full_image:$TAG"
echo ""
echo "Or pull specific architecture:"
for platform in "${PLATFORM_ARRAY[@]}"; do
local target=$(platform_to_target "$platform")
local arch_tag="${target//_/-}"
echo " docker pull $full_image:$TAG-$arch_tag # $platform"
done
fi
}
main "$@"