xref: /illumos-gate/usr/src/lib/brand/solaris10/zone/clone.ksh (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
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 2009 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# Set up ZFS dataset hierarchy for the zone.
34
35ROOT="rpool/ROOT"
36
37# Other brand clone options are invalid for this brand.
38while getopts "R:z:" opt; do
39	case $opt in
40		R)	ZONEPATH="$OPTARG" ;;
41		z)	ZONENAME="$OPTARG" ;;
42		*)	printf "$m_usage\n"
43			exit $ZONE_SUBPROC_USAGE;;
44	esac
45done
46shift $((OPTIND-1))
47
48if [ $# -ne 1 ]; then
49	fail_usage "$0 {sourcezone}";
50fi
51
52sourcezone=$1
53
54# Find the active source zone dataset to clone.
55sourcezonepath=`/usr/sbin/zonecfg -z $sourcezone info zonepath | \
56    /usr/bin/cut -f2 -d' '`
57if [ -z "$sourcezonepath" ]; then
58	fail_fatal "$f_nosource"
59fi
60
61get_zonepath_ds $sourcezonepath
62get_active_ds $ZONEPATH_DS
63
64#
65# Now set up the zone's datasets
66#
67
68#
69# First make the top-level dataset.
70#
71
72pdir=`/usr/bin/dirname $ZONEPATH`
73zpname=`/usr/bin/basename $ZONEPATH`
74
75get_zonepath_ds $pdir
76zpds=$ZONEPATH_DS
77
78# Create the datasets.
79/usr/sbin/zfs create $zpds/$zpname
80if (( $? != 0 )); then
81	fail_fatal "$f_zfs_create"
82fi
83
84/usr/sbin/zfs create -o mountpoint=legacy -o zoned=on $zpds/$zpname/ROOT
85if (( $? != 0 )); then
86	fail_fatal "$f_zfs_create"
87fi
88
89# make snapshot
90SNAPNAME=${ZONENAME}_snap
91SNAPNUM=0
92while [ $SNAPNUM -lt 100 ]; do
93	/usr/sbin/zfs snapshot $ACTIVE_DS@$SNAPNAME
94        if [ $? = 0 ]; then
95                break
96	fi
97	SNAPNUM=`expr $SNAPNUM + 1`
98	SNAPNAME="${ZONENAME}_snap$SNAPNUM"
99done
100
101if [ $SNAPNUM -ge 100 ]; then
102	fail_fatal "$f_zfs_create"
103fi
104
105# do clone
106BENAME=zbe-0
107/usr/sbin/zfs clone -o $PROP_ACTIVE=on -o canmount=noauto \
108     $ACTIVE_DS@$SNAPNAME $zpds/$zpname/ROOT/$BENAME
109if (( $? != 0 )); then
110	fail_fatal "$f_zfs_create"
111fi
112
113if [ ! -d $ZONEPATH/root ]; then
114	/usr/bin/mkdir -p $ZONEPATH/root || \
115	    fail_fatal "$f_mkdir" "$ZONEPATH/root"
116	/usr/bin/chmod 700 $ZONEPATH || \
117	    fail_fatal "$f_chmod" "$ZONEPATH"
118fi
119
120# Don't re-sysunconfig if we've already done so
121if [[ ! -f $ZONEROOT/etc/.UNCONFIGURED ]]; then
122	/usr/sbin/zoneadm -z $ZONENAME boot -f -- -m milestone=none
123	if (( $? != 0 )); then
124		error "$e_badboot"
125		fail_incomplete "$f_sysunconfig"
126	fi
127
128	sysunconfig_zone
129	if (( $? != 0 )); then
130		/usr/sbin/zoneadm -z $ZONENAME halt
131		fail_incomplete "$f_sysunconfig"
132	fi
133
134	/usr/sbin/zoneadm -z $ZONENAME halt
135fi
136
137exit $ZONE_SUBPROC_OK
138