Skip to content

Instantly share code, notes, and snippets.

@jim80net
Last active November 20, 2023 14:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jim80net/5997596 to your computer and use it in GitHub Desktop.
Save jim80net/5997596 to your computer and use it in GitHub Desktop.
Use this script to migrate a SmartOS guest to a target. ./vmigrate.sh UUID TARGETIP - or - ./vmigrate.sh UUID TARGETIP migration-snapshotdate # specify the initial snapshot timestamp to increment the snapshot on the target
#!/bin/bash
# Use this to send and receive files
# Dependencies:
# SSH-Keys setup for root users. I recommend using agent forwarding for this.
# mbuffer installed in /opt/local/bin/mbuffer. Adjust to reality.
#
# This script presumes:
# zsnapper is installed. Comment out the two lines where it is referenced if you don't use it
DATE=$(date +%Y%m%d%H%M)
case $# in
[2])
ACTION=SEND
UUID=$1
TARGET=$2
echo "OK, sending $1 to $2"
;;
[3])
ACTION=INCREMENT
UUID=$1
TARGET=$2
INCREMENTAL=$3
echo "OK, sending $UUID incrementally from snapshot /zones/$UUID@$INCREMENTAL"
;;
*)
echo "Please call by using $0 UUID TARGET <incremental-startpoint>"
exit 1
;;
esac
function die {
echo "ERROR: $1"
exit 99
}
##
# Dependency check
function check_prog {
if which $1 2>&1 > /dev/null
then
donothing=ok
else
die "$1 does not appear to be installed. Please install."
fi
}
check_prog mbuffer
[[ "THISWORKS" == $(ssh $TARGET echo "THISWORKS" 2>/dev/null) ]] || die "Cannot SSH to $TARGET"
function take_snap {
SNAPNAME="$1@migration-$DATE"
zfs snapshot $SNAPNAME && echo "SNAP CREATED: $SNAPNAME"
}
#$1 zfs@snapname
#$2 TARGET
#$3 zfs
function send_snap {
zfs send -v $1 | /opt/local/bin/mbuffer -q -s 128k -m 4G | ssh -c arcfour $2 "/opt/local/bin/mbuffer -q -s 128k -m 4G | zfs receive -F $3"
}
#$1 zfs@snapname
#$2 TARGET
#$3 zfs
#$4 INCREMENTAL
function send_increment {
zfs send -vi $3@$4 $1 | /opt/local/bin/mbuffer -q -s 128k -m 4G | ssh -c arcfour $2 "/opt/local/bin/mbuffer -q -s 128k -m 4G | zfs receive -F $3"
}
#$1 UUID
#$2 TARGET
function create_zone_entry {
echo "Sending manifest..."
scp /etc/zones/$1.xml $2:/etc/zones/ && echo "...done" || die "Unable to send manifest /etc/zones/$1.xml to $2"
echo "Installing index."
echo "$1:installed:/zones/$1:$1" | ssh $2 "/usr/bin/cat >> /etc/zones/index"
ssh -t $2 vmadm list -v | grep $1 && echo "Remote host checks out." || die "Failed to find remote vm installed."
}
##
# MAIN
for disk in $( zfs list | grep $UUID | grep -v cores | awk '{print $1}' )
do
take_snap $disk
DISKS="$DISKS $disk"
done
ssh -t $TARGET svcadm disable zsnapper:default
case $ACTION in
"SEND")
for sdisk in $DISKS
do
echo "Sending $sdisk"
send_snap $sdisk@migration-$DATE $TARGET $sdisk
done
echo "Done sending disks."
create_zone_entry $UUID $TARGET
;;
"INCREMENT")
for sdisk in $DISKS
do
echo "Incrementing $sdisk"
send_increment $sdisk@migration-$DATE $TARGET $sdisk $INCREMENTAL
done
echo "Done incrementing disks."
;;
esac
ssh -t $TARGET svcadm enable zsnapper:default
echo "##############################################################################################"
echo "##"
echo "# When ready, halt the zone on the sender side before starting it on the receive side with:"
echo "vmadm halt $UUID"
echo ""
echo "##"
echo "# If you want to increment the disks, then run this:"
echo "$0 $UUID $TARGET migration-$DATE"
echo ""
echo "##"
echo "# Start the zone on the target with the commmand:"
echo "ssh -t $TARGET vmadm boot $UUID"
echo ""
echo "##"
echo "# Make sure you cleanup the source VM:"
echo "vmadm destroy $UUID"
echo ""
echo "##############################################################################################"
@nahall
Copy link

nahall commented Jul 22, 2013

Do you have suggestions of how to install mbuffer on SmartOS so I can get this script to work? Thanks!

@jim80net
Copy link
Author

Sorry I failed at reading your comment in a timely fashion! I precompile mbuffer in a SmartOS zone. I have a git repo that I checkout when initializing a new host server, that includes a precompiled version of the mbuffer program. The initialization script then runs bmake install from that precompiled directory. This way, I don't have to install gcc on the host server.

Zsnapper, I just installed according to the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment