From 6e3d143f09f4febed4a582b940612b0914a2386a Mon Sep 17 00:00:00 2001 From: Emily Frost Date: Mon, 10 Jun 2019 20:17:30 -0500 Subject: [PATCH] Updated vm-mount to handle multiple IP addresses for one VM. --- vm-mount | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/vm-mount b/vm-mount index b3798ff..3f9dea1 100755 --- a/vm-mount +++ b/vm-mount @@ -18,6 +18,7 @@ if [ "$1" = "-u" ]; then fusermount -u "${mountpoint}" fi + # TODO: Probably don't shut it down by default. if virsh list | grep -q " ${vm_name} "; then echo "Stopping ${vm_name}" virsh destroy "${vm_name}" @@ -43,23 +44,37 @@ else fi echo "Waiting for ${vm_name} to connect to the network..." - vm_ip_address="" + vm_ip_addresses="" # TODO: Exit if this failes or times out. # TODO: This needs a proper timeout. - while [ "${vm_ip_address}" = "" ]; do - vm_ip_address="$(virsh net-dhcp-leases "${NETWORK_NAME}" | awk "${awk_script}")" + while [ "${vm_ip_addresses}" = "" ]; do + vm_ip_addresses="$(virsh net-dhcp-leases "${NETWORK_NAME}" | awk "${awk_script}")" done - # TODO: Exit if this fails. + echo "$vm_ip_addresses" + + # TODO: Make sure this directory isn't already a mountpoint. # TODO: Make sure this directory isn't made if the vm fails to start or connect. - mkdir -p "${mountpoint}" + mkdir -p "${mountpoint}" || \ + { echo "Failed to create mountpoint ${mountpoint}"; exit 1; } # TODO: Exit if this failes or times out. # TODO: This needs a proper timeout. echo "Attempting to mount over ssh." - until sshfs "${vm_ip_address}:${REMOTE_DIRECTORY}" "${mountpoint}"; do - echo "Could not connect via SSH, retrying." + mounted='false' + + until [ $mounted = 'true' ]; do + for ip_address in $vm_ip_addresses; do + echo "Trying at ${ip_address}." + if sshfs "${ip_address}:${REMOTE_DIRECTORY}" "${mountpoint}"; then + echo "Mounted at ${mountpoint}." + exit 0 + else + echo "Could not connect to SSH server on ${ip_address}." + fi + done + sleep 3 done fi