xfstests: randholes: encapsulate argument parsing
[xfstests-dev.git] / common.quota
1 ##/bin/bash
2 #
3 # Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 # All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it would be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write the Free Software Foundation,
17 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 #
19 #
20 # Functions useful for quota tests
21 #
22
23 #
24 # checks that the generic quota support in the kernel is enabled
25 # and that we have valid quota user tools installed.
26 #
27 _require_quota()
28 {
29     [ -n $QUOTA_PROG ] || _notrun "Quota user tools not installed"
30     if [ $FSTYP = "xfs" ]; then
31         [ -f /proc/fs/xfs/xqmstat ] || _notrun "Installed kernel does not support XFS quota"
32     elif [ $FSTYP != "gfs2" ]; then
33         [ -d /proc/sys/fs/quota ] || _notrun "Installed kernel does not support quota"
34     fi
35 }
36
37 #
38 # checks that the XFS quota support in the kernel is enabled
39 # and that we have valid quota user tools installed.
40 #
41 _require_xfs_quota()
42 {
43     src/feature -q $TEST_DEV
44     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
45     [ -n $XFS_QUOTA_PROG ] || _notrun "XFS quota user tools not installed"
46 }
47
48 #
49 # checks that the XFS project quota support in the kernel is enabled.
50 #
51 _require_prjquota()
52 {
53     [ -n "$1" ] && _dev="$1" || _dev="$TEST_DEV"
54     src/feature -p $_dev
55     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
56 }
57
58 #
59 # checks for user nobody in /etc/passwd and /etc/group.
60 #
61 _require_nobody()
62 {
63     _cat_passwd | grep -q '^nobody'
64     [ $? -ne 0 ] && _notrun "password file does not contain user nobody."
65
66     _cat_group | egrep -q '^no(body|group)'
67     [ $? -ne 0 ] && _notrun "group file does not contain nobody/nogroup."
68 }
69
70 # create a file as a specific user (uid)
71 # takes filename, id, type (u/g/p), blocksize, blockcount
72 #
73 _file_as_id()
74 {
75     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
76
77     parent=`dirname $1`
78     if [ $3 = p ]; then
79         echo PARENT: xfs_io -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full
80         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full 2>&1
81         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
82     elif [ $3 = u ]; then
83         magik='$>'      # perlspeak for effective uid
84     elif [ $3 = g ]; then
85         magik='$)'      # perlspeak for effective gid
86     else
87         _notrun "broken type in call to _file_as_id in test $seq"
88     fi
89
90     perl <<EOF >>$seq.full 2>&1
91         \$| = 1;
92         $magik = $2;
93         if ($5 == 0) {
94             print "touch $1";
95             exec "touch $1";
96         } else {
97             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
98             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
99         }
100 EOF
101 # for debugging the above euid change, try... [need write in cwd]
102 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
103
104     if [ $3 = p ]; then
105         echo PARENT: xfs_io -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full
106         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full 2>&1
107     fi
108 }
109
110 _choose_uid()
111 {
112     _cat_passwd | grep '^nobody' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
113 }
114
115 _choose_gid()
116 {
117     _cat_group | egrep '^no(body|group)' | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
118 }
119
120 _choose_prid()
121 {
122     if [ "X$projid_file" == "X" ]; then
123         projid_file=/etc/projid
124     fi
125     if [ ! -f $projid_file ]; then
126         echo 0
127         return
128     fi
129     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
130         $projid_file
131 }
132
133 _qmount()
134 {
135     umount $SCRATCH_DEV >/dev/null 2>&1
136     _scratch_mount || _fail "qmount failed"
137     chmod ugo+rwx $SCRATCH_MNT
138 }
139
140 _qsetup()
141 {
142     # setup exactly what it is we'll be testing
143     enforce=1
144     if src/feature -u $SCRATCH_DEV
145     then
146         type=u ;
147         eval `_choose_uid`
148         [ ! -f $seq.out ] && ln -s $seq.usrquota $seq.out
149     elif src/feature -g $SCRATCH_DEV
150     then
151         type=g
152         eval `_choose_gid`
153         [ ! -f $seq.out ] && ln -s $seq.grpquota $seq.out
154     elif src/feature -p $SCRATCH_DEV
155     then
156         type=p
157         eval `_choose_prid`
158         [ ! -f $seq.out ] && ln -s $seq.prjquota $seq.out
159     elif src/feature -U $SCRATCH_DEV
160     then
161         type=u
162         eval `_choose_uid`
163         [ ! -f $seq.out ] && ln -s $seq.uqnoenforce $seq.out
164         enforce=0
165     elif src/feature -G $SCRATCH_DEV
166     then
167         type=g
168         eval `_choose_gid`
169         [ ! -f $seq.out ] && ln -s $seq.gqnoenforce $seq.out
170         enforce=0
171     elif src/feature -P $SCRATCH_DEV
172     then
173         type=p
174         eval `_choose_prid`
175         [ ! -f $seq.out ] && ln -s $seq.pqnoenforce $seq.out
176         enforce=0
177     else
178         _notrun "No quota support at mount time"
179     fi
180
181     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
182     echo "and using type=$type id=$id" >>$seq.full
183 }
184
185 #
186 # Ensures only the given quota mount option is used
187 #
188 _qmount_option()
189 {
190         # Replace any user defined quota options
191         # with the quota option that we want.
192         # Simplest to do this rather than delete existing ones first because
193         # of the variety of commas and spaces and multiple -o's
194         # that we'd have to cater for. Doesn't matter if we have duplicates.
195         # Use "QUOTA" string so that we don't have any substring confusion
196         # thanks to "quota" which will match with "uquota" and "gquota" etc.
197         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
198         | sed   -e 's/uquota/QUOTA/g'      \
199                 -e 's/usrquota/QUOTA/g'    \
200                 -e 's/gquota/QUOTA/g'      \
201                 -e 's/grpquota/QUOTA/g'    \
202                 -e 's/pquota/QUOTA/g'      \
203                 -e 's/quota/QUOTA/g'       \
204                 -e 's/uqnoenforce/QUOTA/g' \
205                 -e 's/gqnoenforce/QUOTA/g' \
206                 -e 's/pqnoenforce/QUOTA/g' \
207                 -e 's/qnoenforce/QUOTA/g'  \
208                 -e "s/QUOTA/$1/g"`
209
210         # Ensure we have the given quota option - duplicates are fine
211         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $1"
212         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seq.full
213 }
214
215 _check_quota_usage()
216 {
217         # Sync to get delalloc to disk
218         sync
219         VFS_QUOTA=0
220         if [ $FSTYP = "ext2" -o $FSTYP = "ext3" -o $FSTYP = "ext4" -o $FSTYP = "reiserfs" ]; then
221                 VFS_QUOTA=1
222                 quotaon -f -u -g $SCRATCH_MNT 2>/dev/null
223         fi
224         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | filter_scratch |
225                 sort >$tmp.user.orig
226         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | filter_scratch |
227                 sort >$tmp.group.orig
228         if [ $VFS_QUOTA -eq 1 ]; then
229                 quotacheck -u -g $SCRATCH_MNT 2>/dev/null
230         else
231                 # use XFS method to force quotacheck
232                 mount -o remount,noquota $SCRATCH_DEV
233                 mount -o remount,usrquota,grpquota $SCRATCH_DEV
234         fi
235         repquota -u -n $SCRATCH_MNT  | grep -v "^#0" | filter_scratch |
236                 sort >$tmp.user.checked
237         repquota -g -n $SCRATCH_MNT  | grep -v "^#0" | filter_scratch |
238                 sort >$tmp.group.checked
239         if [ $VFS_QUOTA -eq 1 ]; then
240                 quotaon -u -g $SCRATCH_MNT 2>/dev/null
241         fi
242         {
243                 echo "Comparing user usage"
244                 diff $tmp.user.orig $tmp.user.checked
245         } && {
246                 echo "Comparing group usage"
247                 diff $tmp.group.orig $tmp.group.checked
248         }
249 }
250
251 # make sure this script returns success
252 /bin/true