4f82546ae53165e711999bc3d8bd07e5dcfa6f63
[xfstests-dev.git] / common.quota
1 ##/bin/sh
2 #
3 # Functions useful for quota tests
4
5 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
6
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of version 2 of the GNU General Public License as
9 # published by the Free Software Foundation.
10
11 # This program is distributed in the hope that it would be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 # Further, this software is distributed without any warranty that it is
16 # free of the rightful claim of any third person regarding infringement
17 # or the like.  Any license provided herein, whether implied or
18 # otherwise, applies only to this software file.  Patent licenses, if
19 # any, provided herein do not apply to combinations of this program with
20 # other software, or any other product whatsoever.
21
22 # You should have received a copy of the GNU General Public License along
23 # with this program; if not, write the Free Software Foundation, Inc., 59
24 # Temple Place - Suite 330, Boston MA 02111-1307, USA.
25
26 # Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 # Mountain View, CA  94043, or:
28
29 # http://www.sgi.com 
30
31 # For further information regarding this notice, see: 
32
33 # http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #
35
36
37 # checks that the XFS quota support in the kernel is enabled
38 # and that we have valid quota user tools installed.
39
40 _require_quota()
41 {
42     src/feature -q $TEST_DEV
43     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
44     [ -x /usr/etc/quotaon -o -x /sbin/quotaon -o -x /usr/sbin/quotaon ] || \
45         _notrun "Quota user tools not installed"
46 }
47
48 # create a file as a specific user (uid)
49 # takes filename, id, type (u/g), blocksize, blockcount
50
51 _file_as_id()
52 {
53     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
54
55     if [ $3 = u ]
56     then
57         magik='$>'      # perlspeak for effective uid
58     elif [ $3 = g ]
59     then
60         magik='$)'      # perlspeak for effective gid
61     else
62         _notrun "broken type in call to _file_as_id in test $seq"
63     fi
64
65     perl <<EOF >>$seq.full 2>&1
66         \$| = 1;
67         $magik = $2;
68         if ($5 == 0) {
69             print "touch $1";
70             exec "touch $1";
71         } else {
72             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
73             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
74         }
75 EOF
76 # for debugging the above euid change, try... [need write in cwd]
77 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
78 }
79
80 _choose_uid()
81 {
82     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }' \
83         /etc/passwd
84 }
85
86 _choose_gid()
87 {
88     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }' \
89         /etc/group
90 }
91
92 #
93 # filter to get the guts out
94 # and make linux and irix similar
95 #
96 _filter_repquota()
97 {
98     tee -a $seq.full | $AWK_PROG '
99         /File limits/ {
100                 gotit = 1
101         }
102         gotit==1 {
103                 sub(/Disk/, "Block") 
104                 gsub(/timeleft/, "grace")
105                 gsub(/[.]0 /,"")
106                 sub(/- /, "* ")
107                 sub(/[+] /, "* ")
108                 gsub(/1week/,"7days")
109                 gsub(/NOT STARTED/,"")
110                 gsub(/[ \t]+/, " ")
111                 if ($0 ~ / [-+]/)
112                     $1 = "name"
113                 if ($0 !~ /^$/)
114                     print
115                 next
116         }
117     ' | sed -e '/-----------------/d'
118 }
119
120 _qmount()
121 {
122     umount $SCRATCH_DEV >/dev/null 2>&1
123     _scratch_mount || _fail "qmount failed"
124     chmod ugo+rwx $SCRATCH_MNT
125     [ -x /usr/sbin/quot ] && quot $SCRATCH_MNT >>$seq.full 2>&1
126 }
127
128 _qsetup()
129 {
130     # setup exactly what it is we'll be testing
131     enforce=1
132     if src/feature -u $SCRATCH_DEV
133     then
134         type=u ; eval `_choose_uid`; ln -s $seq.usrquota $seq.out
135     elif src/feature -g $SCRATCH_DEV
136     then
137         type=g ; eval `_choose_gid`; ln -s $seq.grpquota $seq.out
138     elif src/feature -U $SCRATCH_DEV
139     then
140         type=u ; eval `_choose_uid`; ln -s $seq.uqnoenforce $seq.out
141         enforce=0
142     elif src/feature -G $SCRATCH_DEV
143     then
144         type=g ; eval `_choose_gid`; ln -s $seq.gqnoenforce $seq.out
145         enforce=0
146     else
147         _notrun "No quota support at mount time"
148     fi
149
150     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
151     echo "and using type=$type id=$id" >>$seq.full
152 }
153
154
155 #-----------------------------------------------------------------------------------
156 #
157 # wrappers for Linux/IRIX differences
158 #
159 _setquota()
160 {
161     _opt_type=$1
162     _id=$2
163     _bsoft=$3
164     _bhard=$4
165     _isoft=$5
166     _ihard=$6
167     _fs=$7
168
169     [ $# -eq 7 ] || _fail "_setquota failed with wrong # args: $*"
170
171     if [ $HOSTOS = "Linux" ]; then
172         echo "setquota $_opt_type $_id $_bsoft $_bhard $_isoft $_ihard $_fs" >>$seq.full
173         setquota $_opt_type $_id $_bsoft $_bhard $_isoft $_ihard $_fs
174     else
175         # IRIX
176         if [ $_opt_type = "-u" ]; then
177                 _opt_type=""
178                 id_param="uid"
179         else 
180                 id_param="gid"
181         fi
182         echo "/usr/etc/edquota $_opt_type -f $_fs -l $id_param=$_id,bsoft=$_bsoft,bhard=$_bhard,isoft=$_isoft,ihard=$_ihard" >>$seq.full
183         /usr/etc/edquota $_opt_type -f $_fs -l $id_param=$_id,bsoft=$_bsoft,bhard=$_bhard,isoft=$_isoft,ihard=$_ihard
184     fi
185 }
186
187 _quota()
188 {
189     echo "quota $*" >>$seq.full
190     if [ $HOSTOS = "Linux" ]; then
191         quota $*
192     else
193         # clear -u
194         args=`echo $*|sed -e 's/-u//'`
195         quota -v $args
196     fi
197 }
198
199 _repquota()
200 {
201     echo "repquota $*" >>$seq.full
202     if [ $HOSTOS = "Linux" ]; then
203         repquota $*
204     else
205         # clear -u
206         args=`echo $*|sed -e 's/-u//'`
207         repquota -v $args
208     fi
209 }
210
211 #-----------------------------------------------------------------------------------
212
213 # make sure this script returns success
214 /bin/true