Skip to content

Instantly share code, notes, and snippets.

@jasonpincin
Last active December 13, 2015 18:49
Show Gist options
  • Save jasonpincin/4958099 to your computer and use it in GitHub Desktop.
Save jasonpincin/4958099 to your computer and use it in GitHub Desktop.
Companion script to import-image script (https://gist.github.com/jasonpincin/4958050), this script is intended for running against VM's in the global zone. It will snapshot them, and send them off to the image server (equipped with the import-image script).
#!/usr/bin/bash
# Creates an image from a VM
# WARNING: You should run sm-prepare-image inside the zone
# BEFORE running this script!
#
# This script requires the smartos-image-server to be
# equipped with the import-image script:
# https://gist.github.com/jasonpincin/4958050
#
# This script stops the VM, takes a snapshot, and sends the
# snapshot to a smartos-image-server equipped with the
# import-image script, which generates the manifest and reloads
# the image server.
#
# Usage:
# mkimg <uuid> <name> <version> <description>
#
# CONFIGURE THESE LINES FOR YOUR ENVIRONMENT
IMGSERVER="datasets.myorg.com" # The FQDN of the smartos-image-server equipped with import-image
LOGIN="node" # The user to login to the image server as
# NO MORE CONFIG NEEDED
REACHABLE=0
ping $IMGSERVER > /dev/null 2>&1 && REACHABLE=1
if [ $REACHABLE -eq 1 ]; then
echo "Stopping $1..."
vmadm stop $1 > /dev/null 2>&1
echo "Snapshotting VM..."
zfs destroy zones/$1@image
zfs snapshot zones/$1@image
echo "Sending zones/$1@image to $LOGIN@$IMGSERVER ..."
IMGUUID=`zfs send zones/$1@image | gzip | ssh $LOGIN@$IMGSERVER "./import-image $2 $3 '$4'"`
echo "Import complete: $IMGUUID"
echo "Updating images..."
imgadm update
imgadm avail |grep $IMGUUID
else
echo "Target image server is unreachable."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment