123294c7dSSean Wilcox#!/bin/ksh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6d62bc4baSyz# Common Development and Distribution License (the "License").
7d62bc4baSyz# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
22adfc3118STruong Nguyen# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2356bce609SAndrew Stormont# Copyright 2017 RackTop Systems.
247c478bd9Sstevel@tonic-gate#
257c478bd9Sstevel@tonic-gate
2670cbfe41SPhilippe Jung# 0a  Initialization.
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate[ -f /lib/svc/share/smf_include.sh ] || exit 1
297c478bd9Sstevel@tonic-gate
30c0889d7aSstevep. /lib/svc/share/smf_include.sh
31c0889d7aSstevep
327c478bd9Sstevel@tonic-gateactivity=false
337c478bd9Sstevel@tonic-gate
349444c26fSTom WhittenEMI_SERVICE="svc:/system/early-manifest-import:default"
35adfc3118STruong NguyenPROFILE_DIR_SITE="/etc/svc/profile/site"
369444c26fSTom Whitten
377c478bd9Sstevel@tonic-gateX=
38293e3ab3STruong Q. NguyenALT_REPOSITORY=
39293e3ab3STruong Q. NguyenALT_MFST_DIR=
409444c26fSTom Whittenearly=false
419444c26fSTom Whitten[ "$SMF_FMRI" == "$EMI_SERVICE" ] && early=true
42293e3ab3STruong Q. Nguyen
43293e3ab3STruong Q. Nguyenusage()
44293e3ab3STruong Q. Nguyen{
45293e3ab3STruong Q. Nguyen	echo "Usage: /lib/svc/method/manifest-import [-n]" \
46293e3ab3STruong Q. Nguyen	    "[-f repository-file -d manifest-directory]"
47293e3ab3STruong Q. Nguyen	echo "\nOptions:"
48293e3ab3STruong Q. Nguyen	echo "-n dryrun"
49293e3ab3STruong Q. Nguyen	echo "-f and -d specify alternate repository and" \
50293e3ab3STruong Q. Nguyen	    "manifest directory for import\n"
51293e3ab3STruong Q. Nguyen	exit 2
52293e3ab3STruong Q. Nguyen}
53293e3ab3STruong Q. Nguyen
54293e3ab3STruong Q. Nguyenwhile getopts "nd:f:" opt; do
557c478bd9Sstevel@tonic-gate	case $opt in
567c478bd9Sstevel@tonic-gate		n)	X=echo;;
57293e3ab3STruong Q. Nguyen		d)	ALT_MFST_DIR=$OPTARG;;
58293e3ab3STruong Q. Nguyen		f)	ALT_REPOSITORY=$OPTARG;;
59293e3ab3STruong Q. Nguyen		?)	usage;;
607c478bd9Sstevel@tonic-gate	esac
617c478bd9Sstevel@tonic-gatedone
627c478bd9Sstevel@tonic-gate
63293e3ab3STruong Q. Nguyen#
64293e3ab3STruong Q. Nguyen# Both -f and -d options must be specified together or not specified at all
65293e3ab3STruong Q. Nguyen#
66293e3ab3STruong Q. Nguyen[ -n "$ALT_REPOSITORY" -a -z "$ALT_MFST_DIR" ] && usage
67293e3ab3STruong Q. Nguyen[ -n "$ALT_MFST_DIR" -a -z "$ALT_REPOSITORY" ] && usage
68293e3ab3STruong Q. Nguyen
6923294c7dSSean Wilcoxfunction svccfg_apply {
707c478bd9Sstevel@tonic-gate	$X /usr/sbin/svccfg apply $1
717c478bd9Sstevel@tonic-gate	if [ $? -ne 0 ]; then
727c478bd9Sstevel@tonic-gate		echo "WARNING: svccfg apply $1 failed" | tee /dev/msglog
737c478bd9Sstevel@tonic-gate	fi
747c478bd9Sstevel@tonic-gate}
757c478bd9Sstevel@tonic-gate
769444c26fSTom Whitten#
7756bce609SAndrew Stormont# If the smf repository has file entries that are missing
789444c26fSTom Whitten# then there is work to be done by the cleanup process.
799444c26fSTom Whitten#
809444c26fSTom Whittenfunction cleanup_needwork {
8156bce609SAndrew Stormont	smfmfiles=`svcprop -p manifestfiles '*' 2>/dev/null |
8256bce609SAndrew Stormont	    nawk -v early="$early" '$2 == "astring" &&
8356bce609SAndrew Stormont	    (early != "true" || $3 ~ "^/lib/") { print $3 }'`
847c478bd9Sstevel@tonic-gate
859444c26fSTom Whitten	nw=`/lib/svc/bin/mfstscan $smfmfiles 2>&1 1>/dev/null`
869444c26fSTom Whitten	[ "$nw" ] && return 1
877c478bd9Sstevel@tonic-gate
889444c26fSTom Whitten	return 0
897c478bd9Sstevel@tonic-gate}
907c478bd9Sstevel@tonic-gate
919444c26fSTom Whitten#
929444c26fSTom Whitten# Upon upgrading to early manifest import code, preserve hashes of system
939444c26fSTom Whitten# profiles which lived under /var/svc/profile so that svccfg apply would
949444c26fSTom Whitten# not re-apply the profiles and overwrite user customizations. Simply
959444c26fSTom Whitten# migrate manifestfile and hash values to new property groups named after
969444c26fSTom Whitten# profiles under /etc/svc/profile. If the profiles don't really exist,
979444c26fSTom Whitten# svccfg cleanup will remove the property groups in a later step.
989444c26fSTom Whitten#
999444c26fSTom Whitten# Existing generic.xml, inetd_services.xml, and name_service.xml symlinks
1009444c26fSTom Whitten# need to be preserved.
1019444c26fSTom Whitten#
1029444c26fSTom Whitten# Don't process site.xml profile since it is still supported under
1039444c26fSTom Whitten# /var/svc/profile directory.
1049444c26fSTom Whitten#
1059444c26fSTom Whittenfunction preserve_system_profiles {
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate	#
1089444c26fSTom Whitten	# If /var is a separate fs, return and let Late Import
1099444c26fSTom Whitten	# preserves the hashes.
1109444c26fSTom Whitten	#
1119444c26fSTom Whitten	[ -d "/var/svc/profile" ] || return 1
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate	#
1149444c26fSTom Whitten	# Preserve hashes for the following profiles: generic (two
1159444c26fSTom Whitten	# cases) and platform (uname -i, uname -m outputs).
1167c478bd9Sstevel@tonic-gate	#
1177c478bd9Sstevel@tonic-gate	gn="var_svc_profile_generic_open_xml"
1187c478bd9Sstevel@tonic-gate	gh=`/usr/bin/svcprop -p ${gn}/md5sum smf/manifest 2>/dev/null`
1197c478bd9Sstevel@tonic-gate	[ $? = 0 ] || gh=""
1209444c26fSTom Whitten	gn="etc_svc_profile_generic_open_xml"
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate	gln="var_svc_profile_generic_limited_net_xml"
1237c478bd9Sstevel@tonic-gate	glh=`/usr/bin/svcprop -p ${gln}/md5sum smf/manifest 2>/dev/null`
1247c478bd9Sstevel@tonic-gate	[ $? = 0 ] || glh=""
1259444c26fSTom Whitten	gln="etc_svc_profile_generic_limited_net_xml"
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate	LC_ALL=C pl=`/usr/bin/uname -i | /usr/bin/tr , _`
1287c478bd9Sstevel@tonic-gate	pln="var_svc_profile_platform_${pl}_xml"
1297c478bd9Sstevel@tonic-gate	plh=`/usr/bin/svcprop -p ${pln}/md5sum smf/manifest 2>/dev/null`
1307c478bd9Sstevel@tonic-gate	[ $? = 0 ] || plh=""
1319444c26fSTom Whitten	pln="etc_svc_profile_platform_${pl}_xml"
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate	LC_ALL=C plm=`/usr/bin/uname -m | /usr/bin/tr , _`
1347c478bd9Sstevel@tonic-gate	if [ $plm != $pl ]; then
1357c478bd9Sstevel@tonic-gate		plmn="var_svc_profile_platform_${plm}_xml"
1367c478bd9Sstevel@tonic-gate		plmh=`/usr/bin/svcprop -p ${plmn}/md5sum smf/manifest \
1377c478bd9Sstevel@tonic-gate		    2>/dev/null`
1387c478bd9Sstevel@tonic-gate		[ $? = 0 ] || plmh=""
1399444c26fSTom Whitten		plmn="etc_svc_profile_platform_${plm}_xml"
1407c478bd9Sstevel@tonic-gate	else
1417c478bd9Sstevel@tonic-gate		plmh=""
1427c478bd9Sstevel@tonic-gate	fi
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate	[ -n "$gh" ] && {
1457c478bd9Sstevel@tonic-gate		echo "Preserving generic hash ($gh)."
1467c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest addpg ${gn} framework
1477c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest setprop ${gn}/md5sum = \
1487c478bd9Sstevel@tonic-gate		    opaque: $gh
1499444c26fSTom Whitten		/usr/sbin/svccfg -s smf/manifest setprop ${gn}/manifestfile = \
1509444c26fSTom Whitten		    astring: "/etc/svc/profile/generic.xml"
1517c478bd9Sstevel@tonic-gate	}
1527c478bd9Sstevel@tonic-gate	[ -n "$glh" ] && {
1537c478bd9Sstevel@tonic-gate		echo "Preserving generic_limited hash ($glh)."
1547c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest addpg ${gln} framework
1557c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest setprop ${gln}/md5sum = \
1567c478bd9Sstevel@tonic-gate		    opaque: $glh
1579444c26fSTom Whitten		/usr/sbin/svccfg -s smf/manifest setprop ${gln}/manifestfile = \
1589444c26fSTom Whitten		    astring: "/etc/svc/profile/generic.xml"
1597c478bd9Sstevel@tonic-gate	}
1607c478bd9Sstevel@tonic-gate	[ -n "$plh" ] && {
1617c478bd9Sstevel@tonic-gate		echo "Preserving platform hash ($plh)."
1627c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest addpg $pln framework
1637c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest setprop $pln/md5sum = \
1647c478bd9Sstevel@tonic-gate		    opaque: $plh
1659444c26fSTom Whitten		/usr/sbin/svccfg -s smf/manifest setprop ${pln}/manifestfile = \
1669444c26fSTom Whitten		    astring: "/etc/svc/profile/platform_${pl}_xml"
1677c478bd9Sstevel@tonic-gate	}
1687c478bd9Sstevel@tonic-gate	[ -n "$plmh" ] && {
1697c478bd9Sstevel@tonic-gate		echo "Preserving platform hash ($plmh)."
1707c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest addpg $plmn framework
1717c478bd9Sstevel@tonic-gate		/usr/sbin/svccfg -s smf/manifest setprop $plmn/md5sum = \
1727c478bd9Sstevel@tonic-gate		    opaque: $plmh
1739444c26fSTom Whitten		/usr/sbin/svccfg -s smf/manifest setprop \
1749444c26fSTom Whitten		    ${plmn}/manifestfile = \
1759444c26fSTom Whitten		    astring: "/etc/svc/profile/platform_${plm}_xml"
1767c478bd9Sstevel@tonic-gate	}
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate	#
1799444c26fSTom Whitten	# Move symlinks from /var/svc/profile to /etc/svc/profile
1807c478bd9Sstevel@tonic-gate	#
1819444c26fSTom Whitten	generic_prof="/var/svc/profile/generic.xml"
1829444c26fSTom Whitten	ns_prof="/var/svc/profile/name_service.xml"
1839444c26fSTom Whitten	inetd_prof="/var/svc/profile/inetd_services.xml"
184adfc3118STruong Nguyen	platform_prof="/var/svc/profile/platform.xml"
1859444c26fSTom Whitten	[ -L "$generic_prof" ] && mv $generic_prof /etc/svc/profile/
1869444c26fSTom Whitten	[ -L "$ns_prof" ] && mv $ns_prof /etc/svc/profile/
1879444c26fSTom Whitten	[ -L "$inetd_prof" ] && mv $inetd_prof /etc/svc/profile/
188adfc3118STruong Nguyen	[ -L "$platform_prof" ] && mv $platform_prof /etc/svc/profile/
1899444c26fSTom Whitten
1909444c26fSTom Whitten	return 0
1919444c26fSTom Whitten}
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate#
1947c478bd9Sstevel@tonic-gate# 2.  Manifest import.  Application directories first, then
1957c478bd9Sstevel@tonic-gate# site-specific manifests.
1967c478bd9Sstevel@tonic-gate#
1979444c26fSTom Whittenfunction import_manifests {
1989444c26fSTom Whitten	typeset basedir=$1
199293e3ab3STruong Q. Nguyen	typeset console_print=$2
2009444c26fSTom Whitten	typeset logf="/etc/svc/volatile/manifest_import.$$"
2017c478bd9Sstevel@tonic-gate
2029444c26fSTom Whitten	rm -f $logf
2037c478bd9Sstevel@tonic-gate
204293e3ab3STruong Q. Nguyen	nonsite_dirs=`/usr/bin/find $basedir/* -name site \
2059444c26fSTom Whitten	    -prune -o -type d -print -prune`
2067c478bd9Sstevel@tonic-gate
2079444c26fSTom Whitten	if [ -n "$_MFST_DEBUG" ]; then
2089444c26fSTom Whitten		nonsite_manifests=`/lib/svc/bin/mfstscan $nonsite_dirs`
209293e3ab3STruong Q. Nguyen		site_manifests=`/lib/svc/bin/mfstscan $basedir/site`
2107c478bd9Sstevel@tonic-gate
2119444c26fSTom Whitten		manifests="$nonsite_manifests $site_manifests"
2127c478bd9Sstevel@tonic-gate
2139444c26fSTom Whitten		echo "Changed manifests to import:"
2149444c26fSTom Whitten		for m in $manifests; do echo "  $m"; done
2159444c26fSTom Whitten	fi
2167c478bd9Sstevel@tonic-gate
217c0889d7aSstevep	#
218293e3ab3STruong Q. Nguyen	# Upon boot, attempt to move the repository to tmpfs.
219c0889d7aSstevep	#
220293e3ab3STruong Q. Nguyen	if [ -z "$ALT_REPOSITORY" -a -z "$ALT_MFST_DIR" ]; then
221293e3ab3STruong Q. Nguyen		/usr/sbin/svcadm _smf_repository_switch fast
222293e3ab3STruong Q. Nguyen	fi
223c0889d7aSstevep
224c0889d7aSstevep	#
2259444c26fSTom Whitten	# Import the manifests while giving a running display of imports on
2269444c26fSTom Whitten	# console, and a final count in the logfile.
227c0889d7aSstevep	#
228293e3ab3STruong Q. Nguyen	dirs="$nonsite_dirs"
229293e3ab3STruong Q. Nguyen	[ -d "$basedir/site" ] && dirs="$dirs $basedir/site"
230293e3ab3STruong Q. Nguyen
231293e3ab3STruong Q. Nguyen	if [ "$console_print" = "true" ]; then
232293e3ab3STruong Q. Nguyen		$X /usr/sbin/svccfg import -p /dev/msglog $dirs > $logf 2>&1
233293e3ab3STruong Q. Nguyen	else
234293e3ab3STruong Q. Nguyen		$X /usr/sbin/svccfg import $dirs > $logf 2>&1
235293e3ab3STruong Q. Nguyen	fi
236c0889d7aSstevep
237*bbf21555SRichard Lowe	grep "Loaded .*. smf(7) service descriptions" $logf > /dev/null 2>&1
2389444c26fSTom Whitten	if [ $? -eq 0 ]; then
2399444c26fSTom Whitten		activity=true
2409444c26fSTom Whitten	fi
2417c478bd9Sstevel@tonic-gate
2429444c26fSTom Whitten	if [ -s $logf ]; then
243*bbf21555SRichard Lowe		grep "smf(7) service descriptions failed to load" $logf > /dev/null 2>&1
2449444c26fSTom Whitten		failures=$?
2459444c26fSTom Whitten		if [ $failures -eq 0 ]; then
2469444c26fSTom Whitten			echo "svccfg warnings:"
2479444c26fSTom Whitten		fi
2489444c26fSTom Whitten		cat $logf
2497c478bd9Sstevel@tonic-gate
250293e3ab3STruong Q. Nguyen		if [ $failures -eq 0 -a "$console_print" = "true" ]; then
2519444c26fSTom Whitten			msg="svccfg import warnings.  See"
2529444c26fSTom Whitten			msg="$msg /var/svc/log/system-manifest-import:default.log ."
2539444c26fSTom Whitten			echo $msg > /dev/msglog
2549444c26fSTom Whitten		fi
2557c478bd9Sstevel@tonic-gate	fi
2569444c26fSTom Whitten	rm -f $logf
2579444c26fSTom Whitten}
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate#
2607c478bd9Sstevel@tonic-gate# 3.  Profile application.  We must create the platform profile upon
2617c478bd9Sstevel@tonic-gate# first boot, as we may be a diskless client of a platform or
2627c478bd9Sstevel@tonic-gate# architecture distinct from our NFS server.
2637c478bd9Sstevel@tonic-gate#
2649444c26fSTom Whitten# Generic and platform profiles are only supported in /etc.
2659444c26fSTom Whitten#
2669444c26fSTom Whittenfunction apply_profile {
2679444c26fSTom Whitten	#
2689444c26fSTom Whitten	# If smf/manifest doesn't have any profile under /etc/var/profile,
2699444c26fSTom Whitten	# this is very likely an import after upgrade so call
2709444c26fSTom Whitten	# preserve_system_profiles in that case.
2719444c26fSTom Whitten	#
2729444c26fSTom Whitten	LC_ALL=C pl=`/usr/bin/uname -i | /usr/bin/tr , _`
2739444c26fSTom Whitten	pln="etc_svc_profile_platform_${pl}_xml"
2747c478bd9Sstevel@tonic-gate
2759444c26fSTom Whitten	LC_ALL=C plm=`/usr/bin/uname -m | /usr/bin/tr , _`
2769444c26fSTom Whitten	[ $plm != $pl ] && plmn="etc_svc_profile_platform_${plm}_xml"
2779444c26fSTom Whitten
2789444c26fSTom Whitten	preserve_profiles=1
2799444c26fSTom Whitten	for prof in $pln $plmn etc_svc_profile_platform_none_xml \
2809444c26fSTom Whitten	    etc_svc_profile_generic_limited_net_xml \
2819444c26fSTom Whitten	    etc_svc_profile_generic_open_xml; do
2829444c26fSTom Whitten		if /usr/bin/svcprop -p $prof smf/manifest >/dev/null 2>&1
2839444c26fSTom Whitten		then
2849444c26fSTom Whitten			preserve_profiles=0
2859444c26fSTom Whitten			break
2869444c26fSTom Whitten		fi
2879444c26fSTom Whitten	done
2887c478bd9Sstevel@tonic-gate
2899444c26fSTom Whitten	if [ $preserve_profiles -eq 1 ]; then
2909444c26fSTom Whitten		echo "/etc/svc system profiles not found: upgrade system profiles"
2919444c26fSTom Whitten		preserve_system_profiles || return
2927c478bd9Sstevel@tonic-gate	fi
2937c478bd9Sstevel@tonic-gate
2949444c26fSTom Whitten	typeset prefix="/etc/svc/profile"
2959444c26fSTom Whitten	svccfg_apply $prefix/generic.xml
2969444c26fSTom Whitten	if [ ! -f $prefix/platform.xml ]; then
2979444c26fSTom Whitten		this_karch=`uname -m`
2989444c26fSTom Whitten		this_plat=`uname -i`
2999444c26fSTom Whitten
3009444c26fSTom Whitten		if [ -f $prefix/platform_$this_plat.xml ]; then
3019444c26fSTom Whitten			platform_profile=platform_$this_plat.xml
3029444c26fSTom Whitten		elif [ -f $prefix/platform_$this_karch.xml ]; then
3039444c26fSTom Whitten			platform_profile=platform_$this_karch.xml
3049444c26fSTom Whitten		else
3059444c26fSTom Whitten			platform_profile=platform_none.xml
3069444c26fSTom Whitten		fi
3077c478bd9Sstevel@tonic-gate
3089444c26fSTom Whitten		ln -s $platform_profile $prefix/platform.xml
3099444c26fSTom Whitten	fi
3109444c26fSTom Whitten
3119444c26fSTom Whitten	svccfg_apply $prefix/platform.xml
3129444c26fSTom Whitten}
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate#
3157c478bd9Sstevel@tonic-gate# 4.  Upgrade handling.  The upgrade file generally consists of a series
316*bbf21555SRichard Lowe# of svcadm(8) and svccfg(8) commands.
3177c478bd9Sstevel@tonic-gate#
3189444c26fSTom Whittenfunction handle_upgrade {
3197c478bd9Sstevel@tonic-gate
3209444c26fSTom Whitten	[ -f /var/svc/profile/upgrade ] && activity=true
3217c478bd9Sstevel@tonic-gate
3229444c26fSTom Whitten	(
3239444c26fSTom Whitten		unset SVCCFG_CHECKHASH
3249444c26fSTom Whitten
3259444c26fSTom Whitten		if [ -f /var/svc/profile/upgrade ]; then
3269444c26fSTom Whitten			. /var/svc/profile/upgrade
3279444c26fSTom Whitten
3289444c26fSTom Whitten			/usr/bin/mv /var/svc/profile/upgrade \
3299444c26fSTom Whitten			    /var/svc/profile/upgrade.app.`date +\%Y\%m\%d\%H\%M\%S`
3309444c26fSTom Whitten		fi
3319444c26fSTom Whitten
3329444c26fSTom Whitten		#
3339444c26fSTom Whitten		# Rename the datalink upgrade script file. This script is used in the
3349444c26fSTom Whitten		# network/physical service to upgrade datalink configuration, but
3359444c26fSTom Whitten		# the file cannot be renamed until now (when the file system becomes
3369444c26fSTom Whitten		# read-write).
3379444c26fSTom Whitten		#
3389444c26fSTom Whitten		datalink_script=/var/svc/profile/upgrade_datalink
3399444c26fSTom Whitten		if [ -f "${datalink_script}" ]; then
3409444c26fSTom Whitten			/usr/bin/mv "${datalink_script}" \
3419444c26fSTom Whitten			    "${datalink_script}".app.`date +\%Y\%m\%d\%H\%M\%S`
3429444c26fSTom Whitten		fi
3439444c26fSTom Whitten	)
3449444c26fSTom Whitten}
3459444c26fSTom Whitten
3469444c26fSTom Whitten#
347adfc3118STruong Nguyen# 5.  Giving administrator the final say, apply site.xml profile and profiles
348adfc3118STruong Nguyen#     under /etc/svc/profile/site directory.
3499444c26fSTom Whitten#
3509444c26fSTom Whittenfunction apply_site_profile {
351293e3ab3STruong Q. Nguyen        typeset prefix="$1"
3529444c26fSTom Whitten	[ -f $prefix/site.xml ] && svccfg_apply $prefix/site.xml
353adfc3118STruong Nguyen
354293e3ab3STruong Q. Nguyen	if [ -d $PROFILE_DIR_SITE -a "$1" = "/etc/svc/profile" ]; then
355adfc3118STruong Nguyen		svccfg_apply $PROFILE_DIR_SITE
356adfc3118STruong Nguyen	fi
3579444c26fSTom Whitten}
3589444c26fSTom Whitten
3599444c26fSTom Whitten#
3609444c26fSTom Whitten# 0b Cleanup deathrow
3619444c26fSTom Whitten#
3629444c26fSTom Whittenif [ "$early" = "false" ];then
3639444c26fSTom Whitten	deathrow=/etc/svc/deathrow
3649444c26fSTom Whitten	if [ -s $deathrow ];then
3659444c26fSTom Whitten		#
3669444c26fSTom Whitten		# svc.startd has unconfigured the services found in deathrow,
3679444c26fSTom Whitten		# clean them now.
3689444c26fSTom Whitten		#
3699444c26fSTom Whitten		while read fmri mfst pkgname; do
3709444c26fSTom Whitten			# Delete services and instances from the deathrow file.
3719444c26fSTom Whitten			/usr/sbin/svccfg delete -f $fmri >/dev/null 2>&1
3729444c26fSTom Whitten			# Remove deathrow manifest hash.
3739444c26fSTom Whitten			/usr/sbin/svccfg delhash -d $mfst >/dev/null 2>&1
3749444c26fSTom Whitten		done < $deathrow
3759444c26fSTom Whitten		/usr/bin/mv $deathrow $deathrow.old
3767c478bd9Sstevel@tonic-gate	fi
3779444c26fSTom Whittenfi
378d62bc4baSyz
3799444c26fSTom WhittenSVCCFG_CHECKHASH=1 export SVCCFG_CHECKHASH
3809444c26fSTom Whitten
3819444c26fSTom Whitten#
3829444c26fSTom Whitten# 0c Clean up repository
3839444c26fSTom Whitten#
3849444c26fSTom Whittenif [ "$early" = "false" ]; then
3859444c26fSTom Whitten	if [ -z "$X" ] && /usr/bin/svcprop smf/manifest 2>/dev/null |
3869444c26fSTom Whitten	    /usr/bin/grep '^ar_svc_[^/]*/md5sum opaque ' >/dev/null
3879444c26fSTom Whitten	then
3889444c26fSTom Whitten		set -- `
3899444c26fSTom Whitten			/usr/bin/svcprop smf/manifest 2>/dev/null |
3909444c26fSTom Whitten			    /usr/bin/grep '^ar_svc[^/]*/md5sum opaque ' |
3919444c26fSTom Whitten			    /usr/bin/tr '/' ' ' |
3929444c26fSTom Whitten			    while read pg prop type value; do
3939444c26fSTom Whitten				echo "$pg/$value"
3949444c26fSTom Whitten			done
3959444c26fSTom Whitten		`
3969444c26fSTom Whitten		backup=`echo "$#/$#" | sed 's/.//g'`
3979444c26fSTom Whitten		fwidth=`echo "$#\c" | wc -c`
3989444c26fSTom Whitten
3999444c26fSTom Whitten		echo "Converting obsolete repository entries: \c" > /dev/msglog
4009444c26fSTom Whitten		i=1; n=$#
4019444c26fSTom Whitten		while [ $# -gt 0 ]; do
4029444c26fSTom Whitten			printf "%${fwidth}s/%${fwidth}s" $i $n > /dev/msglog
4039444c26fSTom Whitten			echo $1 | sed 's:/: :' | (
4049444c26fSTom Whitten				read pg value
4059444c26fSTom Whitten
4069444c26fSTom Whitten				(echo "select /smf/manifest"; echo "delpg v$pg") |
4079444c26fSTom Whitten				    /usr/sbin/svccfg 2>/dev/null >/dev/null
4089444c26fSTom Whitten				(echo "select /smf/manifest"; echo "delpg $pg") |
4099444c26fSTom Whitten				    /usr/sbin/svccfg 2>/dev/null >/dev/null
4109444c26fSTom Whitten				(echo "select /smf/manifest";
4119444c26fSTom Whitten				    echo "addpg v$pg framework") |
4129444c26fSTom Whitten				    /usr/sbin/svccfg 2>/dev/null >/dev/null
4139444c26fSTom Whitten				(echo "select /smf/manifest";
4149444c26fSTom Whitten				    echo "setprop v$pg/md5sum = opaque: $value") |
4159444c26fSTom Whitten				    /usr/sbin/svccfg 2>/dev/null >/dev/null
4169444c26fSTom Whitten			)
4179444c26fSTom Whitten			i=`expr $i + 1`
4189444c26fSTom Whitten			shift
4199444c26fSTom Whitten			echo "$backup\c" > /dev/msglog
4209444c26fSTom Whitten		done
4219444c26fSTom Whitten		echo > /dev/msglog
4229444c26fSTom Whitten		echo "Converted $n obsolete repository entries"
4239444c26fSTom Whitten		activity=true
424d62bc4baSyz	fi
4259444c26fSTom Whitten
4269444c26fSTom Whittenfi
4277c478bd9Sstevel@tonic-gate
428293e3ab3STruong Q. Nguyen#
429293e3ab3STruong Q. Nguyen# If the alternate repository and directory are specified, simply set
430293e3ab3STruong Q. Nguyen# SVCCFG_REPOSITORY env, run svccfg import on the given directory, and
431293e3ab3STruong Q. Nguyen# exit.
432293e3ab3STruong Q. Nguyen#
433293e3ab3STruong Q. Nguyenif [ -n "$ALT_REPOSITORY" -a -n "$ALT_MFST_DIR" ]; then
434293e3ab3STruong Q. Nguyen	SVCCFG_REPOSITORY=$ALT_REPOSITORY export SVCCFG_REPOSITORY
435293e3ab3STruong Q. Nguyen	import_manifests "$ALT_MFST_DIR" false
436293e3ab3STruong Q. Nguyen	unset SVCCFG_REPOSITORY
437293e3ab3STruong Q. Nguyen	exit 0
438293e3ab3STruong Q. Nguyenfi
439293e3ab3STruong Q. Nguyen
4407c478bd9Sstevel@tonic-gate#
4419444c26fSTom Whitten# Call import and apply profiles here
4427c478bd9Sstevel@tonic-gate#
4439444c26fSTom Whittenif [ "$early" = "true" ]; then
444293e3ab3STruong Q. Nguyen	import_manifests "/lib/svc/manifest" true
4459444c26fSTom Whitten	apply_profile
446293e3ab3STruong Q. Nguyen	apply_site_profile "/etc/svc/profile"
4479444c26fSTom Whittenelse
4489444c26fSTom Whitten	#
449293e3ab3STruong Q. Nguyen	# Process both /lib/svc/manifest and /var/svc/manifest
4509444c26fSTom Whitten	# during late manifest-import
4519444c26fSTom Whitten	#
4529444c26fSTom Whitten	# First import the manifests
4539444c26fSTom Whitten	#
454293e3ab3STruong Q. Nguyen	import_manifests "/lib/svc/manifest" true
455293e3ab3STruong Q. Nguyen	import_manifests "/var/svc/manifest" true
4569444c26fSTom Whitten
4579444c26fSTom Whitten	#
4589444c26fSTom Whitten	# Apply profiles
4599444c26fSTom Whitten	#
4609444c26fSTom Whitten	apply_profile
461293e3ab3STruong Q. Nguyen	apply_site_profile "/etc/svc/profile"
4629444c26fSTom Whitten
4639444c26fSTom Whitten	#
4649444c26fSTom Whitten	# Run the upgrade script
4659444c26fSTom Whitten	#
4669444c26fSTom Whitten	handle_upgrade
467293e3ab3STruong Q. Nguyen	apply_site_profile "/var/svc/profile"
4687c478bd9Sstevel@tonic-gatefi
4697c478bd9Sstevel@tonic-gate
4709444c26fSTom Whitten
4717c478bd9Sstevel@tonic-gate#
4727c478bd9Sstevel@tonic-gate# 6.  Final actions.
4737c478bd9Sstevel@tonic-gate#
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gateif $activity; then
4769444c26fSTom Whitten	/usr/sbin/svcadm _smf_backup "manifest_import" || true
4777c478bd9Sstevel@tonic-gatefi
4787c478bd9Sstevel@tonic-gate
4799444c26fSTom Whitten#
4809444c26fSTom Whitten# If the filesystem is NOT read only then move the repo back to perm
4819444c26fSTom Whitten# There is no care wether the switch was made or not, but just want
4829444c26fSTom Whitten# to move it.  If it is already perm this does not affect anything
4839444c26fSTom Whitten# at least on the surface.  REALLY want to improve on this...
4849444c26fSTom Whitten#
4859444c26fSTom Whittentouch /etc/svc/smf_rwtest.$$ > /dev/null 2>&1
4869444c26fSTom Whittenif [ $? -eq 0 ]; then
4879444c26fSTom Whitten	rm -f /etc/svc/smf_rwtest.$$
4889444c26fSTom Whitten	/usr/sbin/svcadm _smf_repository_switch perm || { \
4899444c26fSTom Whitten	    echo "Repository switch back operation failed, \c"
4909444c26fSTom Whitten	    echo "please check the system log for the"
4919444c26fSTom Whitten	    echo "possible fatal error messages."
4929444c26fSTom Whitten	    exit $SMF_EXIT_ERR_FATAL
4939444c26fSTom Whitten	    }
4949444c26fSTom Whittenfi
4959444c26fSTom Whitten
4969444c26fSTom Whittenif $activity; then
4979444c26fSTom Whitten	/usr/sbin/svccfg cleanup | /usr/bin/tee /dev/msglog
4989444c26fSTom Whittenelse
4999444c26fSTom Whitten	cleanup_needwork
5009444c26fSTom Whitten	if [ $? -ne 0 ]; then
5019444c26fSTom Whitten		/usr/sbin/svccfg cleanup -a | /usr/bin/tee /dev/msglog
5029444c26fSTom Whitten	fi
5039444c26fSTom Whittenfi
50423294c7dSSean Wilcox
5057c478bd9Sstevel@tonic-gateexit 0
506