Skip to content

Instantly share code, notes, and snippets.

@jhrcz
Last active August 29, 2015 14:04
Show Gist options
  • Save jhrcz/3a04577e8c1d18014d33 to your computer and use it in GitHub Desktop.
Save jhrcz/3a04577e8c1d18014d33 to your computer and use it in GitHub Desktop.
one-reinstantiate-vm.sh
#!/bin/bash
#
# reinstantiate-vm.sh
#
# reinstantiate vms based on current vmid
# to prevent data loss vm is checked that it does not contain nonpersistent volumes
#
# reinstantiation consists of
# get template from which vm was started
# shutdown vm
# instantiate template
# fix vm owner based on the template owner
#
vmid=$1
[ -n "$vmid" ] || exit 1
echo "vmid=$vmid"
vmname=$(onevm show $vmid --xml | xmlstarlet sel -t -m '//VM' -v NAME )
echo "vmname=$vmname"
tplid=$(onevm show $vmid | grep TEMPLATE_ID | cut -d '"' -f 2)
echo "tplid=$tplid"
if onevm show $vmid --xml | xmlstarlet sel -t -m '//VM/TEMPLATE/DISK' -v SAVE | grep -q NO
then
echo "ERROR: VM $vmid contains nonpersistent volumes, exiting!"
exit 1
fi
echo "waiting 5sec..."
sleep 5
echo "shutting down..."
onevm shutdown $vmid
until [ "$(onevm show $vmid | grep ^STATE | cut -d : -f 2 | tr -d " " )" = "DONE" ]
do
echo "wainting for shutdown..."
sleep 2
done
echo "starting new vm..."
newvmid=$(onetemplate instantiate $tplid)
newvmid=$(echo "$newvmid" | grep "VM ID:" | cut -d : -f 2 | tr -d ' ')
echo "newvmid=$newvmid"
if [ -n "$newvmid" ]
then
echo "fixing vm owner..."
one-fix-vm-owner.sh $newvmid
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment