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