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