1e71ca95cSGerald Jelinek#!/bin/ksh -p
2e71ca95cSGerald Jelinek#
3e71ca95cSGerald Jelinek# CDDL HEADER START
4e71ca95cSGerald Jelinek#
5e71ca95cSGerald Jelinek# The contents of this file are subject to the terms of the
6e71ca95cSGerald Jelinek# Common Development and Distribution License (the "License").
7e71ca95cSGerald Jelinek# You may not use this file except in compliance with the License.
8e71ca95cSGerald Jelinek#
9e71ca95cSGerald Jelinek# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10e71ca95cSGerald Jelinek# or http://www.opensolaris.org/os/licensing.
11e71ca95cSGerald Jelinek# See the License for the specific language governing permissions
12e71ca95cSGerald Jelinek# and limitations under the License.
13e71ca95cSGerald Jelinek#
14e71ca95cSGerald Jelinek# When distributing Covered Code, include this CDDL HEADER in each
15e71ca95cSGerald Jelinek# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16e71ca95cSGerald Jelinek# If applicable, add the following below this CDDL HEADER, with the
17e71ca95cSGerald Jelinek# fields enclosed by brackets "[]" replaced with your own identifying
18e71ca95cSGerald Jelinek# information: Portions Copyright [yyyy] [name of copyright owner]
19e71ca95cSGerald Jelinek#
20e71ca95cSGerald Jelinek# CDDL HEADER END
21e71ca95cSGerald Jelinek#
22e71ca95cSGerald Jelinek#
23ab5dfd5eS# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24e71ca95cSGerald Jelinek# Use is subject to license terms.
25e71ca95cSGerald Jelinek#
26e71ca95cSGerald Jelinek
27e71ca95cSGerald Jelinek. /usr/lib/brand/solaris10/common.ksh
28e71ca95cSGerald Jelinek
29e71ca95cSGerald Jelinekm_usage=$(gettext "solaris10 brand usage: clone {sourcezone}.")
30e71ca95cSGerald Jelinekf_nosource=$(gettext "Error: unable to determine source zone dataset.")
31e71ca95cSGerald Jelinekf_sysunconfig=$(gettext "Error: sys-unconfig failed.")
32e71ca95cSGerald Jelinek
33e71ca95cSGerald Jelinek# Other brand clone options are invalid for this brand.
34e71ca95cSGerald Jelinekwhile getopts "R:z:" opt; do
35e71ca95cSGerald Jelinek	case $opt in
36e71ca95cSGerald Jelinek		R)	ZONEPATH="$OPTARG" ;;
37e71ca95cSGerald Jelinek		z)	ZONENAME="$OPTARG" ;;
38e71ca95cSGerald Jelinek		*)	printf "$m_usage\n"
39e71ca95cSGerald Jelinek			exit $ZONE_SUBPROC_USAGE;;
40e71ca95cSGerald Jelinek	esac
41e71ca95cSGerald Jelinekdone
42e71ca95cSGerald Jelinekshift $((OPTIND-1))
43e71ca95cSGerald Jelinek
44e71ca95cSGerald Jelinekif [ $# -ne 1 ]; then
45e71ca95cSGerald Jelinek	fail_usage "$0 {sourcezone}";
46e71ca95cSGerald Jelinekfi
47e71ca95cSGerald Jelinek
48*090f667dSZONEROOT="$ZONEPATH/root"
49e71ca95cSGerald Jelineksourcezone=$1
50e71ca95cSGerald Jelinek
51e71ca95cSGerald Jelinek# Find the active source zone dataset to clone.
52e71ca95cSGerald Jelineksourcezonepath=`/usr/sbin/zonecfg -z $sourcezone info zonepath | \
53e71ca95cSGerald Jelinek    /usr/bin/cut -f2 -d' '`
54e71ca95cSGerald Jelinekif [ -z "$sourcezonepath" ]; then
55e71ca95cSGerald Jelinek	fail_fatal "$f_nosource"
56e71ca95cSGerald Jelinekfi
57e71ca95cSGerald Jelinek
58e71ca95cSGerald Jelinekget_zonepath_ds $sourcezonepath
59e71ca95cSGerald Jelinekget_active_ds $ZONEPATH_DS
60*090f667dSACTIVE_SRC=$ACTIVE_DS
61e71ca95cSGerald Jelinek
62e71ca95cSGerald Jelinek#
63e71ca95cSGerald Jelinek# Now set up the zone's datasets
64e71ca95cSGerald Jelinek#
65e71ca95cSGerald Jelinek
66e71ca95cSGerald Jelinek#
67e71ca95cSGerald Jelinek# First make the top-level dataset.
68e71ca95cSGerald Jelinek#
69e71ca95cSGerald Jelinek
70e71ca95cSGerald Jelinekpdir=`/usr/bin/dirname $ZONEPATH`
71e71ca95cSGerald Jelinekzpname=`/usr/bin/basename $ZONEPATH`
72e71ca95cSGerald Jelinek
73e71ca95cSGerald Jelinekget_zonepath_ds $pdir
74e71ca95cSGerald Jelinekzpds=$ZONEPATH_DS
75e71ca95cSGerald Jelinek
76e71ca95cSGerald Jelinek# Create the datasets.
77e71ca95cSGerald Jelinek/usr/sbin/zfs create $zpds/$zpname
78e71ca95cSGerald Jelinekif (( $? != 0 )); then
79e71ca95cSGerald Jelinek	fail_fatal "$f_zfs_create"
80e71ca95cSGerald Jelinekfi
81e71ca95cSGerald Jelinek
82e71ca95cSGerald Jelinek/usr/sbin/zfs create -o mountpoint=legacy -o zoned=on $zpds/$zpname/ROOT
83e71ca95cSGerald Jelinekif (( $? != 0 )); then
84e71ca95cSGerald Jelinek	fail_fatal "$f_zfs_create"
85e71ca95cSGerald Jelinekfi
86e71ca95cSGerald Jelinek
87e71ca95cSGerald Jelinek# make snapshot
88e71ca95cSGerald JelinekSNAPNAME=${ZONENAME}_snap
89e71ca95cSGerald JelinekSNAPNUM=0
90e71ca95cSGerald Jelinekwhile [ $SNAPNUM -lt 100 ]; do
91*090f667dS	zfs snapshot $ACTIVE_SRC@$SNAPNAME
92e71ca95cSGerald Jelinek        if [ $? = 0 ]; then
93e71ca95cSGerald Jelinek                break
94e71ca95cSGerald Jelinek	fi
95e71ca95cSGerald Jelinek	SNAPNUM=`expr $SNAPNUM + 1`
96e71ca95cSGerald Jelinek	SNAPNAME="${ZONENAME}_snap$SNAPNUM"
97e71ca95cSGerald Jelinekdone
98e71ca95cSGerald Jelinek
99e71ca95cSGerald Jelinekif [ $SNAPNUM -ge 100 ]; then
100e71ca95cSGerald Jelinek	fail_fatal "$f_zfs_create"
101e71ca95cSGerald Jelinekfi
102e71ca95cSGerald Jelinek
103e71ca95cSGerald Jelinek# do clone
104*090f667dSget_active_ds $zpds/$zpname
105*090f667dSzfs clone -o canmount=noauto $ACTIVE_SRC@$SNAPNAME $ACTIVE_DS
106*090f667dS(( $? != 0 )) && fail_fatal "$f_zfs_create"
107e71ca95cSGerald Jelinek
108*090f667dSif [ ! -d $ZONEROOT ]; then
109*090f667dS	mkdir -p $ZONEROOT || fail_fatal "$f_mkdir" "$ZONEROOT"
110*090f667dS	chmod 700 $ZONEPATH || fail_fatal "$f_chmod" "$ZONEPATH"
111e71ca95cSGerald Jelinekfi
112e71ca95cSGerald Jelinek
113*090f667dSmount -F zfs $ACTIVE_DS $ZONEROOT || fail_fatal "$f_zfs_mount"
114*090f667dS
115e71ca95cSGerald Jelinek# Don't re-sysunconfig if we've already done so
116e71ca95cSGerald Jelinekif [[ ! -f $ZONEROOT/etc/.UNCONFIGURED ]]; then
117e71ca95cSGerald Jelinek	/usr/sbin/zoneadm -z $ZONENAME boot -f -- -m milestone=none
118e71ca95cSGerald Jelinek	if (( $? != 0 )); then
119e71ca95cSGerald Jelinek		error "$e_badboot"
120e71ca95cSGerald Jelinek		fail_incomplete "$f_sysunconfig"
121e71ca95cSGerald Jelinek	fi
122e71ca95cSGerald Jelinek
123e71ca95cSGerald Jelinek	sysunconfig_zone
124e71ca95cSGerald Jelinek	if (( $? != 0 )); then
125e71ca95cSGerald Jelinek		/usr/sbin/zoneadm -z $ZONENAME halt
126e71ca95cSGerald Jelinek		fail_incomplete "$f_sysunconfig"
127e71ca95cSGerald Jelinek	fi
128e71ca95cSGerald Jelinek
129e71ca95cSGerald Jelinek	/usr/sbin/zoneadm -z $ZONENAME halt
130e71ca95cSGerald Jelinekfi
131e71ca95cSGerald Jelinek
132ab5dfd5eS# Add a service tag for this zone.
133ab5dfd5eSadd_svc_tag "$ZONENAME" "clone $sourcezone"
134ab5dfd5eS
135e71ca95cSGerald Jelinekexit $ZONE_SUBPROC_OK
136