Celestino Rey 7 bulan lalu
induk
melakukan
c65cde3dca
2 mengubah file dengan 15 tambahan dan 0 penghapusan
  1. 1 0
      README.md
  2. 14 0
      app/main.py

+ 1 - 0
README.md

@@ -42,6 +42,7 @@ Access the interactive documentation at http://0.0.0.0:8000/docs
 - GET /vms/{vm_id}/start : Start a VM by ID
 - GET /vms/{vm_id}/stop : Stop a VM by ID
 - DELETE /vms/{vm_name} : Delete a VM by name
+- POST /disks?name={name}&size={size} : Create a new disk image with specified size in GB
 
 ## Templates
 

+ 14 - 0
app/main.py

@@ -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)}