from fastapi import FastAPI, UploadFile, File from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse import os import libvirt import subprocess import random import xml.etree.ElementTree as ET import socket import time from pydantic import BaseModel app = FastAPI(title="VirtManager API", description="API for managing virtual machines") app.mount("/static", StaticFiles(directory="static"), name="static") ws_processes = {} stats_cache = {} # Cache for CPU stats to calculate deltas def get_free_port(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind(('', 0)) return s.getsockname()[1] def get_state_string(state_code): states = { 0: "no state", 1: "running", 2: "blocked", 3: "paused", 4: "shutdown", 5: "shut off", 6: "crashed", 7: "pm suspended" } return states.get(state_code, "unknown") class BootOrder(BaseModel): order: list[str] # Connect to libvirt conn = libvirt.open('qemu:///system') @app.get("/") async def root(): return FileResponse("static/index.html") @app.get("/vms") async def list_vms(): domains = conn.listAllDomains() vms = [] for domain in domains: xml_desc = domain.XMLDesc() root = ET.fromstring(xml_desc) graphics = root.find(".//graphics[@type='vnc']") vnc_port = None if graphics is not None: port_str = graphics.get('port') if port_str and port_str != '-1': vnc_port = int(port_str) vms.append({ "id": domain.ID(), "name": domain.name(), "state": get_state_string(domain.state()[0]), "vnc_port": vnc_port }) return {"vms": vms} @app.get("/vms/{vm_name}/info") async def get_vm_info(vm_name: str): try: domain = conn.lookupByName(vm_name) xml_desc = domain.XMLDesc() root = ET.fromstring(xml_desc) # Get basic info memory = root.find("memory").text if root.find("memory") is not None else "Unknown" cpus = root.find("vcpu").text if root.find("vcpu") is not None else "Unknown" # Get VNC port graphics = root.find(".//graphics[@type='vnc']") vnc_port = None if graphics is not None: port_str = graphics.get('port') if port_str and port_str != '-1': vnc_port = int(port_str) # Get disks disks = [] for disk in root.findall(".//disk"): device = disk.get('device') driver = disk.find("driver") source = disk.find("source") target = disk.find("target") disks.append({ "type": device, "driver": driver.get('type') if driver is not None else "unknown", "source": source.get('file') if source is not None else "unknown", "target": target.get('dev') if target is not None else "unknown" }) # Get network interfaces interfaces = [] for iface in root.findall(".//interface"): mac = iface.find("mac") source = iface.find("source") model = iface.find("model") interfaces.append({ "mac": mac.get('address') if mac is not None else "unknown", "network": source.get('network') if source is not None else "unknown", "model": model.get('type') if model is not None else "unknown" }) return { "id": domain.ID(), "name": domain.name(), "state": get_state_string(domain.state()[0]), "memory": int(memory), "cpus": int(cpus), "vnc_port": vnc_port, "disks": disks, "interfaces": interfaces } except Exception as e: return {"error": str(e)}, 500 @app.get("/vms/{vm_name}/stats") async def get_vm_stats(vm_name: str): """Get CPU and memory usage statistics for a VM""" try: domain = conn.lookupByName(vm_name) # Check if VM is running if not domain.isActive(): return { "name": vm_name, "state": "shut off", "cpu_percent": 0, "memory_used": 0, "memory_total": 0, "memory_percent": 0 } # Get domain info (memory is in MiB) info = domain.info() memory_total = info[1] # Maximum memory in MiB cpu_count = info[3] # Number of virtual CPUs # Get CPU stats - calculate real CPU percentage cpu_percent = 0 try: cpu_stats = domain.getCPUStats(True) if cpu_stats and len(cpu_stats) > 0: current_cpu_time_ns = cpu_stats[0].get('cpu_time', 0) current_time = time.time() # Initialize or update cache if vm_name not in stats_cache: stats_cache[vm_name] = { 'cpu_time_ns': current_cpu_time_ns, 'time': current_time, 'memory': 0 } cpu_percent = 0 else: prev_data = stats_cache[vm_name] prev_cpu_time_ns = prev_data['cpu_time_ns'] prev_time = prev_data['time'] # Calculate deltas cpu_time_delta_ns = current_cpu_time_ns - prev_cpu_time_ns time_delta_seconds = current_time - prev_time if time_delta_seconds > 0: # Convert CPU time delta to percentage # cpu_count vCPUs * 100% each = total possible CPU seconds in time_delta max_cpu_ns = (time_delta_seconds * cpu_count) * 1e9 if max_cpu_ns > 0: cpu_percent = min(100, (cpu_time_delta_ns / max_cpu_ns) * 100) # Update cache stats_cache[vm_name] = { 'cpu_time_ns': current_cpu_time_ns, 'time': current_time, 'memory': 0 } except Exception as e: cpu_percent = 0 # Get memory stats memory_used = 0 memory_percent = 0 try: memory_stats = domain.memoryStats() if memory_stats: # Try 'actual' first (actual memory used in KB) if 'actual' in memory_stats: memory_used_kb = memory_stats.get('actual', 0) memory_used = memory_used_kb // 1024 # Convert to MiB # Fallback to 'unused' to calculate used memory elif 'unused' in memory_stats: unused_kb = memory_stats.get('unused', 0) total_kb = memory_total * 1024 memory_used_kb = total_kb - unused_kb memory_used = memory_used_kb // 1024 else: memory_used = 0 # Calculate percentage if memory_total > 0: memory_percent = (memory_used / memory_total) * 100 except Exception as e: memory_used = 0 memory_percent = 0 return { "name": vm_name, "state": "running", "cpu_percent": round(cpu_percent, 2), "memory_used": memory_used, "memory_total": memory_total, "memory_percent": round(memory_percent, 2) } except Exception as e: return {"error": str(e), "state": "error"} @app.get("/vms/{vm_name}/start") async def start_vm(vm_name: str): try: domain = conn.lookupByName(vm_name) domain.create() return {"message": f"VM {vm_name} started"} except Exception as e: return {"error": str(e)} @app.get("/vms/{vm_name}/stop") async def stop_vm(vm_name: str): try: domain = conn.lookupByName(vm_name) domain.destroy() return {"message": f"VM {vm_name} stopped"} except Exception as e: return {"error": str(e)} @app.post("/vms") async def create_vm(name: str, memory: int, disk_size: int, cpus: int = 1, iso: str = ""): if not iso: return {"error": "ISO path is required"} images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images') disk_path = f"{images_path}/{name}.qcow2" iso_path = iso print(f"[CREATE_VM] Starting VM creation: name={name}, memory={memory}, disk_size={disk_size}, cpus={cpus}") print(f"[CREATE_VM] Using ISO path: {iso_path}") # Create disk using sudo script for proper permissions try: # Get absolute path to the script (up one level from app directory) app_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(app_dir) script_path = os.path.join(project_root, 'create_disk.sh') print(f"[CREATE_VM] Script path: {script_path}") result = subprocess.run(['sudo', script_path, disk_path, f'{disk_size}G'], capture_output=True, text=True, timeout=60) print(f"[CREATE_VM] Disk creation return code: {result.returncode}") if result.returncode != 0: error_msg = result.stderr if result.stderr else result.stdout print(f"[CREATE_VM] ERROR creating disk: {error_msg}") return {"error": f"Failed to create disk: {error_msg}"} print(f"[CREATE_VM] Disk created successfully: {disk_path}") except subprocess.TimeoutExpired: return {"error": "Disk creation timed out"} except Exception as e: print(f"[CREATE_VM] Exception creating disk: {str(e)}") return {"error": f"Error creating disk: {str(e)}"} # Verify disk exists if not os.path.exists(disk_path): print(f"[CREATE_VM] ERROR: Disk file not found after creation: {disk_path}") return {"error": f"Disk file not created at {disk_path}"} # Generate unique MAC mac = f"52:54:00:{random.randint(0, 255):02x}:{random.randint(0, 255):02x}:{random.randint(0, 255):02x}" print(f"[CREATE_VM] Generated MAC: {mac}") # Basic XML template xml_template = f""" {name} {memory} {memory} {cpus} hvm destroy restart destroy /usr/bin/kvm
""" print(f"[CREATE_VM] XML template prepared for VM definition") try: print(f"[CREATE_VM] Defining VM with libvirt...") domain = conn.defineXML(xml_template) print(f"[CREATE_VM] VM defined successfully. Starting VM...") domain.create() print(f"[CREATE_VM] VM started successfully!") return {"message": f"VM {name} created with {cpus} CPUs, {memory} MiB memory, {disk_size}G disk, booting from {iso}"} except libvirt.libvirtError as e: print(f"[CREATE_VM] Libvirt error: {str(e)}") # Try to clean up the disk if VM definition failed try: os.remove(disk_path) print(f"[CREATE_VM] Cleaned up disk file after error") except: pass return {"error": f"Libvirt error: {str(e)}"} except Exception as e: print(f"[CREATE_VM] Unexpected error: {str(e)}") # Try to clean up the disk if VM definition failed try: os.remove(disk_path) print(f"[CREATE_VM] Cleaned up disk file after error") except: pass return {"error": f"Error: {str(e)}"} @app.get("/console/{vm_name}") async def get_console(vm_name: str): try: console_conn = libvirt.open('qemu:///system') domain = console_conn.lookupByName(vm_name) xml_desc = domain.XMLDesc() root = ET.fromstring(xml_desc) graphics = root.find(".//graphics[@type='vnc']") console_conn.close() if graphics is None: return {"error": "No VNC graphics found"} vnc_port = graphics.get('port') if not vnc_port or vnc_port == '-1': return {"error": "VNC port not assigned"} vnc_port = int(vnc_port) if vm_name not in ws_processes: ws_port = get_free_port() try: proc = subprocess.Popen(['websockify', str(ws_port), f'localhost:{vnc_port}'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) ws_processes[vm_name] = (proc, ws_port) except FileNotFoundError: return {"error": "websockify not found. Please install it: apt-get install websockify"} _, ws_port = ws_processes[vm_name] return {"url": f"/static/novnc/vnc.html?host={socket.gethostname()}&port={ws_port}"} except Exception as e: import traceback error_msg = f"Error: {str(e)}\n{traceback.format_exc()}" print(error_msg) return {"error": str(e)} @app.delete("/vms/{vm_name}") async def delete_vm(vm_name: str): try: domain = conn.lookupByName(vm_name) if domain.isActive(): domain.destroy() domain.undefine() # Kill websockify process if exists if vm_name in ws_processes: proc, _ = ws_processes[vm_name] proc.terminate() proc.wait() del ws_processes[vm_name] return {"message": f"VM {vm_name} deleted"} except Exception as e: return {"error": str(e)} @app.put("/vms/{vm_name}/boot-order") async def set_boot_order(vm_name: str, boot_order: BootOrder): try: domain = conn.lookupByName(vm_name) if domain.isActive(): return {"error": "VM must be shut off to change boot order"} xml_desc = domain.XMLDesc() root = ET.fromstring(xml_desc) os_section = root.find("os") # Remove existing boot elements for boot in os_section.findall("boot"): os_section.remove(boot) # Add new boot elements for dev in boot_order.order: ET.SubElement(os_section, "boot", {"dev": dev}) new_xml = ET.tostring(root, encoding='unicode') # Undefine and redefine domain.undefine() new_domain = conn.defineXML(new_xml) return {"message": f"Boot order for {vm_name} updated to {boot_order.order}"} except Exception as e: return {"error": str(e)} @app.get("/isos") async def list_isos(): images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images') iso_dir = f"{images_path}/isos" if os.path.exists(iso_dir): isos = [{"name": f, "path": os.path.join(iso_dir, f)} for f in os.listdir(iso_dir) if f.endswith('.iso')] return {"isos": isos} return {"isos": []} @app.post("/upload-iso") async def upload_iso(file: UploadFile = File(...)): images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images') iso_dir = f"{images_path}/isos" os.makedirs(iso_dir, exist_ok=True) file_path = os.path.join(iso_dir, file.filename) with open(file_path, "wb") as f: while chunk := await file.read(1024 * 1024): # Read in 1MB chunks f.write(chunk) return {"message": f"ISO {file.filename} uploaded successfully", "path": file_path} @app.delete("/isos/{iso_name}") async def delete_iso(iso_name: str): images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images') iso_dir = f"{images_path}/isos" file_path = os.path.join(iso_dir, iso_name) if os.path.exists(file_path) and iso_name.endswith('.iso'): os.remove(file_path) return {"message": f"ISO {iso_name} deleted successfully"} else: return {"error": "ISO not found or invalid"} @app.post("/vms/{vm_name}/add-disk") async def add_disk_to_vm(vm_name: str, disk_name: str, disk_size: int): images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images') disk_path = f"{images_path}/{disk_name}.qcow2" print(f"[ADD_DISK] Starting disk addition: vm={vm_name}, disk_name={disk_name}, disk_size={disk_size}") try: # Get the VM first to check its state print(f"[ADD_DISK] Looking up VM: {vm_name}") domain = conn.lookupByName(vm_name) # Check if VM is running BEFORE creating disk print(f"[ADD_DISK] Checking VM state...") if domain.isActive(): print(f"[ADD_DISK] ERROR: VM is running, must be shut off to add disk") return {"error": "Error: La máquina virtual debe estar PARADA (shut off) para agregar un disco"} print(f"[ADD_DISK] VM state OK (shut off)") # Check if disk already exists print(f"[ADD_DISK] Checking if disk exists: {disk_path}") if os.path.exists(disk_path): print(f"[ADD_DISK] ERROR: Disk already exists") return {"error": f"Error: El disco {disk_name}.qcow2 ya existe. Usa un nombre de disco diferente"} print(f"[ADD_DISK] Disk does not exist (OK)") # Create the disk using sudo script for proper permissions print(f"[ADD_DISK] Creating disk...") app_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(app_dir) script_path = os.path.join(project_root, 'create_disk.sh') result = subprocess.run(['sudo', script_path, disk_path, f'{disk_size}G'], capture_output=True, text=True, timeout=60) if result.returncode != 0: error_msg = result.stderr if result.stderr else result.stdout print(f"[ADD_DISK] ERROR creating disk: {error_msg}") return {"error": f"Error al crear disco: {error_msg}"} print(f"[ADD_DISK] Disk created successfully") # Get the XML and add the disk print(f"[ADD_DISK] Getting VM XML...") xml_desc = domain.XMLDesc() root = ET.fromstring(xml_desc) # Find the devices section devices = root.find("devices") if devices is None: print(f"[ADD_DISK] ERROR: No devices section in XML") return {"error": "Error: No se encontró sección devices en la configuración de la VM"} # Find the next available disk target (vda, vdb, vdc, etc.) print(f"[ADD_DISK] Finding next available disk target...") existing_targets = set() for disk in root.findall(".//disk/target"): existing_targets.add(disk.get('dev')) print(f"[ADD_DISK] Existing targets: {existing_targets}") # Generate next available target target_letters = "abcdefghijklmnopqrstuvwxyz" next_target = None for i, letter in enumerate(target_letters): target = f"vd{letter}" if target not in existing_targets: next_target = target break if next_target is None: print(f"[ADD_DISK] ERROR: No available disk targets") return {"error": "Error: No hay dispositivos de disco disponibles"} print(f"[ADD_DISK] Next target: {next_target}") # Create the new disk element print(f"[ADD_DISK] Creating disk XML element...") new_disk = ET.Element("disk", {"type": "file", "device": "disk"}) ET.SubElement(new_disk, "driver", {"name": "qemu", "type": "qcow2"}) ET.SubElement(new_disk, "source", {"file": disk_path}) ET.SubElement(new_disk, "target", {"dev": next_target, "bus": "virtio"}) # Add the disk to devices devices.append(new_disk) # Convert back to string and redefine the VM print(f"[ADD_DISK] Redefining VM with new disk...") new_xml = ET.tostring(root, encoding='unicode') domain.undefine() conn.defineXML(new_xml) print(f"[ADD_DISK] VM redefined successfully") print(f"[ADD_DISK] SUCCESS: Disk added") return {"message": f"Disco {disk_name}.qcow2 ({disk_size}G) agregado a {vm_name} como {next_target}"} except libvirt.libvirtError as e: print(f"[ADD_DISK] Libvirt error: {str(e)}") return {"error": f"Error de libvirt: {str(e)}"} except Exception as e: print(f"[ADD_DISK] Unexpected error: {str(e)}") return {"error": f"Error: {str(e)}"} @app.post("/disks") async def create_disk(name: str, size: int): images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images') disk_path = f"{images_path}/{name}.qcow2" try: result = subprocess.run(['qemu-img', 'create', '-f', 'qcow2', disk_path, f'{size}G'], capture_output=True, text=True) if result.returncode == 0: return {"message": f"Disk {name}.qcow2 created with size {size}G"} else: return {"error": result.stderr} except Exception as e: return {"error": str(e)} @app.get("/vms/{vm_name}/config") async def get_vm_config(vm_name: str): """Get VM configuration in XML format""" try: conn = libvirt.open(None) domain = conn.lookupByName(vm_name) xml_desc = domain.XMLDesc() conn.close() return { "name": vm_name, "xml": xml_desc } except libvirt.libvirtError as e: return {"error": f"Error de libvirt: {str(e)}"} @app.post("/vms/{vm_name}/snapshot") async def create_snapshot(vm_name: str, snapshot_name: str, description: str = ""): """Create a snapshot of a VM""" try: conn = libvirt.open(None) domain = conn.lookupByName(vm_name) # Create snapshot XML snapshot_xml = f""" {snapshot_name} {description} """ print(f"[SNAPSHOT] Creating snapshot '{snapshot_name}' for VM '{vm_name}'") domain.snapshotCreateXML(snapshot_xml, 0) conn.close() print(f"[SNAPSHOT] Snapshot '{snapshot_name}' created successfully") return {"message": f"Snapshot '{snapshot_name}' creado exitosamente"} except libvirt.libvirtError as e: error_msg = str(e) print(f"[SNAPSHOT] Error creating snapshot: {error_msg}") return {"error": f"Error al crear snapshot: {error_msg}"} except Exception as e: error_msg = str(e) print(f"[SNAPSHOT] Unexpected error: {error_msg}") return {"error": f"Error: {error_msg}"} @app.get("/vms/{vm_name}/snapshots") async def list_snapshots(vm_name: str): """List all snapshots of a VM""" try: conn = libvirt.open(None) domain = conn.lookupByName(vm_name) snapshots = [] try: # Get all snapshots snapshot_list = domain.listAllSnapshots() for snapshot in snapshot_list: snap_name = snapshot.getName() # Get snapshot info using available methods try: # Try to get current status is_current = snapshot.isCurrent() state = "current" if is_current else "available" except: state = "available" # Try to get creation time from snapshot info created_time = 0 try: # Get snapshot XML description using getXMLDesc() snap_xml = snapshot.getXMLDesc() # Parse XML to get creationTime root = ET.fromstring(snap_xml) creation_elem = root.find('.//creationTime') if creation_elem is not None and creation_elem.text: created_time = int(creation_elem.text) except Exception as e: print(f"[SNAPSHOT] Error obteniendo creationTime: {str(e)}") created_time = 0 snapshots.append({ "name": snap_name, "created": created_time, "description": "", "state": state }) except libvirt.libvirtError as e: # No snapshots available print(f"[SNAPSHOT] No snapshots found: {str(e)}") pass conn.close() return { "vm_name": vm_name, "snapshots": snapshots } except libvirt.libvirtError as e: error_msg = str(e) print(f"[SNAPSHOT] Error listing snapshots: {error_msg}") return {"error": f"Error al listar snapshots: {error_msg}"} @app.delete("/vms/{vm_name}/snapshots/{snapshot_name}") async def delete_snapshot(vm_name: str, snapshot_name: str): """Delete a snapshot of a VM""" try: conn = libvirt.open(None) domain = conn.lookupByName(vm_name) print(f"[SNAPSHOT] Deleting snapshot '{snapshot_name}' from VM '{vm_name}'") snapshot = domain.snapshotLookupByName(snapshot_name) snapshot.delete(0) conn.close() print(f"[SNAPSHOT] Snapshot '{snapshot_name}' deleted successfully") return {"message": f"Snapshot '{snapshot_name}' eliminado exitosamente"} except libvirt.libvirtError as e: error_msg = str(e) print(f"[SNAPSHOT] Error deleting snapshot: {error_msg}") return {"error": f"Error al eliminar snapshot: {error_msg}"} except Exception as e: error_msg = str(e) print(f"[SNAPSHOT] Unexpected error: {error_msg}")