xref: /titanic_44/usr/src/tools/scripts/onu.sh (revision bdf0047c9427cca40961a023475891c898579c37)
1#!/bin/ksh93 -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#
24# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28PATH=/usr/bin:/usr/sbin
29export PATH
30
31DEFAULTONURI="http://ipkg.sfbay/on-nightly"
32DEFAULTONPUB="on-nightly"
33DEFAULTONEXTRAURI="http://ipkg.sfbay/on-extra"
34DEFAULTONEXTRAPUB="on-extra"
35
36usage()
37{
38	echo "usage: $0 [-e URI [-E pub]] [-O] [-s beName] -t beName [-u URI [-U pub]] [-vZ]"
39	echo "usage: $0 [ -d dir ][ -O ][ -s beName ] -t beName [-vZ]"
40	echo "Use -s to specify the source BE to clone (default is the active BE)."
41	echo "Use -t to specify the target BE name."
42	echo "Use -u to specify the origin URI for the publisher packaging repository"
43	echo "\t(default is \$ONURI if set in the environment, otherwise"
44	echo "\t$DEFAULTONURI )."
45	echo "Use -U to specify the publisher prefix for the packaging repository"
46	echo "\t(default is \$ONPUB if set in the environment, otherwise"
47	echo "\t$DEFAULTONPUB )."
48	echo "Use -e to specify the origin URI for the extra repository (default is"
49	echo "\t\$ONEXTRAURI if set in the environment, otherwise"
50	echo "\t$DEFAULTONEXTRAURI )."
51	echo "Use -E to specify the publisher prefix for the extra repository (default"
52	echo "\tis \$ONEXTRAPUB if set in the environment, otherwise"
53	echo "\t$DEFAULTONEXTRAPUB )."
54	echo "Use -d to specify a directory containing redist and extra directories."
55	echo "\tNote that -d overrides -u, -U, -e and -E."
56	echo "Use -O for open mode, meaning that no extra repository will be used."
57	echo "Use -v for verbose mode."
58	echo "Use -Z to skip updating zones."
59	exit 1
60}
61
62exit_error()
63{
64	echo $*
65	cleanup
66	exit 2
67}
68
69cleanup()
70{
71	[ $redistpid -gt 0 ] && kill $redistpid
72	[ $extrapid -gt 0 ] && kill $extrapid
73	[ -d /tmp/redist.$$ ] && /bin/rm -rf /tmp/redist.$$
74	[ -d /tmp/extra.$$ ] && /bin/rm -rf /tmp/extra.$$
75}
76
77do_cmd()
78{
79	[ $verbose -gt 0 ] && echo $*
80	$*
81	exit_code=$?
82	[ $exit_code -eq 0 ] && return
83	# pkg(1) returns 4 if "nothing to do", which is safe to ignore
84	[ $1 = "pkg" -a $exit_code -eq 4 ] && return
85	exit_error "$*" failed: exit code $exit_code
86}
87
88update()
89{
90	tmpdir=$1
91
92	do_cmd pkg -R $tmpdir set-publisher --non-sticky opensolaris.org
93	pkg -R $tmpdir list entire > /dev/null 2>&1
94	[ $? -eq 0 ] && do_cmd pkg -R $tmpdir uninstall entire
95	do_cmd pkg -R $tmpdir set-publisher -P -O $uri $redistpub
96	[ $open -eq 0 ] && do_cmd pkg -R $tmpdir set-publisher -O $extrauri $extrapub
97	do_cmd pkg -R $tmpdir refresh --full
98	do_cmd pkg -R $tmpdir image-update
99}
100
101update_zone()
102{
103	zone=$1
104
105	name=`echo $zone | cut -d: -f 2`
106	if [ $name = "global" ]; then
107		return
108	fi
109
110	brand=`echo $zone | cut -d: -f 6`
111	if [ $brand != "ipkg" ]; then
112		return
113	fi
114
115	if [ "$zone_warned" = 0 ]; then
116		echo "WARNING: Use of onu(1) will prevent use of zone attach in the new BE" >&2
117		if [ -n "$repodir" ]; then
118			echo "WARNING: use of -d option will prevent new zone installs in the new BE" >&2
119		fi
120
121		echo "See onu(1)" >&2
122		zone_warned=1
123	fi
124
125	state=`echo $zone | cut -d: -f 3`
126
127	case "$state" in
128	configured|incomplete)
129		return
130		;;
131	esac
132
133	zoneroot=`echo $zone | cut -d: -f 4`
134
135	echo "Updating zone $name"
136	update $zoneroot/root
137}
138
139sourcebe=""
140targetbe=""
141uri=""
142extrauri=""
143repodir=""
144verbose=0
145open=0
146redistpid=0
147extrapid=0
148redistport=13000
149extraport=13001
150no_zones=0
151zone_warned=0
152
153trap cleanup 1 2 3 15
154
155while getopts :d:E:e:Os:t:U:u:v:Z i ; do
156	case $i in
157	d)
158		repodir=$OPTARG
159		;;
160	E)
161		extrapub=$OPTARG
162		;;
163	e)
164		extrauri=$OPTARG
165		;;
166	O)
167		open=1
168		;;
169	s)
170		sourcebe=$OPTARG
171		;;
172	t)
173		targetbe=$OPTARG
174		;;
175	U)
176		redistpub=$OPTARG
177		;;
178	u)
179		uri=$OPTARG
180		;;
181	v)
182		verbose=1
183		;;
184	Z)
185		no_zones=1
186		;;
187	*)
188		usage
189	esac
190done
191shift `expr $OPTIND - 1`
192
193[ -z "$targetbe" ] && usage
194[ -z "$uri" ] && uri=$ONURI
195[ -z "$uri" ] && uri=$DEFAULTONURI
196[ -z "$redistpub" ] && redistpub=$ONPUB
197[ -z "$redistpub" ] && redistpub=$DEFAULTONPUB
198[ -z "$extrauri" ] && extrauri=$ONEXTRAURI
199[ -z "$extrauri" ] && extrauri=$DEFAULTONEXTRAURI
200[ -z "$extrapub" ] && extrapub=$ONEXTRAPUB
201[ -z "$extrapub" ] && extrapub=$DEFAULTONEXTRAPUB
202
203if [ -n "$repodir" ]; then
204	redistdir=$repodir/repo.redist
205	[ -d $redistdir ] || exit_error "$redistdir not found"
206	redistpub=$(python2.6 <<# EOF
207		import ConfigParser
208		p = ConfigParser.SafeConfigParser()
209		p.read("$redistdir/cfg_cache")
210		pp = p.get("publisher", "prefix")
211		print "%s" % pp
212		EOF)
213	[ $verbose -gt 0 ] && echo "starting pkg.depotd -d $redistdir -p $redistport"
214	ARGS="--readonly --writable-root"
215	mkdir /tmp/redist.$$
216	/usr/lib/pkg.depotd -d $redistdir -p $redistport $ARGS /tmp/redist.$$ >/dev/null &
217	redistpid=$!
218	uri="http://localhost:$redistport/"
219	if [ $open -eq 0 ]; then
220		extradir=$repodir/repo.extra
221		[ -d $extradir ] || exit_error "$extradir not found"
222		extrapub=$(python2.6 <<# EOF
223			import ConfigParser
224			p = ConfigParser.SafeConfigParser()
225			p.read("$extradir/cfg_cache")
226			pp = p.get("publisher", "prefix")
227			print "%s" % pp
228			EOF)
229		[ $verbose -gt 0 ] && echo "starting pkg.depotd -d $extradir -p $extraport"
230		mkdir /tmp/extra.$$
231		/usr/lib/pkg.depotd -d $extradir -p $extraport $ARGS /tmp/extra.$$ >/dev/null &
232		extrapid=$!
233		extrauri="http://localhost:$extraport/"
234	fi
235fi
236
237createargs=""
238[ -n "$sourcebe" ] && createargs="-e $sourcebe"
239
240# ksh seems to have its own mktemp with slightly different semantics
241tmpdir=`/usr/bin/mktemp -d /tmp/onu.XXXXXX`
242[ -z "$tmpdir" ] && exit_error "mktemp failed"
243
244do_cmd beadm create $createargs $targetbe
245do_cmd beadm mount $targetbe $tmpdir
246update $tmpdir
247do_cmd beadm activate $targetbe
248
249if [ "$no_zones" != 1 ]; then
250	for zone in `do_cmd zoneadm -R $tmpdir list -cip`; do
251		update_zone $zone
252	done
253fi
254
255cleanup
256exit 0
257