]>
Commit | Line | Data |
---|---|---|
6bb24f4d BB |
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 | |
1d3ba0bf | 9 | # or https://opensource.org/licenses/CDDL-1.0. |
6bb24f4d BB |
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 | # | |
618b6adf | 23 | # Copyright (c) 2009, Sun Microsystems Inc. All rights reserved. |
c15d36c6 | 24 | # Copyright (c) 2012, 2020, Delphix. All rights reserved. |
618b6adf KS |
25 | # Copyright (c) 2017, Tim Chase. All rights reserved. |
26 | # Copyright (c) 2017, Nexenta Systems Inc. All rights reserved. | |
27 | # Copyright (c) 2017, Lawrence Livermore National Security LLC. | |
28 | # Copyright (c) 2017, Datto Inc. All rights reserved. | |
29 | # Copyright (c) 2017, Open-E Inc. All rights reserved. | |
c3cb57ae | 30 | # Copyright (c) 2021, The FreeBSD Foundation. |
1b939560 | 31 | # Use is subject to license terms. |
6bb24f4d BB |
32 | # |
33 | ||
5c9f744b AZ |
34 | . ${STF_SUITE}/include/tunables.cfg |
35 | ||
6bb24f4d | 36 | . ${STF_TOOLS}/include/logapi.shlib |
a7004725 | 37 | . ${STF_SUITE}/include/math.shlib |
d3f2cd7e | 38 | . ${STF_SUITE}/include/blkdev.shlib |
6bb24f4d | 39 | |
c1d9abf9 JWK |
40 | # |
41 | # Apply constrained path when available. This is required since the | |
42 | # PATH may have been modified by sudo's secure_path behavior. | |
43 | # | |
44 | if [ -n "$STF_PATH" ]; then | |
e0b53a5d | 45 | export PATH="$STF_PATH" |
c1d9abf9 JWK |
46 | fi |
47 | ||
a584ef26 BB |
48 | # |
49 | # Generic dot version comparison function | |
50 | # | |
51 | # Returns success when version $1 is greater than or equal to $2. | |
52 | # | |
53 | function compare_version_gte | |
54 | { | |
5b0e75ca | 55 | [ "$(printf "$1\n$2" | sort -V | tail -n1)" = "$1" ] |
a584ef26 BB |
56 | } |
57 | ||
0c656a96 GDN |
58 | # Linux kernel version comparison function |
59 | # | |
60 | # $1 Linux version ("4.10", "2.6.32") or blank for installed Linux version | |
61 | # | |
62 | # Used for comparison: if [ $(linux_version) -ge $(linux_version "2.6.32") ] | |
63 | # | |
64 | function linux_version | |
65 | { | |
66 | typeset ver="$1" | |
67 | ||
23914a3b | 68 | [ -z "$ver" ] && ver=$(uname -r | grep -Eo "^[0-9]+\.[0-9]+\.[0-9]+") |
0c656a96 | 69 | |
23914a3b AZ |
70 | typeset version major minor _ |
71 | IFS='.' read -r version major minor _ <<<"$ver" | |
0c656a96 | 72 | |
23914a3b AZ |
73 | [ -z "$version" ] && version=0 |
74 | [ -z "$major" ] && major=0 | |
75 | [ -z "$minor" ] && minor=0 | |
0c656a96 | 76 | |
23914a3b | 77 | echo $((version * 100000 + major * 1000 + minor)) |
0c656a96 GDN |
78 | } |
79 | ||
6bb24f4d BB |
80 | # Determine if this is a Linux test system |
81 | # | |
82 | # Return 0 if platform Linux, 1 if otherwise | |
83 | ||
84 | function is_linux | |
85 | { | |
5c9f744b | 86 | [ "$UNAME" = "Linux" ] |
6bb24f4d BB |
87 | } |
88 | ||
d7164b27 RM |
89 | # Determine if this is an illumos test system |
90 | # | |
91 | # Return 0 if platform illumos, 1 if otherwise | |
92 | function is_illumos | |
93 | { | |
5c9f744b | 94 | [ "$UNAME" = "illumos" ] |
d7164b27 RM |
95 | } |
96 | ||
7839c4b5 MM |
97 | # Determine if this is a FreeBSD test system |
98 | # | |
99 | # Return 0 if platform FreeBSD, 1 if otherwise | |
100 | ||
101 | function is_freebsd | |
102 | { | |
5c9f744b | 103 | [ "$UNAME" = "FreeBSD" ] |
7839c4b5 MM |
104 | } |
105 | ||
e676a196 BB |
106 | # Determine if this is a 32-bit system |
107 | # | |
108 | # Return 0 if platform is 32-bit, 1 if otherwise | |
109 | ||
110 | function is_32bit | |
111 | { | |
bf228f3d | 112 | [ $(getconf LONG_BIT) = "32" ] |
e676a196 BB |
113 | } |
114 | ||
c6ced726 BB |
115 | # Determine if kmemleak is enabled |
116 | # | |
117 | # Return 0 if kmemleak is enabled, 1 if otherwise | |
118 | ||
119 | function is_kmemleak | |
120 | { | |
bf228f3d | 121 | is_linux && [ -e /sys/kernel/debug/kmemleak ] |
c6ced726 BB |
122 | } |
123 | ||
6bb24f4d BB |
124 | # Determine whether a dataset is mounted |
125 | # | |
126 | # $1 dataset name | |
127 | # $2 filesystem type; optional - defaulted to zfs | |
128 | # | |
129 | # Return 0 if dataset is mounted; 1 if unmounted; 2 on error | |
130 | ||
131 | function ismounted | |
132 | { | |
133 | typeset fstype=$2 | |
134 | [[ -z $fstype ]] && fstype=zfs | |
23914a3b | 135 | typeset out dir name |
6bb24f4d BB |
136 | |
137 | case $fstype in | |
138 | zfs) | |
139 | if [[ "$1" == "/"* ]] ; then | |
75746e9a | 140 | ! zfs mount | awk -v fs="$1" '$2 == fs {exit 1}' |
6bb24f4d | 141 | else |
75746e9a | 142 | ! zfs mount | awk -v ds="$1" '$1 == ds {exit 1}' |
6bb24f4d BB |
143 | fi |
144 | ;; | |
145 | ufs|nfs) | |
7839c4b5 MM |
146 | if is_freebsd; then |
147 | mount -pt $fstype | while read dev dir _t _flags; do | |
148 | [[ "$1" == "$dev" || "$1" == "$dir" ]] && return 0 | |
149 | done | |
150 | else | |
23914a3b | 151 | out=$(df -F $fstype $1 2>/dev/null) || return |
6bb24f4d | 152 | |
7839c4b5 MM |
153 | dir=${out%%\(*} |
154 | dir=${dir%% *} | |
155 | name=${out##*\(} | |
156 | name=${name%%\)*} | |
157 | name=${name%% *} | |
6bb24f4d | 158 | |
7839c4b5 MM |
159 | [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0 |
160 | fi | |
6bb24f4d | 161 | ;; |
5c214ae3 | 162 | ext*) |
75746e9a | 163 | df -t $fstype $1 > /dev/null 2>&1 |
6bb24f4d BB |
164 | ;; |
165 | zvol) | |
166 | if [[ -L "$ZVOL_DEVDIR/$1" ]]; then | |
167 | link=$(readlink -f $ZVOL_DEVDIR/$1) | |
168 | [[ -n "$link" ]] && \ | |
c1d9abf9 | 169 | mount | grep -q "^$link" && \ |
6bb24f4d BB |
170 | return 0 |
171 | fi | |
172 | ;; | |
23914a3b AZ |
173 | *) |
174 | false | |
175 | ;; | |
6bb24f4d | 176 | esac |
6bb24f4d BB |
177 | } |
178 | ||
179 | # Return 0 if a dataset is mounted; 1 otherwise | |
180 | # | |
181 | # $1 dataset name | |
182 | # $2 filesystem type; optional - defaulted to zfs | |
183 | ||
184 | function mounted | |
185 | { | |
186 | ismounted $1 $2 | |
6bb24f4d BB |
187 | } |
188 | ||
189 | # Return 0 if a dataset is unmounted; 1 otherwise | |
190 | # | |
191 | # $1 dataset name | |
192 | # $2 filesystem type; optional - defaulted to zfs | |
193 | ||
194 | function unmounted | |
195 | { | |
23914a3b | 196 | ! ismounted $1 $2 |
6bb24f4d BB |
197 | } |
198 | ||
6bb24f4d BB |
199 | function default_setup |
200 | { | |
201 | default_setup_noexit "$@" | |
202 | ||
203 | log_pass | |
204 | } | |
205 | ||
9c53e516 TK |
206 | function default_setup_no_mountpoint |
207 | { | |
208 | default_setup_noexit "$1" "$2" "$3" "yes" | |
209 | ||
210 | log_pass | |
211 | } | |
212 | ||
6bb24f4d BB |
213 | # |
214 | # Given a list of disks, setup storage pools and datasets. | |
215 | # | |
216 | function default_setup_noexit | |
217 | { | |
218 | typeset disklist=$1 | |
219 | typeset container=$2 | |
220 | typeset volume=$3 | |
9c53e516 | 221 | typeset no_mountpoint=$4 |
3c67d83a | 222 | log_note begin default_setup_noexit |
6bb24f4d BB |
223 | |
224 | if is_global_zone; then | |
225 | if poolexists $TESTPOOL ; then | |
226 | destroy_pool $TESTPOOL | |
227 | fi | |
c1d9abf9 JWK |
228 | [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL |
229 | log_must zpool create -f $TESTPOOL $disklist | |
6bb24f4d BB |
230 | else |
231 | reexport_pool | |
232 | fi | |
233 | ||
c1d9abf9 JWK |
234 | rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR |
235 | mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR | |
6bb24f4d | 236 | |
c1d9abf9 | 237 | log_must zfs create $TESTPOOL/$TESTFS |
9c53e516 TK |
238 | if [[ -z $no_mountpoint ]]; then |
239 | log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS | |
240 | fi | |
6bb24f4d BB |
241 | |
242 | if [[ -n $container ]]; then | |
c1d9abf9 | 243 | rm -rf $TESTDIR1 || \ |
6bb24f4d | 244 | log_unresolved Could not remove $TESTDIR1 |
c1d9abf9 | 245 | mkdir -p $TESTDIR1 || \ |
6bb24f4d BB |
246 | log_unresolved Could not create $TESTDIR1 |
247 | ||
c1d9abf9 JWK |
248 | log_must zfs create $TESTPOOL/$TESTCTR |
249 | log_must zfs set canmount=off $TESTPOOL/$TESTCTR | |
250 | log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1 | |
9c53e516 TK |
251 | if [[ -z $no_mountpoint ]]; then |
252 | log_must zfs set mountpoint=$TESTDIR1 \ | |
253 | $TESTPOOL/$TESTCTR/$TESTFS1 | |
254 | fi | |
6bb24f4d BB |
255 | fi |
256 | ||
257 | if [[ -n $volume ]]; then | |
258 | if is_global_zone ; then | |
c1d9abf9 | 259 | log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL |
6bb24f4d BB |
260 | block_device_wait |
261 | else | |
c1d9abf9 | 262 | log_must zfs create $TESTPOOL/$TESTVOL |
6bb24f4d BB |
263 | fi |
264 | fi | |
265 | } | |
266 | ||
267 | # | |
268 | # Given a list of disks, setup a storage pool, file system and | |
269 | # a container. | |
270 | # | |
271 | function default_container_setup | |
272 | { | |
273 | typeset disklist=$1 | |
274 | ||
275 | default_setup "$disklist" "true" | |
276 | } | |
277 | ||
278 | # | |
279 | # Given a list of disks, setup a storage pool,file system | |
280 | # and a volume. | |
281 | # | |
282 | function default_volume_setup | |
283 | { | |
284 | typeset disklist=$1 | |
285 | ||
286 | default_setup "$disklist" "" "true" | |
287 | } | |
288 | ||
289 | # | |
290 | # Given a list of disks, setup a storage pool,file system, | |
291 | # a container and a volume. | |
292 | # | |
293 | function default_container_volume_setup | |
294 | { | |
295 | typeset disklist=$1 | |
296 | ||
297 | default_setup "$disklist" "true" "true" | |
298 | } | |
299 | ||
300 | # | |
301 | # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on | |
302 | # filesystem | |
303 | # | |
d99a0153 | 304 | # $1 Existing filesystem or volume name. Default, $TESTPOOL/$TESTFS |
6bb24f4d BB |
305 | # $2 snapshot name. Default, $TESTSNAP |
306 | # | |
307 | function create_snapshot | |
308 | { | |
d99a0153 | 309 | typeset fs_vol=${1:-$TESTPOOL/$TESTFS} |
6bb24f4d BB |
310 | typeset snap=${2:-$TESTSNAP} |
311 | ||
312 | [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined." | |
313 | [[ -z $snap ]] && log_fail "Snapshot's name is undefined." | |
314 | ||
315 | if snapexists $fs_vol@$snap; then | |
316 | log_fail "$fs_vol@$snap already exists." | |
317 | fi | |
318 | datasetexists $fs_vol || \ | |
319 | log_fail "$fs_vol must exist." | |
320 | ||
c1d9abf9 | 321 | log_must zfs snapshot $fs_vol@$snap |
6bb24f4d BB |
322 | } |
323 | ||
324 | # | |
325 | # Create a clone from a snapshot, default clone name is $TESTCLONE. | |
326 | # | |
327 | # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default. | |
328 | # $2 Clone name, $TESTPOOL/$TESTCLONE is default. | |
329 | # | |
330 | function create_clone # snapshot clone | |
331 | { | |
332 | typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP} | |
333 | typeset clone=${2:-$TESTPOOL/$TESTCLONE} | |
334 | ||
335 | [[ -z $snap ]] && \ | |
336 | log_fail "Snapshot name is undefined." | |
337 | [[ -z $clone ]] && \ | |
338 | log_fail "Clone name is undefined." | |
339 | ||
c1d9abf9 | 340 | log_must zfs clone $snap $clone |
6bb24f4d BB |
341 | } |
342 | ||
aeacdefe GM |
343 | # |
344 | # Create a bookmark of the given snapshot. Defaultly create a bookmark on | |
345 | # filesystem. | |
346 | # | |
347 | # $1 Existing filesystem or volume name. Default, $TESTFS | |
348 | # $2 Existing snapshot name. Default, $TESTSNAP | |
349 | # $3 bookmark name. Default, $TESTBKMARK | |
350 | # | |
351 | function create_bookmark | |
352 | { | |
353 | typeset fs_vol=${1:-$TESTFS} | |
354 | typeset snap=${2:-$TESTSNAP} | |
355 | typeset bkmark=${3:-$TESTBKMARK} | |
356 | ||
357 | [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined." | |
358 | [[ -z $snap ]] && log_fail "Snapshot's name is undefined." | |
359 | [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined." | |
360 | ||
361 | if bkmarkexists $fs_vol#$bkmark; then | |
362 | log_fail "$fs_vol#$bkmark already exists." | |
363 | fi | |
364 | datasetexists $fs_vol || \ | |
365 | log_fail "$fs_vol must exist." | |
366 | snapexists $fs_vol@$snap || \ | |
367 | log_fail "$fs_vol@$snap must exist." | |
368 | ||
c1d9abf9 | 369 | log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark |
aeacdefe GM |
370 | } |
371 | ||
650258d7 | 372 | # |
373 | # Create a temporary clone result of an interrupted resumable 'zfs receive' | |
374 | # $1 Destination filesystem name. Must not exist, will be created as the result | |
375 | # of this function along with its %recv temporary clone | |
376 | # $2 Source filesystem name. Must not exist, will be created and destroyed | |
377 | # | |
378 | function create_recv_clone | |
379 | { | |
380 | typeset recvfs="$1" | |
381 | typeset sendfs="${2:-$TESTPOOL/create_recv_clone}" | |
382 | typeset snap="$sendfs@snap1" | |
383 | typeset incr="$sendfs@snap2" | |
384 | typeset mountpoint="$TESTDIR/create_recv_clone" | |
385 | typeset sendfile="$TESTDIR/create_recv_clone.zsnap" | |
386 | ||
387 | [[ -z $recvfs ]] && log_fail "Recv filesystem's name is undefined." | |
388 | ||
389 | datasetexists $recvfs && log_fail "Recv filesystem must not exist." | |
390 | datasetexists $sendfs && log_fail "Send filesystem must not exist." | |
391 | ||
56fa4aa9 | 392 | log_must zfs create -o compression=off -o mountpoint="$mountpoint" $sendfs |
650258d7 | 393 | log_must zfs snapshot $snap |
394 | log_must eval "zfs send $snap | zfs recv -u $recvfs" | |
395 | log_must mkfile 1m "$mountpoint/data" | |
396 | log_must zfs snapshot $incr | |
30af21b0 PD |
397 | log_must eval "zfs send -i $snap $incr | dd bs=10K count=1 \ |
398 | iflag=fullblock > $sendfile" | |
650258d7 | 399 | log_mustnot eval "zfs recv -su $recvfs < $sendfile" |
c7b55e71 | 400 | destroy_dataset "$sendfs" "-r" |
650258d7 | 401 | log_must rm -f "$sendfile" |
402 | ||
403 | if [[ $(get_prop 'inconsistent' "$recvfs/%recv") -ne 1 ]]; then | |
404 | log_fail "Error creating temporary $recvfs/%recv clone" | |
405 | fi | |
406 | } | |
407 | ||
6bb24f4d BB |
408 | function default_mirror_setup |
409 | { | |
410 | default_mirror_setup_noexit $1 $2 $3 | |
411 | ||
412 | log_pass | |
413 | } | |
414 | ||
415 | # | |
416 | # Given a pair of disks, set up a storage pool and dataset for the mirror | |
417 | # @parameters: $1 the primary side of the mirror | |
418 | # $2 the secondary side of the mirror | |
419 | # @uses: ZPOOL ZFS TESTPOOL TESTFS | |
420 | function default_mirror_setup_noexit | |
421 | { | |
422 | readonly func="default_mirror_setup_noexit" | |
423 | typeset primary=$1 | |
424 | typeset secondary=$2 | |
425 | ||
426 | [[ -z $primary ]] && \ | |
427 | log_fail "$func: No parameters passed" | |
428 | [[ -z $secondary ]] && \ | |
429 | log_fail "$func: No secondary partition passed" | |
c1d9abf9 JWK |
430 | [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL |
431 | log_must zpool create -f $TESTPOOL mirror $@ | |
432 | log_must zfs create $TESTPOOL/$TESTFS | |
433 | log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS | |
6bb24f4d BB |
434 | } |
435 | ||
6bb24f4d BB |
436 | # |
437 | # Destroy the configured testpool mirrors. | |
438 | # the mirrors are of the form ${TESTPOOL}{number} | |
439 | # @uses: ZPOOL ZFS TESTPOOL | |
440 | function destroy_mirrors | |
441 | { | |
442 | default_cleanup_noexit | |
443 | ||
444 | log_pass | |
445 | } | |
446 | ||
1b609d4b BA |
447 | function default_raidz_setup |
448 | { | |
449 | default_raidz_setup_noexit "$*" | |
450 | ||
451 | log_pass | |
452 | } | |
453 | ||
6bb24f4d BB |
454 | # |
455 | # Given a minimum of two disks, set up a storage pool and dataset for the raid-z | |
456 | # $1 the list of disks | |
457 | # | |
1b609d4b | 458 | function default_raidz_setup_noexit |
6bb24f4d BB |
459 | { |
460 | typeset disklist="$*" | |
461 | disks=(${disklist[*]}) | |
462 | ||
463 | if [[ ${#disks[*]} -lt 2 ]]; then | |
464 | log_fail "A raid-z requires a minimum of two disks." | |
465 | fi | |
466 | ||
c1d9abf9 | 467 | [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL |
3fd3e56c | 468 | log_must zpool create -f $TESTPOOL raidz $disklist |
c1d9abf9 JWK |
469 | log_must zfs create $TESTPOOL/$TESTFS |
470 | log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS | |
6bb24f4d BB |
471 | } |
472 | ||
473 | # | |
474 | # Common function used to cleanup storage pools and datasets. | |
475 | # | |
476 | # Invoked at the start of the test suite to ensure the system | |
477 | # is in a known state, and also at the end of each set of | |
478 | # sub-tests to ensure errors from one set of tests doesn't | |
479 | # impact the execution of the next set. | |
480 | ||
481 | function default_cleanup | |
482 | { | |
483 | default_cleanup_noexit | |
484 | ||
485 | log_pass | |
486 | } | |
487 | ||
3fd3e56c | 488 | # |
489 | # Utility function used to list all available pool names. | |
490 | # | |
491 | # NOTE: $KEEP is a variable containing pool names, separated by a newline | |
492 | # character, that must be excluded from the returned list. | |
493 | # | |
494 | function get_all_pools | |
495 | { | |
496 | zpool list -H -o name | grep -Fvx "$KEEP" | grep -v "$NO_POOLS" | |
497 | } | |
498 | ||
6bb24f4d BB |
499 | function default_cleanup_noexit |
500 | { | |
6bb24f4d BB |
501 | typeset pool="" |
502 | # | |
503 | # Destroying the pool will also destroy any | |
504 | # filesystems it contains. | |
505 | # | |
506 | if is_global_zone; then | |
c1d9abf9 | 507 | zfs unmount -a > /dev/null 2>&1 |
3fd3e56c | 508 | ALL_POOLS=$(get_all_pools) |
6bb24f4d BB |
509 | # Here, we loop through the pools we're allowed to |
510 | # destroy, only destroying them if it's safe to do | |
511 | # so. | |
512 | while [ ! -z ${ALL_POOLS} ] | |
513 | do | |
514 | for pool in ${ALL_POOLS} | |
515 | do | |
516 | if safe_to_destroy_pool $pool ; | |
517 | then | |
518 | destroy_pool $pool | |
519 | fi | |
6bb24f4d | 520 | done |
2b95e911 | 521 | ALL_POOLS=$(get_all_pools) |
6bb24f4d BB |
522 | done |
523 | ||
c1d9abf9 | 524 | zfs mount -a |
6bb24f4d BB |
525 | else |
526 | typeset fs="" | |
c1d9abf9 JWK |
527 | for fs in $(zfs list -H -o name \ |
528 | | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do | |
c7b55e71 | 529 | destroy_dataset "$fs" "-Rf" |
6bb24f4d BB |
530 | done |
531 | ||
532 | # Need cleanup here to avoid garbage dir left. | |
c1d9abf9 | 533 | for fs in $(zfs list -H -o name); do |
6bb24f4d | 534 | [[ $fs == /$ZONE_POOL ]] && continue |
c1d9abf9 | 535 | [[ -d $fs ]] && log_must rm -rf $fs/* |
6bb24f4d BB |
536 | done |
537 | ||
538 | # | |
539 | # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to | |
540 | # the default value | |
541 | # | |
c1d9abf9 | 542 | for fs in $(zfs list -H -o name); do |
6bb24f4d | 543 | if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then |
c1d9abf9 JWK |
544 | log_must zfs set reservation=none $fs |
545 | log_must zfs set recordsize=128K $fs | |
546 | log_must zfs set mountpoint=/$fs $fs | |
75746e9a AZ |
547 | typeset enc=$(get_prop encryption $fs) |
548 | if [ -z "$enc" ] || [ "$enc" = "off" ]; then | |
c1d9abf9 | 549 | log_must zfs set checksum=on $fs |
6bb24f4d | 550 | fi |
c1d9abf9 JWK |
551 | log_must zfs set compression=off $fs |
552 | log_must zfs set atime=on $fs | |
553 | log_must zfs set devices=off $fs | |
554 | log_must zfs set exec=on $fs | |
555 | log_must zfs set setuid=on $fs | |
556 | log_must zfs set readonly=off $fs | |
557 | log_must zfs set snapdir=hidden $fs | |
558 | log_must zfs set aclmode=groupmask $fs | |
559 | log_must zfs set aclinherit=secure $fs | |
6bb24f4d BB |
560 | fi |
561 | done | |
562 | fi | |
563 | ||
564 | [[ -d $TESTDIR ]] && \ | |
c1d9abf9 | 565 | log_must rm -rf $TESTDIR |
7050a65d SV |
566 | |
567 | disk1=${DISKS%% *} | |
568 | if is_mpath_device $disk1; then | |
569 | delete_partitions | |
570 | fi | |
52775712 | 571 | |
572 | rm -f $TEST_BASE_DIR/{err,out} | |
6bb24f4d BB |
573 | } |
574 | ||
575 | ||
576 | # | |
577 | # Common function used to cleanup storage pools, file systems | |
578 | # and containers. | |
579 | # | |
580 | function default_container_cleanup | |
581 | { | |
582 | if ! is_global_zone; then | |
583 | reexport_pool | |
584 | fi | |
585 | ||
23914a3b | 586 | ismounted $TESTPOOL/$TESTCTR/$TESTFS1 && |
c1d9abf9 | 587 | log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1 |
6bb24f4d | 588 | |
c7b55e71 GDN |
589 | destroy_dataset "$TESTPOOL/$TESTCTR/$TESTFS1" "-R" |
590 | destroy_dataset "$TESTPOOL/$TESTCTR" "-Rf" | |
6bb24f4d BB |
591 | |
592 | [[ -e $TESTDIR1 ]] && \ | |
62c5ccdf | 593 | log_must rm -rf $TESTDIR1 |
6bb24f4d BB |
594 | |
595 | default_cleanup | |
596 | } | |
597 | ||
598 | # | |
599 | # Common function used to cleanup snapshot of file system or volume. Default to | |
600 | # delete the file system's snapshot | |
601 | # | |
602 | # $1 snapshot name | |
603 | # | |
604 | function destroy_snapshot | |
605 | { | |
606 | typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP} | |
607 | ||
608 | if ! snapexists $snap; then | |
838bd5ff | 609 | log_fail "'$snap' does not exist." |
6bb24f4d BB |
610 | fi |
611 | ||
612 | # | |
613 | # For the sake of the value which come from 'get_prop' is not equal | |
614 | # to the really mountpoint when the snapshot is unmounted. So, firstly | |
615 | # check and make sure this snapshot's been mounted in current system. | |
616 | # | |
617 | typeset mtpt="" | |
618 | if ismounted $snap; then | |
619 | mtpt=$(get_prop mountpoint $snap) | |
6bb24f4d BB |
620 | fi |
621 | ||
c7b55e71 | 622 | destroy_dataset "$snap" |
6bb24f4d | 623 | [[ $mtpt != "" && -d $mtpt ]] && \ |
c1d9abf9 | 624 | log_must rm -rf $mtpt |
6bb24f4d BB |
625 | } |
626 | ||
627 | # | |
628 | # Common function used to cleanup clone. | |
629 | # | |
630 | # $1 clone name | |
631 | # | |
632 | function destroy_clone | |
633 | { | |
634 | typeset clone=${1:-$TESTPOOL/$TESTCLONE} | |
635 | ||
636 | if ! datasetexists $clone; then | |
637 | log_fail "'$clone' does not existed." | |
638 | fi | |
639 | ||
640 | # With the same reason in destroy_snapshot | |
641 | typeset mtpt="" | |
642 | if ismounted $clone; then | |
643 | mtpt=$(get_prop mountpoint $clone) | |
6bb24f4d BB |
644 | fi |
645 | ||
c7b55e71 | 646 | destroy_dataset "$clone" |
6bb24f4d | 647 | [[ $mtpt != "" && -d $mtpt ]] && \ |
c1d9abf9 | 648 | log_must rm -rf $mtpt |
6bb24f4d BB |
649 | } |
650 | ||
aeacdefe GM |
651 | # |
652 | # Common function used to cleanup bookmark of file system or volume. Default | |
653 | # to delete the file system's bookmark. | |
654 | # | |
655 | # $1 bookmark name | |
656 | # | |
657 | function destroy_bookmark | |
658 | { | |
659 | typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK} | |
660 | ||
661 | if ! bkmarkexists $bkmark; then | |
662 | log_fail "'$bkmarkp' does not existed." | |
663 | fi | |
664 | ||
c7b55e71 | 665 | destroy_dataset "$bkmark" |
aeacdefe GM |
666 | } |
667 | ||
6bb24f4d BB |
668 | # Return 0 if a snapshot exists; $? otherwise |
669 | # | |
670 | # $1 - snapshot name | |
671 | ||
672 | function snapexists | |
673 | { | |
c1d9abf9 | 674 | zfs list -H -t snapshot "$1" > /dev/null 2>&1 |
6bb24f4d BB |
675 | } |
676 | ||
aeacdefe GM |
677 | # |
678 | # Return 0 if a bookmark exists; $? otherwise | |
679 | # | |
680 | # $1 - bookmark name | |
681 | # | |
682 | function bkmarkexists | |
683 | { | |
c1d9abf9 | 684 | zfs list -H -t bookmark "$1" > /dev/null 2>&1 |
aeacdefe GM |
685 | } |
686 | ||
3b9edd7b SD |
687 | # |
688 | # Return 0 if a hold exists; $? otherwise | |
689 | # | |
690 | # $1 - hold tag | |
691 | # $2 - snapshot name | |
692 | # | |
693 | function holdexists | |
694 | { | |
75746e9a | 695 | ! zfs holds "$2" | awk -v t="$1" '$2 ~ t { exit 1 }' |
3b9edd7b SD |
696 | } |
697 | ||
6bb24f4d BB |
698 | # |
699 | # Set a property to a certain value on a dataset. | |
700 | # Sets a property of the dataset to the value as passed in. | |
701 | # @param: | |
702 | # $1 dataset who's property is being set | |
703 | # $2 property to set | |
704 | # $3 value to set property to | |
705 | # @return: | |
706 | # 0 if the property could be set. | |
707 | # non-zero otherwise. | |
708 | # @use: ZFS | |
709 | # | |
710 | function dataset_setprop | |
711 | { | |
712 | typeset fn=dataset_setprop | |
713 | ||
714 | if (($# < 3)); then | |
715 | log_note "$fn: Insufficient parameters (need 3, had $#)" | |
716 | return 1 | |
717 | fi | |
718 | typeset output= | |
c1d9abf9 | 719 | output=$(zfs set $2=$3 $1 2>&1) |
6bb24f4d BB |
720 | typeset rv=$? |
721 | if ((rv != 0)); then | |
722 | log_note "Setting property on $1 failed." | |
723 | log_note "property $2=$3" | |
724 | log_note "Return Code: $rv" | |
725 | log_note "Output: $output" | |
726 | return $rv | |
727 | fi | |
728 | return 0 | |
729 | } | |
730 | ||
6bb24f4d BB |
731 | # |
732 | # Check a numeric assertion | |
733 | # @parameter: $@ the assertion to check | |
734 | # @output: big loud notice if assertion failed | |
735 | # @use: log_fail | |
736 | # | |
737 | function assert | |
738 | { | |
739 | (($@)) || log_fail "$@" | |
740 | } | |
741 | ||
742 | # | |
743 | # Function to format partition size of a disk | |
744 | # Given a disk cxtxdx reduces all partitions | |
745 | # to 0 size | |
746 | # | |
747 | function zero_partitions #<whole_disk_name> | |
748 | { | |
749 | typeset diskname=$1 | |
750 | typeset i | |
751 | ||
395a6b19 RM |
752 | if is_freebsd; then |
753 | gpart destroy -F $diskname | |
754 | elif is_linux; then | |
8e5d1484 PZ |
755 | DSK=$DEV_DSKDIR/$diskname |
756 | DSK=$(echo $DSK | sed -e "s|//|/|g") | |
757 | log_must parted $DSK -s -- mklabel gpt | |
758 | blockdev --rereadpt $DSK 2>/dev/null | |
759 | block_device_wait | |
6bb24f4d BB |
760 | else |
761 | for i in 0 1 3 4 5 6 7 | |
762 | do | |
cf8738d8 | 763 | log_must set_partition $i "" 0mb $diskname |
6bb24f4d BB |
764 | done |
765 | fi | |
95401cb6 BB |
766 | |
767 | return 0 | |
6bb24f4d BB |
768 | } |
769 | ||
770 | # | |
771 | # Given a slice, size and disk, this function | |
772 | # formats the slice to the specified size. | |
773 | # Size should be specified with units as per | |
774 | # the `format` command requirements eg. 100mb 3gb | |
775 | # | |
c6e457df | 776 | # NOTE: This entire interface is problematic for the Linux parted utility |
6bb24f4d BB |
777 | # which requires the end of the partition to be specified. It would be |
778 | # best to retire this interface and replace it with something more flexible. | |
779 | # At the moment a best effort is made. | |
780 | # | |
2ff615b2 RE |
781 | # arguments: <slice_num> <slice_start> <size_plus_units> <whole_disk_name> |
782 | function set_partition | |
6bb24f4d BB |
783 | { |
784 | typeset -i slicenum=$1 | |
785 | typeset start=$2 | |
786 | typeset size=$3 | |
a3bddd49 RM |
787 | typeset disk=${4#$DEV_DSKDIR/} |
788 | disk=${disk#$DEV_RDSKDIR/} | |
6bb24f4d | 789 | |
5c9f744b | 790 | case "$UNAME" in |
7839c4b5 | 791 | Linux) |
8e5d1484 PZ |
792 | if [[ -z $size || -z $disk ]]; then |
793 | log_fail "The size or disk name is unspecified." | |
794 | fi | |
a3bddd49 | 795 | disk=$DEV_DSKDIR/$disk |
6bb24f4d BB |
796 | typeset size_mb=${size%%[mMgG]} |
797 | ||
798 | size_mb=${size_mb%%[mMgG][bB]} | |
799 | if [[ ${size:1:1} == 'g' ]]; then | |
800 | ((size_mb = size_mb * 1024)) | |
801 | fi | |
802 | ||
803 | # Create GPT partition table when setting slice 0 or | |
804 | # when the device doesn't already contain a GPT label. | |
2ff615b2 | 805 | parted $disk -s -- print 1 >/dev/null |
6bb24f4d BB |
806 | typeset ret_val=$? |
807 | if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then | |
23914a3b | 808 | if ! parted $disk -s -- mklabel gpt; then |
cf8738d8 | 809 | log_note "Failed to create GPT partition table on $disk" |
810 | return 1 | |
811 | fi | |
6bb24f4d BB |
812 | fi |
813 | ||
814 | # When no start is given align on the first cylinder. | |
815 | if [[ -z "$start" ]]; then | |
816 | start=1 | |
817 | fi | |
818 | ||
819 | # Determine the cylinder size for the device and using | |
820 | # that calculate the end offset in cylinders. | |
821 | typeset -i cly_size_kb=0 | |
75746e9a AZ |
822 | cly_size_kb=$(parted -m $disk -s -- unit cyl print | |
823 | awk -F '[:k.]' 'NR == 3 {print $4}') | |
6bb24f4d BB |
824 | ((end = (size_mb * 1024 / cly_size_kb) + start)) |
825 | ||
2ff615b2 | 826 | parted $disk -s -- \ |
6bb24f4d | 827 | mkpart part$slicenum ${start}cyl ${end}cyl |
2ff615b2 RE |
828 | typeset ret_val=$? |
829 | if [[ $ret_val -ne 0 ]]; then | |
cf8738d8 | 830 | log_note "Failed to create partition $slicenum on $disk" |
831 | return 1 | |
832 | fi | |
6bb24f4d | 833 | |
2ff615b2 RE |
834 | blockdev --rereadpt $disk 2>/dev/null |
835 | block_device_wait $disk | |
7839c4b5 MM |
836 | ;; |
837 | FreeBSD) | |
838 | if [[ -z $size || -z $disk ]]; then | |
839 | log_fail "The size or disk name is unspecified." | |
840 | fi | |
a3bddd49 | 841 | disk=$DEV_DSKDIR/$disk |
7839c4b5 MM |
842 | |
843 | if [[ $slicenum -eq 0 ]] || ! gpart show $disk >/dev/null 2>&1; then | |
844 | gpart destroy -F $disk >/dev/null 2>&1 | |
23914a3b | 845 | if ! gpart create -s GPT $disk; then |
7839c4b5 MM |
846 | log_note "Failed to create GPT partition table on $disk" |
847 | return 1 | |
848 | fi | |
849 | fi | |
850 | ||
851 | typeset index=$((slicenum + 1)) | |
852 | ||
853 | if [[ -n $start ]]; then | |
854 | start="-b $start" | |
855 | fi | |
856 | gpart add -t freebsd-zfs $start -s $size -i $index $disk | |
857 | if [[ $ret_val -ne 0 ]]; then | |
858 | log_note "Failed to create partition $slicenum on $disk" | |
859 | return 1 | |
860 | fi | |
861 | ||
862 | block_device_wait $disk | |
863 | ;; | |
864 | *) | |
8e5d1484 PZ |
865 | if [[ -z $slicenum || -z $size || -z $disk ]]; then |
866 | log_fail "The slice, size or disk name is unspecified." | |
867 | fi | |
868 | ||
6bb24f4d BB |
869 | typeset format_file=/var/tmp/format_in.$$ |
870 | ||
c1d9abf9 JWK |
871 | echo "partition" >$format_file |
872 | echo "$slicenum" >> $format_file | |
873 | echo "" >> $format_file | |
874 | echo "" >> $format_file | |
875 | echo "$start" >> $format_file | |
876 | echo "$size" >> $format_file | |
877 | echo "label" >> $format_file | |
878 | echo "" >> $format_file | |
879 | echo "q" >> $format_file | |
880 | echo "q" >> $format_file | |
6bb24f4d | 881 | |
c1d9abf9 | 882 | format -e -s -d $disk -f $format_file |
2ff615b2 RE |
883 | typeset ret_val=$? |
884 | rm -f $format_file | |
7839c4b5 MM |
885 | ;; |
886 | esac | |
c1d9abf9 | 887 | |
cf8738d8 | 888 | if [[ $ret_val -ne 0 ]]; then |
889 | log_note "Unable to format $disk slice $slicenum to $size" | |
890 | return 1 | |
891 | fi | |
6bb24f4d BB |
892 | return 0 |
893 | } | |
894 | ||
7050a65d SV |
895 | # |
896 | # Delete all partitions on all disks - this is specifically for the use of multipath | |
897 | # devices which currently can only be used in the test suite as raw/un-partitioned | |
898 | # devices (ie a zpool cannot be created on a whole mpath device that has partitions) | |
899 | # | |
900 | function delete_partitions | |
901 | { | |
815a6456 | 902 | typeset disk |
7050a65d | 903 | |
7050a65d SV |
904 | if [[ -z $DISKSARRAY ]]; then |
905 | DISKSARRAY=$DISKS | |
906 | fi | |
907 | ||
908 | if is_linux; then | |
815a6456 RM |
909 | typeset -i part |
910 | for disk in $DISKSARRAY; do | |
911 | for (( part = 1; part < MAX_PARTITIONS; part++ )); do | |
912 | typeset partition=${disk}${SLICE_PREFIX}${part} | |
913 | parted $DEV_DSKDIR/$disk -s rm $part > /dev/null 2>&1 | |
914 | if lsblk | grep -qF ${partition}; then | |
915 | log_fail "Partition ${partition} not deleted" | |
7050a65d | 916 | else |
815a6456 | 917 | log_note "Partition ${partition} deleted" |
7050a65d | 918 | fi |
7050a65d | 919 | done |
815a6456 | 920 | done |
7839c4b5 MM |
921 | elif is_freebsd; then |
922 | for disk in $DISKSARRAY; do | |
923 | if gpart destroy -F $disk; then | |
924 | log_note "Partitions for ${disk} deleted" | |
925 | else | |
926 | log_fail "Partitions for ${disk} not deleted" | |
927 | fi | |
928 | done | |
7050a65d | 929 | fi |
7050a65d SV |
930 | } |
931 | ||
6bb24f4d BB |
932 | # |
933 | # Get the end cyl of the given slice | |
934 | # | |
935 | function get_endslice #<disk> <slice> | |
936 | { | |
937 | typeset disk=$1 | |
938 | typeset slice=$2 | |
939 | if [[ -z $disk || -z $slice ]] ; then | |
940 | log_fail "The disk name or slice number is unspecified." | |
941 | fi | |
942 | ||
5c9f744b | 943 | case "$UNAME" in |
7839c4b5 | 944 | Linux) |
c1d9abf9 | 945 | endcyl=$(parted -s $DEV_DSKDIR/$disk -- unit cyl print | \ |
420b4448 | 946 | awk "/part${slice}/"' {sub(/cyl/, "", $3); print $3}') |
6bb24f4d | 947 | ((endcyl = (endcyl + 1))) |
7839c4b5 MM |
948 | ;; |
949 | FreeBSD) | |
950 | disk=${disk#/dev/zvol/} | |
951 | disk=${disk%p*} | |
952 | slice=$((slice + 1)) | |
953 | endcyl=$(gpart show $disk | \ | |
954 | awk -v slice=$slice '$3 == slice { print $1 + $2 }') | |
955 | ;; | |
956 | *) | |
6bb24f4d BB |
957 | disk=${disk#/dev/dsk/} |
958 | disk=${disk#/dev/rdsk/} | |
959 | disk=${disk%s*} | |
960 | ||
961 | typeset -i ratio=0 | |
c1d9abf9 | 962 | ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \ |
75746e9a | 963 | awk '/sectors\/cylinder/ {print $2}') |
6bb24f4d BB |
964 | |
965 | if ((ratio == 0)); then | |
966 | return | |
967 | fi | |
968 | ||
c1d9abf9 | 969 | typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 | |
75746e9a | 970 | awk -v token="$slice" '$1 == token {print $6}') |
6bb24f4d BB |
971 | |
972 | ((endcyl = (endcyl + 1) / ratio)) | |
7839c4b5 MM |
973 | ;; |
974 | esac | |
7c468940 | 975 | |
6bb24f4d BB |
976 | echo $endcyl |
977 | } | |
978 | ||
979 | ||
980 | # | |
981 | # Given a size,disk and total slice number, this function formats the | |
982 | # disk slices from 0 to the total slice number with the same specified | |
983 | # size. | |
984 | # | |
985 | function partition_disk #<slice_size> <whole_disk_name> <total_slices> | |
986 | { | |
987 | typeset -i i=0 | |
988 | typeset slice_size=$1 | |
989 | typeset disk_name=$2 | |
990 | typeset total_slices=$3 | |
991 | typeset cyl | |
992 | ||
993 | zero_partitions $disk_name | |
994 | while ((i < $total_slices)); do | |
995 | if ! is_linux; then | |
996 | if ((i == 2)); then | |
997 | ((i = i + 1)) | |
998 | continue | |
999 | fi | |
1000 | fi | |
cf8738d8 | 1001 | log_must set_partition $i "$cyl" $slice_size $disk_name |
6bb24f4d BB |
1002 | cyl=$(get_endslice $disk_name $i) |
1003 | ((i = i+1)) | |
1004 | done | |
1005 | } | |
1006 | ||
1007 | # | |
1008 | # This function continues to write to a filenum number of files into dirnum | |
c1d9abf9 | 1009 | # number of directories until either file_write returns an error or the |
6bb24f4d BB |
1010 | # maximum number of files per directory have been written. |
1011 | # | |
1012 | # Usage: | |
1013 | # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data] | |
1014 | # | |
1015 | # Return value: 0 on success | |
1016 | # non 0 on error | |
1017 | # | |
1018 | # Where : | |
1019 | # destdir: is the directory where everything is to be created under | |
1020 | # dirnum: the maximum number of subdirectories to use, -1 no limit | |
1021 | # filenum: the maximum number of files per subdirectory | |
1022 | # bytes: number of bytes to write | |
c6e457df | 1023 | # num_writes: number of types to write out bytes |
4e33ba4c | 1024 | # data: the data that will be written |
6bb24f4d BB |
1025 | # |
1026 | # E.g. | |
9be70c37 | 1027 | # fill_fs /testdir 20 25 1024 256 0 |
6bb24f4d BB |
1028 | # |
1029 | # Note: bytes * num_writes equals the size of the testfile | |
1030 | # | |
1031 | function fill_fs # destdir dirnum filenum bytes num_writes data | |
1032 | { | |
1033 | typeset destdir=${1:-$TESTDIR} | |
1034 | typeset -i dirnum=${2:-50} | |
1035 | typeset -i filenum=${3:-50} | |
1036 | typeset -i bytes=${4:-8192} | |
1037 | typeset -i num_writes=${5:-10240} | |
1b939560 | 1038 | typeset data=${6:-0} |
6bb24f4d | 1039 | |
9be70c37 RM |
1040 | mkdir -p $destdir/{1..$dirnum} |
1041 | for f in $destdir/{1..$dirnum}/$TESTFILE{1..$filenum}; do | |
1042 | file_write -o create -f $f -b $bytes -c $num_writes -d $data \ | |
23914a3b | 1043 | || return |
6bb24f4d | 1044 | done |
6bb24f4d BB |
1045 | } |
1046 | ||
75746e9a | 1047 | # Get the specified dataset property in parsable format or fail |
6bb24f4d BB |
1048 | function get_prop # property dataset |
1049 | { | |
6bb24f4d BB |
1050 | typeset prop=$1 |
1051 | typeset dataset=$2 | |
1052 | ||
75746e9a | 1053 | zfs get -Hpo value "$prop" "$dataset" || log_fail "zfs get $prop $dataset" |
6bb24f4d BB |
1054 | } |
1055 | ||
75746e9a | 1056 | # Get the specified pool property in parsable format or fail |
6bb24f4d BB |
1057 | function get_pool_prop # property pool |
1058 | { | |
6bb24f4d BB |
1059 | typeset prop=$1 |
1060 | typeset pool=$2 | |
1061 | ||
75746e9a | 1062 | zpool get -Hpo value "$prop" "$pool" || log_fail "zpool get $prop $pool" |
6bb24f4d BB |
1063 | } |
1064 | ||
1065 | # Return 0 if a pool exists; $? otherwise | |
1066 | # | |
1067 | # $1 - pool name | |
1068 | ||
1069 | function poolexists | |
1070 | { | |
1071 | typeset pool=$1 | |
1072 | ||
1073 | if [[ -z $pool ]]; then | |
1074 | log_note "No pool name given." | |
1075 | return 1 | |
1076 | fi | |
1077 | ||
c1d9abf9 | 1078 | zpool get name "$pool" > /dev/null 2>&1 |
6bb24f4d BB |
1079 | } |
1080 | ||
1081 | # Return 0 if all the specified datasets exist; $? otherwise | |
1082 | # | |
1083 | # $1-n dataset name | |
1084 | function datasetexists | |
1085 | { | |
1086 | if (($# == 0)); then | |
1087 | log_note "No dataset name given." | |
1088 | return 1 | |
1089 | fi | |
1090 | ||
41ebf403 | 1091 | zfs get name "$@" > /dev/null 2>&1 |
6bb24f4d BB |
1092 | } |
1093 | ||
1094 | # return 0 if none of the specified datasets exists, otherwise return 1. | |
1095 | # | |
1096 | # $1-n dataset name | |
1097 | function datasetnonexists | |
1098 | { | |
1099 | if (($# == 0)); then | |
1100 | log_note "No dataset name given." | |
1101 | return 1 | |
1102 | fi | |
1103 | ||
1104 | while (($# > 0)); do | |
c1d9abf9 | 1105 | zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \ |
6bb24f4d BB |
1106 | && return 1 |
1107 | shift | |
1108 | done | |
1109 | ||
1110 | return 0 | |
1111 | } | |
1112 | ||
88d5580e AZ |
1113 | # FreeBSD breaks exports(5) at whitespace and doesn't process escapes |
1114 | # Solaris just breaks | |
1115 | # | |
1116 | # cf. https://github.com/openzfs/zfs/pull/13165#issuecomment-1059845807 | |
1117 | # | |
1118 | # Linux can have spaces (which are \OOO-escaped), | |
1119 | # but can't have backslashes because they're parsed recursively | |
1120 | function shares_can_have_whitespace | |
1121 | { | |
1122 | is_linux | |
1123 | } | |
1124 | ||
b7dbbf6a | 1125 | function is_shared_freebsd |
6bb24f4d BB |
1126 | { |
1127 | typeset fs=$1 | |
6bb24f4d | 1128 | |
5b0e75ca | 1129 | pgrep -q mountd && showmount -E | grep -qx "$fs" |
b7dbbf6a RM |
1130 | } |
1131 | ||
1132 | function is_shared_illumos | |
1133 | { | |
1134 | typeset fs=$1 | |
1135 | typeset mtpt | |
2f71caf2 | 1136 | |
c1d9abf9 | 1137 | for mtpt in `share | awk '{print $2}'` ; do |
6bb24f4d BB |
1138 | if [[ $mtpt == $fs ]] ; then |
1139 | return 0 | |
1140 | fi | |
1141 | done | |
1142 | ||
c1d9abf9 | 1143 | typeset stat=$(svcs -H -o STA nfs/server:default) |
6bb24f4d BB |
1144 | if [[ $stat != "ON" ]]; then |
1145 | log_note "Current nfs/server status: $stat" | |
1146 | fi | |
1147 | ||
1148 | return 1 | |
1149 | } | |
1150 | ||
b7dbbf6a RM |
1151 | function is_shared_linux |
1152 | { | |
1153 | typeset fs=$1 | |
5b0e75ca | 1154 | ! exportfs -s | awk -v fs="${fs//\\/\\\\}" '/^\// && $1 == fs {exit 1}' |
b7dbbf6a RM |
1155 | } |
1156 | ||
54eb2c41 PS |
1157 | # |
1158 | # Given a mountpoint, or a dataset name, determine if it is shared via NFS. | |
1159 | # | |
1160 | # Returns 0 if shared, 1 otherwise. | |
1161 | # | |
1162 | function is_shared | |
1163 | { | |
1164 | typeset fs=$1 | |
1165 | typeset mtpt | |
1166 | ||
1167 | if [[ $fs != "/"* ]] ; then | |
1168 | if datasetnonexists "$fs" ; then | |
1169 | return 1 | |
1170 | else | |
1171 | mtpt=$(get_prop mountpoint "$fs") | |
5b0e75ca | 1172 | case "$mtpt" in |
54eb2c41 PS |
1173 | none|legacy|-) return 1 |
1174 | ;; | |
1175 | *) fs=$mtpt | |
1176 | ;; | |
1177 | esac | |
1178 | fi | |
1179 | fi | |
1180 | ||
5c9f744b | 1181 | case "$UNAME" in |
b7dbbf6a RM |
1182 | FreeBSD) is_shared_freebsd "$fs" ;; |
1183 | Linux) is_shared_linux "$fs" ;; | |
1184 | *) is_shared_illumos "$fs" ;; | |
1185 | esac | |
54eb2c41 PS |
1186 | } |
1187 | ||
c15d36c6 GW |
1188 | function is_exported_illumos |
1189 | { | |
1190 | typeset fs=$1 | |
5b0e75ca | 1191 | typeset mtpt _ |
c15d36c6 | 1192 | |
5b0e75ca AZ |
1193 | while read -r mtpt _; do |
1194 | [ "$mtpt" = "$fs" ] && return | |
1195 | done < /etc/dfs/sharetab | |
c15d36c6 GW |
1196 | |
1197 | return 1 | |
1198 | } | |
1199 | ||
1200 | function is_exported_freebsd | |
1201 | { | |
1202 | typeset fs=$1 | |
5b0e75ca | 1203 | typeset mtpt _ |
c15d36c6 | 1204 | |
5b0e75ca AZ |
1205 | while read -r mtpt _; do |
1206 | [ "$mtpt" = "$fs" ] && return | |
1207 | done < /etc/zfs/exports | |
c15d36c6 GW |
1208 | |
1209 | return 1 | |
1210 | } | |
1211 | ||
1212 | function is_exported_linux | |
1213 | { | |
1214 | typeset fs=$1 | |
5b0e75ca | 1215 | typeset mtpt _ |
c15d36c6 | 1216 | |
5b0e75ca AZ |
1217 | while read -r mtpt _; do |
1218 | [ "$(printf "$mtpt")" = "$fs" ] && return | |
1219 | done < /etc/exports.d/zfs.exports | |
c15d36c6 GW |
1220 | |
1221 | return 1 | |
1222 | } | |
1223 | ||
1224 | # | |
1225 | # Given a mountpoint, or a dataset name, determine if it is exported via | |
1226 | # the os-specific NFS exports file. | |
1227 | # | |
1228 | # Returns 0 if exported, 1 otherwise. | |
1229 | # | |
1230 | function is_exported | |
1231 | { | |
1232 | typeset fs=$1 | |
1233 | typeset mtpt | |
1234 | ||
1235 | if [[ $fs != "/"* ]] ; then | |
1236 | if datasetnonexists "$fs" ; then | |
1237 | return 1 | |
1238 | else | |
1239 | mtpt=$(get_prop mountpoint "$fs") | |
1240 | case $mtpt in | |
1241 | none|legacy|-) return 1 | |
1242 | ;; | |
1243 | *) fs=$mtpt | |
1244 | ;; | |
1245 | esac | |
1246 | fi | |
1247 | fi | |
1248 | ||
5c9f744b | 1249 | case "$UNAME" in |
c15d36c6 GW |
1250 | FreeBSD) is_exported_freebsd "$fs" ;; |
1251 | Linux) is_exported_linux "$fs" ;; | |
1252 | *) is_exported_illumos "$fs" ;; | |
1253 | esac | |
1254 | } | |
1255 | ||
6bb24f4d | 1256 | # |
2f71caf2 | 1257 | # Given a dataset name determine if it is shared via SMB. |
6bb24f4d | 1258 | # |
2f71caf2 | 1259 | # Returns 0 if shared, 1 otherwise. |
6bb24f4d | 1260 | # |
2f71caf2 | 1261 | function is_shared_smb |
6bb24f4d BB |
1262 | { |
1263 | typeset fs=$1 | |
2f71caf2 | 1264 | |
5b0e75ca | 1265 | datasetexists "$fs" || return |
6bb24f4d BB |
1266 | |
1267 | if is_linux; then | |
dc2fe24c | 1268 | net usershare list | grep -xFq "${fs//[-\/]/_}" |
2f71caf2 | 1269 | else |
5c9f744b | 1270 | log_note "SMB on $UNAME currently unsupported by the test framework" |
6bb24f4d BB |
1271 | return 1 |
1272 | fi | |
2f71caf2 | 1273 | } |
1274 | ||
1275 | # | |
1276 | # Given a mountpoint, determine if it is not shared via NFS. | |
1277 | # | |
1278 | # Returns 0 if not shared, 1 otherwise. | |
1279 | # | |
1280 | function not_shared | |
1281 | { | |
bf228f3d | 1282 | ! is_shared $1 |
6bb24f4d BB |
1283 | } |
1284 | ||
1285 | # | |
2f71caf2 | 1286 | # Given a dataset determine if it is not shared via SMB. |
6bb24f4d | 1287 | # |
2f71caf2 | 1288 | # Returns 0 if not shared, 1 otherwise. |
1289 | # | |
1290 | function not_shared_smb | |
6bb24f4d | 1291 | { |
bf228f3d | 1292 | ! is_shared_smb $1 |
2f71caf2 | 1293 | } |
1294 | ||
1295 | # | |
1296 | # Helper function to unshare a mountpoint. | |
1297 | # | |
1298 | function unshare_fs #fs | |
1299 | { | |
1300 | typeset fs=$1 | |
1301 | ||
bf228f3d | 1302 | if is_shared $fs || is_shared_smb $fs; then |
bd328a58 | 1303 | log_must zfs unshare $fs |
6bb24f4d | 1304 | fi |
6bb24f4d BB |
1305 | } |
1306 | ||
2f71caf2 | 1307 | # |
1308 | # Helper function to share a NFS mountpoint. | |
1309 | # | |
1310 | function share_nfs #fs | |
1311 | { | |
1312 | typeset fs=$1 | |
1313 | ||
5b0e75ca AZ |
1314 | is_shared "$fs" && return |
1315 | ||
5c9f744b | 1316 | case "$UNAME" in |
5b0e75ca AZ |
1317 | Linux) |
1318 | log_must exportfs "*:$fs" | |
1319 | ;; | |
1320 | FreeBSD) | |
1321 | typeset mountd | |
1322 | read -r mountd < /var/run/mountd.pid | |
1323 | log_must eval "printf '%s\t\n' \"$fs\" >> /etc/zfs/exports" | |
1324 | log_must kill -s HUP "$mountd" | |
1325 | ;; | |
1326 | *) | |
1327 | log_must share -F nfs "$fs" | |
1328 | ;; | |
1329 | esac | |
2f71caf2 | 1330 | |
1331 | return 0 | |
1332 | } | |
1333 | ||
1334 | # | |
1335 | # Helper function to unshare a NFS mountpoint. | |
1336 | # | |
1337 | function unshare_nfs #fs | |
1338 | { | |
1339 | typeset fs=$1 | |
1340 | ||
5b0e75ca AZ |
1341 | ! is_shared "$fs" && return |
1342 | ||
5c9f744b | 1343 | case "$UNAME" in |
5b0e75ca AZ |
1344 | Linux) |
1345 | log_must exportfs -u "*:$fs" | |
1346 | ;; | |
1347 | FreeBSD) | |
1348 | typeset mountd | |
1349 | read -r mountd < /var/run/mountd.pid | |
1350 | awk -v fs="${fs//\\/\\\\}" '$1 != fs' /etc/zfs/exports > /etc/zfs/exports.$$ | |
1351 | log_must mv /etc/zfs/exports.$$ /etc/zfs/exports | |
1352 | log_must kill -s HUP "$mountd" | |
1353 | ;; | |
1354 | *) | |
1355 | log_must unshare -F nfs $fs | |
1356 | ;; | |
1357 | esac | |
2f71caf2 | 1358 | |
1359 | return 0 | |
1360 | } | |
1361 | ||
1362 | # | |
1363 | # Helper function to show NFS shares. | |
1364 | # | |
1365 | function showshares_nfs | |
1366 | { | |
5c9f744b | 1367 | case "$UNAME" in |
5b0e75ca AZ |
1368 | Linux) |
1369 | exportfs -v | |
1370 | ;; | |
1371 | FreeBSD) | |
1372 | showmount | |
1373 | ;; | |
1374 | *) | |
c1d9abf9 | 1375 | share -F nfs |
5b0e75ca AZ |
1376 | ;; |
1377 | esac | |
2f71caf2 | 1378 | } |
1379 | ||
c15d36c6 GW |
1380 | function check_nfs |
1381 | { | |
5c9f744b | 1382 | case "$UNAME" in |
5b0e75ca AZ |
1383 | Linux) |
1384 | exportfs -s | |
1385 | ;; | |
1386 | FreeBSD) | |
c15d36c6 | 1387 | showmount -e |
5b0e75ca AZ |
1388 | ;; |
1389 | *) | |
c15d36c6 | 1390 | log_unsupported "Unknown platform" |
5b0e75ca AZ |
1391 | ;; |
1392 | esac || log_unsupported "The NFS utilities are not installed" | |
c15d36c6 GW |
1393 | } |
1394 | ||
6bb24f4d BB |
1395 | # |
1396 | # Check NFS server status and trigger it online. | |
1397 | # | |
1398 | function setup_nfs_server | |
1399 | { | |
1400 | # Cannot share directory in non-global zone. | |
1401 | # | |
1402 | if ! is_global_zone; then | |
1403 | log_note "Cannot trigger NFS server by sharing in LZ." | |
1404 | return | |
1405 | fi | |
1406 | ||
c15d36c6 | 1407 | if is_linux; then |
2a0428f1 BB |
1408 | # |
1409 | # Re-synchronize /var/lib/nfs/etab with /etc/exports and | |
1410 | # /etc/exports.d./* to provide a clean test environment. | |
1411 | # | |
5b0e75ca | 1412 | log_must exportfs -r |
2a0428f1 | 1413 | |
c15d36c6 GW |
1414 | log_note "NFS server must be started prior to running ZTS." |
1415 | return | |
1416 | elif is_freebsd; then | |
5b0e75ca | 1417 | log_must kill -s HUP $(</var/run/mountd.pid) |
c15d36c6 | 1418 | |
2a0428f1 | 1419 | log_note "NFS server must be started prior to running ZTS." |
6bb24f4d BB |
1420 | return |
1421 | fi | |
1422 | ||
1423 | typeset nfs_fmri="svc:/network/nfs/server:default" | |
c1d9abf9 | 1424 | if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then |
6bb24f4d BB |
1425 | # |
1426 | # Only really sharing operation can enable NFS server | |
1427 | # to online permanently. | |
1428 | # | |
1429 | typeset dummy=/tmp/dummy | |
1430 | ||
1431 | if [[ -d $dummy ]]; then | |
c1d9abf9 | 1432 | log_must rm -rf $dummy |
6bb24f4d BB |
1433 | fi |
1434 | ||
c1d9abf9 JWK |
1435 | log_must mkdir $dummy |
1436 | log_must share $dummy | |
6bb24f4d BB |
1437 | |
1438 | # | |
1439 | # Waiting for fmri's status to be the final status. | |
1440 | # Otherwise, in transition, an asterisk (*) is appended for | |
1441 | # instances, unshare will reverse status to 'DIS' again. | |
1442 | # | |
1443 | # Waiting for 1's at least. | |
1444 | # | |
c1d9abf9 | 1445 | log_must sleep 1 |
6bb24f4d | 1446 | timeout=10 |
c1d9abf9 | 1447 | while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]] |
6bb24f4d | 1448 | do |
c1d9abf9 | 1449 | log_must sleep 1 |
6bb24f4d BB |
1450 | |
1451 | ((timeout -= 1)) | |
1452 | done | |
1453 | ||
c1d9abf9 JWK |
1454 | log_must unshare $dummy |
1455 | log_must rm -rf $dummy | |
6bb24f4d BB |
1456 | fi |
1457 | ||
c1d9abf9 | 1458 | log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'" |
6bb24f4d BB |
1459 | } |
1460 | ||
1461 | # | |
1462 | # To verify whether calling process is in global zone | |
1463 | # | |
1464 | # Return 0 if in global zone, 1 in non-global zone | |
1465 | # | |
1466 | function is_global_zone | |
1467 | { | |
7839c4b5 | 1468 | if is_linux || is_freebsd; then |
c1d9abf9 JWK |
1469 | return 0 |
1470 | else | |
1471 | typeset cur_zone=$(zonename 2>/dev/null) | |
bf228f3d | 1472 | [ $cur_zone = "global" ] |
6bb24f4d | 1473 | fi |
6bb24f4d BB |
1474 | } |
1475 | ||
1476 | # | |
1477 | # Verify whether test is permitted to run from | |
1478 | # global zone, local zone, or both | |
1479 | # | |
1480 | # $1 zone limit, could be "global", "local", or "both"(no limit) | |
1481 | # | |
1482 | # Return 0 if permitted, otherwise exit with log_unsupported | |
1483 | # | |
1484 | function verify_runnable # zone limit | |
1485 | { | |
1486 | typeset limit=$1 | |
1487 | ||
1488 | [[ -z $limit ]] && return 0 | |
1489 | ||
1490 | if is_global_zone ; then | |
1491 | case $limit in | |
1492 | global|both) | |
1493 | ;; | |
1494 | local) log_unsupported "Test is unable to run from "\ | |
1495 | "global zone." | |
1496 | ;; | |
1497 | *) log_note "Warning: unknown limit $limit - " \ | |
1498 | "use both." | |
1499 | ;; | |
1500 | esac | |
1501 | else | |
1502 | case $limit in | |
1503 | local|both) | |
1504 | ;; | |
1505 | global) log_unsupported "Test is unable to run from "\ | |
1506 | "local zone." | |
1507 | ;; | |
1508 | *) log_note "Warning: unknown limit $limit - " \ | |
1509 | "use both." | |
1510 | ;; | |
1511 | esac | |
1512 | ||
1513 | reexport_pool | |
1514 | fi | |
1515 | ||
1516 | return 0 | |
1517 | } | |
1518 | ||
1519 | # Return 0 if create successfully or the pool exists; $? otherwise | |
1520 | # Note: In local zones, this function should return 0 silently. | |
1521 | # | |
1522 | # $1 - pool name | |
1523 | # $2-n - [keyword] devs_list | |
1524 | ||
1525 | function create_pool #pool devs_list | |
1526 | { | |
1527 | typeset pool=${1%%/*} | |
1528 | ||
1529 | shift | |
1530 | ||
1531 | if [[ -z $pool ]]; then | |
1532 | log_note "Missing pool name." | |
1533 | return 1 | |
1534 | fi | |
1535 | ||
1536 | if poolexists $pool ; then | |
1537 | destroy_pool $pool | |
1538 | fi | |
1539 | ||
1540 | if is_global_zone ; then | |
c1d9abf9 JWK |
1541 | [[ -d /$pool ]] && rm -rf /$pool |
1542 | log_must zpool create -f $pool $@ | |
6bb24f4d BB |
1543 | fi |
1544 | ||
1545 | return 0 | |
1546 | } | |
1547 | ||
1548 | # Return 0 if destroy successfully or the pool exists; $? otherwise | |
1549 | # Note: In local zones, this function should return 0 silently. | |
1550 | # | |
1551 | # $1 - pool name | |
1552 | # Destroy pool with the given parameters. | |
1553 | ||
1554 | function destroy_pool #pool | |
1555 | { | |
1556 | typeset pool=${1%%/*} | |
1557 | typeset mtpt | |
1558 | ||
1559 | if [[ -z $pool ]]; then | |
1560 | log_note "No pool name given." | |
1561 | return 1 | |
1562 | fi | |
1563 | ||
1564 | if is_global_zone ; then | |
1565 | if poolexists "$pool" ; then | |
1566 | mtpt=$(get_prop mountpoint "$pool") | |
1567 | ||
851aa99c BB |
1568 | # At times, syseventd/udev activity can cause attempts |
1569 | # to destroy a pool to fail with EBUSY. We retry a few | |
6bb24f4d BB |
1570 | # times allowing failures before requiring the destroy |
1571 | # to succeed. | |
851aa99c | 1572 | log_must_busy zpool destroy -f $pool |
6bb24f4d BB |
1573 | |
1574 | [[ -d $mtpt ]] && \ | |
c1d9abf9 | 1575 | log_must rm -rf $mtpt |
6bb24f4d BB |
1576 | else |
1577 | log_note "Pool does not exist. ($pool)" | |
1578 | return 1 | |
1579 | fi | |
1580 | fi | |
1581 | ||
1582 | return 0 | |
1583 | } | |
1584 | ||
93491c4b JWK |
1585 | # Return 0 if created successfully; $? otherwise |
1586 | # | |
1587 | # $1 - dataset name | |
1588 | # $2-n - dataset options | |
1589 | ||
1590 | function create_dataset #dataset dataset_options | |
1591 | { | |
1592 | typeset dataset=$1 | |
1593 | ||
1594 | shift | |
1595 | ||
1596 | if [[ -z $dataset ]]; then | |
1597 | log_note "Missing dataset name." | |
1598 | return 1 | |
1599 | fi | |
1600 | ||
1601 | if datasetexists $dataset ; then | |
1602 | destroy_dataset $dataset | |
1603 | fi | |
1604 | ||
1605 | log_must zfs create $@ $dataset | |
1606 | ||
1607 | return 0 | |
1608 | } | |
1609 | ||
c7b55e71 GDN |
1610 | # Return 0 if destroy successfully or the dataset exists; $? otherwise |
1611 | # Note: In local zones, this function should return 0 silently. | |
1612 | # | |
1613 | # $1 - dataset name | |
1614 | # $2 - custom arguments for zfs destroy | |
1615 | # Destroy dataset with the given parameters. | |
1616 | ||
41ebf403 | 1617 | function destroy_dataset # dataset [args] |
c7b55e71 GDN |
1618 | { |
1619 | typeset dataset=$1 | |
1620 | typeset mtpt | |
1621 | typeset args=${2:-""} | |
1622 | ||
1623 | if [[ -z $dataset ]]; then | |
1624 | log_note "No dataset name given." | |
1625 | return 1 | |
1626 | fi | |
1627 | ||
1628 | if is_global_zone ; then | |
1629 | if datasetexists "$dataset" ; then | |
1630 | mtpt=$(get_prop mountpoint "$dataset") | |
1631 | log_must_busy zfs destroy $args $dataset | |
1632 | ||
41ebf403 | 1633 | [ -d $mtpt ] && log_must rm -rf $mtpt |
c7b55e71 GDN |
1634 | else |
1635 | log_note "Dataset does not exist. ($dataset)" | |
1636 | return 1 | |
1637 | fi | |
1638 | fi | |
1639 | ||
1640 | return 0 | |
1641 | } | |
1642 | ||
6bb24f4d BB |
1643 | # |
1644 | # Reexport TESTPOOL & TESTPOOL(1-4) | |
1645 | # | |
1646 | function reexport_pool | |
1647 | { | |
1648 | typeset -i cntctr=5 | |
1649 | typeset -i i=0 | |
1650 | ||
1651 | while ((i < cntctr)); do | |
1652 | if ((i == 0)); then | |
1653 | TESTPOOL=$ZONE_POOL/$ZONE_CTR$i | |
1654 | if ! ismounted $TESTPOOL; then | |
c1d9abf9 | 1655 | log_must zfs mount $TESTPOOL |
6bb24f4d BB |
1656 | fi |
1657 | else | |
1658 | eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i | |
1659 | if eval ! ismounted \$TESTPOOL$i; then | |
c1d9abf9 | 1660 | log_must eval zfs mount \$TESTPOOL$i |
6bb24f4d BB |
1661 | fi |
1662 | fi | |
1663 | ((i += 1)) | |
1664 | done | |
1665 | } | |
1666 | ||
1667 | # | |
ec0e24c2 | 1668 | # Verify a given disk or pool state |
6bb24f4d BB |
1669 | # |
1670 | # Return 0 is pool/disk matches expected state, 1 otherwise | |
1671 | # | |
ec0e24c2 | 1672 | function check_state # pool disk state{online,offline,degraded} |
6bb24f4d BB |
1673 | { |
1674 | typeset pool=$1 | |
1675 | typeset disk=${2#$DEV_DSKDIR/} | |
1676 | typeset state=$3 | |
1677 | ||
ec0e24c2 SV |
1678 | [[ -z $pool ]] || [[ -z $state ]] \ |
1679 | && log_fail "Arguments invalid or missing" | |
1680 | ||
1681 | if [[ -z $disk ]]; then | |
1682 | #check pool state only | |
23914a3b | 1683 | zpool get -H -o value health $pool | grep -qi "$state" |
ec0e24c2 | 1684 | else |
23914a3b | 1685 | zpool status -v $pool | grep "$disk" | grep -qi "$state" |
ec0e24c2 | 1686 | fi |
6bb24f4d BB |
1687 | } |
1688 | ||
1689 | # | |
1690 | # Get the mountpoint of snapshot | |
1691 | # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap> | |
1692 | # as its mountpoint | |
1693 | # | |
1694 | function snapshot_mountpoint | |
1695 | { | |
1696 | typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP} | |
1697 | ||
1698 | if [[ $dataset != *@* ]]; then | |
1699 | log_fail "Error name of snapshot '$dataset'." | |
1700 | fi | |
1701 | ||
1702 | typeset fs=${dataset%@*} | |
1703 | typeset snap=${dataset#*@} | |
1704 | ||
1705 | if [[ -z $fs || -z $snap ]]; then | |
1706 | log_fail "Error name of snapshot '$dataset'." | |
1707 | fi | |
1708 | ||
c1d9abf9 | 1709 | echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap |
6bb24f4d BB |
1710 | } |
1711 | ||
dddef7d6 | 1712 | # |
1713 | # Given a device and 'ashift' value verify it's correctly set on every label | |
1714 | # | |
1715 | function verify_ashift # device ashift | |
1716 | { | |
1717 | typeset device="$1" | |
1718 | typeset ashift="$2" | |
1719 | ||
75746e9a AZ |
1720 | zdb -e -lll $device | awk -v ashift=$ashift ' |
1721 | /ashift: / { | |
1722 | if (ashift != $2) | |
1723 | exit 1; | |
1724 | else | |
1725 | count++; | |
1726 | } | |
1727 | END { | |
1728 | exit (count != 4); | |
dddef7d6 | 1729 | }' |
dddef7d6 | 1730 | } |
1731 | ||
6bb24f4d BB |
1732 | # |
1733 | # Given a pool and file system, this function will verify the file system | |
1734 | # using the zdb internal tool. Note that the pool is exported and imported | |
1735 | # to ensure it has consistent state. | |
1736 | # | |
1737 | function verify_filesys # pool filesystem dir | |
1738 | { | |
1739 | typeset pool="$1" | |
1740 | typeset filesys="$2" | |
1741 | typeset zdbout="/tmp/zdbout.$$" | |
1742 | ||
1743 | shift | |
1744 | shift | |
1745 | typeset dirs=$@ | |
1746 | typeset search_path="" | |
1747 | ||
c1d9abf9 JWK |
1748 | log_note "Calling zdb to verify filesystem '$filesys'" |
1749 | zfs unmount -a > /dev/null 2>&1 | |
1750 | log_must zpool export $pool | |
6bb24f4d BB |
1751 | |
1752 | if [[ -n $dirs ]] ; then | |
1753 | for dir in $dirs ; do | |
1754 | search_path="$search_path -d $dir" | |
1755 | done | |
1756 | fi | |
1757 | ||
c1d9abf9 | 1758 | log_must zpool import $search_path $pool |
6bb24f4d | 1759 | |
23914a3b | 1760 | if ! zdb -cudi $filesys > $zdbout 2>&1; then |
c1d9abf9 JWK |
1761 | log_note "Output: zdb -cudi $filesys" |
1762 | cat $zdbout | |
23914a3b | 1763 | rm -f $zdbout |
c1d9abf9 | 1764 | log_fail "zdb detected errors with: '$filesys'" |
6bb24f4d BB |
1765 | fi |
1766 | ||
c1d9abf9 JWK |
1767 | log_must zfs mount -a |
1768 | log_must rm -rf $zdbout | |
6bb24f4d BB |
1769 | } |
1770 | ||
7c9a4292 BB |
1771 | # |
1772 | # Given a pool issue a scrub and verify that no checksum errors are reported. | |
1773 | # | |
1774 | function verify_pool | |
1775 | { | |
1776 | typeset pool=${1:-$TESTPOOL} | |
1777 | ||
1778 | log_must zpool scrub $pool | |
1779 | log_must wait_scrubbed $pool | |
1780 | ||
6ed4391d RM |
1781 | typeset -i cksum=$(zpool status $pool | awk ' |
1782 | !NF { isvdev = 0 } | |
1783 | isvdev { errors += $NF } | |
1784 | /CKSUM$/ { isvdev = 1 } | |
1785 | END { print errors } | |
1786 | ') | |
7c9a4292 BB |
1787 | if [[ $cksum != 0 ]]; then |
1788 | log_must zpool status -v | |
1789 | log_fail "Unexpected CKSUM errors found on $pool ($cksum)" | |
1790 | fi | |
1791 | } | |
1792 | ||
6bb24f4d BB |
1793 | # |
1794 | # Given a pool, and this function list all disks in the pool | |
1795 | # | |
1796 | function get_disklist # pool | |
1797 | { | |
f63c9dc7 AZ |
1798 | echo $(zpool iostat -v $1 | awk '(NR > 4) {print $1}' | \ |
1799 | grep -vEe '^-----' -e "^(mirror|raidz[1-3]|draid[1-3]|spare|log|cache|special|dedup)|\-[0-9]$") | |
6bb24f4d BB |
1800 | } |
1801 | ||
3c67d83a TH |
1802 | # |
1803 | # Given a pool, and this function list all disks in the pool with their full | |
1804 | # path (like "/dev/sda" instead of "sda"). | |
1805 | # | |
1806 | function get_disklist_fullpath # pool | |
1807 | { | |
270d0a5a | 1808 | get_disklist "-P $1" |
3c67d83a TH |
1809 | } |
1810 | ||
1811 | ||
1812 | ||
6bb24f4d BB |
1813 | # /** |
1814 | # This function kills a given list of processes after a time period. We use | |
1815 | # this in the stress tests instead of STF_TIMEOUT so that we can have processes | |
1816 | # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT | |
1817 | # would be listed as FAIL, which we don't want : we're happy with stress tests | |
1818 | # running for a certain amount of time, then finishing. | |
1819 | # | |
1820 | # @param $1 the time in seconds after which we should terminate these processes | |
1821 | # @param $2..$n the processes we wish to terminate. | |
1822 | # */ | |
1823 | function stress_timeout | |
1824 | { | |
1825 | typeset -i TIMEOUT=$1 | |
1826 | shift | |
1827 | typeset cpids="$@" | |
1828 | ||
1829 | log_note "Waiting for child processes($cpids). " \ | |
1830 | "It could last dozens of minutes, please be patient ..." | |
c1d9abf9 | 1831 | log_must sleep $TIMEOUT |
6bb24f4d BB |
1832 | |
1833 | log_note "Killing child processes after ${TIMEOUT} stress timeout." | |
1834 | typeset pid | |
1835 | for pid in $cpids; do | |
23914a3b | 1836 | ps -p $pid > /dev/null 2>&1 && |
c1d9abf9 | 1837 | log_must kill -USR1 $pid |
6bb24f4d BB |
1838 | done |
1839 | } | |
1840 | ||
1841 | # | |
1842 | # Verify a given hotspare disk is inuse or avail | |
1843 | # | |
1844 | # Return 0 is pool/disk matches expected state, 1 otherwise | |
1845 | # | |
1846 | function check_hotspare_state # pool disk state{inuse,avail} | |
1847 | { | |
1848 | typeset pool=$1 | |
1849 | typeset disk=${2#$DEV_DSKDIR/} | |
1850 | typeset state=$3 | |
1851 | ||
1852 | cur_state=$(get_device_state $pool $disk "spares") | |
1853 | ||
bd328a58 | 1854 | [ $state = $cur_state ] |
6bb24f4d BB |
1855 | } |
1856 | ||
d9daa7ab DQ |
1857 | # |
1858 | # Wait until a hotspare transitions to a given state or times out. | |
1859 | # | |
1860 | # Return 0 when pool/disk matches expected state, 1 on timeout. | |
1861 | # | |
1862 | function wait_hotspare_state # pool disk state timeout | |
1863 | { | |
1864 | typeset pool=$1 | |
6fa1e1e7 | 1865 | typeset disk=${2#*$DEV_DSKDIR/} |
d9daa7ab DQ |
1866 | typeset state=$3 |
1867 | typeset timeout=${4:-60} | |
1868 | typeset -i i=0 | |
1869 | ||
1870 | while [[ $i -lt $timeout ]]; do | |
1871 | if check_hotspare_state $pool $disk $state; then | |
1872 | return 0 | |
1873 | fi | |
1874 | ||
1875 | i=$((i+1)) | |
1876 | sleep 1 | |
1877 | done | |
1878 | ||
1879 | return 1 | |
1880 | } | |
1881 | ||
6bb24f4d BB |
1882 | # |
1883 | # Verify a given vdev disk is inuse or avail | |
1884 | # | |
1885 | # Return 0 is pool/disk matches expected state, 1 otherwise | |
1886 | # | |
55c12724 | 1887 | function check_vdev_state # pool disk state{online,offline,unavail,removed} |
6bb24f4d BB |
1888 | { |
1889 | typeset pool=$1 | |
6fa1e1e7 | 1890 | typeset disk=${2#*$DEV_DSKDIR/} |
6bb24f4d BB |
1891 | typeset state=$3 |
1892 | ||
1893 | cur_state=$(get_device_state $pool $disk) | |
1894 | ||
bd328a58 | 1895 | [ $state = $cur_state ] |
6bb24f4d BB |
1896 | } |
1897 | ||
d9daa7ab DQ |
1898 | # |
1899 | # Wait until a vdev transitions to a given state or times out. | |
1900 | # | |
1901 | # Return 0 when pool/disk matches expected state, 1 on timeout. | |
1902 | # | |
1903 | function wait_vdev_state # pool disk state timeout | |
1904 | { | |
1905 | typeset pool=$1 | |
6fa1e1e7 | 1906 | typeset disk=${2#*$DEV_DSKDIR/} |
d9daa7ab DQ |
1907 | typeset state=$3 |
1908 | typeset timeout=${4:-60} | |
1909 | typeset -i i=0 | |
1910 | ||
1911 | while [[ $i -lt $timeout ]]; do | |
1912 | if check_vdev_state $pool $disk $state; then | |
1913 | return 0 | |
1914 | fi | |
1915 | ||
1916 | i=$((i+1)) | |
1917 | sleep 1 | |
1918 | done | |
1919 | ||
1920 | return 1 | |
1921 | } | |
1922 | ||
6bb24f4d BB |
1923 | # |
1924 | # Check the output of 'zpool status -v <pool>', | |
1925 | # and to see if the content of <token> contain the <keyword> specified. | |
1926 | # | |
1927 | # Return 0 is contain, 1 otherwise | |
1928 | # | |
0ea05c64 | 1929 | function check_pool_status # pool token keyword <verbose> |
6bb24f4d BB |
1930 | { |
1931 | typeset pool=$1 | |
1932 | typeset token=$2 | |
1933 | typeset keyword=$3 | |
0ea05c64 | 1934 | typeset verbose=${4:-false} |
6bb24f4d | 1935 | |
964d4180 | 1936 | scan=$(zpool status -v "$pool" 2>/dev/null | awk -v token="$token:" '$1==token') |
0ea05c64 AP |
1937 | if [[ $verbose == true ]]; then |
1938 | log_note $scan | |
1939 | fi | |
f63c9dc7 | 1940 | echo $scan | grep -qi "$keyword" |
6bb24f4d BB |
1941 | } |
1942 | ||
1943 | # | |
e60e158e | 1944 | # The following functions are instance of check_pool_status() |
9a49d3f3 BB |
1945 | # is_pool_resilvering - to check if the pool resilver is in progress |
1946 | # is_pool_resilvered - to check if the pool resilver is completed | |
1947 | # is_pool_scrubbing - to check if the pool scrub is in progress | |
1948 | # is_pool_scrubbed - to check if the pool scrub is completed | |
1949 | # is_pool_scrub_stopped - to check if the pool scrub is stopped | |
1950 | # is_pool_scrub_paused - to check if the pool scrub has paused | |
1951 | # is_pool_removing - to check if the pool removing is a vdev | |
1952 | # is_pool_removed - to check if the pool remove is completed | |
1953 | # is_pool_discarding - to check if the pool checkpoint is being discarded | |
c8b3dda1 | 1954 | # is_pool_replacing - to check if the pool is performing a replacement |
6bb24f4d | 1955 | # |
0ea05c64 AP |
1956 | function is_pool_resilvering #pool <verbose> |
1957 | { | |
9a49d3f3 | 1958 | check_pool_status "$1" "scan" \ |
b2255edc | 1959 | "resilver[ ()0-9A-Za-z:_-]* in progress since" $2 |
0ea05c64 AP |
1960 | } |
1961 | ||
1962 | function is_pool_resilvered #pool <verbose> | |
6bb24f4d | 1963 | { |
0ea05c64 | 1964 | check_pool_status "$1" "scan" "resilvered " $2 |
6bb24f4d BB |
1965 | } |
1966 | ||
0ea05c64 | 1967 | function is_pool_scrubbing #pool <verbose> |
6bb24f4d | 1968 | { |
0ea05c64 | 1969 | check_pool_status "$1" "scan" "scrub in progress since " $2 |
6bb24f4d BB |
1970 | } |
1971 | ||
0ea05c64 | 1972 | function is_pool_scrubbed #pool <verbose> |
6bb24f4d | 1973 | { |
0ea05c64 | 1974 | check_pool_status "$1" "scan" "scrub repaired" $2 |
6bb24f4d BB |
1975 | } |
1976 | ||
0ea05c64 | 1977 | function is_pool_scrub_stopped #pool <verbose> |
6bb24f4d | 1978 | { |
0ea05c64 | 1979 | check_pool_status "$1" "scan" "scrub canceled" $2 |
6bb24f4d BB |
1980 | } |
1981 | ||
0ea05c64 | 1982 | function is_pool_scrub_paused #pool <verbose> |
6bb24f4d | 1983 | { |
0ea05c64 | 1984 | check_pool_status "$1" "scan" "scrub paused since " $2 |
6bb24f4d BB |
1985 | } |
1986 | ||
a1d477c2 MA |
1987 | function is_pool_removing #pool |
1988 | { | |
1989 | check_pool_status "$1" "remove" "in progress since " | |
a1d477c2 MA |
1990 | } |
1991 | ||
1992 | function is_pool_removed #pool | |
1993 | { | |
1994 | check_pool_status "$1" "remove" "completed on" | |
a1d477c2 MA |
1995 | } |
1996 | ||
e60e158e JG |
1997 | function is_pool_discarding #pool |
1998 | { | |
1999 | check_pool_status "$1" "checkpoint" "discarding" | |
e60e158e | 2000 | } |
c8b3dda1 BB |
2001 | function is_pool_replacing #pool |
2002 | { | |
2003 | zpool status "$1" | grep -qE 'replacing-[0-9]+' | |
2004 | } | |
e60e158e | 2005 | |
ab44e511 JWK |
2006 | function wait_for_degraded |
2007 | { | |
2008 | typeset pool=$1 | |
2009 | typeset timeout=${2:-30} | |
2010 | typeset t0=$SECONDS | |
2011 | ||
2012 | while :; do | |
2013 | [[ $(get_pool_prop health $pool) == "DEGRADED" ]] && break | |
2014 | log_note "$pool is not yet degraded." | |
2015 | sleep 1 | |
2016 | if ((SECONDS - t0 > $timeout)); then | |
2017 | log_note "$pool not degraded after $timeout seconds." | |
2018 | return 1 | |
2019 | fi | |
2020 | done | |
2021 | ||
2022 | return 0 | |
2023 | } | |
2024 | ||
6bb24f4d | 2025 | # |
4e33ba4c | 2026 | # Use create_pool()/destroy_pool() to clean up the information in |
6bb24f4d BB |
2027 | # in the given disk to avoid slice overlapping. |
2028 | # | |
2029 | function cleanup_devices #vdevs | |
2030 | { | |
2031 | typeset pool="foopool$$" | |
2032 | ||
581ca281 BB |
2033 | for vdev in $@; do |
2034 | zero_partitions $vdev | |
2035 | done | |
6bb24f4d | 2036 | |
581ca281 | 2037 | poolexists $pool && destroy_pool $pool |
6bb24f4d BB |
2038 | create_pool $pool $@ |
2039 | destroy_pool $pool | |
2040 | ||
2041 | return 0 | |
2042 | } | |
2043 | ||
6bb24f4d BB |
2044 | #/** |
2045 | # A function to find and locate free disks on a system or from given | |
2046 | # disks as the parameter. It works by locating disks that are in use | |
2047 | # as swap devices and dump devices, and also disks listed in /etc/vfstab | |
2048 | # | |
2049 | # $@ given disks to find which are free, default is all disks in | |
2050 | # the test system | |
2051 | # | |
2052 | # @return a string containing the list of available disks | |
2053 | #*/ | |
2054 | function find_disks | |
2055 | { | |
2056 | # Trust provided list, no attempt is made to locate unused devices. | |
7839c4b5 | 2057 | if is_linux || is_freebsd; then |
c1d9abf9 | 2058 | echo "$@" |
6bb24f4d BB |
2059 | return |
2060 | fi | |
2061 | ||
2062 | ||
2063 | sfi=/tmp/swaplist.$$ | |
2064 | dmpi=/tmp/dumpdev.$$ | |
2065 | max_finddisksnum=${MAX_FINDDISKSNUM:-6} | |
2066 | ||
c1d9abf9 JWK |
2067 | swap -l > $sfi |
2068 | dumpadm > $dmpi 2>/dev/null | |
6bb24f4d | 2069 | |
75746e9a AZ |
2070 | disks=${@:-$(echo "" | format -e 2>/dev/null | awk ' |
2071 | BEGIN { FS="."; } | |
6bb24f4d | 2072 | |
75746e9a AZ |
2073 | /^Specify disk/{ |
2074 | searchdisks=0; | |
2075 | } | |
6bb24f4d | 2076 | |
75746e9a AZ |
2077 | { |
2078 | if (searchdisks && $2 !~ "^$"){ | |
2079 | split($2,arr," "); | |
2080 | print arr[1]; | |
6bb24f4d | 2081 | } |
75746e9a | 2082 | } |
6bb24f4d | 2083 | |
75746e9a AZ |
2084 | /^AVAILABLE DISK SELECTIONS:/{ |
2085 | searchdisks=1; | |
2086 | } | |
2087 | ')} | |
6bb24f4d BB |
2088 | |
2089 | unused="" | |
2090 | for disk in $disks; do | |
2091 | # Check for mounted | |
23914a3b | 2092 | grep -q "${disk}[sp]" /etc/mnttab && continue |
6bb24f4d | 2093 | # Check for swap |
23914a3b | 2094 | grep -q "${disk}[sp]" $sfi && continue |
6bb24f4d | 2095 | # check for dump device |
23914a3b | 2096 | grep -q "${disk}[sp]" $dmpi && continue |
6bb24f4d BB |
2097 | # check to see if this disk hasn't been explicitly excluded |
2098 | # by a user-set environment variable | |
23914a3b | 2099 | echo "${ZFS_HOST_DEVICES_IGNORE}" | grep -q "${disk}" && continue |
6bb24f4d BB |
2100 | unused_candidates="$unused_candidates $disk" |
2101 | done | |
23914a3b | 2102 | rm $sfi $dmpi |
6bb24f4d BB |
2103 | |
2104 | # now just check to see if those disks do actually exist | |
2105 | # by looking for a device pointing to the first slice in | |
2106 | # each case. limit the number to max_finddisksnum | |
2107 | count=0 | |
2108 | for disk in $unused_candidates; do | |
665684d7 RM |
2109 | if is_disk_device $DEV_DSKDIR/${disk}s0 && \ |
2110 | [ $count -lt $max_finddisksnum ]; then | |
6bb24f4d BB |
2111 | unused="$unused $disk" |
2112 | # do not impose limit if $@ is provided | |
2113 | [[ -z $@ ]] && ((count = count + 1)) | |
2114 | fi | |
6bb24f4d BB |
2115 | done |
2116 | ||
2117 | # finally, return our disk list | |
c1d9abf9 | 2118 | echo $unused |
6bb24f4d BB |
2119 | } |
2120 | ||
7839c4b5 MM |
2121 | function add_user_freebsd #<group_name> <user_name> <basedir> |
2122 | { | |
2123 | typeset group=$1 | |
2124 | typeset user=$2 | |
2125 | typeset basedir=$3 | |
2126 | ||
2127 | # Check to see if the user exists. | |
2128 | if id $user > /dev/null 2>&1; then | |
2129 | return 0 | |
2130 | fi | |
2131 | ||
2132 | # Assign 1000 as the base uid | |
2133 | typeset -i uid=1000 | |
2134 | while true; do | |
7839c4b5 | 2135 | pw useradd -u $uid -g $group -d $basedir/$user -m -n $user |
23914a3b | 2136 | case $? in |
7839c4b5 MM |
2137 | 0) break ;; |
2138 | # The uid is not unique | |
2139 | 65) ((uid += 1)) ;; | |
2140 | *) return 1 ;; | |
2141 | esac | |
2142 | if [[ $uid == 65000 ]]; then | |
2143 | log_fail "No user id available under 65000 for $user" | |
2144 | fi | |
2145 | done | |
2146 | ||
2147 | # Silence MOTD | |
2148 | touch $basedir/$user/.hushlogin | |
2149 | ||
2150 | return 0 | |
2151 | } | |
2152 | ||
2153 | # | |
2154 | # Delete the specified user. | |
2155 | # | |
2156 | # $1 login name | |
2157 | # | |
2158 | function del_user_freebsd #<logname> | |
2159 | { | |
2160 | typeset user=$1 | |
2161 | ||
2162 | if id $user > /dev/null 2>&1; then | |
2163 | log_must pw userdel $user | |
2164 | fi | |
2165 | ||
2166 | return 0 | |
2167 | } | |
2168 | ||
2169 | # | |
2170 | # Select valid gid and create specified group. | |
2171 | # | |
2172 | # $1 group name | |
2173 | # | |
2174 | function add_group_freebsd #<group_name> | |
2175 | { | |
2176 | typeset group=$1 | |
2177 | ||
2178 | # See if the group already exists. | |
2179 | if pw groupshow $group >/dev/null 2>&1; then | |
2180 | return 0 | |
2181 | fi | |
2182 | ||
2183 | # Assign 1000 as the base gid | |
2184 | typeset -i gid=1000 | |
2185 | while true; do | |
2186 | pw groupadd -g $gid -n $group > /dev/null 2>&1 | |
23914a3b | 2187 | case $? in |
7839c4b5 MM |
2188 | 0) return 0 ;; |
2189 | # The gid is not unique | |
2190 | 65) ((gid += 1)) ;; | |
2191 | *) return 1 ;; | |
2192 | esac | |
2193 | if [[ $gid == 65000 ]]; then | |
2194 | log_fail "No user id available under 65000 for $group" | |
2195 | fi | |
2196 | done | |
2197 | } | |
2198 | ||
2199 | # | |
2200 | # Delete the specified group. | |
2201 | # | |
2202 | # $1 group name | |
2203 | # | |
2204 | function del_group_freebsd #<group_name> | |
2205 | { | |
2206 | typeset group=$1 | |
2207 | ||
2208 | pw groupdel -n $group > /dev/null 2>&1 | |
23914a3b | 2209 | case $? in |
7839c4b5 MM |
2210 | # Group does not exist, or was deleted successfully. |
2211 | 0|6|65) return 0 ;; | |
2212 | # Name already exists as a group name | |
2213 | 9) log_must pw groupdel $group ;; | |
2214 | *) return 1 ;; | |
2215 | esac | |
2216 | ||
2217 | return 0 | |
2218 | } | |
2219 | ||
2220 | function add_user_illumos #<group_name> <user_name> <basedir> | |
2221 | { | |
2222 | typeset group=$1 | |
2223 | typeset user=$2 | |
2224 | typeset basedir=$3 | |
2225 | ||
2226 | log_must useradd -g $group -d $basedir/$user -m $user | |
2227 | ||
2228 | return 0 | |
2229 | } | |
2230 | ||
2231 | function del_user_illumos #<user_name> | |
2232 | { | |
2233 | typeset user=$1 | |
2234 | ||
2235 | if id $user > /dev/null 2>&1; then | |
2236 | log_must_retry "currently used" 6 userdel $user | |
2237 | fi | |
2238 | ||
2239 | return 0 | |
2240 | } | |
2241 | ||
2242 | function add_group_illumos #<group_name> | |
2243 | { | |
2244 | typeset group=$1 | |
2245 | ||
2246 | typeset -i gid=100 | |
2247 | while true; do | |
2248 | groupadd -g $gid $group > /dev/null 2>&1 | |
23914a3b | 2249 | case $? in |
7839c4b5 MM |
2250 | 0) return 0 ;; |
2251 | # The gid is not unique | |
2252 | 4) ((gid += 1)) ;; | |
2253 | *) return 1 ;; | |
2254 | esac | |
2255 | done | |
2256 | } | |
2257 | ||
2258 | function del_group_illumos #<group_name> | |
2259 | { | |
2260 | typeset group=$1 | |
2261 | ||
2262 | groupmod -n $grp $grp > /dev/null 2>&1 | |
23914a3b | 2263 | case $? in |
7839c4b5 MM |
2264 | # Group does not exist. |
2265 | 6) return 0 ;; | |
2266 | # Name already exists as a group name | |
2267 | 9) log_must groupdel $grp ;; | |
2268 | *) return 1 ;; | |
2269 | esac | |
2270 | } | |
2271 | ||
2272 | function add_user_linux #<group_name> <user_name> <basedir> | |
2273 | { | |
2274 | typeset group=$1 | |
2275 | typeset user=$2 | |
2276 | typeset basedir=$3 | |
2277 | ||
2278 | log_must useradd -g $group -d $basedir/$user -m $user | |
2279 | ||
2280 | # Add new users to the same group and the command line utils. | |
2281 | # This allows them to be run out of the original users home | |
2282 | # directory as long as it permissioned to be group readable. | |
592cf7f1 | 2283 | cmd_group=$(stat --format="%G" $(command -v zfs)) |
7839c4b5 MM |
2284 | log_must usermod -a -G $cmd_group $user |
2285 | ||
2286 | return 0 | |
2287 | } | |
2288 | ||
2289 | function del_user_linux #<user_name> | |
2290 | { | |
2291 | typeset user=$1 | |
2292 | ||
2293 | if id $user > /dev/null 2>&1; then | |
2294 | log_must_retry "currently used" 6 userdel $user | |
2295 | fi | |
7839c4b5 MM |
2296 | } |
2297 | ||
2298 | function add_group_linux #<group_name> | |
2299 | { | |
2300 | typeset group=$1 | |
2301 | ||
2302 | # Assign 100 as the base gid, a larger value is selected for | |
2303 | # Linux because for many distributions 1000 and under are reserved. | |
2304 | while true; do | |
2305 | groupadd $group > /dev/null 2>&1 | |
23914a3b | 2306 | case $? in |
7839c4b5 MM |
2307 | 0) return 0 ;; |
2308 | *) return 1 ;; | |
2309 | esac | |
2310 | done | |
2311 | } | |
2312 | ||
2313 | function del_group_linux #<group_name> | |
2314 | { | |
2315 | typeset group=$1 | |
2316 | ||
2317 | getent group $group > /dev/null 2>&1 | |
23914a3b | 2318 | case $? in |
7839c4b5 MM |
2319 | # Group does not exist. |
2320 | 2) return 0 ;; | |
2321 | # Name already exists as a group name | |
2322 | 0) log_must groupdel $group ;; | |
2323 | *) return 1 ;; | |
2324 | esac | |
2325 | ||
2326 | return 0 | |
2327 | } | |
2328 | ||
6bb24f4d BB |
2329 | # |
2330 | # Add specified user to specified group | |
2331 | # | |
2332 | # $1 group name | |
2333 | # $2 user name | |
2334 | # $3 base of the homedir (optional) | |
2335 | # | |
2336 | function add_user #<group_name> <user_name> <basedir> | |
2337 | { | |
7839c4b5 MM |
2338 | typeset group=$1 |
2339 | typeset user=$2 | |
6bb24f4d BB |
2340 | typeset basedir=${3:-"/var/tmp"} |
2341 | ||
7839c4b5 | 2342 | if ((${#group} == 0 || ${#user} == 0)); then |
6bb24f4d BB |
2343 | log_fail "group name or user name are not defined." |
2344 | fi | |
2345 | ||
5c9f744b | 2346 | case "$UNAME" in |
7839c4b5 MM |
2347 | FreeBSD) |
2348 | add_user_freebsd "$group" "$user" "$basedir" | |
2349 | ;; | |
2350 | Linux) | |
2351 | add_user_linux "$group" "$user" "$basedir" | |
2352 | ;; | |
2353 | *) | |
2354 | add_user_illumos "$group" "$user" "$basedir" | |
2355 | ;; | |
2356 | esac | |
6bb24f4d BB |
2357 | |
2358 | return 0 | |
2359 | } | |
2360 | ||
2361 | # | |
2362 | # Delete the specified user. | |
2363 | # | |
2364 | # $1 login name | |
2365 | # $2 base of the homedir (optional) | |
2366 | # | |
2367 | function del_user #<logname> <basedir> | |
2368 | { | |
2369 | typeset user=$1 | |
2370 | typeset basedir=${2:-"/var/tmp"} | |
2371 | ||
2372 | if ((${#user} == 0)); then | |
2373 | log_fail "login name is necessary." | |
2374 | fi | |
2375 | ||
5c9f744b | 2376 | case "$UNAME" in |
7839c4b5 MM |
2377 | FreeBSD) |
2378 | del_user_freebsd "$user" | |
2379 | ;; | |
2380 | Linux) | |
2381 | del_user_linux "$user" | |
2382 | ;; | |
2383 | *) | |
2384 | del_user_illumos "$user" | |
2385 | ;; | |
2386 | esac | |
6bb24f4d | 2387 | |
c1d9abf9 | 2388 | [[ -d $basedir/$user ]] && rm -fr $basedir/$user |
6bb24f4d BB |
2389 | |
2390 | return 0 | |
2391 | } | |
2392 | ||
2393 | # | |
2394 | # Select valid gid and create specified group. | |
2395 | # | |
2396 | # $1 group name | |
2397 | # | |
2398 | function add_group #<group_name> | |
2399 | { | |
2400 | typeset group=$1 | |
2401 | ||
2402 | if ((${#group} == 0)); then | |
2403 | log_fail "group name is necessary." | |
2404 | fi | |
2405 | ||
5c9f744b | 2406 | case "$UNAME" in |
7839c4b5 MM |
2407 | FreeBSD) |
2408 | add_group_freebsd "$group" | |
2409 | ;; | |
2410 | Linux) | |
2411 | add_group_linux "$group" | |
2412 | ;; | |
2413 | *) | |
2414 | add_group_illumos "$group" | |
2415 | ;; | |
2416 | esac | |
2417 | ||
2418 | return 0 | |
6bb24f4d BB |
2419 | } |
2420 | ||
2421 | # | |
2422 | # Delete the specified group. | |
2423 | # | |
2424 | # $1 group name | |
2425 | # | |
2426 | function del_group #<group_name> | |
2427 | { | |
7839c4b5 MM |
2428 | typeset group=$1 |
2429 | ||
2430 | if ((${#group} == 0)); then | |
6bb24f4d BB |
2431 | log_fail "group name is necessary." |
2432 | fi | |
2433 | ||
5c9f744b | 2434 | case "$UNAME" in |
7839c4b5 MM |
2435 | FreeBSD) |
2436 | del_group_freebsd "$group" | |
2437 | ;; | |
2438 | Linux) | |
2439 | del_group_linux "$group" | |
2440 | ;; | |
2441 | *) | |
2442 | del_group_illumos "$group" | |
2443 | ;; | |
2444 | esac | |
6bb24f4d BB |
2445 | |
2446 | return 0 | |
2447 | } | |
2448 | ||
2449 | # | |
2450 | # This function will return true if it's safe to destroy the pool passed | |
2451 | # as argument 1. It checks for pools based on zvols and files, and also | |
2452 | # files contained in a pool that may have a different mountpoint. | |
2453 | # | |
2454 | function safe_to_destroy_pool { # $1 the pool name | |
2455 | ||
2456 | typeset pool="" | |
2457 | typeset DONT_DESTROY="" | |
2458 | ||
2459 | # We check that by deleting the $1 pool, we're not | |
2460 | # going to pull the rug out from other pools. Do this | |
2461 | # by looking at all other pools, ensuring that they | |
2462 | # aren't built from files or zvols contained in this pool. | |
2463 | ||
c1d9abf9 | 2464 | for pool in $(zpool list -H -o name) |
6bb24f4d BB |
2465 | do |
2466 | ALTMOUNTPOOL="" | |
2467 | ||
2468 | # this is a list of the top-level directories in each of the | |
2469 | # files that make up the path to the files the pool is based on | |
75746e9a | 2470 | FILEPOOL=$(zpool status -v $pool | awk -v pool="/$1/" '$0 ~ pool {print $1}') |
6bb24f4d BB |
2471 | |
2472 | # this is a list of the zvols that make up the pool | |
75746e9a | 2473 | ZVOLPOOL=$(zpool status -v $pool | awk -v zvols="$ZVOL_DEVDIR/$1$" '$0 ~ zvols {print $1}') |
6bb24f4d BB |
2474 | |
2475 | # also want to determine if it's a file-based pool using an | |
2476 | # alternate mountpoint... | |
c1d9abf9 | 2477 | POOL_FILE_DIRS=$(zpool status -v $pool | \ |
75746e9a AZ |
2478 | awk '/\// {print $1}' | \ |
2479 | awk -F/ '!/dev/ {print $2}') | |
6bb24f4d BB |
2480 | |
2481 | for pooldir in $POOL_FILE_DIRS | |
2482 | do | |
c1d9abf9 | 2483 | OUTPUT=$(zfs list -H -r -o mountpoint $1 | \ |
75746e9a | 2484 | awk -v pd="${pooldir}$" '$0 ~ pd {print $1}') |
6bb24f4d BB |
2485 | |
2486 | ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}" | |
2487 | done | |
2488 | ||
2489 | ||
2490 | if [ ! -z "$ZVOLPOOL" ] | |
2491 | then | |
2492 | DONT_DESTROY="true" | |
2493 | log_note "Pool $pool is built from $ZVOLPOOL on $1" | |
2494 | fi | |
2495 | ||
2496 | if [ ! -z "$FILEPOOL" ] | |
2497 | then | |
2498 | DONT_DESTROY="true" | |
2499 | log_note "Pool $pool is built from $FILEPOOL on $1" | |
2500 | fi | |
2501 | ||
2502 | if [ ! -z "$ALTMOUNTPOOL" ] | |
2503 | then | |
2504 | DONT_DESTROY="true" | |
2505 | log_note "Pool $pool is built from $ALTMOUNTPOOL on $1" | |
2506 | fi | |
2507 | done | |
2508 | ||
2509 | if [ -z "${DONT_DESTROY}" ] | |
2510 | then | |
2511 | return 0 | |
2512 | else | |
2513 | log_note "Warning: it is not safe to destroy $1!" | |
2514 | return 1 | |
2515 | fi | |
2516 | } | |
2517 | ||
6bb24f4d BB |
2518 | # |
2519 | # Verify zfs operation with -p option work as expected | |
2520 | # $1 operation, value could be create, clone or rename | |
2521 | # $2 dataset type, value could be fs or vol | |
2522 | # $3 dataset name | |
2523 | # $4 new dataset name | |
2524 | # | |
2525 | function verify_opt_p_ops | |
2526 | { | |
2527 | typeset ops=$1 | |
2528 | typeset datatype=$2 | |
2529 | typeset dataset=$3 | |
2530 | typeset newdataset=$4 | |
2531 | ||
2532 | if [[ $datatype != "fs" && $datatype != "vol" ]]; then | |
2533 | log_fail "$datatype is not supported." | |
2534 | fi | |
2535 | ||
2536 | # check parameters accordingly | |
2537 | case $ops in | |
2538 | create) | |
2539 | newdataset=$dataset | |
2540 | dataset="" | |
2541 | if [[ $datatype == "vol" ]]; then | |
2542 | ops="create -V $VOLSIZE" | |
2543 | fi | |
2544 | ;; | |
2545 | clone) | |
2546 | if [[ -z $newdataset ]]; then | |
2547 | log_fail "newdataset should not be empty" \ | |
2548 | "when ops is $ops." | |
2549 | fi | |
2550 | log_must datasetexists $dataset | |
2551 | log_must snapexists $dataset | |
2552 | ;; | |
2553 | rename) | |
2554 | if [[ -z $newdataset ]]; then | |
2555 | log_fail "newdataset should not be empty" \ | |
2556 | "when ops is $ops." | |
2557 | fi | |
2558 | log_must datasetexists $dataset | |
6bb24f4d BB |
2559 | ;; |
2560 | *) | |
2561 | log_fail "$ops is not supported." | |
2562 | ;; | |
2563 | esac | |
2564 | ||
2565 | # make sure the upper level filesystem does not exist | |
c7b55e71 | 2566 | destroy_dataset "${newdataset%/*}" "-rRf" |
6bb24f4d BB |
2567 | |
2568 | # without -p option, operation will fail | |
c1d9abf9 | 2569 | log_mustnot zfs $ops $dataset $newdataset |
6bb24f4d BB |
2570 | log_mustnot datasetexists $newdataset ${newdataset%/*} |
2571 | ||
2572 | # with -p option, operation should succeed | |
c1d9abf9 | 2573 | log_must zfs $ops -p $dataset $newdataset |
6bb24f4d BB |
2574 | block_device_wait |
2575 | ||
2576 | if ! datasetexists $newdataset ; then | |
2577 | log_fail "-p option does not work for $ops" | |
2578 | fi | |
2579 | ||
2580 | # when $ops is create or clone, redo the operation still return zero | |
2581 | if [[ $ops != "rename" ]]; then | |
c1d9abf9 | 2582 | log_must zfs $ops -p $dataset $newdataset |
6bb24f4d BB |
2583 | fi |
2584 | ||
2585 | return 0 | |
2586 | } | |
2587 | ||
2588 | # | |
2589 | # Get configuration of pool | |
2590 | # $1 pool name | |
2591 | # $2 config name | |
2592 | # | |
2593 | function get_config | |
2594 | { | |
2595 | typeset pool=$1 | |
2596 | typeset config=$2 | |
6bb24f4d BB |
2597 | |
2598 | if ! poolexists "$pool" ; then | |
2599 | return 1 | |
2600 | fi | |
75746e9a AZ |
2601 | if [ "$(get_pool_prop cachefile "$pool")" = "none" ]; then |
2602 | zdb -e $pool | |
6bb24f4d | 2603 | else |
75746e9a AZ |
2604 | zdb -C $pool |
2605 | fi | awk -F: -v cfg="$config:" '$0 ~ cfg {sub(/^'\''/, $2); sub(/'\''$/, $2); print $2}' | |
6bb24f4d BB |
2606 | } |
2607 | ||
2608 | # | |
2609 | # Privated function. Random select one of items from arguments. | |
2610 | # | |
2611 | # $1 count | |
2612 | # $2-n string | |
2613 | # | |
2614 | function _random_get | |
2615 | { | |
2616 | typeset cnt=$1 | |
2617 | shift | |
2618 | ||
2619 | typeset str="$@" | |
2620 | typeset -i ind | |
2621 | ((ind = RANDOM % cnt + 1)) | |
2622 | ||
75746e9a | 2623 | echo "$str" | cut -f $ind -d ' ' |
6bb24f4d BB |
2624 | } |
2625 | ||
2626 | # | |
2627 | # Random select one of item from arguments which include NONE string | |
2628 | # | |
2629 | function random_get_with_non | |
2630 | { | |
2631 | typeset -i cnt=$# | |
2632 | ((cnt =+ 1)) | |
2633 | ||
2634 | _random_get "$cnt" "$@" | |
2635 | } | |
2636 | ||
2637 | # | |
2638 | # Random select one of item from arguments which doesn't include NONE string | |
2639 | # | |
2640 | function random_get | |
2641 | { | |
2642 | _random_get "$#" "$@" | |
2643 | } | |
2644 | ||
6bb24f4d BB |
2645 | # |
2646 | # The function will generate a dataset name with specific length | |
2647 | # $1, the length of the name | |
2648 | # $2, the base string to construct the name | |
2649 | # | |
2650 | function gen_dataset_name | |
2651 | { | |
2652 | typeset -i len=$1 | |
2653 | typeset basestr="$2" | |
2654 | typeset -i baselen=${#basestr} | |
2655 | typeset -i iter=0 | |
2656 | typeset l_name="" | |
2657 | ||
2658 | if ((len % baselen == 0)); then | |
2659 | ((iter = len / baselen)) | |
2660 | else | |
2661 | ((iter = len / baselen + 1)) | |
2662 | fi | |
2663 | while ((iter > 0)); do | |
2664 | l_name="${l_name}$basestr" | |
2665 | ||
2666 | ((iter -= 1)) | |
2667 | done | |
2668 | ||
c1d9abf9 | 2669 | echo $l_name |
6bb24f4d BB |
2670 | } |
2671 | ||
2672 | # | |
2673 | # Get cksum tuple of dataset | |
2674 | # $1 dataset name | |
2675 | # | |
2676 | # sample zdb output: | |
2677 | # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp | |
2678 | # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4 | |
2679 | # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P | |
2680 | # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744 | |
2681 | function datasetcksum | |
2682 | { | |
2683 | typeset cksum | |
c1d9abf9 | 2684 | sync |
7454275a | 2685 | sync_all_pools |
75746e9a | 2686 | zdb -vvv $1 | awk -F= -v ds="^Dataset $1 "'\\[' '$0 ~ ds && /cksum/ {print $7}' |
6bb24f4d BB |
2687 | } |
2688 | ||
2689 | # | |
2690 | # Get the given disk/slice state from the specific field of the pool | |
2691 | # | |
2692 | function get_device_state #pool disk field("", "spares","logs") | |
2693 | { | |
2694 | typeset pool=$1 | |
2695 | typeset disk=${2#$DEV_DSKDIR/} | |
2696 | typeset field=${3:-$pool} | |
2697 | ||
270d0a5a AZ |
2698 | zpool status -v "$pool" 2>/dev/null | \ |
2699 | awk -v device=$disk -v pool=$pool -v field=$field \ | |
6bb24f4d BB |
2700 | 'BEGIN {startconfig=0; startfield=0; } |
2701 | /config:/ {startconfig=1} | |
2702 | (startconfig==1) && ($1==field) {startfield=1; next;} | |
2703 | (startfield==1) && ($1==device) {print $2; exit;} | |
2704 | (startfield==1) && | |
270d0a5a | 2705 | ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}' |
6bb24f4d BB |
2706 | } |
2707 | ||
6bb24f4d BB |
2708 | # |
2709 | # get the root filesystem name if it's zfsroot system. | |
2710 | # | |
2711 | # return: root filesystem name | |
2712 | function get_rootfs | |
2713 | { | |
2714 | typeset rootfs="" | |
8aab1218 | 2715 | |
7839c4b5 MM |
2716 | if is_freebsd; then |
2717 | rootfs=$(mount -p | awk '$2 == "/" && $3 == "zfs" {print $1}') | |
2718 | elif ! is_linux; then | |
75746e9a | 2719 | rootfs=$(awk '$2 == "/" && $3 == "zfs" {print $1}' \ |
8aab1218 TS |
2720 | /etc/mnttab) |
2721 | fi | |
6bb24f4d BB |
2722 | if [[ -z "$rootfs" ]]; then |
2723 | log_fail "Can not get rootfs" | |
2724 | fi | |
23914a3b | 2725 | if datasetexists $rootfs; then |
c1d9abf9 | 2726 | echo $rootfs |
6bb24f4d BB |
2727 | else |
2728 | log_fail "This is not a zfsroot system." | |
2729 | fi | |
2730 | } | |
2731 | ||
2732 | # | |
2733 | # get the rootfs's pool name | |
2734 | # return: | |
2735 | # rootpool name | |
2736 | # | |
2737 | function get_rootpool | |
2738 | { | |
bd328a58 AZ |
2739 | typeset rootfs=$(get_rootfs) |
2740 | echo ${rootfs%%/*} | |
6bb24f4d BB |
2741 | } |
2742 | ||
2743 | # | |
2744 | # To verify if the require numbers of disks is given | |
2745 | # | |
2746 | function verify_disk_count | |
2747 | { | |
2748 | typeset -i min=${2:-1} | |
2749 | ||
bd328a58 | 2750 | typeset -i count=$(echo "$1" | wc -w) |
6bb24f4d BB |
2751 | |
2752 | if ((count < min)); then | |
2753 | log_untested "A minimum of $min disks is required to run." \ | |
2754 | " You specified $count disk(s)" | |
2755 | fi | |
2756 | } | |
2757 | ||
2758 | function ds_is_volume | |
2759 | { | |
2760 | typeset type=$(get_prop type $1) | |
caccfc87 | 2761 | [ $type = "volume" ] |
6bb24f4d BB |
2762 | } |
2763 | ||
2764 | function ds_is_filesystem | |
2765 | { | |
2766 | typeset type=$(get_prop type $1) | |
caccfc87 | 2767 | [ $type = "filesystem" ] |
6bb24f4d BB |
2768 | } |
2769 | ||
6bb24f4d BB |
2770 | # |
2771 | # Check if Trusted Extensions are installed and enabled | |
2772 | # | |
2773 | function is_te_enabled | |
2774 | { | |
bd328a58 | 2775 | svcs -H -o state labeld 2>/dev/null | grep -q "enabled" |
6bb24f4d BB |
2776 | } |
2777 | ||
6f73d021 TH |
2778 | # Return the number of CPUs (cross-platform) |
2779 | function get_num_cpus | |
2780 | { | |
2781 | if is_linux ; then | |
2782 | grep -c '^processor' /proc/cpuinfo | |
2783 | elif is_freebsd; then | |
2784 | sysctl -n kern.smp.cpus | |
2785 | else | |
2786 | psrinfo | wc -l | |
2787 | fi | |
2788 | } | |
2789 | ||
6bb24f4d BB |
2790 | # Utility function to determine if a system has multiple cpus. |
2791 | function is_mp | |
2792 | { | |
6f73d021 | 2793 | [[ $(get_num_cpus) -gt 1 ]] |
6bb24f4d BB |
2794 | } |
2795 | ||
2796 | function get_cpu_freq | |
2797 | { | |
2798 | if is_linux; then | |
c1d9abf9 | 2799 | lscpu | awk '/CPU MHz/ { print $3 }' |
7839c4b5 | 2800 | elif is_freebsd; then |
9be70c37 | 2801 | sysctl -n hw.clockrate |
6bb24f4d | 2802 | else |
c1d9abf9 | 2803 | psrinfo -v 0 | awk '/processor operates at/ {print $6}' |
6bb24f4d BB |
2804 | fi |
2805 | } | |
2806 | ||
2807 | # Run the given command as the user provided. | |
2808 | function user_run | |
2809 | { | |
2810 | typeset user=$1 | |
2811 | shift | |
2812 | ||
e0b53a5d RM |
2813 | log_note "user: $user" |
2814 | log_note "cmd: $*" | |
2815 | ||
2816 | typeset out=$TEST_BASE_DIR/out | |
2817 | typeset err=$TEST_BASE_DIR/err | |
2818 | ||
2819 | sudo -Eu $user env PATH="$PATH" ksh <<<"$*" >$out 2>$err | |
2820 | typeset res=$? | |
2821 | log_note "out: $(<$out)" | |
2822 | log_note "err: $(<$err)" | |
2823 | return $res | |
6bb24f4d BB |
2824 | } |
2825 | ||
2826 | # | |
2827 | # Check if the pool contains the specified vdevs | |
2828 | # | |
2829 | # $1 pool | |
2830 | # $2..n <vdev> ... | |
2831 | # | |
2832 | # Return 0 if the vdevs are contained in the pool, 1 if any of the specified | |
2833 | # vdevs is not in the pool, and 2 if pool name is missing. | |
2834 | # | |
2835 | function vdevs_in_pool | |
2836 | { | |
2837 | typeset pool=$1 | |
2838 | typeset vdev | |
2839 | ||
2840 | if [[ -z $pool ]]; then | |
2841 | log_note "Missing pool name." | |
2842 | return 2 | |
2843 | fi | |
2844 | ||
2845 | shift | |
2846 | ||
7c9a4292 BB |
2847 | # We could use 'zpool list' to only get the vdevs of the pool but we |
2848 | # can't reference a mirror/raidz vdev using its ID (i.e mirror-0), | |
2849 | # therefore we use the 'zpool status' output. | |
c1d9abf9 | 2850 | typeset tmpfile=$(mktemp) |
7c9a4292 | 2851 | zpool status -v "$pool" | grep -A 1000 "config:" >$tmpfile |
23914a3b | 2852 | for vdev in "$@"; do |
964d4180 | 2853 | grep -wq ${vdev##*/} $tmpfile || return 1 |
6bb24f4d BB |
2854 | done |
2855 | ||
c1d9abf9 | 2856 | rm -f $tmpfile |
23914a3b | 2857 | return 0 |
6bb24f4d BB |
2858 | } |
2859 | ||
679d73e9 JWK |
2860 | function get_max |
2861 | { | |
2862 | typeset -l i max=$1 | |
2863 | shift | |
2864 | ||
2865 | for i in "$@"; do | |
9be70c37 | 2866 | max=$((max > i ? max : i)) |
679d73e9 JWK |
2867 | done |
2868 | ||
2869 | echo $max | |
2870 | } | |
2871 | ||
a7004725 DK |
2872 | # Write data that can be compressed into a directory |
2873 | function write_compressible | |
2874 | { | |
2875 | typeset dir=$1 | |
2876 | typeset megs=$2 | |
2877 | typeset nfiles=${3:-1} | |
2878 | typeset bs=${4:-1024k} | |
2879 | typeset fname=${5:-file} | |
2880 | ||
2881 | [[ -d $dir ]] || log_fail "No directory: $dir" | |
2882 | ||
2883 | # Under Linux fio is not currently used since its behavior can | |
2884 | # differ significantly across versions. This includes missing | |
2885 | # command line options and cases where the --buffer_compress_* | |
2886 | # options fail to behave as expected. | |
2887 | if is_linux; then | |
2888 | typeset file_bytes=$(to_bytes $megs) | |
2889 | typeset bs_bytes=4096 | |
2890 | typeset blocks=$(($file_bytes / $bs_bytes)) | |
2891 | ||
2892 | for (( i = 0; i < $nfiles; i++ )); do | |
2893 | truncate -s $file_bytes $dir/$fname.$i | |
2894 | ||
2895 | # Write every third block to get 66% compression. | |
2896 | for (( j = 0; j < $blocks; j += 3 )); do | |
2897 | dd if=/dev/urandom of=$dir/$fname.$i \ | |
2898 | seek=$j bs=$bs_bytes count=1 \ | |
2899 | conv=notrunc >/dev/null 2>&1 | |
2900 | done | |
2901 | done | |
2902 | else | |
2d9da5e1 | 2903 | command -v fio > /dev/null || log_unsupported "fio missing" |
bd328a58 | 2904 | log_must eval fio \ |
a7004725 DK |
2905 | --name=job \ |
2906 | --fallocate=0 \ | |
2907 | --minimal \ | |
2908 | --randrepeat=0 \ | |
2909 | --buffer_compress_percentage=66 \ | |
2910 | --buffer_compress_chunk=4096 \ | |
bd328a58 AZ |
2911 | --directory="$dir" \ |
2912 | --numjobs="$nfiles" \ | |
2913 | --nrfiles="$nfiles" \ | |
a7004725 | 2914 | --rw=write \ |
bd328a58 AZ |
2915 | --bs="$bs" \ |
2916 | --filesize="$megs" \ | |
2917 | "--filename_format='$fname.\$jobnum' >/dev/null" | |
a7004725 DK |
2918 | fi |
2919 | } | |
2920 | ||
2921 | function get_objnum | |
2922 | { | |
2923 | typeset pathname=$1 | |
2924 | typeset objnum | |
2925 | ||
2926 | [[ -e $pathname ]] || log_fail "No such file or directory: $pathname" | |
7839c4b5 MM |
2927 | if is_freebsd; then |
2928 | objnum=$(stat -f "%i" $pathname) | |
2929 | else | |
2930 | objnum=$(stat -c %i $pathname) | |
2931 | fi | |
a7004725 DK |
2932 | echo $objnum |
2933 | } | |
2934 | ||
1de321e6 | 2935 | # |
bec1067d | 2936 | # Sync data to the pool |
1de321e6 JX |
2937 | # |
2938 | # $1 pool name | |
bec1067d | 2939 | # $2 boolean to force uberblock (and config including zpool cache file) update |
1de321e6 | 2940 | # |
bec1067d | 2941 | function sync_pool #pool <force> |
1de321e6 JX |
2942 | { |
2943 | typeset pool=${1:-$TESTPOOL} | |
bec1067d | 2944 | typeset force=${2:-false} |
1de321e6 | 2945 | |
bec1067d AP |
2946 | if [[ $force == true ]]; then |
2947 | log_must zpool sync -f $pool | |
2948 | else | |
2949 | log_must zpool sync $pool | |
2950 | fi | |
2951 | ||
2952 | return 0 | |
1de321e6 | 2953 | } |
d834b9ce | 2954 | |
7454275a AJ |
2955 | # |
2956 | # Sync all pools | |
2957 | # | |
2958 | # $1 boolean to force uberblock (and config including zpool cache file) update | |
2959 | # | |
2960 | function sync_all_pools #<force> | |
2961 | { | |
2962 | typeset force=${1:-false} | |
2963 | ||
2964 | if [[ $force == true ]]; then | |
2965 | log_must zpool sync -f | |
2966 | else | |
2967 | log_must zpool sync | |
2968 | fi | |
2969 | ||
2970 | return 0 | |
2971 | } | |
2972 | ||
d834b9ce GM |
2973 | # |
2974 | # Wait for zpool 'freeing' property drops to zero. | |
2975 | # | |
2976 | # $1 pool name | |
2977 | # | |
2978 | function wait_freeing #pool | |
2979 | { | |
2980 | typeset pool=${1:-$TESTPOOL} | |
2981 | while true; do | |
c1d9abf9 JWK |
2982 | [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break |
2983 | log_must sleep 1 | |
d834b9ce GM |
2984 | done |
2985 | } | |
7a4500a1 | 2986 | |
dddef7d6 | 2987 | # |
2988 | # Wait for every device replace operation to complete | |
2989 | # | |
2990 | # $1 pool name | |
c8b3dda1 | 2991 | # $2 timeout |
dddef7d6 | 2992 | # |
c8b3dda1 | 2993 | function wait_replacing #pool timeout |
dddef7d6 | 2994 | { |
c8b3dda1 | 2995 | typeset timeout=${2:-300} |
dddef7d6 | 2996 | typeset pool=${1:-$TESTPOOL} |
c8b3dda1 BB |
2997 | for (( timer = 0; timer < $timeout; timer++ )); do |
2998 | is_pool_replacing $pool || break; | |
2999 | sleep 1; | |
dddef7d6 | 3000 | done |
3001 | } | |
3002 | ||
bf95a000 TH |
3003 | # Wait for a pool to be scrubbed |
3004 | # | |
3005 | # $1 pool name | |
56fa4aa9 | 3006 | # $2 timeout |
bf95a000 | 3007 | # |
56fa4aa9 | 3008 | function wait_scrubbed #pool timeout |
bf95a000 | 3009 | { |
56fa4aa9 RE |
3010 | typeset timeout=${2:-300} |
3011 | typeset pool=${1:-$TESTPOOL} | |
3012 | for (( timer = 0; timer < $timeout; timer++ )); do | |
3013 | is_pool_scrubbed $pool && break; | |
3014 | sleep 1; | |
3015 | done | |
bf95a000 TH |
3016 | } |
3017 | ||
639b1894 TH |
3018 | # Backup the zed.rc in our test directory so that we can edit it for our test. |
3019 | # | |
3020 | # Returns: Backup file name. You will need to pass this to zed_rc_restore(). | |
3021 | function zed_rc_backup | |
3022 | { | |
3023 | zedrc_backup="$(mktemp)" | |
3024 | cp $ZEDLET_DIR/zed.rc $zedrc_backup | |
3025 | echo $zedrc_backup | |
3026 | } | |
3027 | ||
3028 | function zed_rc_restore | |
3029 | { | |
3030 | mv $1 $ZEDLET_DIR/zed.rc | |
3031 | } | |
3032 | ||
95401cb6 BB |
3033 | # |
3034 | # Setup custom environment for the ZED. | |
3035 | # | |
bf95a000 | 3036 | # $@ Optional list of zedlets to run under zed. |
95401cb6 BB |
3037 | function zed_setup |
3038 | { | |
3039 | if ! is_linux; then | |
5c9f744b | 3040 | log_unsupported "No zed on $UNAME" |
95401cb6 BB |
3041 | fi |
3042 | ||
3043 | if [[ ! -d $ZEDLET_DIR ]]; then | |
3044 | log_must mkdir $ZEDLET_DIR | |
3045 | fi | |
3046 | ||
3047 | if [[ ! -e $VDEVID_CONF ]]; then | |
3048 | log_must touch $VDEVID_CONF | |
3049 | fi | |
3050 | ||
3051 | if [[ -e $VDEVID_CONF_ETC ]]; then | |
3052 | log_fail "Must not have $VDEVID_CONF_ETC file present on system" | |
3053 | fi | |
bf95a000 | 3054 | EXTRA_ZEDLETS=$@ |
95401cb6 BB |
3055 | |
3056 | # Create a symlink for /etc/zfs/vdev_id.conf file. | |
3057 | log_must ln -s $VDEVID_CONF $VDEVID_CONF_ETC | |
3058 | ||
3059 | # Setup minimal ZED configuration. Individual test cases should | |
3060 | # add additional ZEDLETs as needed for their specific test. | |
3f03fc8d BB |
3061 | log_must cp ${ZEDLET_ETC_DIR}/zed.rc $ZEDLET_DIR |
3062 | log_must cp ${ZEDLET_ETC_DIR}/zed-functions.sh $ZEDLET_DIR | |
95401cb6 | 3063 | |
bf95a000 TH |
3064 | # Scripts must only be user writable. |
3065 | if [[ -n "$EXTRA_ZEDLETS" ]] ; then | |
3066 | saved_umask=$(umask) | |
3067 | log_must umask 0022 | |
3068 | for i in $EXTRA_ZEDLETS ; do | |
3069 | log_must cp ${ZEDLET_LIBEXEC_DIR}/$i $ZEDLET_DIR | |
3070 | done | |
3071 | log_must umask $saved_umask | |
3072 | fi | |
3073 | ||
3f03fc8d BB |
3074 | # Customize the zed.rc file to enable the full debug log. |
3075 | log_must sed -i '/\#ZED_DEBUG_LOG=.*/d' $ZEDLET_DIR/zed.rc | |
d5e024cb | 3076 | echo "ZED_DEBUG_LOG=$ZED_DEBUG_LOG" >>$ZEDLET_DIR/zed.rc |
3f03fc8d | 3077 | |
95401cb6 BB |
3078 | } |
3079 | ||
3080 | # | |
3081 | # Cleanup custom ZED environment. | |
3082 | # | |
bf95a000 | 3083 | # $@ Optional list of zedlets to remove from our test zed.d directory. |
95401cb6 BB |
3084 | function zed_cleanup |
3085 | { | |
3086 | if ! is_linux; then | |
3087 | return | |
3088 | fi | |
3089 | ||
bd328a58 AZ |
3090 | for extra_zedlet; do |
3091 | log_must rm -f ${ZEDLET_DIR}/$extra_zedlet | |
3092 | done | |
3093 | log_must rm -fd ${ZEDLET_DIR}/zed.rc ${ZEDLET_DIR}/zed-functions.sh ${ZEDLET_DIR}/all-syslog.sh ${ZEDLET_DIR}/all-debug.sh ${ZEDLET_DIR}/state \ | |
3094 | $ZED_LOG $ZED_DEBUG_LOG $VDEVID_CONF_ETC $VDEVID_CONF \ | |
3095 | $ZEDLET_DIR | |
95401cb6 BB |
3096 | } |
3097 | ||
56fa4aa9 RE |
3098 | # |
3099 | # Check if ZED is currently running; if so, returns PIDs | |
3100 | # | |
3101 | function zed_check | |
3102 | { | |
3103 | if ! is_linux; then | |
3104 | return | |
3105 | fi | |
3106 | zedpids="$(pgrep -x zed)" | |
56fa4aa9 | 3107 | zedpids2="$(pgrep -x lt-zed)" |
56fa4aa9 RE |
3108 | echo ${zedpids} ${zedpids2} |
3109 | } | |
3110 | ||
7a4500a1 SV |
3111 | # |
3112 | # Check if ZED is currently running, if not start ZED. | |
3113 | # | |
3114 | function zed_start | |
3115 | { | |
95401cb6 BB |
3116 | if ! is_linux; then |
3117 | return | |
3118 | fi | |
7a4500a1 | 3119 | |
95401cb6 BB |
3120 | # ZEDLET_DIR=/var/tmp/zed |
3121 | if [[ ! -d $ZEDLET_DIR ]]; then | |
3122 | log_must mkdir $ZEDLET_DIR | |
3123 | fi | |
7a4500a1 | 3124 | |
95401cb6 | 3125 | # Verify the ZED is not already running. |
56fa4aa9 RE |
3126 | zedpids=$(zed_check) |
3127 | if [ -n "$zedpids" ]; then | |
3128 | # We never, ever, really want it to just keep going if zed | |
3129 | # is already running - usually this implies our test cases | |
3130 | # will break very strangely because whatever we wanted to | |
3131 | # configure zed for won't be listening to our changes in the | |
3132 | # tmpdir | |
3133 | log_fail "ZED already running - ${zedpids}" | |
03431390 OF |
3134 | else |
3135 | log_note "Starting ZED" | |
3136 | # run ZED in the background and redirect foreground logging | |
3137 | # output to $ZED_LOG. | |
3138 | log_must truncate -s 0 $ZED_DEBUG_LOG | |
73218f41 AZ |
3139 | log_must eval "zed -vF -d $ZEDLET_DIR -P $PATH" \ |
3140 | "-s $ZEDLET_DIR/state -j 1 2>$ZED_LOG &" | |
7a4500a1 | 3141 | fi |
95401cb6 | 3142 | |
3f03fc8d | 3143 | return 0 |
7a4500a1 SV |
3144 | } |
3145 | ||
3146 | # | |
3147 | # Kill ZED process | |
3148 | # | |
3149 | function zed_stop | |
3150 | { | |
95401cb6 | 3151 | if ! is_linux; then |
56fa4aa9 | 3152 | return "" |
95401cb6 BB |
3153 | fi |
3154 | ||
3f03fc8d | 3155 | log_note "Stopping ZED" |
73218f41 | 3156 | while true; do |
56fa4aa9 RE |
3157 | zedpids=$(zed_check) |
3158 | [ ! -n "$zedpids" ] && break | |
73218f41 AZ |
3159 | |
3160 | log_must kill $zedpids | |
3161 | sleep 1 | |
3162 | done | |
3f03fc8d | 3163 | return 0 |
7a4500a1 | 3164 | } |
8c54ddd3 | 3165 | |
4e9b1569 | 3166 | # |
3167 | # Drain all zevents | |
3168 | # | |
3169 | function zed_events_drain | |
3170 | { | |
3171 | while [ $(zpool events -H | wc -l) -ne 0 ]; do | |
3172 | sleep 1 | |
3173 | zpool events -c >/dev/null | |
3174 | done | |
3175 | } | |
3176 | ||
639b1894 TH |
3177 | # Set a variable in zed.rc to something, un-commenting it in the process. |
3178 | # | |
3179 | # $1 variable | |
3180 | # $2 value | |
3181 | function zed_rc_set | |
3182 | { | |
3183 | var="$1" | |
3184 | val="$2" | |
3185 | # Remove the line | |
3186 | cmd="'/$var/d'" | |
3187 | eval sed -i $cmd $ZEDLET_DIR/zed.rc | |
3188 | ||
3189 | # Add it at the end | |
3190 | echo "$var=$val" >> $ZEDLET_DIR/zed.rc | |
3191 | } | |
3192 | ||
3193 | ||
8c54ddd3 BB |
3194 | # |
3195 | # Check is provided device is being active used as a swap device. | |
3196 | # | |
3197 | function is_swap_inuse | |
3198 | { | |
3199 | typeset device=$1 | |
3200 | ||
3201 | if [[ -z $device ]] ; then | |
3202 | log_note "No device specified." | |
3203 | return 1 | |
3204 | fi | |
3205 | ||
5c9f744b | 3206 | case "$UNAME" in |
62c5ccdf AZ |
3207 | Linux) |
3208 | swapon -s | grep -wq $(readlink -f $device) | |
3209 | ;; | |
3210 | FreeBSD) | |
3211 | swapctl -l | grep -wq $device | |
3212 | ;; | |
3213 | *) | |
3214 | swap -l | grep -wq $device | |
3215 | ;; | |
3216 | esac | |
8c54ddd3 BB |
3217 | } |
3218 | ||
3219 | # | |
3220 | # Setup a swap device using the provided device. | |
3221 | # | |
3222 | function swap_setup | |
3223 | { | |
3224 | typeset swapdev=$1 | |
3225 | ||
5c9f744b | 3226 | case "$UNAME" in |
62c5ccdf | 3227 | Linux) |
c7a7601c | 3228 | log_must eval "mkswap $swapdev > /dev/null 2>&1" |
8c54ddd3 | 3229 | log_must swapon $swapdev |
62c5ccdf AZ |
3230 | ;; |
3231 | FreeBSD) | |
7839c4b5 | 3232 | log_must swapctl -a $swapdev |
62c5ccdf AZ |
3233 | ;; |
3234 | *) | |
3235 | log_must swap -a $swapdev | |
3236 | ;; | |
3237 | esac | |
8c54ddd3 BB |
3238 | |
3239 | return 0 | |
3240 | } | |
3241 | ||
3242 | # | |
3243 | # Cleanup a swap device on the provided device. | |
3244 | # | |
3245 | function swap_cleanup | |
3246 | { | |
3247 | typeset swapdev=$1 | |
3248 | ||
3249 | if is_swap_inuse $swapdev; then | |
3250 | if is_linux; then | |
3251 | log_must swapoff $swapdev | |
7839c4b5 MM |
3252 | elif is_freebsd; then |
3253 | log_must swapoff $swapdev | |
8c54ddd3 BB |
3254 | else |
3255 | log_must swap -d $swapdev | |
3256 | fi | |
3257 | fi | |
3258 | ||
3259 | return 0 | |
3260 | } | |
379ca9cf OF |
3261 | |
3262 | # | |
3263 | # Set a global system tunable (64-bit value) | |
3264 | # | |
2476f103 | 3265 | # $1 tunable name (use a NAME defined in tunables.cfg) |
379ca9cf OF |
3266 | # $2 tunable values |
3267 | # | |
3268 | function set_tunable64 | |
3269 | { | |
3270 | set_tunable_impl "$1" "$2" Z | |
3271 | } | |
3272 | ||
3273 | # | |
3274 | # Set a global system tunable (32-bit value) | |
3275 | # | |
2476f103 | 3276 | # $1 tunable name (use a NAME defined in tunables.cfg) |
379ca9cf OF |
3277 | # $2 tunable values |
3278 | # | |
3279 | function set_tunable32 | |
3280 | { | |
3281 | set_tunable_impl "$1" "$2" W | |
3282 | } | |
3283 | ||
3284 | function set_tunable_impl | |
3285 | { | |
2476f103 | 3286 | typeset name="$1" |
379ca9cf OF |
3287 | typeset value="$2" |
3288 | typeset mdb_cmd="$3" | |
379ca9cf | 3289 | |
2476f103 RM |
3290 | eval "typeset tunable=\$$name" |
3291 | case "$tunable" in | |
3292 | UNSUPPORTED) | |
5c9f744b | 3293 | log_unsupported "Tunable '$name' is unsupported on $UNAME" |
2476f103 RM |
3294 | ;; |
3295 | "") | |
3296 | log_fail "Tunable '$name' must be added to tunables.cfg" | |
3297 | ;; | |
3298 | *) | |
3299 | ;; | |
3300 | esac | |
3301 | ||
379ca9cf OF |
3302 | [[ -z "$value" ]] && return 1 |
3303 | [[ -z "$mdb_cmd" ]] && return 1 | |
3304 | ||
5c9f744b | 3305 | case "$UNAME" in |
379ca9cf | 3306 | Linux) |
ad9e7676 | 3307 | typeset zfs_tunables="/sys/module/zfs/parameters" |
23914a3b | 3308 | echo "$value" >"$zfs_tunables/$tunable" |
379ca9cf | 3309 | ;; |
7839c4b5 MM |
3310 | FreeBSD) |
3311 | sysctl vfs.zfs.$tunable=$value | |
7839c4b5 | 3312 | ;; |
379ca9cf | 3313 | SunOS) |
379ca9cf | 3314 | echo "${tunable}/${mdb_cmd}0t${value}" | mdb -kw |
379ca9cf OF |
3315 | ;; |
3316 | esac | |
3317 | } | |
3318 | ||
3319 | # | |
3320 | # Get a global system tunable | |
3321 | # | |
2476f103 | 3322 | # $1 tunable name (use a NAME defined in tunables.cfg) |
379ca9cf OF |
3323 | # |
3324 | function get_tunable | |
3325 | { | |
3326 | get_tunable_impl "$1" | |
3327 | } | |
3328 | ||
3329 | function get_tunable_impl | |
3330 | { | |
2476f103 | 3331 | typeset name="$1" |
379ca9cf | 3332 | typeset module="${2:-zfs}" |
6f73d021 | 3333 | typeset check_only="$3" |
379ca9cf | 3334 | |
2476f103 RM |
3335 | eval "typeset tunable=\$$name" |
3336 | case "$tunable" in | |
3337 | UNSUPPORTED) | |
6f73d021 TH |
3338 | if [ -z "$check_only" ] ; then |
3339 | log_unsupported "Tunable '$name' is unsupported on $UNAME" | |
3340 | else | |
3341 | return 1 | |
3342 | fi | |
2476f103 RM |
3343 | ;; |
3344 | "") | |
6f73d021 TH |
3345 | if [ -z "$check_only" ] ; then |
3346 | log_fail "Tunable '$name' must be added to tunables.cfg" | |
3347 | else | |
3348 | return 1 | |
3349 | fi | |
2476f103 RM |
3350 | ;; |
3351 | *) | |
3352 | ;; | |
3353 | esac | |
379ca9cf | 3354 | |
5c9f744b | 3355 | case "$UNAME" in |
379ca9cf OF |
3356 | Linux) |
3357 | typeset zfs_tunables="/sys/module/$module/parameters" | |
379ca9cf | 3358 | cat $zfs_tunables/$tunable |
379ca9cf | 3359 | ;; |
7839c4b5 MM |
3360 | FreeBSD) |
3361 | sysctl -n vfs.zfs.$tunable | |
3362 | ;; | |
379ca9cf OF |
3363 | SunOS) |
3364 | [[ "$module" -eq "zfs" ]] || return 1 | |
3365 | ;; | |
3366 | esac | |
d2734cce | 3367 | } |
240c015a | 3368 | |
6f73d021 TH |
3369 | # Does a tunable exist? |
3370 | # | |
3371 | # $1: Tunable name | |
3372 | function tunable_exists | |
3373 | { | |
3374 | get_tunable_impl $1 "zfs" 1 | |
3375 | } | |
3376 | ||
240c015a RM |
3377 | # |
3378 | # Compute MD5 digest for given file or stdin if no file given. | |
3379 | # Note: file path must not contain spaces | |
3380 | # | |
3381 | function md5digest | |
3382 | { | |
3383 | typeset file=$1 | |
3384 | ||
5c9f744b | 3385 | case "$UNAME" in |
7839c4b5 MM |
3386 | FreeBSD) |
3387 | md5 -q $file | |
3388 | ;; | |
3389 | *) | |
75746e9a AZ |
3390 | typeset sum _ |
3391 | read -r sum _ < <(md5sum -b $file) | |
3392 | echo $sum | |
7839c4b5 MM |
3393 | ;; |
3394 | esac | |
240c015a RM |
3395 | } |
3396 | ||
3397 | # | |
3398 | # Compute SHA256 digest for given file or stdin if no file given. | |
3399 | # Note: file path must not contain spaces | |
3400 | # | |
3401 | function sha256digest | |
3402 | { | |
3403 | typeset file=$1 | |
3404 | ||
5c9f744b | 3405 | case "$UNAME" in |
7839c4b5 MM |
3406 | FreeBSD) |
3407 | sha256 -q $file | |
3408 | ;; | |
3409 | *) | |
75746e9a AZ |
3410 | typeset sum _ |
3411 | read -r sum _ < <(sha256sum -b $file) | |
3412 | echo $sum | |
7839c4b5 MM |
3413 | ;; |
3414 | esac | |
3415 | } | |
3416 | ||
3417 | function new_fs #<args> | |
3418 | { | |
5c9f744b | 3419 | case "$UNAME" in |
7839c4b5 MM |
3420 | FreeBSD) |
3421 | newfs "$@" | |
3422 | ;; | |
3423 | *) | |
3424 | echo y | newfs -v "$@" | |
3425 | ;; | |
3426 | esac | |
3427 | } | |
3428 | ||
3429 | function stat_size #<path> | |
3430 | { | |
3431 | typeset path=$1 | |
3432 | ||
5c9f744b | 3433 | case "$UNAME" in |
7839c4b5 MM |
3434 | FreeBSD) |
3435 | stat -f %z "$path" | |
3436 | ;; | |
3437 | *) | |
3438 | stat -c %s "$path" | |
3439 | ;; | |
3440 | esac | |
240c015a | 3441 | } |
9fb2771a | 3442 | |
9681de46 US |
3443 | function stat_mtime #<path> |
3444 | { | |
3445 | typeset path=$1 | |
3446 | ||
3447 | case "$UNAME" in | |
3448 | FreeBSD) | |
3449 | stat -f %m "$path" | |
3450 | ;; | |
3451 | *) | |
3452 | stat -c %Y "$path" | |
3453 | ;; | |
3454 | esac | |
3455 | } | |
3456 | ||
8ae86e2e RM |
3457 | function stat_ctime #<path> |
3458 | { | |
3459 | typeset path=$1 | |
3460 | ||
5c9f744b | 3461 | case "$UNAME" in |
8ae86e2e RM |
3462 | FreeBSD) |
3463 | stat -f %c "$path" | |
3464 | ;; | |
3465 | *) | |
3466 | stat -c %Z "$path" | |
3467 | ;; | |
3468 | esac | |
3469 | } | |
3470 | ||
3471 | function stat_crtime #<path> | |
3472 | { | |
3473 | typeset path=$1 | |
3474 | ||
5c9f744b | 3475 | case "$UNAME" in |
8ae86e2e RM |
3476 | FreeBSD) |
3477 | stat -f %B "$path" | |
3478 | ;; | |
3479 | *) | |
3480 | stat -c %W "$path" | |
3481 | ;; | |
3482 | esac | |
3483 | } | |
3484 | ||
3fa5266d RM |
3485 | function stat_generation #<path> |
3486 | { | |
3487 | typeset path=$1 | |
3488 | ||
5c9f744b | 3489 | case "$UNAME" in |
3fa5266d RM |
3490 | Linux) |
3491 | getversion "${path}" | |
3492 | ;; | |
3493 | *) | |
3494 | stat -f %v "${path}" | |
3495 | ;; | |
3496 | esac | |
3497 | } | |
3498 | ||
9fb2771a TH |
3499 | # Run a command as if it was being run in a TTY. |
3500 | # | |
3501 | # Usage: | |
3502 | # | |
3503 | # faketty command | |
3504 | # | |
3505 | function faketty | |
3506 | { | |
3507 | if is_freebsd; then | |
834f274f | 3508 | script -q /dev/null env "$@" |
9fb2771a TH |
3509 | else |
3510 | script --return --quiet -c "$*" /dev/null | |
3511 | fi | |
3512 | } | |
90ae4873 RM |
3513 | |
3514 | # | |
3515 | # Produce a random permutation of the integers in a given range (inclusive). | |
3516 | # | |
3517 | function range_shuffle # begin end | |
3518 | { | |
3519 | typeset -i begin=$1 | |
3520 | typeset -i end=$2 | |
3521 | ||
7a298ae9 | 3522 | seq ${begin} ${end} | sort -R |
90ae4873 | 3523 | } |
6e1c594d RM |
3524 | |
3525 | # | |
3526 | # Cross-platform xattr helpers | |
3527 | # | |
3528 | ||
3529 | function get_xattr # name path | |
3530 | { | |
3531 | typeset name=$1 | |
3532 | typeset path=$2 | |
3533 | ||
5c9f744b | 3534 | case "$UNAME" in |
6e1c594d RM |
3535 | FreeBSD) |
3536 | getextattr -qq user "${name}" "${path}" | |
3537 | ;; | |
3538 | *) | |
3539 | attr -qg "${name}" "${path}" | |
3540 | ;; | |
3541 | esac | |
3542 | } | |
3543 | ||
3544 | function set_xattr # name value path | |
3545 | { | |
3546 | typeset name=$1 | |
3547 | typeset value=$2 | |
3548 | typeset path=$3 | |
3549 | ||
5c9f744b | 3550 | case "$UNAME" in |
6e1c594d RM |
3551 | FreeBSD) |
3552 | setextattr user "${name}" "${value}" "${path}" | |
3553 | ;; | |
3554 | *) | |
3555 | attr -qs "${name}" -V "${value}" "${path}" | |
3556 | ;; | |
3557 | esac | |
3558 | } | |
3559 | ||
3560 | function set_xattr_stdin # name value | |
3561 | { | |
3562 | typeset name=$1 | |
3563 | typeset path=$2 | |
3564 | ||
5c9f744b | 3565 | case "$UNAME" in |
6e1c594d RM |
3566 | FreeBSD) |
3567 | setextattr -i user "${name}" "${path}" | |
3568 | ;; | |
3569 | *) | |
3570 | attr -qs "${name}" "${path}" | |
3571 | ;; | |
3572 | esac | |
3573 | } | |
3574 | ||
3575 | function rm_xattr # name path | |
3576 | { | |
3577 | typeset name=$1 | |
3578 | typeset path=$2 | |
3579 | ||
5c9f744b | 3580 | case "$UNAME" in |
6e1c594d RM |
3581 | FreeBSD) |
3582 | rmextattr -q user "${name}" "${path}" | |
3583 | ;; | |
3584 | *) | |
3585 | attr -qr "${name}" "${path}" | |
3586 | ;; | |
3587 | esac | |
3588 | } | |
3589 | ||
3590 | function ls_xattr # path | |
3591 | { | |
3592 | typeset path=$1 | |
3593 | ||
5c9f744b | 3594 | case "$UNAME" in |
6e1c594d RM |
3595 | FreeBSD) |
3596 | lsextattr -qq user "${path}" | |
3597 | ;; | |
3598 | *) | |
3599 | attr -ql "${path}" | |
3600 | ;; | |
3601 | esac | |
3602 | } | |
37c22948 | 3603 | |
73989f4b RM |
3604 | function kstat # stat flags? |
3605 | { | |
3606 | typeset stat=$1 | |
3607 | typeset flags=${2-"-n"} | |
3608 | ||
5c9f744b | 3609 | case "$UNAME" in |
73989f4b RM |
3610 | FreeBSD) |
3611 | sysctl $flags kstat.zfs.misc.$stat | |
3612 | ;; | |
3613 | Linux) | |
75746e9a | 3614 | cat "/proc/spl/kstat/zfs/$stat" 2>/dev/null |
73989f4b RM |
3615 | ;; |
3616 | *) | |
3617 | false | |
3618 | ;; | |
3619 | esac | |
3620 | } | |
3621 | ||
37c22948 GA |
3622 | function get_arcstat # stat |
3623 | { | |
9f0a21e6 MM |
3624 | typeset stat=$1 |
3625 | ||
5c9f744b | 3626 | case "$UNAME" in |
9f0a21e6 | 3627 | FreeBSD) |
73989f4b | 3628 | kstat arcstats.$stat |
9f0a21e6 MM |
3629 | ;; |
3630 | Linux) | |
75746e9a | 3631 | kstat arcstats | awk "/$stat/"' { print $3 }' |
9f0a21e6 MM |
3632 | ;; |
3633 | *) | |
3634 | false | |
3635 | ;; | |
3636 | esac | |
37c22948 | 3637 | } |
c15d36c6 | 3638 | |
c3cb57ae KHN |
3639 | function punch_hole # offset length file |
3640 | { | |
3641 | typeset offset=$1 | |
3642 | typeset length=$2 | |
3643 | typeset file=$3 | |
3644 | ||
5c9f744b | 3645 | case "$UNAME" in |
c3cb57ae KHN |
3646 | FreeBSD) |
3647 | truncate -d -o $offset -l $length "$file" | |
3648 | ;; | |
3649 | Linux) | |
3650 | fallocate --punch-hole --offset $offset --length $length "$file" | |
3651 | ;; | |
3652 | *) | |
3653 | false | |
3654 | ;; | |
3655 | esac | |
3656 | } | |
3657 | ||
b657f2c5 RE |
3658 | function zero_range # offset length file |
3659 | { | |
3660 | typeset offset=$1 | |
3661 | typeset length=$2 | |
3662 | typeset file=$3 | |
3663 | ||
3664 | case "$UNAME" in | |
3665 | Linux) | |
3666 | fallocate --zero-range --offset $offset --length $length "$file" | |
3667 | ;; | |
3668 | *) | |
3669 | false | |
3670 | ;; | |
3671 | esac | |
3672 | } | |
3673 | ||
a76e4e67 GA |
3674 | # |
3675 | # Wait for the specified arcstat to reach non-zero quiescence. | |
3676 | # If echo is 1 echo the value after reaching quiescence, otherwise | |
3677 | # if echo is 0 print the arcstat we are waiting on. | |
3678 | # | |
3679 | function arcstat_quiescence # stat echo | |
3680 | { | |
3681 | typeset stat=$1 | |
3682 | typeset echo=$2 | |
3683 | typeset do_once=true | |
3684 | ||
3685 | if [[ $echo -eq 0 ]]; then | |
3686 | echo "Waiting for arcstat $1 quiescence." | |
3687 | fi | |
3688 | ||
3689 | while $do_once || [ $stat1 -ne $stat2 ] || [ $stat2 -eq 0 ]; do | |
3690 | typeset stat1=$(get_arcstat $stat) | |
3691 | sleep 2 | |
3692 | typeset stat2=$(get_arcstat $stat) | |
3693 | do_once=false | |
3694 | done | |
3695 | ||
3696 | if [[ $echo -eq 1 ]]; then | |
3697 | echo $stat2 | |
3698 | fi | |
3699 | } | |
3700 | ||
3701 | function arcstat_quiescence_noecho # stat | |
3702 | { | |
3703 | typeset stat=$1 | |
3704 | arcstat_quiescence $stat 0 | |
3705 | } | |
3706 | ||
3707 | function arcstat_quiescence_echo # stat | |
3708 | { | |
3709 | typeset stat=$1 | |
3710 | arcstat_quiescence $stat 1 | |
3711 | } | |
3712 | ||
c15d36c6 GW |
3713 | # |
3714 | # Given an array of pids, wait until all processes | |
3715 | # have completed and check their return status. | |
3716 | # | |
3717 | function wait_for_children #children | |
3718 | { | |
3719 | rv=0 | |
3720 | children=("$@") | |
3721 | for child in "${children[@]}" | |
3722 | do | |
3723 | child_exit=0 | |
3724 | wait ${child} || child_exit=$? | |
3725 | if [ $child_exit -ne 0 ]; then | |
3726 | echo "child ${child} failed with ${child_exit}" | |
3727 | rv=1 | |
3728 | fi | |
3729 | done | |
3730 | return $rv | |
3731 | } | |
669683c4 AS |
3732 | |
3733 | # | |
3734 | # Compare two directory trees recursively in a manner similar to diff(1), but | |
3735 | # using rsync. If there are any discrepancies, a summary of the differences are | |
3736 | # output and a non-zero error is returned. | |
3737 | # | |
3738 | # If you're comparing a directory after a ZIL replay, you should set | |
3739 | # LIBTEST_DIFF_ZIL_REPLAY=1 or use replay_directory_diff which will cause | |
3740 | # directory_diff to ignore mtime changes (the ZIL replay won't fix up mtime | |
3741 | # information). | |
3742 | # | |
3743 | function directory_diff # dir_a dir_b | |
3744 | { | |
3745 | dir_a="$1" | |
3746 | dir_b="$2" | |
3747 | zil_replay="${LIBTEST_DIFF_ZIL_REPLAY:-0}" | |
3748 | ||
3749 | # If one of the directories doesn't exist, return 2. This is to match the | |
3750 | # semantics of diff. | |
3751 | if ! [ -d "$dir_a" -a -d "$dir_b" ]; then | |
3752 | return 2 | |
3753 | fi | |
3754 | ||
3755 | # Run rsync with --dry-run --itemize-changes to get something akin to diff | |
3756 | # output, but rsync is far more thorough in detecting differences (diff | |
3757 | # doesn't compare file metadata, and cannot handle special files). | |
3758 | # | |
3759 | # Also make sure to filter out non-user.* xattrs when comparing. On | |
3760 | # SELinux-enabled systems the copied tree will probably have different | |
3761 | # SELinux labels. | |
3762 | args=("-nicaAHX" '--filter=-x! user.*' "--delete") | |
3763 | ||
3764 | # NOTE: Quite a few rsync builds do not support --crtimes which would be | |
3765 | # necessary to verify that creation times are being maintained properly. | |
3766 | # Unfortunately because of this we cannot use it unconditionally but we can | |
3767 | # check if this rsync build supports it and use it then. This check is | |
3768 | # based on the same check in the rsync test suite (testsuite/crtimes.test). | |
3769 | # | |
3770 | # We check ctimes even with zil_replay=1 because the ZIL does store | |
3771 | # creation times and we should make sure they match (if the creation times | |
3772 | # do not match there is a "c" entry in one of the columns). | |
964d4180 | 3773 | if rsync --version | grep -q "[, ] crtimes"; then |
669683c4 AS |
3774 | args+=("--crtimes") |
3775 | else | |
964d4180 | 3776 | log_note "This rsync package does not support --crtimes (-N)." |
669683c4 AS |
3777 | fi |
3778 | ||
3779 | # If we are testing a ZIL replay, we need to ignore timestamp changes. | |
3780 | # Unfortunately --no-times doesn't do what we want -- it will still tell | |
3781 | # you if the timestamps don't match but rsync will set the timestamps to | |
3782 | # the current time (leading to an itemised change entry). It's simpler to | |
3783 | # just filter out those lines. | |
3784 | if [ "$zil_replay" -eq 0 ]; then | |
3785 | filter=("cat") | |
3786 | else | |
3787 | # Different rsync versions have different numbers of columns. So just | |
3788 | # require that aside from the first two, all other columns must be | |
3789 | # blank (literal ".") or a timestamp field ("[tT]"). | |
3790 | filter=("grep" "-v" '^\..[.Tt]\+ ') | |
3791 | fi | |
3792 | ||
3793 | diff="$(rsync "${args[@]}" "$dir_a/" "$dir_b/" | "${filter[@]}")" | |
3794 | rv=0 | |
3795 | if [ -n "$diff" ]; then | |
3796 | echo "$diff" | |
3797 | rv=1 | |
3798 | fi | |
3799 | return $rv | |
3800 | } | |
3801 | ||
3802 | # | |
3803 | # Compare two directory trees recursively, without checking whether the mtimes | |
3804 | # match (creation times will be checked if the available rsync binary supports | |
3805 | # it). This is necessary for ZIL replay checks (because the ZIL does not | |
3806 | # contain mtimes and thus after a ZIL replay, mtimes won't match). | |
3807 | # | |
3808 | # This is shorthand for LIBTEST_DIFF_ZIL_REPLAY=1 directory_diff <...>. | |
3809 | # | |
3810 | function replay_directory_diff # dir_a dir_b | |
3811 | { | |
3812 | LIBTEST_DIFF_ZIL_REPLAY=1 directory_diff "$@" | |
669683c4 | 3813 | } |
598fed7e AZ |
3814 | |
3815 | # | |
3816 | # Put coredumps into $1/core.{basename} | |
3817 | # | |
3818 | # Output must be saved and passed to pop_coredump_pattern on cleanup | |
3819 | # | |
3820 | function push_coredump_pattern # dir | |
3821 | { | |
3822 | ulimit -c unlimited | |
5c9f744b | 3823 | case "$UNAME" in |
598fed7e AZ |
3824 | Linux) |
3825 | cat /proc/sys/kernel/core_pattern /proc/sys/kernel/core_uses_pid | |
3826 | echo "$1/core.%e" >/proc/sys/kernel/core_pattern && | |
3827 | echo 0 >/proc/sys/kernel/core_uses_pid | |
3828 | ;; | |
3829 | FreeBSD) | |
3830 | sysctl -n kern.corefile | |
3831 | sysctl kern.corefile="$1/core.%N" >/dev/null | |
3832 | ;; | |
3833 | *) | |
3834 | # Nothing to output – set only for this shell | |
3835 | coreadm -p "$1/core.%f" | |
3836 | ;; | |
3837 | esac | |
3838 | } | |
3839 | ||
3840 | # | |
3841 | # Put coredumps back into the default location | |
3842 | # | |
3843 | function pop_coredump_pattern | |
3844 | { | |
3845 | [ -s "$1" ] || return 0 | |
5c9f744b | 3846 | case "$UNAME" in |
598fed7e AZ |
3847 | Linux) |
3848 | typeset pat pid | |
3849 | { read -r pat; read -r pid; } < "$1" | |
3850 | echo "$pat" >/proc/sys/kernel/core_pattern && | |
3851 | echo "$pid" >/proc/sys/kernel/core_uses_pid | |
3852 | ;; | |
3853 | FreeBSD) | |
3854 | sysctl kern.corefile="$(<"$1")" >/dev/null | |
3855 | ;; | |
3856 | esac | |
3857 | } |