xref: /freebsd/tests/sys/cddl/zfs/tests/userquota/userquota_common.kshlib (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1# vim: filetype=sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)userquota_common.kshlib	1.1	09/06/22 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
33. $STF_SUITE/tests/userquota/userquota.cfg
34
35
36#
37# Check if the test box support userquota or not.
38#
39function is_userquota_supported
40{
41	if ! fs_prop_exist "userquota@..."; then
42	 	return 1
43	fi
44
45	return 0
46}
47
48#
49# reset the userquota and groupquota and delete temporary files
50#
51function cleanup_quota
52{
53	if datasetexists $QFS; then
54		log_must $ZFS set userquota@$QUSER1=none $QFS
55		log_must $ZFS set userquota@$QUSER2=none $QFS
56		log_must $ZFS set groupquota@$QGROUP=none $QFS
57		recovery_writable $QFS
58	fi
59
60	[[ -f $QFILE ]] && log_must $RM -f $QFILE
61	[[ -f $OFILE ]] && log_must $RM -f $OFILE
62
63	 return 0
64}
65
66#
67# delete user and group that created during the test
68#
69function clean_user_group
70{
71	for usr in $QUSER1 $QUSER2; do
72		log_must del_user $usr
73	done
74
75	log_must del_group $QGROUP
76
77	return 0
78}
79
80#
81#  make the $QFS's mountpoint writable for all users
82#
83function mkmount_writable
84{
85	typeset fs=$1
86	typeset mntp=$(get_prop mountpoint $fs)
87	log_must $CHMOD 0777 $mntp
88}
89
90#
91# recovery the directory permission for $QFS
92#
93function recovery_writable
94{
95	typeset fs=$1
96	typeset mntp=$(get_prop mountpoint $fs)
97	log_must $CHMOD 0755 $mntp
98}
99
100#
101# run command as specific user
102#
103function user_run
104{
105        typeset user=$1
106        typeset group=$($GROUPS $user)
107
108        shift
109
110        eval \$RUNWATTR -u \$user -g \$group \"$@\" > /dev/null 2>&1
111        return $?
112}
113
114#
115# check the quota value of a specific FS
116#
117function check_quota
118{
119	typeset fs=$2
120	typeset prop=$1
121	typeset expected=$3
122	typeset value=$(get_prop $prop $fs)
123
124	if (( $value != $expected )); then
125		return 1
126	fi
127}
128
129#
130# zfs get prop, which return raw value not -p value.
131#
132function get_value # property dataset
133{
134        typeset prop_val
135        typeset prop=$1
136        typeset dataset=$2
137
138        prop_val=$($ZFS get -H -o value $prop $dataset 2>/dev/null)
139        if [[ $? -ne 0 ]]; then
140                log_note "Unable to get $prop property for dataset " \
141                "$dataset"
142                return 1
143        fi
144
145        $ECHO $prop_val
146        return 0
147}
148