| Current File : //sbin/swapadd |
#!/bin/ksh
#
# Copyright (c) 1991, 2011, Oracle and/or its affiliates. All rights reserved.
#
# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
# All rights reserved.
#
# Set noinuse checking during boot. We want to disable device in use checking
# so that normal swap use, such as specified in /etc/vfstab, will not cause
# swap devices to fail to be configured during boot.
NOINUSE_CHECK=1; export NOINUSE_CHECK
PATH=/usr/sbin:/usr/bin; export PATH
USAGE="Usage: swapadd [-12] [file_system_table]"
VFSTAB=/etc/vfstab # Default file system table
PASS=2 # Default to checking for existing swap
#
# Check to see if there is an entry in the fstab for a specified file and
# mount it. This allows swap files (e.g. nfs files) to be mounted before
# being added for swap.
#
checkmount()
{
while read rspecial rfsckdev rmountp rfstype rfsckpass rautomnt rmntopts
do
#
# Ignore comments, empty lines, and no-action lines
#
case "$rspecial" in
'#'* | '' | '-') continue ;;
esac
if [ "x$rmountp" = "x$1" ]; then
#
# If mount options are '-', default to 'rw'
#
[ "x$rmntopts" = x- ] && rmntopts=rw
if /usr/sbin/mount -m -o $rmntopts $rspecial \
>/dev/null 2>&1; then
echo "Mounting $rmountp for swap"
else
echo "Mount of $rmountp for swap failed"
fi
return
fi
done <$VFSTAB
}
die()
{
echo "$*" >& 2
exit 1
}
while getopts 12 opt; do
case "$opt" in
1|2) PASS=$opt ;;
\?) die "$USAGE" ;;
esac
done
shift `expr $OPTIND - 1`
[ $# -gt 1 ] && die "$USAGE"
[ $# -gt 0 ] && VFSTAB="$1"
#
# If $VFSTAB is not "-" (stdin), re-open stdin as the specified file
#
if [ "x$VFSTAB" != x- ]; then
[ -s "$VFSTAB" ] || die "swapadd: file system table ($VFSTAB) not found"
exec <$VFSTAB
fi
#
# Read the file system table to find entries of file system type "swap".
# Add the swap device or file specified in the first column.
#
while read special t1 t2 fstype t3 t4 options; do
#
# Ignore comments, empty lines, and no-action lines
#
case "$special" in
'#'* | '' | '-') continue ;;
esac
#
# Ignore non-swap fstypes
#
[ "$fstype" != swap ] && continue
if [ "$options" = "encrypted" ]; then
special=`lofiadm -c aes-256-cbc -e -a $special`
fi
if [ $PASS = 1 ]; then
#
# Pass 1 should handle adding the swap files that
# are accessable immediately; block devices, files
# in / and /usr, and direct nfs mounted files.
#
if [ ! -b $special ]; then
#
# Read the file system table searching for mountpoints
# matching the swap file about to be added.
#
# NB: This won't work correctly if the file to added
# for swapping is a sub-directory of the mountpoint.
# e.g. swapfile-> servername:/export/swap/clientname
# mountpoint-> servername:/export/swap
#
checkmount $special
fi
if [ -f $special -a -w $special -o -b $special ]; then
swap -$PASS -a $special >/dev/null
fi
else
#
# Pass 2 should skip all the swap already added. If something
# added earlier uses the same name as something to be added
# later, the following test won't work. This should only happen
# if parts of a particular swap file are added or deleted by
# hand between invocations.
#
swap -l 2>/dev/null | grep '\<'${special}'\>' >/dev/null 2>&1 \
|| swap -$PASS -a $special >/dev/null
fi
done