add arg to start vm after modification

This commit is contained in:
m0duspwnens
2024-09-23 10:13:22 -04:00
parent 0b326370bd
commit 50bd8448cc

View File

@@ -9,10 +9,10 @@
Script to modify hardware parameters of a KVM virtual machine. Script to modify hardware parameters of a KVM virtual machine.
Usage: Usage:
python so-kvm-modify-hardware.py -v <vm_name> [-c <cpu_count>] [-m <memory_amount>] [-p <pci_id>] python so-kvm-modify-hardware.py -v <vm_name> [-c <cpu_count>] [-m <memory_amount>] [-p <pci_id>] [-s]
Example: Example:
python so-kvm-modify-hardware.py -v my_vm -c 4 -m 8192 -p 0000:00:1f.2 python so-kvm-modify-hardware.py -v my_vm -c 4 -m 8192 -p 0000:00:1f.2 -s
""" """
import argparse import argparse
@@ -48,6 +48,7 @@ def parse_arguments():
parser.add_argument('-c', '--cpu', type=int, help='Number of virtual CPUs to assign.') parser.add_argument('-c', '--cpu', type=int, help='Number of virtual CPUs to assign.')
parser.add_argument('-m', '--memory', type=int, help='Amount of memory to assign in MiB.') parser.add_argument('-m', '--memory', type=int, help='Amount of memory to assign in MiB.')
parser.add_argument('-p', '--pci', help='PCI hardware ID to passthrough to the VM (e.g., 0000:00:1f.2).') parser.add_argument('-p', '--pci', help='PCI hardware ID to passthrough to the VM (e.g., 0000:00:1f.2).')
parser.add_argument('-s', '--start', action='store_true', help='Start the VM after modification.')
args = parser.parse_args() args = parser.parse_args()
return args return args
@@ -152,6 +153,7 @@ def main():
cpu_count = args.cpu cpu_count = args.cpu
memory_amount = args.memory memory_amount = args.memory
pci_id = args.pci pci_id = args.pci
start_vm_flag = args.start
# Connect to libvirt # Connect to libvirt
try: try:
@@ -169,9 +171,12 @@ def main():
# Redefine VM # Redefine VM
redefine_vm(conn, new_xml_desc, logger) redefine_vm(conn, new_xml_desc, logger)
# Start VM # Start VM if -s or --start argument is provided
dom = conn.lookupByName(vm_name) if start_vm_flag:
start_vm(dom, logger) dom = conn.lookupByName(vm_name)
start_vm(dom, logger)
else:
logger.info("VM start flag not provided; VM will remain stopped.")
# Close connection # Close connection
conn.close() conn.close()