gestiona-pc1.py is a Python script designed to automate the creation, configuration, and management of a virtualized network environment.
Its main purpose is to deploy a Load Balancer (LB) scenario using QEMU/KVM (libvirt), configurable web servers, and virtual bridges, simplifying the setup of complex network labs for testing and educational purposes.
The primary goal is to provide a single command-line interface to manage a complete load balancing scenario, including:
- Automated VM Provisioning: Create disk images (.qcow2) and definition files (.xml) from templates.
- Network Setup: Configure Linux bridges (LAN1, LAN2) and host routing to interconnect the scenario.
- Service Configuration: Automatically configure networking (IPs, gateways, hostnames) and critical services like HAProxy.
- Lifecycle Management: Handle VM start, stop, reboot, and complete destruction of the virtual lab.
The resulting architecture consists of a client LAN (LAN1) separated from a server farm LAN (LAN2) by the load balancer.
- Virtualization Backend: QEMU/KVM managed via libvirt (
virsh). - Disk Image Management:
qemu-imgto create copy-on-write images from a base. - VM Configuration:
libguestfs-tools(virt-copy-in,virt-edit) for VM customization before first boot. - Network Abstraction: Linux Bridges (
brctl) to define virtual LANs. - Load Balancing Service: HAProxy configured in Round Robin mode.
sys,os,json,subprocesslxmlfor programmatic manipulation of libvirt XML filesloggingfor structured debug and informational output
python3 gestiona-pc1.py <command> [sub-parameters]| Command | Description | Example |
|---|---|---|
create |
Builds the entire scenario. Configurable with the number of servers (1-5) and debug flag. | python3 gestiona-pc1.py create 3 debug |
start |
Boots the VMs and opens an xterm console for interaction. | python3 gestiona-pc1.py start lb |
stop |
Shuts down the specified VM(s) using virsh shutdown. |
python3 gestiona-pc1.py stop s1 |
destroy |
CLEANUP: Stops all VMs, undefines them, deletes all generated files (.qcow2, .xml, etc.), and removes the bridges. | python3 gestiona-pc1.py destroy |
monitor |
Provides real-time status or diagnostics of a VM. | python3 gestiona-pc1.py monitor lb cpu |
reboot |
Restarts the specified VM(s). | python3 gestiona-pc1.py reboot |
--help |
Displays the full usage guide. | python3 gestiona-pc1.py --help |
The monitor command requires a VM name (c1, lb, or sX) and a function:
| Function | Description |
|---|---|
ping |
Performs a 5-packet connectivity test from the host to the VM. |
info |
Displays detailed VM information (virsh dominfo) in a live watch window. |
estado |
Shows the current power state of the VM (virsh domstate) in a live watch window. |
cpu |
Displays VM CPU statistics (virsh cpu-stats) in a live watch window. |
The following files and configurations are created by the create command and removed by the destroy command:
gestiona-pc1.jsonβ Stores scenario configuration (number of servers, debug flag).<VM>.qcow2β VM disk images (c1.qcow2,lb.qcow2,sX.qcow2).<VM>.xmlβ libvirt VM definition files.LAN1,LAN2β Virtual Linux bridges.- Temporary configuration files used for VM customization:
hostshostnameinterfaceshaproxy.cfgrc.localindex.html