xref: /illumos-gate/usr/src/cmd/power/svc-power (revision 5ad742a005935ecb1bc6a7883595a83e26f56282)
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
6*5ad742a0Smh27603# Common Development and Distribution License (the "License").
7*5ad742a0Smh27603# 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#
23*5ad742a0Smh27603# 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	)
697c478bd9Sstevel@tonic-gate}
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gatecase "$1" in
727c478bd9Sstevel@tonic-gate'start')
73*5ad742a0Smh27603	/usr/sbin/uadmin 3 2 2>/dev/null
74*5ad742a0Smh27603	if [ $? -eq 0 ]; then
757c478bd9Sstevel@tonic-gate		init_statefile_entry
767c478bd9Sstevel@tonic-gate	fi
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate	if [ -x /usr/sbin/pmconfig -a -r /etc/power.conf ]; then
797c478bd9Sstevel@tonic-gate		/usr/sbin/pmconfig >/dev/console 2>&1
807c478bd9Sstevel@tonic-gate	fi
817c478bd9Sstevel@tonic-gate	;;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate'stop')
847c478bd9Sstevel@tonic-gate	if [ -x /usr/sbin/pmconfig ]; then
857c478bd9Sstevel@tonic-gate		/usr/sbin/pmconfig -r >/dev/null 2>/dev/null
867c478bd9Sstevel@tonic-gate	fi
877c478bd9Sstevel@tonic-gate	;;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate*)
907c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
917c478bd9Sstevel@tonic-gate	exit 1
927c478bd9Sstevel@tonic-gate	;;
937c478bd9Sstevel@tonic-gateesac
947c478bd9Sstevel@tonic-gateexit 0
95