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