fedora-csb-system-manager
  1#!/usr/bin/env bash
  2
  3# Fedora CSB VM Creation Script
  4# Description: Creates a libvirt VM from a Fedora CSB ISO
  5
  6set -euo pipefail
  7
  8# Color output for better readability
  9readonly RED='\033[0;31m'
 10readonly GREEN='\033[0;32m'
 11readonly YELLOW='\033[1;33m'
 12readonly NC='\033[0m' # No Color
 13
 14# Default configuration
 15VM_NAME="${VM_NAME:-fedora-csb}"
 16VM_RAM="${VM_RAM:-4096}"       # RAM in MB
 17VM_CPUS="${VM_CPUS:-2}"
 18VM_DISK_SIZE="${VM_DISK_SIZE:-40}" # Disk size in GB
 19VM_DISK_PATH="${VM_DISK_PATH:-/var/lib/libvirt/images/${VM_NAME}.qcow2}"
 20VM_NETWORK="${VM_NETWORK:-default}"
 21
 22# Logging functions
 23log_info() {
 24	echo -e "${GREEN}[INFO]${NC} $*"
 25}
 26
 27log_warn() {
 28	echo -e "${YELLOW}[WARN]${NC} $*"
 29}
 30
 31log_error() {
 32	echo -e "${RED}[ERROR]${NC} $*" >&2
 33}
 34
 35usage() {
 36	cat <<EOF
 37Usage: $(basename "$0") <iso-path>
 38
 39Creates a libvirt VM from a Fedora CSB ISO.
 40
 41Arguments:
 42  iso-path    Path to the Fedora CSB ISO file
 43
 44Environment variables (optional):
 45  VM_NAME       VM name (default: fedora-csb)
 46  VM_RAM        RAM in MB (default: 4096)
 47  VM_CPUS       Number of CPUs (default: 2)
 48  VM_DISK_SIZE  Disk size in GB (default: 40)
 49  VM_DISK_PATH  Path to VM disk image (default: /var/lib/libvirt/images/\$VM_NAME.qcow2)
 50  VM_NETWORK    Libvirt network (default: default)
 51
 52Example:
 53  $(basename "$0") ~/Downloads/fedora-csb.iso
 54  VM_RAM=8192 VM_CPUS=4 $(basename "$0") ~/Downloads/fedora-csb.iso
 55EOF
 56	exit 1
 57}
 58
 59check_dependencies() {
 60	local deps=("virt-install" "virsh" "qemu-img")
 61	local missing=()
 62
 63	for dep in "${deps[@]}"; do
 64		if ! command -v "$dep" &>/dev/null; then
 65			missing+=("$dep")
 66		fi
 67	done
 68
 69	if [[ ${#missing[@]} -gt 0 ]]; then
 70		log_error "Missing dependencies: ${missing[*]}"
 71		log_error "Install with: sudo dnf install libvirt virt-install qemu-kvm"
 72		exit 1
 73	fi
 74}
 75
 76check_libvirt_running() {
 77	if ! systemctl is-active --quiet libvirtd; then
 78		log_error "libvirtd is not running"
 79		log_error "Start with: sudo systemctl start libvirtd"
 80		exit 1
 81	fi
 82}
 83
 84check_vm_exists() {
 85	if virsh dominfo "$VM_NAME" &>/dev/null; then
 86		log_warn "VM '$VM_NAME' already exists"
 87		read -rp "Delete and recreate? [y/N] " response
 88		if [[ "$response" =~ ^[Yy]$ ]]; then
 89			log_info "Destroying existing VM..."
 90			virsh destroy "$VM_NAME" 2>/dev/null || true
 91			virsh undefine "$VM_NAME" --remove-all-storage 2>/dev/null || true
 92		else
 93			log_error "Aborting. Use VM_NAME=other-name to create with different name."
 94			exit 1
 95		fi
 96	fi
 97}
 98
 99create_vm() {
100	local iso_path="$1"
101
102	log_info "Creating VM '$VM_NAME'..."
103	log_info "  RAM: ${VM_RAM}MB"
104	log_info "  CPUs: ${VM_CPUS}"
105	log_info "  Disk: ${VM_DISK_SIZE}GB at ${VM_DISK_PATH}"
106	log_info "  Network: ${VM_NETWORK}"
107	log_info "  ISO: ${iso_path}"
108
109	virt-install \
110		--name "$VM_NAME" \
111		--memory "$VM_RAM" \
112		--vcpus "$VM_CPUS" \
113		--disk "path=${VM_DISK_PATH},size=${VM_DISK_SIZE},format=qcow2" \
114		--cdrom "$iso_path" \
115		--os-variant "fedora-unknown" \
116		--network "network=${VM_NETWORK}" \
117		--graphics spice \
118		--video virtio \
119		--boot uefi \
120		--noautoconsole
121
122	log_info "VM created successfully!"
123	log_info ""
124	log_info "Next steps:"
125	log_info "  1. Connect to VM: virt-manager (or: virsh console $VM_NAME)"
126	log_info "  2. Wait for CSB installation to complete"
127	log_info "  3. After reboot, run bootstrap.sh inside the VM"
128	log_info ""
129	log_info "To get VM IP after install:"
130	log_info "  virsh domifaddr $VM_NAME"
131}
132
133main() {
134	if [[ $# -lt 1 ]]; then
135		usage
136	fi
137
138	local iso_path="$1"
139
140	# Validate ISO path
141	if [[ ! -f "$iso_path" ]]; then
142		log_error "ISO file not found: $iso_path"
143		exit 1
144	fi
145
146	log_info "Checking dependencies..."
147	check_dependencies
148	check_libvirt_running
149	check_vm_exists
150
151	create_vm "$iso_path"
152}
153
154main "$@"