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 # ident "@(#)setupFS.ksh 1.3 08/07/31 SMI" 28 # 29 30 # 31 # The program to setup LOFI filesystem for testing. 32 # It must be run as root. 33 # 34 35 NAME=$(basename $0) 36 DIR=$(dirname $0) 37 38 id | grep "0(root)" > /dev/null 2>&1 39 if (( $? != 0 )); then 40 echo "Must be root to run this test!" 41 exit 99 42 fi 43 44 Usage="Usage: $0 -s|-c FS_name [fs_size|noxattr]\n 45 \t-s: to setup the LOFI test filesystem\n 46 \t-c: to cleanup the LOFI test filesystem\n 47 \tFS_name: new filesystem name, full path to mount the LOFI\n 48 \tfs_size: new filesystem size, must be in format of #m,\n 49 \t\tand it's optional, default is 5m (5MB)\n" 50 51 if (( $# < 2 )); then 52 echo $Usage 53 exit 99 54 fi 55 56 OP=$(echo $1 | sed 's/\-//') 57 FSNAME=$2 58 FSIZE=5m 59 UFSOPT="rw" 60 FNAME=LOFI_file.$$ 61 62 if (( $# > 2 )); then 63 if [[ "$3" == "noxattr" ]]; then 64 FSIZE=2m 65 UFSOPT="noxattr" 66 else 67 FSIZE=$3 # must be in the format of 5m/8m 68 fi 69 fi 70 71 function cleanup 72 { 73 # remove temp files and exit 74 rm -fr $DIR/*.out.$$ 75 exit $1 76 } 77 78 case $OP in 79 s) # Setup the LOFI filesystem 80 echo "Setting up a LOFI/FS using [$DIR/$FNAME] file..." 81 # use LOFI to create a filesystem: 82 [[ -f $DIR/$FNAME ]] && rm -f $DIR/$FNAME 83 mkfile $FSIZE $DIR/$FNAME > $DIR/mkfile.out.$$ 2>&1 84 if (( $? != 0 )); then 85 echo "$NAME: mkfile $FSIZE failed:" 86 cat $DIR/mkfile.out.$$ 87 cleanup 88 88 fi 89 lofiadm -a $DIR/$FNAME > $DIR/lofi-a.out.$$ 2>&1 90 if (( $? != 0 )); then 91 echo "$NAME: lofiadm -a failed:" 92 cat $DIR/lofi-a.out.$$ 93 cleanup 88 94 fi 95 # build the UFS filesystem: 96 LDEV=$(head -1 $DIR/lofi-a.out.$$ | awk '{print $1}') 97 echo "y" | newfs $LDEV > $DIR/newfs.out.$$ 2>&1 98 if (( $? != 0 )); then 99 echo "$NAME: newfs $LDEV failed:" 100 cat $DIR/newfs.out.$$ 101 cleanup 88 102 fi 103 # Mount the device 104 [[ ! -d $FSNAME ]] && mkdir -p $FSNAME 105 if [[ "$UFSOPT" == "noxattr" ]]; then 106 mount -F tmpfs -o$UFSOPT $LDEV $FSNAME > $DIR/mnt.out.$$ 2>&1 107 else 108 mount -F ufs -orw $LDEV $FSNAME > $DIR/mnt.out.$$ 2>&1 109 fi 110 if (( $? != 0 )); then 111 echo "$NAME: mount $LDEV failed:" 112 cat $DIR/mnt.out.$$ 113 cleanup 88 114 fi 115 chmod 0777 $FSNAME 116 # save lofi-file and corresponding dev name for cleanup 117 echo "$DIR/$FNAME" > $FSNAME/..LOFI_file 118 echo "$LDEV" > $FSNAME/..LOFI_file.dev 119 120 echo "New Filesystem [$FSNAME] has been created successfully." 121 cleanup 0 122 ;; 123 124 c) # Cleanup the LOFI filesystem 125 # First retrieve the LOFI device and LOFI file names 126 FNAME=$(cat $FSNAME/..LOFI_file) 127 LDEV=$(cat $FSNAME/..LOFI_file.dev) 128 mount -p | grep "$FSNAME" | grep tmpfs > /dev/null 2>&1 129 if (( $? == 0 )); then 130 umount $FSNAME > $DIR/umnt.out.$$ 2>&1 131 else 132 umount -f $FSNAME > $DIR/umnt.out.$$ 2>&1 133 fi 134 if (( $? != 0 )); then 135 echo "$NAME: umount [$FSNAME] failed:" 136 cat $DIR/umnt.out.$$ 137 echo "\t please manually cleanup the LOFI-FS [$FSNAME]." 138 cleanup 99 139 fi 140 lofiadm -d $LDEV > $DIR/lofi-d.out.$$ 2>&1 141 if (( $? != 0 )); then 142 echo "$NAME: lofiadm -d $LDEV failed:" 143 cat $DIR/lofi-d.out.$$ 144 cleanup 99 145 fi 146 147 rm -rf $FNAME $FSNAME 148 echo "Successfully completed cleanup of [$FSNAME]." 149 cleanup 0 150 ;; 151 152 \?) 153 echo $Usage 154 exit 2 155 ;; 156 esac 157 158 exit 0 159