xfstests: Convert all tests to use /bin/bash
[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     [ -x /usr/bin/quota ] || _notrun "Quota user tools not installed"
30     _scratch_mount "-o usrquota,grpquota"
31     ret=$?
32     umount $SCRATCH_MNT
33     [ $ret -ne 0 ] && _notrun "Installed kernel does not support quota"
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_xfs_quota()
41 {
42     src/feature -q $TEST_DEV
43     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
44     [ -x /usr/sbin/xfs_quota ] || _notrun "XFS quota user tools not installed"
45 }
46
47 #
48 # checks that the XFS project quota support in the kernel is enabled.
49 #
50 _require_prjquota()
51 {
52     src/feature -p $TEST_DEV
53     [ $? -ne 0 ] && _notrun "Installed kernel does not support project quotas"
54 }
55
56 #
57 # checks for user nobody in /etc/passwd and /etc/group.
58 #
59 _require_nobody()
60 {
61     grep -q '^nobody' /etc/passwd
62     [ $? -ne 0 ] && _notrun "/etc/passwd does not contain user nobody."
63
64     egrep -q '^no(body|group)' /etc/group
65     [ $? -ne 0 ] && _notrun "/etc/group does not contain nobody/nogroup."
66 }
67
68 # create a file as a specific user (uid)
69 # takes filename, id, type (u/g/p), blocksize, blockcount
70 #
71 _file_as_id()
72 {
73     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
74
75     parent=`dirname $1`
76     if [ $3 = p ]; then
77         echo PARENT: xfs_io -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full
78         $XFS_IO_PROG -r -c "chproj $2" -c "chattr +P" $parent >>$seq.full 2>&1
79         magik='$>'      # (irrelevent, above set projid-inherit-on-parent)
80     elif [ $3 = u ]; then
81         magik='$>'      # perlspeak for effective uid
82     elif [ $3 = g ]; then
83         magik='$)'      # perlspeak for effective gid
84     else
85         _notrun "broken type in call to _file_as_id in test $seq"
86     fi
87
88     perl <<EOF >>$seq.full 2>&1
89         \$| = 1;
90         $magik = $2;
91         if ($5 == 0) {
92             print "touch $1";
93             exec "touch $1";
94         } else {
95             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
96             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
97         }
98 EOF
99 # for debugging the above euid change, try... [need write in cwd]
100 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
101
102     if [ $3 = p ]; then
103         echo PARENT: xfs_io -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full
104         $XFS_IO_PROG -r -c "chproj 0" -c "chattr -P" $parent >>$seq.full 2>&1
105     fi
106 }
107
108 _choose_uid()
109 {
110     grep '^nobody' /etc/passwd | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
111 }
112
113 _choose_gid()
114 {
115     egrep '^no(body|group)' /etc/group | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
116 }
117
118 _choose_prid()
119 {
120     if [ "X$projid_file" == "X" ]; then
121         projid_file=/etc/projid
122     fi
123     if [ ! -f $projid_file ]; then
124         echo 0
125         return
126     fi
127     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
128         $projid_file
129 }
130
131 _qmount()
132 {
133     umount $SCRATCH_DEV >/dev/null 2>&1
134     _scratch_mount || _fail "qmount failed"
135     chmod ugo+rwx $SCRATCH_MNT
136 }
137
138 _qsetup()
139 {
140     # setup exactly what it is we'll be testing
141     enforce=1
142     if src/feature -u $SCRATCH_DEV
143     then
144         type=u ;
145         eval `_choose_uid`
146         [ ! -f $seq.out ] && ln -s $seq.usrquota $seq.out
147     elif src/feature -g $SCRATCH_DEV
148     then
149         type=g
150         eval `_choose_gid`
151         [ ! -f $seq.out ] && ln -s $seq.grpquota $seq.out
152     elif src/feature -p $SCRATCH_DEV
153     then
154         type=p
155         eval `_choose_prid`
156         [ ! -f $seq.out ] && ln -s $seq.prjquota $seq.out
157     elif src/feature -U $SCRATCH_DEV
158     then
159         type=u
160         eval `_choose_uid`
161         [ ! -f $seq.out ] && ln -s $seq.uqnoenforce $seq.out
162         enforce=0
163     elif src/feature -G $SCRATCH_DEV
164     then
165         type=g
166         eval `_choose_gid`
167         [ ! -f $seq.out ] && ln -s $seq.gqnoenforce $seq.out
168         enforce=0
169     elif src/feature -P $SCRATCH_DEV
170     then
171         type=p
172         eval `_choose_prid`
173         [ ! -f $seq.out ] && ln -s $seq.pqnoenforce $seq.out
174         enforce=0
175     else
176         _notrun "No quota support at mount time"
177     fi
178
179     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
180     echo "and using type=$type id=$id" >>$seq.full
181 }
182
183 #
184 # Ensures only the given quota mount option is used
185 #
186 _qmount_option()
187 {
188         # Replace any user defined quota options
189         # with the quota option that we want.
190         # Simplest to do this rather than delete existing ones first because
191         # of the variety of commas and spaces and multiple -o's
192         # that we'd have to cater for. Doesn't matter if we have duplicates.
193         # Use "QUOTA" string so that we don't have any substring confusion
194         # thanks to "quota" which will match with "uquota" and "gquota" etc.
195         export MOUNT_OPTIONS=`echo $MOUNT_OPTIONS \
196         | sed   -e 's/uquota/QUOTA/g'      \
197                 -e 's/usrquota/QUOTA/g'    \
198                 -e 's/gquota/QUOTA/g'      \
199                 -e 's/grpquota/QUOTA/g'    \
200                 -e 's/pquota/QUOTA/g'      \
201                 -e 's/quota/QUOTA/g'       \
202                 -e 's/uqnoenforce/QUOTA/g' \
203                 -e 's/gqnoenforce/QUOTA/g' \
204                 -e 's/pqnoenforce/QUOTA/g' \
205                 -e 's/qnoenforce/QUOTA/g'  \
206                 -e "s/QUOTA/$1/g"`
207
208         # Ensure we have the given quota option - duplicates are fine
209         export MOUNT_OPTIONS="$MOUNT_OPTIONS -o $1"
210         echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seq.full
211 }
212
213 # make sure this script returns success
214 /bin/true