xref: /illumos-gate/usr/src/cmd/power/svc-power (revision e7cbe64f)
17c478bd9Sstevel@tonic-gate#!/sbin/sh
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
65ad742a0Smh# Common Development and Distribution License (the "License").
75ad742a0Smh# 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#
227c478bd9Sstevel@tonic-gate#
235ad742a0Smh# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate# If the /etc/power.conf file does not have a "statefile" entry
297c478bd9Sstevel@tonic-gate# to specify the pathname of the cpr statefile, build one and
307c478bd9Sstevel@tonic-gate# add the line.  We choose the largest of the standard Sun partitions.
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gateinit_statefile_entry() {
337c478bd9Sstevel@tonic-gate	[ ! -f /etc/power.conf -o ! -w /etc/power.conf ] && return
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate	# Whitespace regular expression below is [<TAB><SPACE>]
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate	pattern="^[ 	]*statefile[	 ][	 ]*/"
387c478bd9Sstevel@tonic-gate	[ `/usr/bin/grep -c "$pattern" /etc/power.conf` -ge 1 ] && return
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate	avail=0			# Free blocks in current filesystem
417c478bd9Sstevel@tonic-gate	max_avail=0		# Most available free blocks encountered so far
427c478bd9Sstevel@tonic-gate 	statefile=.CPR		# Default cpr statefile name
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate	# Remove old statefile (if any) from root
457c478bd9Sstevel@tonic-gate	[ -f /$statefile ] && /usr/bin/rm -f /$statefile
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate	/usr/bin/df -k -F ufs |
487c478bd9Sstevel@tonic-gate	(
497c478bd9Sstevel@tonic-gate		read line	# Skip past the header line of the df output
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate		while read device kbytes used avail capacity filesys; do
527c478bd9Sstevel@tonic-gate			case $filesys in
537c478bd9Sstevel@tonic-gate			/|/usr|/var|/export/home)
547c478bd9Sstevel@tonic-gate				if [ $avail -gt $max_avail ]; then
557c478bd9Sstevel@tonic-gate					max_avail=$avail
567c478bd9Sstevel@tonic-gate					winner=$filesys
577c478bd9Sstevel@tonic-gate				fi
587c478bd9Sstevel@tonic-gate				;;
597c478bd9Sstevel@tonic-gate			esac
607c478bd9Sstevel@tonic-gate		done
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate		if [ $max_avail -gt 0 ]; then
637c478bd9Sstevel@tonic-gate			echo "statefile		${winner}/${statefile}" \
647c478bd9Sstevel@tonic-gate			    >>/etc/power.conf
657c478bd9Sstevel@tonic-gate		fi
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate		return
687c478bd9Sstevel@tonic-gate	)
69*e7cbe64fSgw	if [ $max_avail -eq 0 ]; then
70*e7cbe64fSgw		if [ X`df -n / | awk '{print $3}'` != "Xzfs" ] ; then
71*e7cbe64fSgw			return
72*e7cbe64fSgw		fi
73*e7cbe64fSgw		rootpool=`zfs mount | grep ' \/$' | awk '{print $1 }' |\
74*e7cbe64fSgw		    sed 's/\/.*$//'`
75*e7cbe64fSgw		if [ X$rootpool = "X" ] || \
76*e7cbe64fSgw		    [ ! -L /dev/zvol/dsk/$rootpool/dump ]; then
77*e7cbe64fSgw			return
78*e7cbe64fSgw		fi
79*e7cbe64fSgw		echo "statefile /dev/zvol/dsk/$rootpool/dump" \
80*e7cbe64fSgw			>> /etc/power.conf
81*e7cbe64fSgw	fi
827c478bd9Sstevel@tonic-gate}
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gatecase "$1" in
857c478bd9Sstevel@tonic-gate'start')
865ad742a0Smh	/usr/sbin/uadmin 3 2 2>/dev/null
875ad742a0Smh	if [ $? -eq 0 ]; then
887c478bd9Sstevel@tonic-gate		init_statefile_entry
897c478bd9Sstevel@tonic-gate	fi
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate	if [ -x /usr/sbin/pmconfig -a -r /etc/power.conf ]; then
927c478bd9Sstevel@tonic-gate		/usr/sbin/pmconfig >/dev/console 2>&1
937c478bd9Sstevel@tonic-gate	fi
947c478bd9Sstevel@tonic-gate	;;
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate'stop')
977c478bd9Sstevel@tonic-gate	if [ -x /usr/sbin/pmconfig ]; then
987c478bd9Sstevel@tonic-gate		/usr/sbin/pmconfig -r >/dev/null 2>/dev/null
997c478bd9Sstevel@tonic-gate	fi
1007c478bd9Sstevel@tonic-gate	;;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate*)
1037c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
1047c478bd9Sstevel@tonic-gate	exit 1
1057c478bd9Sstevel@tonic-gate	;;
1067c478bd9Sstevel@tonic-gateesac
1077c478bd9Sstevel@tonic-gateexit 0
108