xref: /onnv-gate/usr/src/cmd/power/sysidpm.sh (revision 5677:48c350661711)
10Sstevel@tonic-gate#!/bin/sh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*5677Sbick# Common Development and Distribution License (the "License").
7*5677Sbick# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#
23*5677Sbick# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate# Use is subject to license terms.
250Sstevel@tonic-gate#
26*5677Sbick# ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate# The -c and -u options are used by system configruation.
290Sstevel@tonic-gateUSAGE="$0 [-c|-u]"
300Sstevel@tonic-gateTEXTDOMAIN="SUNW_OST_OSCMD"
310Sstevel@tonic-gateexport TEXTDOMAIN
320Sstevel@tonic-gate
330Sstevel@tonic-gate# As of Oct. 1, 1995, any new system shipped will have root
340Sstevel@tonic-gate# property "energystar-v2" defined in its prom.
350Sstevel@tonic-gateESTAR_PROP="energystar-v2"
360Sstevel@tonic-gate
370Sstevel@tonic-gate# Power Management configuration file
380Sstevel@tonic-gatePWR_CONF=/etc/power.conf
390Sstevel@tonic-gateSHUTDOWN_PATTERN="autoshutdown[ 	]"
400Sstevel@tonic-gateTMP=/var/run/tmp1.$$
410Sstevel@tonic-gate
420Sstevel@tonic-gate# If this flag is present, user will be asked the autoshutdown
430Sstevel@tonic-gate# question again even when autoshutdown is already configured.
440Sstevel@tonic-gateASK_AGAIN_FLAG=/etc/.PM_RECONFIGURE
450Sstevel@tonic-gate
460Sstevel@tonic-gate# This is provided for auto-install.
470Sstevel@tonic-gate# If either of the files is present, autoshutdown will be configured
480Sstevel@tonic-gate# accordingly silently.
490Sstevel@tonic-gateSHUTDOWN_ENABLE_FLAG=/autoshutdown
500Sstevel@tonic-gateSHUTDOWN_DISABLE_FLAG=/noautoshutdown
510Sstevel@tonic-gate
520Sstevel@tonic-gate# Autoshutdown is not supported on diskless systems.
530Sstevel@tonic-gateIS_DISKLESS=""
540Sstevel@tonic-gate
550Sstevel@tonic-gate# Default autoshutdown setup.
560Sstevel@tonic-gateDEFAULT_IDLE_TIME="30"
570Sstevel@tonic-gateDEFAULT_START_TIME="9:00"
580Sstevel@tonic-gateDEFAULT_FINISH_TIME="9:00"
590Sstevel@tonic-gate
600Sstevel@tonic-gate# Currently autoshutdown setup in the configuration file.
610Sstevel@tonic-gateCURRENT_IDLE_TIME=""
620Sstevel@tonic-gateCURRENT_START_TIME=""
630Sstevel@tonic-gateCURRENT_FINISH_TIME=""
640Sstevel@tonic-gateCURRENT_BEHAVIOR=""
650Sstevel@tonic-gate
660Sstevel@tonic-gate# Autoshutdown confirmation message to be prompted in the question1.
670Sstevel@tonic-gate# In the following message, each escape sequence requires three
680Sstevel@tonic-gate# backslashes to prevent the interpretation by the shell and gettext
690Sstevel@tonic-gate# before being passed to ckyorn.  In the message catalog, each message
700Sstevel@tonic-gate# will have two backslashes.
710Sstevel@tonic-gateMSG1=`gettext '\\\tAfter 30 minutes of idle time on this system, your system \
720Sstevel@tonic-gatestate\\\n\\\twill automatically be saved to disk, and the system will \
730Sstevel@tonic-gatepower-off.\\\n\\\n\\\tLater, when you want to use the system again, and \
740Sstevel@tonic-gateyou turn the power\\\n\\\tback on, your system will be restored to its \
750Sstevel@tonic-gateprevious state,\\\n\\\tincluding all the programs that you were running.\
760Sstevel@tonic-gate\\\n\\\n\\\tDo you want this automatic power-saving shutdown?\\\n\
770Sstevel@tonic-gate\\\t(If this system is used as a server, answer n)'`
780Sstevel@tonic-gate
790Sstevel@tonic-gate# Message to be prompted in question2.
800Sstevel@tonic-gate# In the following message, each escape sequence requires three
810Sstevel@tonic-gate# backslashes to prevent the interpretation by the shell and gettext
820Sstevel@tonic-gate# before being passed to ckyorn.  In the message catalog, each message
830Sstevel@tonic-gate# will have two backslashes.
840Sstevel@tonic-gateMSG2=`gettext '\\\tDo you want the system to ask about this again, when \
850Sstevel@tonic-gateyou next reboot?\\\n\\\t(This gives you the chance to try it before deciding \
860Sstevel@tonic-gatewhether\\\n\\\tto keep it.)'`
870Sstevel@tonic-gate
880Sstevel@tonic-gate# The autoshutdown comment to be put into the power management config file.
890Sstevel@tonic-gateSHUTDOWN_COMMENT="# Auto-Shutdown\t\tIdle(min)\tStart/Finish(hh:mm)\tBehavior"
900Sstevel@tonic-gate
910Sstevel@tonic-gate# Set up path.
920Sstevel@tonic-gatePATH="/usr/bin:/usr/sbin:${PATH}"
930Sstevel@tonic-gateexport PATH
940Sstevel@tonic-gate
950Sstevel@tonic-gate#
960Sstevel@tonic-gate# Get current autoshutdown setup.
970Sstevel@tonic-gate#
980Sstevel@tonic-gateget_behavior() {
990Sstevel@tonic-gate	grep -s "$SHUTDOWN_PATTERN" $PWR_CONF > /dev/null
1000Sstevel@tonic-gate	if [ $? = 0 ]; then
1010Sstevel@tonic-gate		set - `grep "$SHUTDOWN_PATTERN" $PWR_CONF`
1020Sstevel@tonic-gate		CURRENT_IDLE_TIME=$2
1030Sstevel@tonic-gate		CURRENT_START_TIME=$3
1040Sstevel@tonic-gate		CURRENT_FINISH_TIME=$4
1050Sstevel@tonic-gate		CURRENT_BEHAVIOR=$5
1060Sstevel@tonic-gate	fi
1070Sstevel@tonic-gate}
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate#
1100Sstevel@tonic-gate# Set the autoshutdown behavior in the configuration file.
1110Sstevel@tonic-gate# The autoshutdown token can be preceded by spaces.
1120Sstevel@tonic-gate# The resulting configuration will be based on the first autoshutdown
1130Sstevel@tonic-gate# line if there is more than one in the configuration file.
1140Sstevel@tonic-gate#
1150Sstevel@tonic-gateset_behavior() {
1160Sstevel@tonic-gate	BEHAVIOR="$1"
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate	grep -s "$SHUTDOWN_PATTERN" $PWR_CONF > /dev/null
1190Sstevel@tonic-gate	if [ $? = 0 ]; then
1200Sstevel@tonic-gate		set - `grep "$SHUTDOWN_PATTERN" $PWR_CONF`
1210Sstevel@tonic-gate		CURRENT_IDLE_TIME=$2
1220Sstevel@tonic-gate		CURRENT_START_TIME=$3
1230Sstevel@tonic-gate		CURRENT_FINISH_TIME=$4
1240Sstevel@tonic-gate		CURRENT_BEHAVIOR=$5
1250Sstevel@tonic-gate	fi
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate	if [ "$BEHAVIOR" = "unconfigured" ]; then
1280Sstevel@tonic-gate		IDLE=$DEFAULT_IDLE_TIME
1290Sstevel@tonic-gate		START=$DEFAULT_START_TIME
1300Sstevel@tonic-gate		FINISH=$DEFAULT_FINISH_TIME
1310Sstevel@tonic-gate	else {
1320Sstevel@tonic-gate		if [ "$CURRENT_IDLE_TIME" = "" ]; then
1330Sstevel@tonic-gate			IDLE="$DEFAULT_IDLE_TIME"
1340Sstevel@tonic-gate		else
1350Sstevel@tonic-gate			IDLE="$CURRENT_IDLE_TIME"
1360Sstevel@tonic-gate		fi
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate		if [ "$CURRENT_START_TIME" = "" ]; then
1390Sstevel@tonic-gate			START="$DEFAULT_START_TIME"
1400Sstevel@tonic-gate		else
1410Sstevel@tonic-gate			START="$CURRENT_START_TIME"
1420Sstevel@tonic-gate		fi
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate		if [ "$CURRENT_FINISH_TIME" = "" ]; then
1450Sstevel@tonic-gate			FINISH="$DEFAULT_FINISH_TIME"
1460Sstevel@tonic-gate		else
1470Sstevel@tonic-gate			FINISH="$CURRENT_FINISH_TIME"
1480Sstevel@tonic-gate		fi
1490Sstevel@tonic-gate	} fi
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate	grep -v "# Auto-Shutdown[	]" $PWR_CONF | grep -v "$SHUTDOWN_PATTERN" > $TMP
1520Sstevel@tonic-gate	echo $SHUTDOWN_COMMENT >> $TMP
1530Sstevel@tonic-gate	echo "autoshutdown\t\t${IDLE}\t\t${START} ${FINISH}\t\t${BEHAVIOR}" >> \
1540Sstevel@tonic-gate		$TMP
1550Sstevel@tonic-gate	cp $TMP $PWR_CONF
1560Sstevel@tonic-gate	rm $TMP
1570Sstevel@tonic-gate}
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate#
1600Sstevel@tonic-gate# Print out the Energystar guidelines.
1610Sstevel@tonic-gate#
1620Sstevel@tonic-gateprint_estar_guidelines() {
1630Sstevel@tonic-gate	echo
1640Sstevel@tonic-gate	echo "`gettext '\t================================================================'`"
1650Sstevel@tonic-gate	echo "`gettext '\tThis system is configured to conserve energy.'`"
1660Sstevel@tonic-gate        echo "`gettext '\t================================================================'`"
1670Sstevel@tonic-gate}
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate#
1700Sstevel@tonic-gate# Ask user for autoshutdown confirmation.
1710Sstevel@tonic-gate#
1720Sstevel@tonic-gatequestion1() {
1730Sstevel@tonic-gate        ans=`ckyorn -Q -d y -p "$1"`
1740Sstevel@tonic-gate	case $ans in
1750Sstevel@tonic-gate		y|yes|Y|Yes|YES)
1760Sstevel@tonic-gate			set_behavior shutdown
1770Sstevel@tonic-gate			echo
1780Sstevel@tonic-gate			echo "`gettext '\tAutoshutdown remains enabled.'`"
1790Sstevel@tonic-gate			break
1800Sstevel@tonic-gate			;;
1810Sstevel@tonic-gate		n|no|N|No|NO)
1820Sstevel@tonic-gate			set_behavior noshutdown
1830Sstevel@tonic-gate			echo
1840Sstevel@tonic-gate			echo "`gettext '\tAutoshutdown has been disabled.'`"
1850Sstevel@tonic-gate			break
1860Sstevel@tonic-gate			;;
1870Sstevel@tonic-gate	esac
1880Sstevel@tonic-gate}
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate#
1910Sstevel@tonic-gate# Ask user whether they want to be asked about the question again during
1920Sstevel@tonic-gate# next reboot.
1930Sstevel@tonic-gate#
1940Sstevel@tonic-gatequestion2() {
1950Sstevel@tonic-gate        ans=`ckyorn -Q -d n -p "$1"`
1960Sstevel@tonic-gate	case $ans in
1970Sstevel@tonic-gate		y|yes|Y|Yes|YES)
1980Sstevel@tonic-gate			touch $ASK_AGAIN_FLAG
1990Sstevel@tonic-gate			echo "`gettext '\n\tThe system will ask you about automatic shutdown at the next reboot.'`"
2000Sstevel@tonic-gate			break
2010Sstevel@tonic-gate			;;
2020Sstevel@tonic-gate		n|no|N|No|NO)
2030Sstevel@tonic-gate			rm -f $ASK_AGAIN_FLAG
2040Sstevel@tonic-gate			echo "`gettext '\n\tThe system will not ask you again about automatic shutdown.'`"
2050Sstevel@tonic-gate			break
2060Sstevel@tonic-gate			;;
2070Sstevel@tonic-gate	esac
2080Sstevel@tonic-gate}
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate################
2120Sstevel@tonic-gate#     Main     #
2130Sstevel@tonic-gate################
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate#
2160Sstevel@tonic-gate# Exit if /etc/power.conf does not exist or is not writable.
2170Sstevel@tonic-gate#
2180Sstevel@tonic-gateif [ ! -f $PWR_CONF -o ! -w $PWR_CONF ]; then
2190Sstevel@tonic-gate	exit 1
2200Sstevel@tonic-gatefi
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate#
2240Sstevel@tonic-gate# Usage: sysidpm [-c|-u]
2250Sstevel@tonic-gate#
2260Sstevel@tonic-gateif [ $# -gt 1 ]; then
2270Sstevel@tonic-gate	echo $USAGE
2280Sstevel@tonic-gate	exit 1
2290Sstevel@tonic-gatefi
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate#
2330Sstevel@tonic-gate# The postinstall script of some power management package should have
2340Sstevel@tonic-gate# added this command into the application list in /etc/.sysidconfig.apps.
2350Sstevel@tonic-gate# System configuration and unconfiguration will call those applications
2360Sstevel@tonic-gate# with option -c and -u respectively.
2370Sstevel@tonic-gate#
2380Sstevel@tonic-gateif [ $# -eq 1 ]; then
2390Sstevel@tonic-gate	case $1 in
2400Sstevel@tonic-gate		-c)	# Does not need to do anything.
2410Sstevel@tonic-gate			exit 0 ;;
2420Sstevel@tonic-gate		-u)
2430Sstevel@tonic-gate			# Reset the behavior back to unconfigured state.
2440Sstevel@tonic-gate			set_behavior unconfigured
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate			# Remove the statefile line too.
2470Sstevel@tonic-gate			grep -v statefile $PWR_CONF > $TMP
2480Sstevel@tonic-gate			cp $TMP $PWR_CONF
2490Sstevel@tonic-gate			rm $TMP
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate			exit 0 ;;
2520Sstevel@tonic-gate		*)
2530Sstevel@tonic-gate			echo $USAGE
2540Sstevel@tonic-gate			exit 1 ;;
2550Sstevel@tonic-gate	esac
2560Sstevel@tonic-gatefi
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate#
2600Sstevel@tonic-gate# Get current autoshutdown setup.
2610Sstevel@tonic-gate#
2620Sstevel@tonic-gateget_behavior
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate#
2650Sstevel@tonic-gate# If this is a diskless system, silently disable autoshutdown and exit.
2660Sstevel@tonic-gate#
2670Sstevel@tonic-gateROOT_FSTYPE=`df -n / | (read w1 w2 w3; echo $w3)`
2680Sstevel@tonic-gateif [ $ROOT_FSTYPE != "ufs" ]; then
2690Sstevel@tonic-gate	set_behavior noshutdown
2700Sstevel@tonic-gate	exit 0
2710Sstevel@tonic-gatefi
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate#
2750Sstevel@tonic-gate# If /autoshutdown is present, silently enable autoshutdown and exit.
2760Sstevel@tonic-gate#
2770Sstevel@tonic-gateif [ -f $SHUTDOWN_ENABLE_FLAG ]; then
2780Sstevel@tonic-gate	set_behavior shutdown
2790Sstevel@tonic-gate	rm $SHUTDOWN_ENABLE_FLAG
2800Sstevel@tonic-gate	exit 0
2810Sstevel@tonic-gatefi
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate#
2840Sstevel@tonic-gate# If /noautoshutdown is present, silently disable autoshutdown and
2850Sstevel@tonic-gate# exit.
2860Sstevel@tonic-gate#
2870Sstevel@tonic-gateif [ -f $SHUTDOWN_DISABLE_FLAG ]; then
2880Sstevel@tonic-gate	set_behavior noshutdown
2890Sstevel@tonic-gate	rm $SHUTDOWN_DISABLE_FLAG
2900Sstevel@tonic-gate	exit 0
2910Sstevel@tonic-gatefi
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate#
2950Sstevel@tonic-gate# If this is an EnergyStar compliant system, the default should
2960Sstevel@tonic-gate# have autoshutdown enabled. However we don't want to surprise
2970Sstevel@tonic-gate# users, so let's confirm with the user.
2980Sstevel@tonic-gate#
2990Sstevel@tonic-gateprtconf -vp | grep -s -w ${ESTAR_PROP} > /dev/null
3000Sstevel@tonic-gateif [ $? = 0 ]; then
3010Sstevel@tonic-gate	if [ "$CURRENT_BEHAVIOR" = "unconfigured" -o -f $ASK_AGAIN_FLAG ]; then
3020Sstevel@tonic-gate		print_estar_guidelines
3030Sstevel@tonic-gate		question1 "$MSG1"
3040Sstevel@tonic-gate		question2 "$MSG2"
305*5677Sbick		# Currently, we do not have documentation for changing and
306*5677Sbick		# setting workstation energy-saving features.  When we do,
307*5677Sbick		# gettext messages should be emitting here. see CR6520924
308*5677Sbick		#
3090Sstevel@tonic-gate		echo
3100Sstevel@tonic-gate	fi
3110Sstevel@tonic-gate	exit 0
3120Sstevel@tonic-gatefi
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate#
3150Sstevel@tonic-gate# The rest of the cases will have 'default' autoshutdown behavior.
3160Sstevel@tonic-gate#
3170Sstevel@tonic-gateif [ "$CURRENT_BEHAVIOR" = "unconfigured" ]; then
3180Sstevel@tonic-gate	set_behavior default
3190Sstevel@tonic-gate	exit 0
3200Sstevel@tonic-gatefi
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate#
3230Sstevel@tonic-gate# We are here because either the autoshutdown line has been
3240Sstevel@tonic-gate# removed or the behavior has been configured. It can be a result
3250Sstevel@tonic-gate# of upgrade. In that case, the configuration file should not
3260Sstevel@tonic-gate# be changed. Let's exit.
3270Sstevel@tonic-gateexit 0
328