treewide: replace /sbin/sh hashbangs with /bin/sh
[unleashed.git] / usr / src / cmd / initpkg / swapadd.sh
blob89cb6d84a2c0fd98a91f43c8927b59c8920acae7
1 #!/bin/sh
3 # CDDL HEADER START
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.
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.
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]
21 # CDDL HEADER END
24 # Copyright 2005 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
27 # Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
28 # All rights reserved.
31 #ident "%Z%%M% %I% %E% SMI"
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.
36 NOINUSE_CHECK=1; export NOINUSE_CHECK
38 PATH=/usr/sbin:/usr/bin; export PATH
39 USAGE="Usage: swapadd [-12] [file_system_table]"
41 VFSTAB=/etc/vfstab # Default file system table
42 PASS=2 # Default to checking for existing swap
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.
49 checkmount()
51 while read rspecial rfsckdev rmountp rfstype rfsckpass rautomnt rmntopts
54 # Ignore comments, empty lines, and no-action lines
56 case "$rspecial" in
57 '#'* | '' | '-') continue ;;
58 esac
60 if [ "x$rmountp" = "x$1" ]; then
62 # If mount options are '-', default to 'rw'
64 [ "x$rmntopts" = x- ] && rmntopts=rw
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"
72 return
74 done <$VFSTAB
77 die()
79 echo "$*" >& 2
80 exit 1
83 while getopts 12 opt; do
84 case "$opt" in
85 1|2) PASS=$opt ;;
86 \?) die "$USAGE" ;;
87 esac
88 done
89 shift `expr $OPTIND - 1`
91 [ $# -gt 1 ] && die "$USAGE"
92 [ $# -gt 0 ] && VFSTAB="$1"
95 # If $VFSTAB is not "-" (stdin), re-open stdin as the specified file
97 if [ "x$VFSTAB" != x- ]; then
98 [ -s "$VFSTAB" ] || die "swapadd: file system table ($VFSTAB) not found"
99 exec <$VFSTAB
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.
106 while read special t1 t2 fstype t3 t4 t5; do
108 # Ignore comments, empty lines, and no-action lines
110 case "$special" in
111 '#'* | '' | '-') continue ;;
112 esac
115 # Ignore non-swap fstypes
117 [ "$fstype" != swap ] && continue
119 if [ $PASS = 1 ]; then
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.
125 if [ ! -b $special ]; then
127 # Read the file system table searching for mountpoints
128 # matching the swap file about to be added.
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
135 checkmount $special
137 if [ -f $special -a -w $special -o -b $special ]; then
138 swap -$PASS -a $special >/dev/null
140 else
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.
148 swap -l 2>/dev/null | grep '\<'${special}'\>' >/dev/null 2>&1 \
149 || swap -$PASS -a $special >/dev/null
151 done