--- old/usr/src/lib/brand/lx/zone/lx_install.ksh	Wed Oct  5 00:33:22 2016
+++ new/usr/src/lib/brand/lx/zone/lx_install.ksh	Wed Oct  5 00:33:22 2016
@@ -10,185 +10,128 @@
 # http://www.illumos.org/license/CDDL.
 #
 # Copyright 2016 Joyent, Inc.  All rights reserved.
+# Copyright 2016 OmniTI Computer Consulting, Inc.  All rights reserved.
 #
 
-#
-# This is only an example install script. It is not currently used for anything.
-#
-
 PATH=/bin:/usr/bin:/usr/sbin
 export PATH
 
-fullpath()
-{
-	typeset path="$1"
+. /usr/lib/brand/shared/common.ksh
 
-	echo $path | egrep -s "^/" || path="${PWD:=$(pwd)}/$path"
-	echo $path
-}
+ZFS_SEED=""
+TAR_SEED=""
 
-makedir()
-{
-	typeset dirname=$(fullpath "$1")
-	typeset mode=""
-
-	[[ $# -eq 2 ]] && mode="-m $2"
-
-	[[ -d "$dirname" ]] && return
-
-	if ! mkdir $mode -p "$dirname"; then
-		echo $(gettext "Aborting installation...")
-		exit 255
-	fi
+bad_usage() {
+	echo "LX zone install bad option"
+	echo "Available options are:"
+	echo "	-s <absolute-pathname>	Path to ZFS send stream or gzip thereof"
+	echo "	-t <absolute-pathname>	Path to tar archive or gzip thereof"
+	exit $ZONE_SUBPROC_USAGE
 }
 
-symlink()
-{
-	typeset src="$1"
-	typeset dst=$(fullpath "$2")
+while getopts "R:s:t:z:" opt
+do
+	case "$opt" in
+		R)	ZONEPATH="$OPTARG";;
+		z)	ZONENAME="$OPTARG";;
+		s)	ZFS_SEED="$OPTARG";;
+		t)	TAR_SEED="$OPTARG";;
+		*)	bad_usage ;;
+	esac
+done
+shift OPTIND-1
 
-	[[ -e "$dst" || -h "$dst" ]] && rm -f "$dst"
-	
-	if ! ln -s "$src" "$dst"; then
-		echo $(gettext "Aborting installation...")
-		exit 255
-	fi
-}
+if [[ $ZFS_SEED == "" && $TAR_SEED == "" ]]; then
+    echo "The -s <absolute-pathname> argument or the -t <absolute-pathname>"
+    echo "argument is required for LX installation."
+    bad_usage
+fi
 
-install_ln()
-{
-	typeset source="$1"
-	typeset target=$(fullpath "$2")
+if [[ $ZFS_SEED != "" && $TAR_SEED != "" ]]; then
+    echo "You must only specify one of -s or -t for LX installation."
+    bad_usage
+fi
 
-	log "    Installing \"$target\""
+# Set the ZONEPATH_DS variable so we know the zone's dataset.
+get_zonepath_ds $ZONEPATH
 
-	mv -f "$target" "$target.$tag" 2>/dev/null
+# Do something based on whatever ZFS_SEED is.
 
-	if ! ln -s "$source" "$target"; then
-		return 1
+if [[ -f $TAR_SEED ]]; then
+    type=`file -b $TAR_SEED | awk '{print $1}'`
+    if [[ $type == "gzip" ]]; then
+	args="-xzf"
+    else
+	args="-xf"
+    fi
+    cd $ZONEPATH
+    # Be very precise about permissions and ownership.
+    mkdir -m 0755 dev
+    chgrp sys dev
+    mkdir -m 0755 root
+    chgrp root sys
+    cd root
+    gtar $args $TAR_SEED
+    exit 0
+elif [[ ! -f $ZFS_SEED ]]; then
+    # Try and eat a snapshot or a filesystem.
+    outstr=`zfs list -Ht filesystem $ZFS_SEED 2>/dev/null | awk '{print $1}'`
+    if [[ $outstr == $ZFS_SEED ]]; then
+	# We have a zfs filesystem name.
+	# Snapshot it using today's date/time
+	snapname=`date -u "+%Y-%m-%d:%H:%M:%S"`
+	ZFS_SEED=$ZFS_SEED@$snapname
+	zfs snapshot $ZFS_SEED
+	if [[ $? != 0 ]]; then
+	    echo "ZFS snapshot ($ZFS_SEED) command failed ($?)."
+	    exit $ZONE_SUBPROC_FATAL
 	fi
+	# else continue on with the new snapshot...
+    fi
 
-	return 0
-}
-
-# If we weren't passed 3 arguments, exit now.
-[[ $# -lt 3 ]] && exit 254
-
-# Extract the brand directory name from the path.
-branddir=$(dirname "$0")
-zonename="$1"
-zoneroot="$2"
-install_src="3"
-install_root="$zoneroot/root"
-ZPOOL=`df $ZONEROOT | awk -F '[()]' '{split($2, field, "/"); print field[1]; }'`
-if [ -z "$ZPOOL" ]; then
-	ROOTDEV="none"
+    outstr=`zfs list -Ht snapshot $ZFS_SEED 2>/dev/null | awk '{print $1}'`
+    if [[ $outstr == $ZFS_SEED ]]; then
+	# Hmmm, we found a snapshot name!
+	echo "Cloning from snapshot $ZFS_SEED"
+	# zoneadm already created $ZONEPATH_DS, destroy it before we clone.
+	zfs destroy $ZONEPATH_DS
+	zfs clone $ZFS_SEED $ZONEPATH_DS
+	if [[ $? != 0 ]]; then
+	    echo "ZFS clone ($ZFS_SEED to $ZONEPATH_DS) failed ($?)."
+	    exit $ZONE_SUBPROC_FAIL
+	fi
+	# zfs promote $ZONEPATH_DS
+	# if [[ $? != 0 ]]; then
+	#    echo "ZFS promote ($ZONEPATH_DS) failed ($?)."
+	#    exit $ZONE_SUBPROC_FAIL
+	# fi
+    else 
+	echo "Seed file $ZFS_SEED $TAR_SEED not found."
+	bad_usage
+    fi
 else
-	ROOTDEV="/dev/$ZPOOL"
-fi
+    type=`file -b $ZFS_SEED | awk '{print $1}'`
 
-if [[ ! -f "$install_src" ]]; then
-	echo "$install_src: file not found\n"
-	exit 254
-fi
+    # For now, we are dependent on the output of file(1).
+    # I'm being cheesy in checking the first word of file(1)'s output.
+    if [[ $type == "ZFS" ]]; then
+	zfs recv -F $ZONEPATH_DS < $ZFS_SEED
+    elif [[ $type == "gzip" ]]; then
+	gunzip -c $ZFS_SEED | zfs recv -F $ZONEPATH_DS
+    else
+	echo "Seed file $ZFS_SEED not a ZFS receive (or compressed) one."
+	bad_usage
+    fi
 
-if [[ ! -d "$install_root" ]]; then
-	if ! mkdir -p "$install_root" 2>/dev/null; then
-		echo "Could not create install directory $install_root"
-		exit 254
-	fi
+    if [[ $? != 0 ]]; then
+	echo "ZFS receive command failed ($?)."
+	exit $ZONE_SUBPROC_FATAL
+    fi
 fi
 
-if ! ( cd "$install_root" && gtar -xzf "$install_src" ) ; then
-	echo "Error: extraction from tar archive failed"
-	exit 255
-fi
+# One Joyent-ism we need to clean up.
+rmdir $ZONEPATH/cores
+# And one we should probably adopt.
+zfs set devices=off $ZONEPATH_DS
 
-tag="lxsave_$(date +%m.%d.%Y@%T)"
-
-if [[ ! -d "$install_root" ]]; then
-	exit 255
-fi
-
-cd "$install_root"
-
-makedir native/dev
-makedir native/etc/default
-makedir native/etc/svc/volatile
-makedir native/lib
-makedir native/proc
-makedir native/tmp 1777
-makedir native/usr
-makedir native/var
-
-makedir mnt
-makedir opt
-makedir usr/local/bin
-makedir usr/local/include
-makedir usr/local/lib
-makedir usr/local/sbin
-makedir usr/local/share
-makedir usr/local/src
-
-makedir dev 0755
-makedir tmp 1777
-makedir proc 0555
-makedir boot 0755
-
-symlink /bin/sh sbin/sh
-symlink /bin/su usr/bin/su
-symlink /native/usr/lib/ld.so.1 usr/lib/ld.so.1
-
-libpam_so="$(echo lib/libpam.so.0.*)"
-libpam_misc="$(echo lib/libpam_misc.so.0.*)"
-libpamc_so="$(echo lib/libpamc.so.0.*)"
-
-symlink "/$libpam_so" lib/libpam.so.0
-symlink "/$libpam_misc" lib/libpam_misc.so.0
-symlink "/$libpamc_so" lib/libpamc.so.0
-
-makedir var/ld
-
-if ! crle -c var/ld/ld.config -l /native/lib:/native/usr/lib \
-     -s /native/lib/secure:/native/usr/lib/secure; then
-	exit 255
-fi
-
-mv -f etc/fstab etc/fstab.$tag 2>/dev/null
-
-cat > etc/fstab <<- EOF
-	$ROOTDEV	/			zfs	defaults	1 1
-	proc		/proc			proc	defaults	0 0
-EOF
-
-if [[ $? -ne 0 ]]; then
-	exit 255
-fi
-
-if [[ ! -e "$install_root/etc/hosts" ]]; then
-	cat > "$install_root/etc/hosts" <<-_EOF_
-		127.0.0.1		localhost
-	_EOF_
-fi
-
-#
-# Perform distribution-specific changes.
-#
-distro=""
-if [[ -f etc/redhat-release ]]; then
-	distro="redhat"
-elif [[ -f etc/lsb-release ]]; then
-	if egrep -s Ubuntu etc/lsb-release; then
-		distro="ubuntu"
-	elif [[ -f etc/debian_version ]]; then
-		distro="debian"
-	fi
-elif [[ -f etc/debian_version ]]; then
-	distro="debian"
-fi
-
-if [[ -z $distro ]]; then
-	exit 255
-fi
-
 exit 0
