xref: /illumos-gate/usr/src/cmd/initpkg/swapadd.sh (revision 0e7f62d9)
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 2005 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
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
33# Set noinuse checking during boot. We want to disable device in use checking
34# so that normal swap use, such as specified in /etc/vfstab, will not cause
35# swap devices to fail to be configured during boot.
36NOINUSE_CHECK=1; export NOINUSE_CHECK
37
38PATH=/usr/sbin:/usr/bin; export PATH
39USAGE="Usage: swapadd [-12] [file_system_table]"
40
41VFSTAB=/etc/vfstab	# Default file system table
42PASS=2			# Default to checking for existing swap
43
44#
45# Check to see if there is an entry in the fstab for a specified file and
46# mount it.  This allows swap files (e.g. nfs files) to be mounted before
47# being added for swap.
48#
49checkmount()
50{
51	while read rspecial rfsckdev rmountp rfstype rfsckpass rautomnt rmntopts
52	do
53		#
54		# Ignore comments, empty lines, and no-action lines
55		#
56		case "$rspecial" in
57		'#'* | '' | '-') continue ;;
58		esac
59
60		if [ "x$rmountp" = "x$1" ]; then
61			#
62			# If mount options are '-', default to 'rw'
63			#
64			[ "x$rmntopts" = x- ] && rmntopts=rw
65
66			if /sbin/mount -m -o $rmntopts $rspecial \
67			    >/dev/null 2>&1; then
68				echo "Mounting $rmountp for swap"
69			else
70				echo "Mount of $rmountp for swap failed"
71			fi
72			return
73		fi
74	done <$VFSTAB
75}
76
77die()
78{
79	echo "$*" >& 2
80	exit 1
81}
82
83while getopts 12 opt; do
84	case "$opt" in
85	1|2) PASS=$opt ;;
86	 \?) die "$USAGE" ;;
87	esac
88done
89shift `expr $OPTIND - 1`
90
91[ $# -gt 1 ] && die "$USAGE"
92[ $# -gt 0 ] && VFSTAB="$1"
93
94#
95# If $VFSTAB is not "-" (stdin), re-open stdin as the specified file
96#
97if [ "x$VFSTAB" != x- ]; then
98	[ -s "$VFSTAB" ] || die "swapadd: file system table ($VFSTAB) not found"
99	exec <$VFSTAB
100fi
101
102#
103# Read the file system table to find entries of file system type "swap".
104# Add the swap device or file specified in the first column.
105#
106while read special t1 t2 fstype t3 t4 t5; do
107	#
108	# Ignore comments, empty lines, and no-action lines
109	#
110	case "$special" in
111	'#'* | '' | '-') continue ;;
112	esac
113
114	#
115	# Ignore non-swap fstypes
116	#
117	[ "$fstype" != swap ] && continue
118
119	if [ $PASS = 1 ]; then
120		#
121		# Pass 1 should handle adding the swap files that
122		# are accessable immediately; block devices, files
123		# in / and /usr, and direct nfs mounted files.
124		#
125		if [ ! -b $special ]; then
126			#
127			# Read the file system table searching for mountpoints
128			# matching the swap file about to be added.
129			#
130			# NB: This won't work correctly if the file to added
131			# for swapping is a sub-directory of the mountpoint.
132			# e.g.	swapfile-> servername:/export/swap/clientname
133			# 	mountpoint-> servername:/export/swap
134			#
135			checkmount $special
136		fi
137		if [ -f $special -a -w $special -o -b $special ]; then
138			swap -$PASS -a $special >/dev/null
139		fi
140	else
141		#
142		# Pass 2 should skip all the swap already added.  If something
143		# added earlier uses the same name as something to be added
144		# later, the following test won't work. This should only happen
145		# if parts of a particular swap file are added or deleted by
146		# hand between invocations.
147		#
148		swap -l 2>/dev/null | grep '\<'${special}'\>' >/dev/null 2>&1 \
149		    || swap -$PASS -a $special >/dev/null
150	fi
151done
152