161 lines
3.9 KiB
Bash
Executable file
161 lines
3.9 KiB
Bash
Executable file
#!/bin/bash
|
|
# create-and-boot-vm.sh - Create and boot the debian-bootc VM
|
|
|
|
set -euo pipefail
|
|
|
|
VM_NAME="debian-bootc"
|
|
VM_XML="debian-bootc-vm.xml"
|
|
QCOW2_PATH="/opt/Projects/overseer/alternate_paths/output/debian-bootc.qcow2"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# Check prerequisites
|
|
check_prerequisites() {
|
|
log_info "Checking prerequisites..."
|
|
|
|
if ! command -v virsh &> /dev/null; then
|
|
log_error "virsh not found. Please install libvirt-clients:"
|
|
log_info " sudo apt install libvirt-clients qemu-system-x86"
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v qemu-system-x86_64 &> /dev/null; then
|
|
log_error "qemu-system-x86_64 not found. Please install qemu:"
|
|
log_info " sudo apt install qemu-system-x86"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$QCOW2_PATH" ]; then
|
|
log_error "QCOW2 image not found: $QCOW2_PATH"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$VM_XML" ]; then
|
|
log_error "VM XML configuration not found: $VM_XML"
|
|
exit 1
|
|
fi
|
|
|
|
log_info "All prerequisites satisfied."
|
|
}
|
|
|
|
# Check if VM already exists
|
|
check_vm_exists() {
|
|
if virsh list --all --name | grep -q "^$VM_NAME$"; then
|
|
log_warn "VM '$VM_NAME' already exists."
|
|
read -p "Do you want to remove it and recreate? (y/N): " -n 1 -r
|
|
echo
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
log_info "Removing existing VM..."
|
|
virsh destroy "$VM_NAME" 2>/dev/null || true
|
|
virsh undefine "$VM_NAME" 2>/dev/null || true
|
|
else
|
|
log_info "Using existing VM."
|
|
return 0
|
|
fi
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# Create the VM
|
|
create_vm() {
|
|
log_info "Creating VM '$VM_NAME'..."
|
|
|
|
# Check if libvirt is running
|
|
if ! systemctl is-active --quiet libvirtd; then
|
|
log_warn "libvirtd is not running. Starting it..."
|
|
sudo systemctl start libvirtd
|
|
sudo systemctl enable libvirtd
|
|
fi
|
|
|
|
# Create the VM from XML
|
|
virsh define "$VM_XML"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
log_info "VM '$VM_NAME' created successfully!"
|
|
else
|
|
log_error "Failed to create VM."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Start the VM
|
|
start_vm() {
|
|
log_info "Starting VM '$VM_NAME'..."
|
|
|
|
virsh start "$VM_NAME"
|
|
|
|
if [ $? -eq 0 ]; then
|
|
log_info "VM '$VM_NAME' started successfully!"
|
|
else
|
|
log_error "Failed to start VM."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Show VM status
|
|
show_vm_status() {
|
|
log_info "VM Status:"
|
|
virsh list --all | grep "$VM_NAME" || true
|
|
|
|
log_info "VM Details:"
|
|
virsh dominfo "$VM_NAME" 2>/dev/null || true
|
|
}
|
|
|
|
# Show connection info
|
|
show_connection_info() {
|
|
log_info "=== Connection Information ==="
|
|
log_info "VM Name: $VM_NAME"
|
|
log_info "QCOW2 Image: $QCOW2_PATH"
|
|
|
|
# Get VNC port
|
|
VNC_PORT=$(virsh vncdisplay "$VM_NAME" 2>/dev/null | cut -d: -f2 || echo "N/A")
|
|
if [ "$VNC_PORT" != "N/A" ]; then
|
|
log_info "VNC Display: :$VNC_PORT"
|
|
log_info "VNC URL: vnc://localhost:$VNC_PORT"
|
|
fi
|
|
|
|
log_info "Serial Console: virsh console $VM_NAME"
|
|
log_info "SSH (if network works): ssh debian@<VM_IP>"
|
|
log_info "Login Credentials: debian / debian123"
|
|
log_info "================================"
|
|
}
|
|
|
|
# Main execution
|
|
main() {
|
|
log_info "Creating and booting debian-bootc VM..."
|
|
|
|
check_prerequisites
|
|
|
|
if ! check_vm_exists; then
|
|
create_vm
|
|
fi
|
|
|
|
start_vm
|
|
show_vm_status
|
|
show_connection_info
|
|
|
|
log_info "VM is now running!"
|
|
log_info "Use 'virsh console $VM_NAME' to access the serial console"
|
|
log_info "Use 'virsh destroy $VM_NAME' to stop the VM"
|
|
log_info "Use 'virsh undefine $VM_NAME' to remove the VM"
|
|
}
|
|
|
|
# Run main function
|
|
main "$@"
|