#!/usr/bin/ksh

#PREFIX="echo "
PREFIX=""

if [ $# -eq 0 ] ; then
  echo "Usage: $0 [zone1] [...] [zone#]"
  exit 5
fi

SNAPSHOT_NAME="snapshot_$( date +%y%m%d_%H%M%S )"

if [ "$1"x = "all"x ] ; then
  set -- "$( zoneadm list -i | grep -v global)"
fi

for CURZONE in $* ; do
  echo "Processing zone \"${CURZONE}\" ..."
  ZONE_PATH="$( zoneadm -z ${CURZONE} list -p | cut -f4 -d ":" )"
  if [ "${ZONE_PATH}"x = ""x ] ; then
    echo "ERROR: Can not detect the zone path for the zone \"${CURZONE}\"!"
    continue 
  else
    ZONE_ZFS_VOL="$( zfs list -H  "${ZONE_PATH}"  | cut -f1  )"
    if [ "${ZONE_ZFS_VOL}"x = ""x ] ; then
      echo "ERROR: The zone path is not on an ZFS volume"
      continue 
    else
      SNAPSHOT="${ZONE_ZFS_VOL}@${SNAPSHOT_NAME}"
      echo "Creating the snapshot \"${SNAPSHOT}\" ..."
      ${PREFIX} zfs snapshot ${SNAPSHOT}
    fi
  fi 
done


