xref: /titanic_54/usr/src/cmd/initpkg/swapadd.sh (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate#!/sbin/sh
2*7c478bd9Sstevel@tonic-gate#
3*7c478bd9Sstevel@tonic-gate# CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate#
5*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate# with the License.
9*7c478bd9Sstevel@tonic-gate#
10*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate# and limitations under the License.
14*7c478bd9Sstevel@tonic-gate#
15*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate#
21*7c478bd9Sstevel@tonic-gate# CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate#
23*7c478bd9Sstevel@tonic-gate#
24*7c478bd9Sstevel@tonic-gate# Copyright (c) 1991, 1998-1999 by Sun Microsystems, Inc.
25*7c478bd9Sstevel@tonic-gate# All rights reserved.
26*7c478bd9Sstevel@tonic-gate#
27*7c478bd9Sstevel@tonic-gate# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
28*7c478bd9Sstevel@tonic-gate# All rights reserved.
29*7c478bd9Sstevel@tonic-gate#
30*7c478bd9Sstevel@tonic-gate#
31*7c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
34*7c478bd9Sstevel@tonic-gateUSAGE="Usage: swapadd [-12] [file_system_table]"
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gateVFSTAB=/etc/vfstab	# Default file system table
37*7c478bd9Sstevel@tonic-gatePASS=2			# Default to checking for existing swap
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate#
40*7c478bd9Sstevel@tonic-gate# Check to see if there is an entry in the fstab for a specified file and
41*7c478bd9Sstevel@tonic-gate# mount it.  This allows swap files (e.g. nfs files) to be mounted before
42*7c478bd9Sstevel@tonic-gate# being added for swap.
43*7c478bd9Sstevel@tonic-gate#
44*7c478bd9Sstevel@tonic-gatecheckmount()
45*7c478bd9Sstevel@tonic-gate{
46*7c478bd9Sstevel@tonic-gate	while read rspecial rfsckdev rmountp rfstype rfsckpass rautomnt rmntopts
47*7c478bd9Sstevel@tonic-gate	do
48*7c478bd9Sstevel@tonic-gate		#
49*7c478bd9Sstevel@tonic-gate		# Ignore comments, empty lines, and no-action lines
50*7c478bd9Sstevel@tonic-gate		#
51*7c478bd9Sstevel@tonic-gate		case "$rspecial" in
52*7c478bd9Sstevel@tonic-gate		'#'* | '' | '-') continue ;;
53*7c478bd9Sstevel@tonic-gate		esac
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate		if [ "x$rmountp" = "x$1" ]; then
56*7c478bd9Sstevel@tonic-gate			#
57*7c478bd9Sstevel@tonic-gate			# If mount options are '-', default to 'rw'
58*7c478bd9Sstevel@tonic-gate			#
59*7c478bd9Sstevel@tonic-gate			[ "x$rmntopts" = x- ] && rmntopts=rw
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate			if /sbin/mount -m -o $rmntopts $rspecial \
62*7c478bd9Sstevel@tonic-gate			    >/dev/null 2>&1; then
63*7c478bd9Sstevel@tonic-gate				echo "Mounting $rmountp for swap"
64*7c478bd9Sstevel@tonic-gate			else
65*7c478bd9Sstevel@tonic-gate				echo "Mount of $rmountp for swap failed"
66*7c478bd9Sstevel@tonic-gate			fi
67*7c478bd9Sstevel@tonic-gate			return
68*7c478bd9Sstevel@tonic-gate		fi
69*7c478bd9Sstevel@tonic-gate	done <$VFSTAB
70*7c478bd9Sstevel@tonic-gate}
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gatedie()
73*7c478bd9Sstevel@tonic-gate{
74*7c478bd9Sstevel@tonic-gate	echo "$*" >& 2
75*7c478bd9Sstevel@tonic-gate	exit 1
76*7c478bd9Sstevel@tonic-gate}
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gatewhile getopts 12 opt; do
79*7c478bd9Sstevel@tonic-gate	case "$opt" in
80*7c478bd9Sstevel@tonic-gate	1|2) PASS=$opt ;;
81*7c478bd9Sstevel@tonic-gate	 \?) die "$USAGE" ;;
82*7c478bd9Sstevel@tonic-gate	esac
83*7c478bd9Sstevel@tonic-gatedone
84*7c478bd9Sstevel@tonic-gateshift `expr $OPTIND - 1`
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate[ $# -gt 1 ] && die "$USAGE"
87*7c478bd9Sstevel@tonic-gate[ $# -gt 0 ] && VFSTAB="$1"
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate#
90*7c478bd9Sstevel@tonic-gate# If $VFSTAB is not "-" (stdin), re-open stdin as the specified file
91*7c478bd9Sstevel@tonic-gate#
92*7c478bd9Sstevel@tonic-gateif [ "x$VFSTAB" != x- ]; then
93*7c478bd9Sstevel@tonic-gate	[ -s "$VFSTAB" ] || die "swapadd: file system table ($VFSTAB) not found"
94*7c478bd9Sstevel@tonic-gate	exec <$VFSTAB
95*7c478bd9Sstevel@tonic-gatefi
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate#
98*7c478bd9Sstevel@tonic-gate# Read the file system table to find entries of file system type "swap".
99*7c478bd9Sstevel@tonic-gate# Add the swap device or file specified in the first column.
100*7c478bd9Sstevel@tonic-gate#
101*7c478bd9Sstevel@tonic-gatewhile read special t1 t2 fstype t3 t4 t5; do
102*7c478bd9Sstevel@tonic-gate	#
103*7c478bd9Sstevel@tonic-gate	# Ignore comments, empty lines, and no-action lines
104*7c478bd9Sstevel@tonic-gate	#
105*7c478bd9Sstevel@tonic-gate	case "$special" in
106*7c478bd9Sstevel@tonic-gate	'#'* | '' | '-') continue ;;
107*7c478bd9Sstevel@tonic-gate	esac
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate	#
110*7c478bd9Sstevel@tonic-gate	# Ignore non-swap fstypes
111*7c478bd9Sstevel@tonic-gate	#
112*7c478bd9Sstevel@tonic-gate	[ "$fstype" != swap ] && continue
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate	if [ $PASS = 1 ]; then
115*7c478bd9Sstevel@tonic-gate		#
116*7c478bd9Sstevel@tonic-gate		# Pass 1 should handle adding the swap files that
117*7c478bd9Sstevel@tonic-gate		# are accessable immediately; block devices, files
118*7c478bd9Sstevel@tonic-gate		# in / and /usr, and direct nfs mounted files.
119*7c478bd9Sstevel@tonic-gate		#
120*7c478bd9Sstevel@tonic-gate		if [ ! -b $special ]; then
121*7c478bd9Sstevel@tonic-gate			#
122*7c478bd9Sstevel@tonic-gate			# Read the file system table searching for mountpoints
123*7c478bd9Sstevel@tonic-gate			# matching the swap file about to be added.
124*7c478bd9Sstevel@tonic-gate			#
125*7c478bd9Sstevel@tonic-gate			# NB: This won't work correctly if the file to added
126*7c478bd9Sstevel@tonic-gate			# for swapping is a sub-directory of the mountpoint.
127*7c478bd9Sstevel@tonic-gate			# e.g.	swapfile-> servername:/export/swap/clientname
128*7c478bd9Sstevel@tonic-gate			# 	mountpoint-> servername:/export/swap
129*7c478bd9Sstevel@tonic-gate			#
130*7c478bd9Sstevel@tonic-gate			checkmount $special
131*7c478bd9Sstevel@tonic-gate		fi
132*7c478bd9Sstevel@tonic-gate		if [ -f $special -a -w $special -o -b $special ]; then
133*7c478bd9Sstevel@tonic-gate			swap -$PASS -a $special >/dev/null
134*7c478bd9Sstevel@tonic-gate		fi
135*7c478bd9Sstevel@tonic-gate	else
136*7c478bd9Sstevel@tonic-gate		#
137*7c478bd9Sstevel@tonic-gate		# Pass 2 should skip all the swap already added.  If something
138*7c478bd9Sstevel@tonic-gate		# added earlier uses the same name as something to be added
139*7c478bd9Sstevel@tonic-gate		# later, the following test won't work. This should only happen
140*7c478bd9Sstevel@tonic-gate		# if parts of a particular swap file are added or deleted by
141*7c478bd9Sstevel@tonic-gate		# hand between invocations.
142*7c478bd9Sstevel@tonic-gate		#
143*7c478bd9Sstevel@tonic-gate		swap -l 2>/dev/null | grep '\<'${special}'\>' >/dev/null 2>&1 \
144*7c478bd9Sstevel@tonic-gate		    || swap -$PASS -a $special >/dev/null
145*7c478bd9Sstevel@tonic-gate	fi
146*7c478bd9Sstevel@tonic-gatedone
147