itenv_tools/tools/install_cloudhypervisor.sh
2025-06-15 18:33:35 +02:00

178 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Cloud Hypervisor Installation Script
# Downloads and installs Cloud Hypervisor from GitHub releases
# Compatible with Ubuntu/Debian systems
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Cloud Hypervisor configuration
CH_VERSION="v46.0"
CH_URL="https://github.com/cloud-hypervisor/cloud-hypervisor/releases/download/${CH_VERSION}/cloud-hypervisor-static"
CH_BINARY="cloud-hypervisor"
MIN_SIZE_MB=4
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
}
warn() {
echo -e "${YELLOW}[$(date +'%Y-%m-%d %H:%M:%S')] WARNING: $1${NC}"
}
error() {
echo -e "${RED}[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: $1${NC}"
exit 1
}
# Check if running as root
if [ "$EUID" -eq 0 ]; then
log "Running as root. Cloud Hypervisor will be installed system-wide."
INSTALL_DIR="/usr/local/bin"
else
log "Installing Cloud Hypervisor for current user"
INSTALL_DIR="$HOME/.local/bin"
# Create local bin directory if it doesn't exist
mkdir -p "$INSTALL_DIR"
fi
# Check if Cloud Hypervisor is already installed
if command -v cloud-hypervisor &> /dev/null; then
CH_CURRENT_VERSION=$(cloud-hypervisor --version 2>/dev/null | head -n1 || echo "")
if [ -n "$CH_CURRENT_VERSION" ]; then
warn "Cloud Hypervisor is already installed: $CH_CURRENT_VERSION"
read -p "Do you want to update/reinstall? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log "Installation cancelled"
exit 0
fi
fi
fi
# Check for required dependencies
log "Checking system dependencies..."
MISSING_DEPS=()
# Check for curl
if ! command -v curl &> /dev/null; then
MISSING_DEPS+=("curl")
fi
# Install missing dependencies
if [ ${#MISSING_DEPS[@]} -gt 0 ]; then
log "Installing missing dependencies: ${MISSING_DEPS[*]}"
if [ "$EUID" -eq 0 ]; then
apt update
apt install -y "${MISSING_DEPS[@]}"
else
log "Please install the following packages and run this script again:"
log "sudo apt update && sudo apt install -y ${MISSING_DEPS[*]}"
exit 1
fi
fi
log "Starting Cloud Hypervisor installation..."
log "Version: $CH_VERSION"
log "Install directory: $INSTALL_DIR"
# Create temporary directory for download
TEMP_DIR=$(mktemp -d)
TEMP_FILE="$TEMP_DIR/$CH_BINARY"
# Cleanup function
cleanup() {
rm -rf "$TEMP_DIR"
}
trap cleanup EXIT
log "Downloading Cloud Hypervisor from $CH_URL..."
# Download with size verification
if ! curl -L --fail --progress-bar -o "$TEMP_FILE" "$CH_URL"; then
error "Failed to download Cloud Hypervisor from $CH_URL"
fi
# Check file size
FILE_SIZE=$(stat -c%s "$TEMP_FILE" 2>/dev/null || stat -f%z "$TEMP_FILE" 2>/dev/null || echo "0")
FILE_SIZE_MB=$((FILE_SIZE / 1024 / 1024))
log "Downloaded file size: ${FILE_SIZE_MB}MB"
if [ "$FILE_SIZE_MB" -lt "$MIN_SIZE_MB" ]; then
error "Downloaded file is too small (${FILE_SIZE_MB}MB). Expected at least ${MIN_SIZE_MB}MB. Download may be corrupted."
fi
log "✅ Download size verification passed (${FILE_SIZE_MB}MB >= ${MIN_SIZE_MB}MB)"
# Make the binary executable
chmod +x "$TEMP_FILE"
# Test the binary before installation
log "Testing downloaded binary..."
if ! "$TEMP_FILE" --version &> /dev/null; then
error "Downloaded binary is not working correctly"
fi
log "✅ Binary test passed"
# Install the binary
log "Installing Cloud Hypervisor to $INSTALL_DIR..."
if [ "$EUID" -eq 0 ]; then
cp "$TEMP_FILE" "$INSTALL_DIR/$CH_BINARY"
else
cp "$TEMP_FILE" "$INSTALL_DIR/$CH_BINARY"
# Add to PATH if not already there
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
warn "Adding $INSTALL_DIR to PATH in ~/.bashrc"
echo "export PATH=\"$INSTALL_DIR:\$PATH\"" >> ~/.bashrc
export PATH="$INSTALL_DIR:$PATH"
fi
fi
# Verify installation
log "Verifying Cloud Hypervisor installation..."
# Set PATH for verification
if [ "$EUID" -ne 0 ]; then
export PATH="$INSTALL_DIR:$PATH"
fi
if command -v cloud-hypervisor &> /dev/null; then
CH_VERSION_OUTPUT=$(cloud-hypervisor --version 2>/dev/null || echo "")
if [ -n "$CH_VERSION_OUTPUT" ]; then
log "✅ Cloud Hypervisor installation successful!"
log "Version: $CH_VERSION_OUTPUT"
log "Location: $(which cloud-hypervisor)"
# Test basic functionality
log "Testing basic functionality..."
if cloud-hypervisor --help &> /dev/null; then
log "✅ Basic functionality test passed"
else
warn "Basic functionality test failed, but binary is installed"
fi
log "✅ Cloud Hypervisor installation completed successfully!"
log ""
log "Next steps:"
if [ "$EUID" -eq 0 ]; then
log "- Cloud Hypervisor is available system-wide"
else
log "- Run 'source ~/.bashrc' to update PATH in this session"
log "- Or restart your terminal"
fi
log "- Run 'cloud-hypervisor --help' to see available options"
log "- Check the documentation at: https://github.com/cloud-hypervisor/cloud-hypervisor"
else
error "Cloud Hypervisor was installed but version check failed"
fi
else
error "Cloud Hypervisor installation failed. Binary not found in PATH."
fi