xref: /illumos-gate/usr/src/cmd/initpkg/swapadd.sh (revision 0e7f62d9)
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
67c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
77c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
87c478bd9Sstevel@tonic-gate# with the License.
97c478bd9Sstevel@tonic-gate#
107c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
117c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
127c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
137c478bd9Sstevel@tonic-gate# and limitations under the License.
147c478bd9Sstevel@tonic-gate#
157c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
167c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
177c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
187c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
197c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
207c478bd9Sstevel@tonic-gate#
217c478bd9Sstevel@tonic-gate# CDDL HEADER END
227c478bd9Sstevel@tonic-gate#
237c478bd9Sstevel@tonic-gate#
24*0e7f62d9Ssjelinek# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*0e7f62d9Ssjelinek# Use is subject to license terms.
267c478bd9Sstevel@tonic-gate#
277c478bd9Sstevel@tonic-gate# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate# All rights reserved.
297c478bd9Sstevel@tonic-gate#
307c478bd9Sstevel@tonic-gate#
317c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate
33*0e7f62d9Ssjelinek# Set noinuse checking during boot. We want to disable device in use checking
34*0e7f62d9Ssjelinek# so that normal swap use, such as specified in /etc/vfstab, will not cause
35*0e7f62d9Ssjelinek# swap devices to fail to be configured during boot.
36*0e7f62d9SsjelinekNOINUSE_CHECK=1; export NOINUSE_CHECK
37*0e7f62d9Ssjelinek
387c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
397c478bd9Sstevel@tonic-gateUSAGE="Usage: swapadd [-12] [file_system_table]"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gateVFSTAB=/etc/vfstab	# Default file system table
427c478bd9Sstevel@tonic-gatePASS=2			# Default to checking for existing swap
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate#
457c478bd9Sstevel@tonic-gate# Check to see if there is an entry in the fstab for a specified file and
467c478bd9Sstevel@tonic-gate# mount it.  This allows swap files (e.g. nfs files) to be mounted before
477c478bd9Sstevel@tonic-gate# being added for swap.
487c478bd9Sstevel@tonic-gate#
497c478bd9Sstevel@tonic-gatecheckmount()
507c478bd9Sstevel@tonic-gate{
517c478bd9Sstevel@tonic-gate	while read rspecial rfsckdev rmountp rfstype rfsckpass rautomnt rmntopts
527c478bd9Sstevel@tonic-gate	do
537c478bd9Sstevel@tonic-gate		#
547c478bd9Sstevel@tonic-gate		# Ignore comments, empty lines, and no-action lines
557c478bd9Sstevel@tonic-gate		#
567c478bd9Sstevel@tonic-gate		case "$rspecial" in
577c478bd9Sstevel@tonic-gate		'#'* | '' | '-') continue ;;
587c478bd9Sstevel@tonic-gate		esac
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate		if [ "x$rmountp" = "x$1" ]; then
617c478bd9Sstevel@tonic-gate			#
627c478bd9Sstevel@tonic-gate			# If mount options are '-', default to 'rw'
637c478bd9Sstevel@tonic-gate			#
647c478bd9Sstevel@tonic-gate			[ "x$rmntopts" = x- ] && rmntopts=rw
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate			if /sbin/mount -m -o $rmntopts $rspecial \
677c478bd9Sstevel@tonic-gate			    >/dev/null 2>&1; then
687c478bd9Sstevel@tonic-gate				echo "Mounting $rmountp for swap"
697c478bd9Sstevel@tonic-gate			else
707c478bd9Sstevel@tonic-gate				echo "Mount of $rmountp for swap failed"
717c478bd9Sstevel@tonic-gate			fi
727c478bd9Sstevel@tonic-gate			return
737c478bd9Sstevel@tonic-gate		fi
747c478bd9Sstevel@tonic-gate	done <$VFSTAB
757c478bd9Sstevel@tonic-gate}
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gatedie()
787c478bd9Sstevel@tonic-gate{
797c478bd9Sstevel@tonic-gate	echo "$*" >& 2
807c478bd9Sstevel@tonic-gate	exit 1
817c478bd9Sstevel@tonic-gate}
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gatewhile getopts 12 opt; do
847c478bd9Sstevel@tonic-gate	case "$opt" in
857c478bd9Sstevel@tonic-gate	1|2) PASS=$opt ;;
867c478bd9Sstevel@tonic-gate	 \?) die "$USAGE" ;;
877c478bd9Sstevel@tonic-gate	esac
887c478bd9Sstevel@tonic-gatedone
897c478bd9Sstevel@tonic-gateshift `expr $OPTIND - 1`
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate[ $# -gt 1 ] && die "$USAGE"
927c478bd9Sstevel@tonic-gate[ $# -gt 0 ] && VFSTAB="$1"
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate#
957c478bd9Sstevel@tonic-gate# If $VFSTAB is not "-" (stdin), re-open stdin as the specified file
967c478bd9Sstevel@tonic-gate#
977c478bd9Sstevel@tonic-gateif [ "x$VFSTAB" != x- ]; then
987c478bd9Sstevel@tonic-gate	[ -s "$VFSTAB" ] || die "swapadd: file system table ($VFSTAB) not found"
997c478bd9Sstevel@tonic-gate	exec <$VFSTAB
1007c478bd9Sstevel@tonic-gatefi
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate#
1037c478bd9Sstevel@tonic-gate# Read the file system table to find entries of file system type "swap".
1047c478bd9Sstevel@tonic-gate# Add the swap device or file specified in the first column.
1057c478bd9Sstevel@tonic-gate#
1067c478bd9Sstevel@tonic-gatewhile read special t1 t2 fstype t3 t4 t5; do
1077c478bd9Sstevel@tonic-gate	#
1087c478bd9Sstevel@tonic-gate	# Ignore comments, empty lines, and no-action lines
1097c478bd9Sstevel@tonic-gate	#
1107c478bd9Sstevel@tonic-gate	case "$special" in
1117c478bd9Sstevel@tonic-gate	'#'* | '' | '-') continue ;;
1127c478bd9Sstevel@tonic-gate	esac
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate	#
1157c478bd9Sstevel@tonic-gate	# Ignore non-swap fstypes
1167c478bd9Sstevel@tonic-gate	#
1177c478bd9Sstevel@tonic-gate	[ "$fstype" != swap ] && continue
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate	if [ $PASS = 1 ]; then
1207c478bd9Sstevel@tonic-gate		#
1217c478bd9Sstevel@tonic-gate		# Pass 1 should handle adding the swap files that
1227c478bd9Sstevel@tonic-gate		# are accessable immediately; block devices, files
1237c478bd9Sstevel@tonic-gate		# in / and /usr, and direct nfs mounted files.
1247c478bd9Sstevel@tonic-gate		#
1257c478bd9Sstevel@tonic-gate		if [ ! -b $special ]; then
1267c478bd9Sstevel@tonic-gate			#
1277c478bd9Sstevel@tonic-gate			# Read the file system table searching for mountpoints
1287c478bd9Sstevel@tonic-gate			# matching the swap file about to be added.
1297c478bd9Sstevel@tonic-gate			#
1307c478bd9Sstevel@tonic-gate			# NB: This won't work correctly if the file to added
1317c478bd9Sstevel@tonic-gate			# for swapping is a sub-directory of the mountpoint.
1327c478bd9Sstevel@tonic-gate			# e.g.	swapfile-> servername:/export/swap/clientname
1337c478bd9Sstevel@tonic-gate			# 	mountpoint-> servername:/export/swap
1347c478bd9Sstevel@tonic-gate			#
1357c478bd9Sstevel@tonic-gate			checkmount $special
1367c478bd9Sstevel@tonic-gate		fi
1377c478bd9Sstevel@tonic-gate		if [ -f $special -a -w $special -o -b $special ]; then
1387c478bd9Sstevel@tonic-gate			swap -$PASS -a $special >/dev/null
1397c478bd9Sstevel@tonic-gate		fi
1407c478bd9Sstevel@tonic-gate	else
1417c478bd9Sstevel@tonic-gate		#
1427c478bd9Sstevel@tonic-gate		# Pass 2 should skip all the swap already added.  If something
1437c478bd9Sstevel@tonic-gate		# added earlier uses the same name as something to be added
1447c478bd9Sstevel@tonic-gate		# later, the following test won't work. This should only happen
1457c478bd9Sstevel@tonic-gate		# if parts of a particular swap file are added or deleted by
1467c478bd9Sstevel@tonic-gate		# hand between invocations.
1477c478bd9Sstevel@tonic-gate		#
1487c478bd9Sstevel@tonic-gate		swap -l 2>/dev/null | grep '\<'${special}'\>' >/dev/null 2>&1 \
1497c478bd9Sstevel@tonic-gate		    || swap -$PASS -a $special >/dev/null
1507c478bd9Sstevel@tonic-gate	fi
1517c478bd9Sstevel@tonic-gatedone
152