#!/sbin/sh # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License, Version 1.0 only # (the "License"). You may not use this file except in compliance # with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2005 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T # All rights reserved. # # #ident "%Z%%M% %I% %E% SMI" # 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. setvar NOINUSE_CHECK = '1'; export NOINUSE_CHECK setvar PATH = "/usr/sbin:/usr/bin"; export PATH setvar USAGE = ""Usage: swapadd [-12] [file_system_table]"" setvar VFSTAB = '/etc/vfstab' # Default file system table setvar 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. # proc checkmount { while read rspecial rfsckdev rmountp rfstype rfsckpass rautomnt rmntopts { # # Ignore comments, empty lines, and no-action lines # match $rspecial { with '#'* | '' | '-' continue } if test "x$rmountp" = "x$1" { # # If mount options are '-', default to 'rw' # test "x$rmntopts" = x- && setvar rmntopts = 'rw' if /sbin/mount -m -o $rmntopts $rspecial \ >/dev/null 2>&1 { echo "Mounting $rmountp for swap" } else { echo "Mount of $rmountp for swap failed" } return } } <$VFSTAB } proc die { echo "$ifsjoin(ARGV)" >& 2 exit 1 } while getopts 12 opt { match $opt { with 1|2 setvar PASS = "$opt" with \? die $USAGE } } shift $(expr $OPTIND - 1) test $Argc -gt 1 && die $USAGE test $Argc -gt 0 && setvar VFSTAB = "$1" # # If $VFSTAB is not "-" (stdin), re-open stdin as the specified file # if test "x$VFSTAB" != x- { test -s $VFSTAB || die "swapadd: file system table ($VFSTAB) not found" exec <$VFSTAB } # # 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 t5 { # # Ignore comments, empty lines, and no-action lines # match $special { with '#'* | '' | '-' continue } # # Ignore non-swap fstypes # test $fstype != swap && continue if test $PASS = 1 { # # Pass 1 should handle adding the swap files that # are accessable immediately; block devices, files # in / and /usr, and direct nfs mounted files. # if test ! -b $special { # # 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 } if test -f $special -a -w $special -o -b $special { swap -$PASS -a $special >/dev/null } } 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 } }