Update quota tests to work with project quota (and xfs_quota).
[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     grep -q '^nobody' /etc/group
38     [ $? -ne 0 ] && _notrun "/etc/group does not contain user nobody."
39 }
40
41 # create a file as a specific user (uid)
42 # takes filename, id, type (u/g), blocksize, blockcount
43 #
44 _file_as_id()
45 {
46     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
47
48     if [ $3 = p ]; then
49         size=`expr $4 \* $5`;
50         echo xfs_io -f -c "chproj $2" -c "pwrite -b $4 0 $size" $1 >>$seq.full
51         $XFS_IO_PROG -f -c "chproj $2" -c "pwrite -b $4 0 $size" $1 \
52                 >>$seq.full 2>&1
53         return
54     elif [ $3 = u ]; then
55         magik='$>'      # perlspeak for effective uid
56     elif [ $3 = g ]; then
57         magik='$)'      # perlspeak for effective gid
58     else
59         _notrun "broken type in call to _file_as_id in test $seq"
60     fi
61
62     perl <<EOF >>$seq.full 2>&1
63         \$| = 1;
64         $magik = $2;
65         if ($5 == 0) {
66             print "touch $1";
67             exec "touch $1";
68         } else {
69             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
70             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
71         }
72 EOF
73 # for debugging the above euid change, try... [need write in cwd]
74 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
75 }
76
77 _choose_uid()
78 {
79     grep '^nobody' /etc/passwd | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
80 }
81
82 _choose_gid()
83 {
84     grep '^nobody' /etc/group | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
85 }
86
87 _choose_prid()
88 {
89     if [ ! -f /etc/projid ]; then
90         echo 0
91         return
92     fi
93     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
94         /etc/projid
95 }
96
97 _qmount()
98 {
99     umount $SCRATCH_DEV >/dev/null 2>&1
100     _scratch_mount || _fail "qmount failed"
101     chmod ugo+rwx $SCRATCH_MNT
102 }
103
104 _qsetup()
105 {
106     # setup exactly what it is we'll be testing
107     enforce=1
108     if src/feature -u $SCRATCH_DEV
109     then
110         type=u ; eval `_choose_uid`; ln -s $seq.usrquota $seq.out
111     elif src/feature -g $SCRATCH_DEV
112     then
113         type=g ; eval `_choose_gid`; ln -s $seq.grpquota $seq.out
114     elif src/feature -p $SCRATCH_DEV
115     then
116         type=p ; eval `_choose_prid`; ln -s $seq.prjquota $seq.out
117     elif src/feature -U $SCRATCH_DEV
118     then
119         type=u ; eval `_choose_uid`; ln -s $seq.uqnoenforce $seq.out
120         enforce=0
121     elif src/feature -G $SCRATCH_DEV
122     then
123         type=g ; eval `_choose_gid`; ln -s $seq.gqnoenforce $seq.out
124         enforce=0
125     elif src/feature -P $SCRATCH_DEV
126     then
127         type=p ; eval `_choose_prid`; ln -s $seq.pqnoenforce $seq.out
128         enforce=0
129     else
130         _notrun "No quota support at mount time"
131     fi
132
133     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
134     echo "and using type=$type id=$id" >>$seq.full
135 }
136
137 # make sure this script returns success
138 /bin/true