diff --git a/define-vms.sh b/define-vms.sh new file mode 100755 index 0000000..f9a9b0c --- /dev/null +++ b/define-vms.sh @@ -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." \ No newline at end of file diff --git a/undefine-vms.sh b/undefine-vms.sh new file mode 100755 index 0000000..9abe99c --- /dev/null +++ b/undefine-vms.sh @@ -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."