Просмотр исходного кода

Se puede elegir el nombre del disco

Celestino Rey 7 месяцев назад
Родитель
Сommit
153a5fab91
3 измененных файлов с 12 добавлено и 5 удалено
  1. 5 2
      README.md
  2. 6 2
      app/main.py
  3. 1 1
      templates/ubuntu.xml

+ 5 - 2
README.md

@@ -22,7 +22,10 @@ API for managing virtual machines on Ubuntu using FastAPI and libvirt.
 
 ## Running the API
 
+Set the environment variable for the images path if needed (default is /var/lib/libvirt/images):
+
 ```bash
+export LIBVIRT_IMAGES_PATH=/path/to/images
 source venv/bin/activate
 uvicorn app.main:app --reload --host 0.0.0.0
 ```
@@ -35,13 +38,13 @@ Access the interactive documentation at http://0.0.0.0:8000/docs
 
 - GET / : Root message
 - GET /vms : List all VMs
-- POST /vms?name={name}&template={template} : Create a new VM from a template
+- POST /vms?name={name}&template={template}&disk={disk} : Create a new VM from a template with custom disk name
 - 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
 
 ## Templates
 
-VM templates are XML files stored in the `templates/` directory. Each template defines the base configuration for a VM. When creating a VM, the name in the XML is replaced with the provided name.
+VM templates are XML files stored in the `templates/` directory. Each template defines the base configuration for a VM. When creating a VM, the name in the XML is replaced with the provided name, and the disk file path is set using the `LIBVIRT_IMAGES_PATH` environment variable (default: /var/lib/libvirt/images) plus the provided disk name.
 
 Example template: `templates/ubuntu.xml`

+ 6 - 2
app/main.py

@@ -45,19 +45,23 @@ async def stop_vm(vm_id: int):
     return {"message": f"VM {vm_id} stopped (mock)"}
 
 @app.post("/vms")
-async def create_vm(name: str, template: str):
+async def create_vm(name: str, template: str, disk: str):
     xml_path = f"templates/{template}.xml"
     if not os.path.exists(xml_path):
         return {"error": f"Template {template} not found"}
     with open(xml_path, 'r') as f:
         xml = f.read()
+    # Get images path from env
+    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
     # Replace the name in XML
     xml = re.sub(r'<name>.*?</name>', f'<name>{name}</name>', xml)
+    # Replace the disk file name
+    xml = re.sub(r'file=\'/var/lib/libvirt/images/.*?\.qcow2\'', f"file='{images_path}/{disk}.qcow2'", xml)
     try:
         # Create VM from template
         domain = conn.defineXML(xml)
         domain.create()
-        return {"message": f"VM {name} created from template {template}"}
+        return {"message": f"VM {name} created from template {template} with disk {disk}.qcow2"}
     except Exception as e:
         return {"error": str(e)}
 

+ 1 - 1
templates/ubuntu.xml

@@ -28,7 +28,7 @@
     <emulator>/usr/bin/kvm</emulator>
     <disk type='file' device='disk'>
       <driver name='qemu' type='qcow2'/>
-      <source file='/mnt/vms/images/ubuntu.qcow2'/>
+      <source file='/var/lib/libvirt/images/ubuntu.qcow2'/>
       <target dev='vda' bus='virtio'/>
     </disk>
     <disk type='file' device='cdrom'>