Ordered test and golden output files as test was failing due to sizeof( xfs_alloctype...
[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), 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     egrep '^no(body|group)' /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 [ $projid_file == "" ]; then
90         projid_file=/etc/projid
91     fi
92     if [ ! -f $projid_file ]; then
93         echo 0
94         return
95     fi
96     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
97         $projid_file
98 }
99
100 _qmount()
101 {
102     umount $SCRATCH_DEV >/dev/null 2>&1
103     _scratch_mount || _fail "qmount failed"
104     chmod ugo+rwx $SCRATCH_MNT
105 }
106
107 _qsetup()
108 {
109     # setup exactly what it is we'll be testing
110     enforce=1
111     if src/feature -u $SCRATCH_DEV
112     then
113         type=u ;
114         eval `_choose_uid`
115         [ ! -f $seq.out ] && ln -s $seq.usrquota $seq.out
116     elif src/feature -g $SCRATCH_DEV
117     then
118         type=g
119         eval `_choose_gid`
120         [ ! -f $seq.out ] && ln -s $seq.grpquota $seq.out
121     elif src/feature -p $SCRATCH_DEV
122     then
123         type=p
124         eval `_choose_prid`
125         [ ! -f $seq.out ] && ln -s $seq.prjquota $seq.out
126     elif src/feature -U $SCRATCH_DEV
127     then
128         type=u
129         eval `_choose_uid`
130         [ ! -f $seq.out ] && ln -s $seq.uqnoenforce $seq.out
131         enforce=0
132     elif src/feature -G $SCRATCH_DEV
133     then
134         type=g
135         eval `_choose_gid`
136         [ ! -f $seq.out ] && ln -s $seq.gqnoenforce $seq.out
137         enforce=0
138     elif src/feature -P $SCRATCH_DEV
139     then
140         type=p
141         eval `_choose_prid`
142         [ ! -f $seq.out ] && ln -s $seq.pqnoenforce $seq.out
143         enforce=0
144     else
145         _notrun "No quota support at mount time"
146     fi
147
148     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
149     echo "and using type=$type id=$id" >>$seq.full
150 }
151
152 # make sure this script returns success
153 /bin/true