|
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|
|
import os
|
|
|
import re
|
|
|
import libvirt
|
|
|
+import subprocess
|
|
|
|
|
|
app = FastAPI(title="VirtManager API", description="API for managing virtual machines")
|
|
|
|
|
|
@@ -73,5 +74,18 @@ async def delete_vm(vm_name: str):
|
|
|
domain.destroy()
|
|
|
domain.undefine()
|
|
|
return {"message": f"VM {vm_name} deleted"}
|
|
|
+ except Exception as e:
|
|
|
+ return {"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)}
|