scripts to define and undefine vms

This commit is contained in:
Lukas Greve 2025-05-30 09:39:53 +02:00
parent 49c18900ac
commit 6110cfc9e6
2 changed files with 119 additions and 0 deletions

57
define-vms.sh Executable file
View File

@ -0,0 +1,57 @@
#!/bin/bash
# Directory containing XML definitions of virtual machines requiring root permissions
SYSTEM="./system/"
# Ensure the directory exists
if [ ! -d "$SYSTEM" ]; then
echo "The specified directory does not exist."
exit 1
fi
# Loop over each XML file in the directory
for root_vm in "$SYSTEM"/*.xml; do
if [ -f "$root_vm" ]; then
# Use virsh define to create a virtual machine from each XML file
sudo virsh define "$root_vm"
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully defined virtual machine from $root_vm"
else
echo "Failed to define virtual machine from $root_vm"
fi
else
echo "No XML files found in directory: $SYSTEM"
fi
done
echo "Finished defining all root virtual machines."
# Directory containing XML definitions of virtual machines requiring user permissions
SESSION="./session/"
# Ensure the directory exists
if [ ! -d "$SESSION" ]; then
echo "The specified directory does not exist."
exit 1
fi
# Loop over each XML file in the directory
for rootless_vm in "$SESSION"/*.xml; do
if [ -f "$rootless_vm" ]; then
# Use virsh define to create a virtual machine from each XML file
virsh define "$rootless_vm"
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully defined virtual machine from $rootless_vm"
else
echo "Failed to define virtual machine from $rootless_vm"
fi
else
echo "No XML files found in directory: $SESSION"
fi
done
echo "Finished defining all rootless virtual machines."

62
undefine-vms.sh Executable file
View File

@ -0,0 +1,62 @@
#!/bin/bash
# Function to undefine a virtual machine. Takes just the VM name.
# This makes it more robust and aligned with how virsh expects the name.
undefine_vm() {
local vm_name="$1"
# Use virsh undefine to undefine a virtual machine
sudo virsh undefine "$vm_name"
# Check if the command was successful
if [ $? -eq 0 ]; then
echo "Successfully undefined virtual machine $vm_name"
else
echo "Failed to undefine virtual machine $vm_name"
fi
}
# Directory containing XML definitions of virtual machines requiring root permissions
SYSTEM="./system/"
# Ensure the directory exists
if [ ! -d "$SYSTEM" ]; then
echo "The specified directory does not exist."
exit 1
fi
# Loop over each XML file in the directory
for root_vm in "$SYSTEM"/*.xml; do
if [ -f "$root_vm" ]; then
# Extract the VM name from the full path
vm_name=$(basename "$root_vm" .xml) # Removes the .xml extension
# Call the undefine function
undefine_vm "$vm_name"
fi
done
echo "Finished undefining all root virtual machines."
# Directory containing XML definitions of virtual machines requiring user permissions
SESSION="./session/"
# Ensure the directory exists
if [ ! -d "$SESSION" ]; then
echo "The specified directory does not exist."
exit 1
fi
# Loop over each XML file in the directory
for rootless_vm in "$SESSION"/*.xml; do # Corrected the pattern
if [ -f "$rootless_vm" ]; then
# Extract the VM name from the full path
vm_name=$(basename "$rootless_vm" .xml) #remove .xml extension
# Call the undefine function
virsh undefine "$vm_name"
fi
done
echo "Finished undefining all rootless virtual machines."