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