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