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