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