1 #! /usr/bin/ksh -p
   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 (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 
  23 #
  24 # Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 # The program to setup LOFI filesystem for testing.
  28 # It must be run as root.
  29 #
  30 
  31 [[ -n $DEBUG ]] && [[ $DEBUG != 0 ]] && set -x
  32 
  33 NAME=$(basename $0)
  34 DIR=$(dirname $0)
  35 
  36 id | grep "0(root)" > /dev/null 2>&1
  37 if (( $? != 0 )); then
  38         echo "Must be root to run this test."
  39         exit 99
  40 fi
  41 
  42 Usage="Usage: $0 -s|-c FS_name [fs_size]\n
  43 \t-s: to setup the LOFI or ZFS test filesystem\n
  44 \t-c: to cleanup the LOFI or ZFS test filesystem\n
  45 \tFS_name: new filesystem name, e.g. NFSv4_fs1 to mount the LOFI or ZFS;\n
  46 \tfs_size: new filesystem size, must be in format of #m;\n
  47 \t\tand it's optional, default is 5m (5MB).\n
  48 "
  49 
  50 if (( $# < 2 )); then
  51         echo $Usage
  52         exit 99
  53 fi
  54 OP=$(echo $1 | sed 's/-//')
  55 FSNAME=$2
  56 FSIZE=5m
  57 UFSOPT="rw"
  58 if (( $# > 2 )); then
  59         FSIZE=$3        # must be in the format of 5m/8m
  60         [[ -n $4 ]] && UFSOPT=$4
  61 fi
  62 
  63 function cleanup                # remove temp files and exit
  64 {
  65         rm -fr $TMPDIR/*.out.$$
  66         exit $1
  67 }
  68 
  69 PATH=/usr/sbin:/usr/bin:$PATH; export PATH
  70 SRVTESTDIR=${SRVTESTDIR:-"/export"}
  71 TMPDIR=${TMPDIR:-"/usr/tmp"}
  72 FNAME=LOFI_file.$$
  73 
  74 
  75 case $OP in
  76   s)            # Setup the LOFI filesystem 
  77         echo "Setting up a LOFI/FS using [$SRVTESTDIR/$FNAME] file..."
  78         # use LOFI to create a filesystem under:
  79         if [[ ! -d $SRVTESTDIR ]]; then
  80                 echo "$NAME: SRVTESTDIR=[$SRVTESTDIR] not found."
  81                 cleanup 88
  82         fi
  83         rm -f $SRVTESTDIR/$FNAME
  84         mkfile $FSIZE $SRVTESTDIR/$FNAME > $TMPDIR/mkfile.out.$$ 2>&1
  85         if (( $? != 0 )); then
  86                 echo "$NAME: mkfile $FSIZE failed:"
  87                 cat $TMPDIR/mkfile.out.$$
  88                 cleanup 88
  89         fi
  90         lofiadm -a $SRVTESTDIR/$FNAME > $TMPDIR/lofi-a.out.$$ 2>&1
  91         if (( $? != 0 )); then
  92                 echo "$NAME: lofiadm -a failed:"
  93                 cat $TMPDIR/lofi-a.out.$$
  94                 cleanup 88
  95         fi
  96         # build the UFS filesystem (with less inode if NoSPC):
  97         LDEV=$(head -1 $TMPDIR/lofi-a.out.$$ | awk '{print $1}')
  98         echo $FSNAME | grep "NoSPC" > /dev/null 2>&1
  99         if (( $? == 0 )); then
 100                 echo "y" | newfs -i 10240 $LDEV > $TMPDIR/newfs.out.$$ 2>&1
 101         else
 102                 echo "y" | newfs $LDEV > $TMPDIR/newfs.out.$$ 2>&1
 103         fi
 104         if (( $? != 0 )); then
 105                 echo "$NAME: newfs $LDEV failed:"
 106                 cat $TMPDIR/newfs.out.$$
 107                 cleanup 88
 108         fi
 109         # Mount the device
 110         [[ ! -d $FSNAME ]] && mkdir -p $FSNAME
 111         if [[ $UFSOPT == noxattr ]]; then
 112             mount -F tmpfs -o$UFSOPT $LDEV $FSNAME > $TMPDIR/mnt.out.$$ 2>&1
 113         else
 114             mount -F ufs -orw $LDEV $FSNAME > $TMPDIR/mnt.out.$$ 2>&1
 115         fi
 116         if (( $? != 0 )); then
 117                 echo "$NAME: mount -orw $LDEV failed:"
 118                 cat $TMPDIR/mnt.out.$$
 119                 cleanup 88
 120         fi
 121         chmod 0777 $FSNAME
 122         # save lofi-file name for cleanup
 123         echo "$SRVTESTDIR/$FNAME" > $FSNAME/..LOFI_file
 124         echo "$LDEV" > $FSNAME/..LOFI_file.dev
 125         
 126         echo "New Filesystem [$FSNAME] has been created successfully."
 127         cleanup 0
 128         ;;
 129 
 130   c)            # Cleanup the LOFI filesystem 
 131         # First retrieve the LOFI device and LOFI file names
 132         FNAME=$(cat $FSNAME/..LOFI_file)
 133         LDEV=$(cat $FSNAME/..LOFI_file.dev)
 134         mount -p | grep "$FSNAME" | grep tmpfs > /dev/null 2>&1
 135         if (( $? == 0 )); then
 136                 umount $FSNAME > $TMPDIR/umnt.out.$$ 2>&1
 137         else
 138                 umount -f $FSNAME > $TMPDIR/umnt.out.$$ 2>&1
 139         fi
 140         if (( $? != 0 )); then
 141                 echo "$NAME: umount [$FSNAME] failed:"
 142                 cat $TMPDIR/umnt.out.$$
 143                 echo "\t please manually cleanup the LOFI-FS [$FSNAME]."
 144                 cleanup 99
 145         fi
 146         lofiadm -d $LDEV > $TMPDIR/lofi-d.out.$$ 2>&1
 147         if (( $? != 0 )); then
 148                 echo "$NAME: lofiadm -d $LDEV failed."
 149                 cat $TMPDIR/lofi-d.out.$$
 150                 cleanup 99
 151         fi
 152 
 153         rm -rf $FNAME $FSNAME
 154         echo "Successfully completed cleanup of [$FSNAME]."
 155         cleanup 0
 156         ;;
 157 
 158   \?) 
 159         echo $Usage
 160         exit 2
 161         ;;
 162 esac
 163 
 164 exit 0
 165