My EdgeRouter ping and start up script for VM optimized.
#!/bin/bash
#
# edgerouter_wait_start_vms.sh
#
# Description: Waits for edgerouter to become available before starting libvirt VMs.
# Useful for automated VM startup after host/network reboots.
#
# Usage: ./edgerouter_wait_start_vms.sh
#
#
# Dependencies:
# - libvirt-clients (virsh)
# - ping
#
EDGEROUTER_IP="192.168.1.1" # EdgeRouter gateway IP
VM_NAME="VMName1" # VM to start
MAX_ATTEMPTS=60 # Maximum number of attempts (10 minutes with 10s sleep)
SLEEP_INTERVAL=10 # Seconds to wait between attempts
PING_COUNT=2 # Number of successful pings required
PING_TIMEOUT=2 # Ping timeout in seconds
# Function to check if EdgeRouter is responding
check_edgerouter() {
ping -c ${PING_COUNT} -W ${PING_TIMEOUT} ${EDGEROUTER_IP} >/dev/null 2>&1
return $?
}
# Function to check if virsh is available
check_virsh() {
if ! command -v virsh >/dev/null 2>&1; then
echo "Error: virsh command not found. Please install libvirt-clients package."
exit 1
fi
}
# Main script
echo "Waiting for EdgeRouter (${EDGEROUTER_IP}) to become available..."
attempt=0
check_virsh
while ! check_edgerouter; do
printf "."
attempt=$((attempt + 1))
if [ ${attempt} -ge ${MAX_ATTEMPTS} ]; then
echo -e "\nError: Timeout waiting for EdgeRouter to respond after $((MAX_ATTEMPTS * SLEEP_INTERVAL)) seconds"
exit 1
fi
sleep ${SLEEP_INTERVAL}
done
echo -e "\nedgerouter is online!"
# Start the VM
echo "Starting VM: ${VM_NAME}"
if virsh start "${VM_NAME}"; then
echo "Successfully started ${VM_NAME}"
else
echo "Error: Failed to start ${VM_NAME}"
exit 1
fi
exit 0
Key improvements made for my operations:
- Added proper variable declarations at the top for easy configuration
- Added configurable timeout and retry settings
- Improved error handling and status messages
- Added check for virsh availability
- Added more robust ping check with configurable count and timeout
- Added proper exit codes for error handling
- Added feedback for VM startup success/failure
- Made the script more maintainable with comments and consistent formatting
- Added protection against unbound variables with proper quoting
- Added proper stdout/stderr redirection
Optional improvements you might want to consider for your additional VM’s:
- If you want to start multiple VMs with delays, you could add an array:
# Add this to the variables section:
VM_NAMES=("VM1" "VM2" "VM3")
VM_DELAYS=(0 30 60) # Delays in seconds before starting each VM
# Replace the VM start section with:
for i in "${!VM_NAMES[@]}"; do
if [ "${VM_DELAYS[$i]}" -gt 0 ]; then
echo "Waiting ${VM_DELAYS[$i]} seconds before starting ${VM_NAMES[$i]}"
sleep "${VM_DELAYS[$i]}"
fi
echo "Starting VM: ${VM_NAMES[$i]}"
if virsh start "${VM_NAMES[$i]}"; then
echo "Successfully started ${VM_NAMES[$i]}"
else
echo "Error: Failed to start ${VM_NAMES[$i]}"
exit 1
fi
done
- You might want to add a logger:
# Add to the top after shebang:
exec 1> >(logger -s -t "$(basename "$0")") 2>&1
- You could add a trap for cleanup:
# Add near the top:
trap 'echo "Script interrupted. Exiting..."; exit 1' INT TERM