]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/include/libtest.shlib
OpenZFS 7290 - ZFS test suite needs to control what utilities it can run
[mirror_zfs.git] / tests / zfs-tests / include / libtest.shlib
1 #!/bin/ksh -p
2 #
3 # CDDL HEADER START
4 #
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
8 #
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
13 #
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 #
20 # CDDL HEADER END
21 #
22
23 #
24 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
26 # Copyright (c) 2012, 2016 by Delphix. All rights reserved.
27 # Copyright 2016 Nexenta Systems, Inc.
28 # Copyright (c) 2017 Lawrence Livermore National Security, LLC.
29 #
30
31 . ${STF_TOOLS}/include/logapi.shlib
32
33 #
34 # Apply constrained path when available. This is required since the
35 # PATH may have been modified by sudo's secure_path behavior.
36 #
37 if [ -n "$STF_PATH" ]; then
38 PATH="$STF_PATH"
39 fi
40
41 # Determine if this is a Linux test system
42 #
43 # Return 0 if platform Linux, 1 if otherwise
44
45 function is_linux
46 {
47 if [[ $(uname -o) == "GNU/Linux" ]]; then
48 return 0
49 else
50 return 1
51 fi
52 }
53
54 # Determine if this is a 32-bit system
55 #
56 # Return 0 if platform is 32-bit, 1 if otherwise
57
58 function is_32bit
59 {
60 if [[ $(getconf LONG_BIT) == "32" ]]; then
61 return 0
62 else
63 return 1
64 fi
65 }
66
67 # Determine if kmemleak is enabled
68 #
69 # Return 0 if kmemleak is enabled, 1 if otherwise
70
71 function is_kmemleak
72 {
73 if is_linux && [[ -e /sys/kernel/debug/kmemleak ]]; then
74 return 0
75 else
76 return 1
77 fi
78 }
79
80 # Determine whether a dataset is mounted
81 #
82 # $1 dataset name
83 # $2 filesystem type; optional - defaulted to zfs
84 #
85 # Return 0 if dataset is mounted; 1 if unmounted; 2 on error
86
87 function ismounted
88 {
89 typeset fstype=$2
90 [[ -z $fstype ]] && fstype=zfs
91 typeset out dir name ret
92
93 case $fstype in
94 zfs)
95 if [[ "$1" == "/"* ]] ; then
96 for out in $(zfs mount | awk '{print $2}'); do
97 [[ $1 == $out ]] && return 0
98 done
99 else
100 for out in $(zfs mount | awk '{print $1}'); do
101 [[ $1 == $out ]] && return 0
102 done
103 fi
104 ;;
105 ufs|nfs)
106 out=$(df -F $fstype $1 2>/dev/null)
107 ret=$?
108 (($ret != 0)) && return $ret
109
110 dir=${out%%\(*}
111 dir=${dir%% *}
112 name=${out##*\(}
113 name=${name%%\)*}
114 name=${name%% *}
115
116 [[ "$1" == "$dir" || "$1" == "$name" ]] && return 0
117 ;;
118 ext2)
119 out=$(df -t $fstype $1 2>/dev/null)
120 return $?
121 ;;
122 zvol)
123 if [[ -L "$ZVOL_DEVDIR/$1" ]]; then
124 link=$(readlink -f $ZVOL_DEVDIR/$1)
125 [[ -n "$link" ]] && \
126 mount | grep -q "^$link" && \
127 return 0
128 fi
129 ;;
130 esac
131
132 return 1
133 }
134
135 # Return 0 if a dataset is mounted; 1 otherwise
136 #
137 # $1 dataset name
138 # $2 filesystem type; optional - defaulted to zfs
139
140 function mounted
141 {
142 ismounted $1 $2
143 (($? == 0)) && return 0
144 return 1
145 }
146
147 # Return 0 if a dataset is unmounted; 1 otherwise
148 #
149 # $1 dataset name
150 # $2 filesystem type; optional - defaulted to zfs
151
152 function unmounted
153 {
154 ismounted $1 $2
155 (($? == 1)) && return 0
156 return 1
157 }
158
159 # split line on ","
160 #
161 # $1 - line to split
162
163 function splitline
164 {
165 echo $1 | sed "s/,/ /g"
166 }
167
168 function default_setup
169 {
170 default_setup_noexit "$@"
171
172 log_pass
173 }
174
175 #
176 # Given a list of disks, setup storage pools and datasets.
177 #
178 function default_setup_noexit
179 {
180 typeset disklist=$1
181 typeset container=$2
182 typeset volume=$3
183 log_note begin default_setup_noexit
184
185 if is_global_zone; then
186 if poolexists $TESTPOOL ; then
187 destroy_pool $TESTPOOL
188 fi
189 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
190 log_must zpool create -f $TESTPOOL $disklist
191 else
192 reexport_pool
193 fi
194
195 rm -rf $TESTDIR || log_unresolved Could not remove $TESTDIR
196 mkdir -p $TESTDIR || log_unresolved Could not create $TESTDIR
197
198 log_must zfs create $TESTPOOL/$TESTFS
199 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
200
201 if [[ -n $container ]]; then
202 rm -rf $TESTDIR1 || \
203 log_unresolved Could not remove $TESTDIR1
204 mkdir -p $TESTDIR1 || \
205 log_unresolved Could not create $TESTDIR1
206
207 log_must zfs create $TESTPOOL/$TESTCTR
208 log_must zfs set canmount=off $TESTPOOL/$TESTCTR
209 log_must zfs create $TESTPOOL/$TESTCTR/$TESTFS1
210 log_must zfs set mountpoint=$TESTDIR1 \
211 $TESTPOOL/$TESTCTR/$TESTFS1
212 fi
213
214 if [[ -n $volume ]]; then
215 if is_global_zone ; then
216 log_must zfs create -V $VOLSIZE $TESTPOOL/$TESTVOL
217 block_device_wait
218 else
219 log_must zfs create $TESTPOOL/$TESTVOL
220 fi
221 fi
222 }
223
224 #
225 # Given a list of disks, setup a storage pool, file system and
226 # a container.
227 #
228 function default_container_setup
229 {
230 typeset disklist=$1
231
232 default_setup "$disklist" "true"
233 }
234
235 #
236 # Given a list of disks, setup a storage pool,file system
237 # and a volume.
238 #
239 function default_volume_setup
240 {
241 typeset disklist=$1
242
243 default_setup "$disklist" "" "true"
244 }
245
246 #
247 # Given a list of disks, setup a storage pool,file system,
248 # a container and a volume.
249 #
250 function default_container_volume_setup
251 {
252 typeset disklist=$1
253
254 default_setup "$disklist" "true" "true"
255 }
256
257 #
258 # Create a snapshot on a filesystem or volume. Defaultly create a snapshot on
259 # filesystem
260 #
261 # $1 Existing filesystem or volume name. Default, $TESTFS
262 # $2 snapshot name. Default, $TESTSNAP
263 #
264 function create_snapshot
265 {
266 typeset fs_vol=${1:-$TESTFS}
267 typeset snap=${2:-$TESTSNAP}
268
269 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
270 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
271
272 if snapexists $fs_vol@$snap; then
273 log_fail "$fs_vol@$snap already exists."
274 fi
275 datasetexists $fs_vol || \
276 log_fail "$fs_vol must exist."
277
278 log_must zfs snapshot $fs_vol@$snap
279 }
280
281 #
282 # Create a clone from a snapshot, default clone name is $TESTCLONE.
283 #
284 # $1 Existing snapshot, $TESTPOOL/$TESTFS@$TESTSNAP is default.
285 # $2 Clone name, $TESTPOOL/$TESTCLONE is default.
286 #
287 function create_clone # snapshot clone
288 {
289 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
290 typeset clone=${2:-$TESTPOOL/$TESTCLONE}
291
292 [[ -z $snap ]] && \
293 log_fail "Snapshot name is undefined."
294 [[ -z $clone ]] && \
295 log_fail "Clone name is undefined."
296
297 log_must zfs clone $snap $clone
298 }
299
300 #
301 # Create a bookmark of the given snapshot. Defaultly create a bookmark on
302 # filesystem.
303 #
304 # $1 Existing filesystem or volume name. Default, $TESTFS
305 # $2 Existing snapshot name. Default, $TESTSNAP
306 # $3 bookmark name. Default, $TESTBKMARK
307 #
308 function create_bookmark
309 {
310 typeset fs_vol=${1:-$TESTFS}
311 typeset snap=${2:-$TESTSNAP}
312 typeset bkmark=${3:-$TESTBKMARK}
313
314 [[ -z $fs_vol ]] && log_fail "Filesystem or volume's name is undefined."
315 [[ -z $snap ]] && log_fail "Snapshot's name is undefined."
316 [[ -z $bkmark ]] && log_fail "Bookmark's name is undefined."
317
318 if bkmarkexists $fs_vol#$bkmark; then
319 log_fail "$fs_vol#$bkmark already exists."
320 fi
321 datasetexists $fs_vol || \
322 log_fail "$fs_vol must exist."
323 snapexists $fs_vol@$snap || \
324 log_fail "$fs_vol@$snap must exist."
325
326 log_must zfs bookmark $fs_vol@$snap $fs_vol#$bkmark
327 }
328
329 function default_mirror_setup
330 {
331 default_mirror_setup_noexit $1 $2 $3
332
333 log_pass
334 }
335
336 #
337 # Given a pair of disks, set up a storage pool and dataset for the mirror
338 # @parameters: $1 the primary side of the mirror
339 # $2 the secondary side of the mirror
340 # @uses: ZPOOL ZFS TESTPOOL TESTFS
341 function default_mirror_setup_noexit
342 {
343 readonly func="default_mirror_setup_noexit"
344 typeset primary=$1
345 typeset secondary=$2
346
347 [[ -z $primary ]] && \
348 log_fail "$func: No parameters passed"
349 [[ -z $secondary ]] && \
350 log_fail "$func: No secondary partition passed"
351 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
352 log_must zpool create -f $TESTPOOL mirror $@
353 log_must zfs create $TESTPOOL/$TESTFS
354 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
355 }
356
357 #
358 # create a number of mirrors.
359 # We create a number($1) of 2 way mirrors using the pairs of disks named
360 # on the command line. These mirrors are *not* mounted
361 # @parameters: $1 the number of mirrors to create
362 # $... the devices to use to create the mirrors on
363 # @uses: ZPOOL ZFS TESTPOOL
364 function setup_mirrors
365 {
366 typeset -i nmirrors=$1
367
368 shift
369 while ((nmirrors > 0)); do
370 log_must test -n "$1" -a -n "$2"
371 [[ -d /$TESTPOOL$nmirrors ]] && rm -rf /$TESTPOOL$nmirrors
372 log_must zpool create -f $TESTPOOL$nmirrors mirror $1 $2
373 shift 2
374 ((nmirrors = nmirrors - 1))
375 done
376 }
377
378 #
379 # create a number of raidz pools.
380 # We create a number($1) of 2 raidz pools using the pairs of disks named
381 # on the command line. These pools are *not* mounted
382 # @parameters: $1 the number of pools to create
383 # $... the devices to use to create the pools on
384 # @uses: ZPOOL ZFS TESTPOOL
385 function setup_raidzs
386 {
387 typeset -i nraidzs=$1
388
389 shift
390 while ((nraidzs > 0)); do
391 log_must test -n "$1" -a -n "$2"
392 [[ -d /$TESTPOOL$nraidzs ]] && rm -rf /$TESTPOOL$nraidzs
393 log_must zpool create -f $TESTPOOL$nraidzs raidz $1 $2
394 shift 2
395 ((nraidzs = nraidzs - 1))
396 done
397 }
398
399 #
400 # Destroy the configured testpool mirrors.
401 # the mirrors are of the form ${TESTPOOL}{number}
402 # @uses: ZPOOL ZFS TESTPOOL
403 function destroy_mirrors
404 {
405 default_cleanup_noexit
406
407 log_pass
408 }
409
410 #
411 # Given a minimum of two disks, set up a storage pool and dataset for the raid-z
412 # $1 the list of disks
413 #
414 function default_raidz_setup
415 {
416 typeset disklist="$*"
417 disks=(${disklist[*]})
418
419 if [[ ${#disks[*]} -lt 2 ]]; then
420 log_fail "A raid-z requires a minimum of two disks."
421 fi
422
423 [[ -d /$TESTPOOL ]] && rm -rf /$TESTPOOL
424 log_must zpool create -f $TESTPOOL raidz $1 $2 $3
425 log_must zfs create $TESTPOOL/$TESTFS
426 log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
427
428 log_pass
429 }
430
431 #
432 # Common function used to cleanup storage pools and datasets.
433 #
434 # Invoked at the start of the test suite to ensure the system
435 # is in a known state, and also at the end of each set of
436 # sub-tests to ensure errors from one set of tests doesn't
437 # impact the execution of the next set.
438
439 function default_cleanup
440 {
441 default_cleanup_noexit
442
443 log_pass
444 }
445
446 function default_cleanup_noexit
447 {
448 typeset exclude=""
449 typeset pool=""
450 #
451 # Destroying the pool will also destroy any
452 # filesystems it contains.
453 #
454 if is_global_zone; then
455 zfs unmount -a > /dev/null 2>&1
456 exclude=`eval echo \"'(${KEEP})'\"`
457 ALL_POOLS=$(zpool list -H -o name \
458 | grep -v "$NO_POOLS" | egrep -v "$exclude")
459 # Here, we loop through the pools we're allowed to
460 # destroy, only destroying them if it's safe to do
461 # so.
462 while [ ! -z ${ALL_POOLS} ]
463 do
464 for pool in ${ALL_POOLS}
465 do
466 if safe_to_destroy_pool $pool ;
467 then
468 destroy_pool $pool
469 fi
470 ALL_POOLS=$(zpool list -H -o name \
471 | grep -v "$NO_POOLS" \
472 | egrep -v "$exclude")
473 done
474 done
475
476 zfs mount -a
477 else
478 typeset fs=""
479 for fs in $(zfs list -H -o name \
480 | grep "^$ZONE_POOL/$ZONE_CTR[01234]/"); do
481 datasetexists $fs && \
482 log_must zfs destroy -Rf $fs
483 done
484
485 # Need cleanup here to avoid garbage dir left.
486 for fs in $(zfs list -H -o name); do
487 [[ $fs == /$ZONE_POOL ]] && continue
488 [[ -d $fs ]] && log_must rm -rf $fs/*
489 done
490
491 #
492 # Reset the $ZONE_POOL/$ZONE_CTR[01234] file systems property to
493 # the default value
494 #
495 for fs in $(zfs list -H -o name); do
496 if [[ $fs == $ZONE_POOL/$ZONE_CTR[01234] ]]; then
497 log_must zfs set reservation=none $fs
498 log_must zfs set recordsize=128K $fs
499 log_must zfs set mountpoint=/$fs $fs
500 typeset enc=""
501 enc=$(get_prop encryption $fs)
502 if [[ $? -ne 0 ]] || [[ -z "$enc" ]] || \
503 [[ "$enc" == "off" ]]; then
504 log_must zfs set checksum=on $fs
505 fi
506 log_must zfs set compression=off $fs
507 log_must zfs set atime=on $fs
508 log_must zfs set devices=off $fs
509 log_must zfs set exec=on $fs
510 log_must zfs set setuid=on $fs
511 log_must zfs set readonly=off $fs
512 log_must zfs set snapdir=hidden $fs
513 log_must zfs set aclmode=groupmask $fs
514 log_must zfs set aclinherit=secure $fs
515 fi
516 done
517 fi
518
519 [[ -d $TESTDIR ]] && \
520 log_must rm -rf $TESTDIR
521
522 disk1=${DISKS%% *}
523 if is_mpath_device $disk1; then
524 delete_partitions
525 fi
526 }
527
528
529 #
530 # Common function used to cleanup storage pools, file systems
531 # and containers.
532 #
533 function default_container_cleanup
534 {
535 if ! is_global_zone; then
536 reexport_pool
537 fi
538
539 ismounted $TESTPOOL/$TESTCTR/$TESTFS1
540 [[ $? -eq 0 ]] && \
541 log_must zfs unmount $TESTPOOL/$TESTCTR/$TESTFS1
542
543 datasetexists $TESTPOOL/$TESTCTR/$TESTFS1 && \
544 log_must zfs destroy -R $TESTPOOL/$TESTCTR/$TESTFS1
545
546 datasetexists $TESTPOOL/$TESTCTR && \
547 log_must zfs destroy -Rf $TESTPOOL/$TESTCTR
548
549 [[ -e $TESTDIR1 ]] && \
550 log_must rm -rf $TESTDIR1 > /dev/null 2>&1
551
552 default_cleanup
553 }
554
555 #
556 # Common function used to cleanup snapshot of file system or volume. Default to
557 # delete the file system's snapshot
558 #
559 # $1 snapshot name
560 #
561 function destroy_snapshot
562 {
563 typeset snap=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
564
565 if ! snapexists $snap; then
566 log_fail "'$snap' does not existed."
567 fi
568
569 #
570 # For the sake of the value which come from 'get_prop' is not equal
571 # to the really mountpoint when the snapshot is unmounted. So, firstly
572 # check and make sure this snapshot's been mounted in current system.
573 #
574 typeset mtpt=""
575 if ismounted $snap; then
576 mtpt=$(get_prop mountpoint $snap)
577 (($? != 0)) && \
578 log_fail "get_prop mountpoint $snap failed."
579 fi
580
581 log_must zfs destroy $snap
582 [[ $mtpt != "" && -d $mtpt ]] && \
583 log_must rm -rf $mtpt
584 }
585
586 #
587 # Common function used to cleanup clone.
588 #
589 # $1 clone name
590 #
591 function destroy_clone
592 {
593 typeset clone=${1:-$TESTPOOL/$TESTCLONE}
594
595 if ! datasetexists $clone; then
596 log_fail "'$clone' does not existed."
597 fi
598
599 # With the same reason in destroy_snapshot
600 typeset mtpt=""
601 if ismounted $clone; then
602 mtpt=$(get_prop mountpoint $clone)
603 (($? != 0)) && \
604 log_fail "get_prop mountpoint $clone failed."
605 fi
606
607 log_must zfs destroy $clone
608 [[ $mtpt != "" && -d $mtpt ]] && \
609 log_must rm -rf $mtpt
610 }
611
612 #
613 # Common function used to cleanup bookmark of file system or volume. Default
614 # to delete the file system's bookmark.
615 #
616 # $1 bookmark name
617 #
618 function destroy_bookmark
619 {
620 typeset bkmark=${1:-$TESTPOOL/$TESTFS#$TESTBKMARK}
621
622 if ! bkmarkexists $bkmark; then
623 log_fail "'$bkmarkp' does not existed."
624 fi
625
626 log_must zfs destroy $bkmark
627 }
628
629 # Return 0 if a snapshot exists; $? otherwise
630 #
631 # $1 - snapshot name
632
633 function snapexists
634 {
635 zfs list -H -t snapshot "$1" > /dev/null 2>&1
636 return $?
637 }
638
639 #
640 # Return 0 if a bookmark exists; $? otherwise
641 #
642 # $1 - bookmark name
643 #
644 function bkmarkexists
645 {
646 zfs list -H -t bookmark "$1" > /dev/null 2>&1
647 return $?
648 }
649
650 #
651 # Set a property to a certain value on a dataset.
652 # Sets a property of the dataset to the value as passed in.
653 # @param:
654 # $1 dataset who's property is being set
655 # $2 property to set
656 # $3 value to set property to
657 # @return:
658 # 0 if the property could be set.
659 # non-zero otherwise.
660 # @use: ZFS
661 #
662 function dataset_setprop
663 {
664 typeset fn=dataset_setprop
665
666 if (($# < 3)); then
667 log_note "$fn: Insufficient parameters (need 3, had $#)"
668 return 1
669 fi
670 typeset output=
671 output=$(zfs set $2=$3 $1 2>&1)
672 typeset rv=$?
673 if ((rv != 0)); then
674 log_note "Setting property on $1 failed."
675 log_note "property $2=$3"
676 log_note "Return Code: $rv"
677 log_note "Output: $output"
678 return $rv
679 fi
680 return 0
681 }
682
683 #
684 # Assign suite defined dataset properties.
685 # This function is used to apply the suite's defined default set of
686 # properties to a dataset.
687 # @parameters: $1 dataset to use
688 # @uses: ZFS COMPRESSION_PROP CHECKSUM_PROP
689 # @returns:
690 # 0 if the dataset has been altered.
691 # 1 if no pool name was passed in.
692 # 2 if the dataset could not be found.
693 # 3 if the dataset could not have it's properties set.
694 #
695 function dataset_set_defaultproperties
696 {
697 typeset dataset="$1"
698
699 [[ -z $dataset ]] && return 1
700
701 typeset confset=
702 typeset -i found=0
703 for confset in $(zfs list); do
704 if [[ $dataset = $confset ]]; then
705 found=1
706 break
707 fi
708 done
709 [[ $found -eq 0 ]] && return 2
710 if [[ -n $COMPRESSION_PROP ]]; then
711 dataset_setprop $dataset compression $COMPRESSION_PROP || \
712 return 3
713 log_note "Compression set to '$COMPRESSION_PROP' on $dataset"
714 fi
715 if [[ -n $CHECKSUM_PROP ]]; then
716 dataset_setprop $dataset checksum $CHECKSUM_PROP || \
717 return 3
718 log_note "Checksum set to '$CHECKSUM_PROP' on $dataset"
719 fi
720 return 0
721 }
722
723 #
724 # Check a numeric assertion
725 # @parameter: $@ the assertion to check
726 # @output: big loud notice if assertion failed
727 # @use: log_fail
728 #
729 function assert
730 {
731 (($@)) || log_fail "$@"
732 }
733
734 #
735 # Function to format partition size of a disk
736 # Given a disk cxtxdx reduces all partitions
737 # to 0 size
738 #
739 function zero_partitions #<whole_disk_name>
740 {
741 typeset diskname=$1
742 typeset i
743
744 if is_linux; then
745 log_must parted $DEV_DSKDIR/$diskname -s -- mklabel gpt
746 else
747 for i in 0 1 3 4 5 6 7
748 do
749 set_partition $i "" 0mb $diskname
750 done
751 fi
752 }
753
754 #
755 # Given a slice, size and disk, this function
756 # formats the slice to the specified size.
757 # Size should be specified with units as per
758 # the `format` command requirements eg. 100mb 3gb
759 #
760 # NOTE: This entire interface is problematic for the Linux parted utilty
761 # which requires the end of the partition to be specified. It would be
762 # best to retire this interface and replace it with something more flexible.
763 # At the moment a best effort is made.
764 #
765 function set_partition #<slice_num> <slice_start> <size_plus_units> <whole_disk_name>
766 {
767 typeset -i slicenum=$1
768 typeset start=$2
769 typeset size=$3
770 typeset disk=$4
771 [[ -z $slicenum || -z $size || -z $disk ]] && \
772 log_fail "The slice, size or disk name is unspecified."
773
774 if is_linux; then
775 typeset size_mb=${size%%[mMgG]}
776
777 size_mb=${size_mb%%[mMgG][bB]}
778 if [[ ${size:1:1} == 'g' ]]; then
779 ((size_mb = size_mb * 1024))
780 fi
781
782 # Create GPT partition table when setting slice 0 or
783 # when the device doesn't already contain a GPT label.
784 parted $DEV_DSKDIR/$disk -s -- print 1 >/dev/null
785 typeset ret_val=$?
786 if [[ $slicenum -eq 0 || $ret_val -ne 0 ]]; then
787 log_must parted $DEV_DSKDIR/$disk -s -- mklabel gpt
788 fi
789
790 # When no start is given align on the first cylinder.
791 if [[ -z "$start" ]]; then
792 start=1
793 fi
794
795 # Determine the cylinder size for the device and using
796 # that calculate the end offset in cylinders.
797 typeset -i cly_size_kb=0
798 cly_size_kb=$(parted -m $DEV_DSKDIR/$disk -s -- \
799 unit cyl print | head -3 | tail -1 | \
800 awk -F '[:k.]' '{print $4}')
801 ((end = (size_mb * 1024 / cly_size_kb) + start))
802
803 log_must parted $DEV_DSKDIR/$disk -s -- \
804 mkpart part$slicenum ${start}cyl ${end}cyl
805
806 blockdev --rereadpt $DEV_DSKDIR/$disk 2>/dev/null
807 block_device_wait
808 else
809 typeset format_file=/var/tmp/format_in.$$
810
811 echo "partition" >$format_file
812 echo "$slicenum" >> $format_file
813 echo "" >> $format_file
814 echo "" >> $format_file
815 echo "$start" >> $format_file
816 echo "$size" >> $format_file
817 echo "label" >> $format_file
818 echo "" >> $format_file
819 echo "q" >> $format_file
820 echo "q" >> $format_file
821
822 format -e -s -d $disk -f $format_file
823 fi
824
825 typeset ret_val=$?
826 rm -f $format_file
827 [[ $ret_val -ne 0 ]] && \
828 log_fail "Unable to format $disk slice $slicenum to $size"
829 return 0
830 }
831
832 #
833 # Delete all partitions on all disks - this is specifically for the use of multipath
834 # devices which currently can only be used in the test suite as raw/un-partitioned
835 # devices (ie a zpool cannot be created on a whole mpath device that has partitions)
836 #
837 function delete_partitions
838 {
839 typeset -i j=1
840
841 if [[ -z $DISK_ARRAY_NUM ]]; then
842 DISK_ARRAY_NUM=$(echo ${DISKS} | nawk '{print NF}')
843 fi
844 if [[ -z $DISKSARRAY ]]; then
845 DISKSARRAY=$DISKS
846 fi
847
848 if is_linux; then
849 if (( $DISK_ARRAY_NUM == 1 )); then
850 while ((j < MAX_PARTITIONS)); do
851 parted $DEV_DSKDIR/$DISK -s rm $j \
852 > /dev/null 2>&1
853 if (( $? == 1 )); then
854 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
855 if (( $? == 1 )); then
856 log_note "Partitions for $DISK should be deleted"
857 else
858 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
859 fi
860 return 0
861 else
862 lsblk | egrep ${DISK}${SLICE_PREFIX}${j} > /dev/null
863 if (( $? == 0 )); then
864 log_fail "Partition for ${DISK}${SLICE_PREFIX}${j} not deleted"
865 fi
866 fi
867 ((j = j+1))
868 done
869 else
870 for disk in `echo $DISKSARRAY`; do
871 while ((j < MAX_PARTITIONS)); do
872 parted $DEV_DSKDIR/$disk -s rm $j > /dev/null 2>&1
873 if (( $? == 1 )); then
874 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
875 if (( $? == 1 )); then
876 log_note "Partitions for $disk should be deleted"
877 else
878 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
879 fi
880 j=7
881 else
882 lsblk | egrep ${disk}${SLICE_PREFIX}${j} > /dev/null
883 if (( $? == 0 )); then
884 log_fail "Partition for ${disk}${SLICE_PREFIX}${j} not deleted"
885 fi
886 fi
887 ((j = j+1))
888 done
889 j=1
890 done
891 fi
892 fi
893 return 0
894 }
895
896 #
897 # Get the end cyl of the given slice
898 #
899 function get_endslice #<disk> <slice>
900 {
901 typeset disk=$1
902 typeset slice=$2
903 if [[ -z $disk || -z $slice ]] ; then
904 log_fail "The disk name or slice number is unspecified."
905 fi
906
907 if is_linux; then
908 endcyl=$(parted -s $DEV_DSKDIR/$disk -- unit cyl print | \
909 grep "part${slice}" | \
910 awk '{print $3}' | \
911 sed 's,cyl,,')
912 ((endcyl = (endcyl + 1)))
913 else
914 disk=${disk#/dev/dsk/}
915 disk=${disk#/dev/rdsk/}
916 disk=${disk%s*}
917
918 typeset -i ratio=0
919 ratio=$(prtvtoc /dev/rdsk/${disk}s2 | \
920 grep "sectors\/cylinder" | \
921 awk '{print $2}')
922
923 if ((ratio == 0)); then
924 return
925 fi
926
927 typeset -i endcyl=$(prtvtoc -h /dev/rdsk/${disk}s2 |
928 nawk -v token="$slice" '{if ($1==token) print $6}')
929
930 ((endcyl = (endcyl + 1) / ratio))
931 fi
932
933 echo $endcyl
934 }
935
936
937 #
938 # Given a size,disk and total slice number, this function formats the
939 # disk slices from 0 to the total slice number with the same specified
940 # size.
941 #
942 function partition_disk #<slice_size> <whole_disk_name> <total_slices>
943 {
944 typeset -i i=0
945 typeset slice_size=$1
946 typeset disk_name=$2
947 typeset total_slices=$3
948 typeset cyl
949
950 zero_partitions $disk_name
951 while ((i < $total_slices)); do
952 if ! is_linux; then
953 if ((i == 2)); then
954 ((i = i + 1))
955 continue
956 fi
957 fi
958 set_partition $i "$cyl" $slice_size $disk_name
959 cyl=$(get_endslice $disk_name $i)
960 ((i = i+1))
961 done
962 }
963
964 #
965 # This function continues to write to a filenum number of files into dirnum
966 # number of directories until either file_write returns an error or the
967 # maximum number of files per directory have been written.
968 #
969 # Usage:
970 # fill_fs [destdir] [dirnum] [filenum] [bytes] [num_writes] [data]
971 #
972 # Return value: 0 on success
973 # non 0 on error
974 #
975 # Where :
976 # destdir: is the directory where everything is to be created under
977 # dirnum: the maximum number of subdirectories to use, -1 no limit
978 # filenum: the maximum number of files per subdirectory
979 # bytes: number of bytes to write
980 # num_writes: numer of types to write out bytes
981 # data: the data that will be written
982 #
983 # E.g.
984 # file_fs /testdir 20 25 1024 256 0
985 #
986 # Note: bytes * num_writes equals the size of the testfile
987 #
988 function fill_fs # destdir dirnum filenum bytes num_writes data
989 {
990 typeset destdir=${1:-$TESTDIR}
991 typeset -i dirnum=${2:-50}
992 typeset -i filenum=${3:-50}
993 typeset -i bytes=${4:-8192}
994 typeset -i num_writes=${5:-10240}
995 typeset -i data=${6:-0}
996
997 typeset -i odirnum=1
998 typeset -i idirnum=0
999 typeset -i fn=0
1000 typeset -i retval=0
1001
1002 log_must mkdir -p $destdir/$idirnum
1003 while (($odirnum > 0)); do
1004 if ((dirnum >= 0 && idirnum >= dirnum)); then
1005 odirnum=0
1006 break
1007 fi
1008 file_write -o create -f $destdir/$idirnum/$TESTFILE.$fn \
1009 -b $bytes -c $num_writes -d $data
1010 retval=$?
1011 if (($retval != 0)); then
1012 odirnum=0
1013 break
1014 fi
1015 if (($fn >= $filenum)); then
1016 fn=0
1017 ((idirnum = idirnum + 1))
1018 log_must mkdir -p $destdir/$idirnum
1019 else
1020 ((fn = fn + 1))
1021 fi
1022 done
1023 return $retval
1024 }
1025
1026 #
1027 # Simple function to get the specified property. If unable to
1028 # get the property then exits.
1029 #
1030 # Note property is in 'parsable' format (-p)
1031 #
1032 function get_prop # property dataset
1033 {
1034 typeset prop_val
1035 typeset prop=$1
1036 typeset dataset=$2
1037
1038 prop_val=$(zfs get -pH -o value $prop $dataset 2>/dev/null)
1039 if [[ $? -ne 0 ]]; then
1040 log_note "Unable to get $prop property for dataset " \
1041 "$dataset"
1042 return 1
1043 fi
1044
1045 echo "$prop_val"
1046 return 0
1047 }
1048
1049 #
1050 # Simple function to get the specified property of pool. If unable to
1051 # get the property then exits.
1052 #
1053 # Note property is in 'parsable' format (-p)
1054 #
1055 function get_pool_prop # property pool
1056 {
1057 typeset prop_val
1058 typeset prop=$1
1059 typeset pool=$2
1060
1061 if poolexists $pool ; then
1062 prop_val=$(zpool get -pH $prop $pool 2>/dev/null | tail -1 | \
1063 awk '{print $3}')
1064 if [[ $? -ne 0 ]]; then
1065 log_note "Unable to get $prop property for pool " \
1066 "$pool"
1067 return 1
1068 fi
1069 else
1070 log_note "Pool $pool not exists."
1071 return 1
1072 fi
1073
1074 echo "$prop_val"
1075 return 0
1076 }
1077
1078 # Return 0 if a pool exists; $? otherwise
1079 #
1080 # $1 - pool name
1081
1082 function poolexists
1083 {
1084 typeset pool=$1
1085
1086 if [[ -z $pool ]]; then
1087 log_note "No pool name given."
1088 return 1
1089 fi
1090
1091 zpool get name "$pool" > /dev/null 2>&1
1092 return $?
1093 }
1094
1095 # Return 0 if all the specified datasets exist; $? otherwise
1096 #
1097 # $1-n dataset name
1098 function datasetexists
1099 {
1100 if (($# == 0)); then
1101 log_note "No dataset name given."
1102 return 1
1103 fi
1104
1105 while (($# > 0)); do
1106 zfs get name $1 > /dev/null 2>&1 || \
1107 return $?
1108 shift
1109 done
1110
1111 return 0
1112 }
1113
1114 # return 0 if none of the specified datasets exists, otherwise return 1.
1115 #
1116 # $1-n dataset name
1117 function datasetnonexists
1118 {
1119 if (($# == 0)); then
1120 log_note "No dataset name given."
1121 return 1
1122 fi
1123
1124 while (($# > 0)); do
1125 zfs list -H -t filesystem,snapshot,volume $1 > /dev/null 2>&1 \
1126 && return 1
1127 shift
1128 done
1129
1130 return 0
1131 }
1132
1133 #
1134 # Given a mountpoint, or a dataset name, determine if it is shared via NFS.
1135 #
1136 # Returns 0 if shared, 1 otherwise.
1137 #
1138 function is_shared
1139 {
1140 typeset fs=$1
1141 typeset mtpt
1142
1143 if [[ $fs != "/"* ]] ; then
1144 if datasetnonexists "$fs" ; then
1145 return 1
1146 else
1147 mtpt=$(get_prop mountpoint "$fs")
1148 case $mtpt in
1149 none|legacy|-) return 1
1150 ;;
1151 *) fs=$mtpt
1152 ;;
1153 esac
1154 fi
1155 fi
1156
1157 if is_linux; then
1158 for mtpt in `share | awk '{print $1}'` ; do
1159 if [[ $mtpt == $fs ]] ; then
1160 return 0
1161 fi
1162 done
1163 return 1
1164 fi
1165
1166 for mtpt in `share | awk '{print $2}'` ; do
1167 if [[ $mtpt == $fs ]] ; then
1168 return 0
1169 fi
1170 done
1171
1172 typeset stat=$(svcs -H -o STA nfs/server:default)
1173 if [[ $stat != "ON" ]]; then
1174 log_note "Current nfs/server status: $stat"
1175 fi
1176
1177 return 1
1178 }
1179
1180 #
1181 # Given a dataset name determine if it is shared via SMB.
1182 #
1183 # Returns 0 if shared, 1 otherwise.
1184 #
1185 function is_shared_smb
1186 {
1187 typeset fs=$1
1188 typeset mtpt
1189
1190 if datasetnonexists "$fs" ; then
1191 return 1
1192 else
1193 fs=$(echo $fs | sed 's@/@_@g')
1194 fi
1195
1196 if is_linux; then
1197 for mtpt in `net usershare list | awk '{print $1}'` ; do
1198 if [[ $mtpt == $fs ]] ; then
1199 return 0
1200 fi
1201 done
1202 return 1
1203 else
1204 log_unsupported "Currently unsupported by the test framework"
1205 return 1
1206 fi
1207 }
1208
1209 #
1210 # Given a mountpoint, determine if it is not shared via NFS.
1211 #
1212 # Returns 0 if not shared, 1 otherwise.
1213 #
1214 function not_shared
1215 {
1216 typeset fs=$1
1217
1218 is_shared $fs
1219 if (($? == 0)); then
1220 return 1
1221 fi
1222
1223 return 0
1224 }
1225
1226 #
1227 # Given a dataset determine if it is not shared via SMB.
1228 #
1229 # Returns 0 if not shared, 1 otherwise.
1230 #
1231 function not_shared_smb
1232 {
1233 typeset fs=$1
1234
1235 is_shared_smb $fs
1236 if (($? == 0)); then
1237 return 1
1238 fi
1239
1240 return 0
1241 }
1242
1243 #
1244 # Helper function to unshare a mountpoint.
1245 #
1246 function unshare_fs #fs
1247 {
1248 typeset fs=$1
1249
1250 is_shared $fs || is_shared_smb $fs
1251 if (($? == 0)); then
1252 log_must zfs unshare $fs
1253 fi
1254
1255 return 0
1256 }
1257
1258 #
1259 # Helper function to share a NFS mountpoint.
1260 #
1261 function share_nfs #fs
1262 {
1263 typeset fs=$1
1264
1265 if is_linux; then
1266 is_shared $fs
1267 if (($? != 0)); then
1268 log_must share "*:$fs"
1269 fi
1270 else
1271 is_shared $fs
1272 if (($? != 0)); then
1273 log_must share -F nfs $fs
1274 fi
1275 fi
1276
1277 return 0
1278 }
1279
1280 #
1281 # Helper function to unshare a NFS mountpoint.
1282 #
1283 function unshare_nfs #fs
1284 {
1285 typeset fs=$1
1286
1287 if is_linux; then
1288 is_shared $fs
1289 if (($? == 0)); then
1290 log_must unshare -u "*:$fs"
1291 fi
1292 else
1293 is_shared $fs
1294 if (($? == 0)); then
1295 log_must unshare -F nfs $fs
1296 fi
1297 fi
1298
1299 return 0
1300 }
1301
1302 #
1303 # Helper function to show NFS shares.
1304 #
1305 function showshares_nfs
1306 {
1307 if is_linux; then
1308 share -v
1309 else
1310 share -F nfs
1311 fi
1312
1313 return 0
1314 }
1315
1316 #
1317 # Helper function to show SMB shares.
1318 #
1319 function showshares_smb
1320 {
1321 if is_linux; then
1322 net usershare list
1323 else
1324 share -F smb
1325 fi
1326
1327 return 0
1328 }
1329
1330 #
1331 # Check NFS server status and trigger it online.
1332 #
1333 function setup_nfs_server
1334 {
1335 # Cannot share directory in non-global zone.
1336 #
1337 if ! is_global_zone; then
1338 log_note "Cannot trigger NFS server by sharing in LZ."
1339 return
1340 fi
1341
1342 if is_linux; then
1343 log_note "NFS server must started prior to running test framework."
1344 return
1345 fi
1346
1347 typeset nfs_fmri="svc:/network/nfs/server:default"
1348 if [[ $(svcs -Ho STA $nfs_fmri) != "ON" ]]; then
1349 #
1350 # Only really sharing operation can enable NFS server
1351 # to online permanently.
1352 #
1353 typeset dummy=/tmp/dummy
1354
1355 if [[ -d $dummy ]]; then
1356 log_must rm -rf $dummy
1357 fi
1358
1359 log_must mkdir $dummy
1360 log_must share $dummy
1361
1362 #
1363 # Waiting for fmri's status to be the final status.
1364 # Otherwise, in transition, an asterisk (*) is appended for
1365 # instances, unshare will reverse status to 'DIS' again.
1366 #
1367 # Waiting for 1's at least.
1368 #
1369 log_must sleep 1
1370 timeout=10
1371 while [[ timeout -ne 0 && $(svcs -Ho STA $nfs_fmri) == *'*' ]]
1372 do
1373 log_must sleep 1
1374
1375 ((timeout -= 1))
1376 done
1377
1378 log_must unshare $dummy
1379 log_must rm -rf $dummy
1380 fi
1381
1382 log_note "Current NFS status: '$(svcs -Ho STA,FMRI $nfs_fmri)'"
1383 }
1384
1385 #
1386 # To verify whether calling process is in global zone
1387 #
1388 # Return 0 if in global zone, 1 in non-global zone
1389 #
1390 function is_global_zone
1391 {
1392 if is_linux; then
1393 return 0
1394 else
1395 typeset cur_zone=$(zonename 2>/dev/null)
1396 if [[ $cur_zone != "global" ]]; then
1397 return 1
1398 fi
1399 return 0
1400 fi
1401 }
1402
1403 #
1404 # Verify whether test is permitted to run from
1405 # global zone, local zone, or both
1406 #
1407 # $1 zone limit, could be "global", "local", or "both"(no limit)
1408 #
1409 # Return 0 if permitted, otherwise exit with log_unsupported
1410 #
1411 function verify_runnable # zone limit
1412 {
1413 typeset limit=$1
1414
1415 [[ -z $limit ]] && return 0
1416
1417 if is_global_zone ; then
1418 case $limit in
1419 global|both)
1420 ;;
1421 local) log_unsupported "Test is unable to run from "\
1422 "global zone."
1423 ;;
1424 *) log_note "Warning: unknown limit $limit - " \
1425 "use both."
1426 ;;
1427 esac
1428 else
1429 case $limit in
1430 local|both)
1431 ;;
1432 global) log_unsupported "Test is unable to run from "\
1433 "local zone."
1434 ;;
1435 *) log_note "Warning: unknown limit $limit - " \
1436 "use both."
1437 ;;
1438 esac
1439
1440 reexport_pool
1441 fi
1442
1443 return 0
1444 }
1445
1446 # Return 0 if create successfully or the pool exists; $? otherwise
1447 # Note: In local zones, this function should return 0 silently.
1448 #
1449 # $1 - pool name
1450 # $2-n - [keyword] devs_list
1451
1452 function create_pool #pool devs_list
1453 {
1454 typeset pool=${1%%/*}
1455
1456 shift
1457
1458 if [[ -z $pool ]]; then
1459 log_note "Missing pool name."
1460 return 1
1461 fi
1462
1463 if poolexists $pool ; then
1464 destroy_pool $pool
1465 fi
1466
1467 if is_global_zone ; then
1468 [[ -d /$pool ]] && rm -rf /$pool
1469 log_must zpool create -f $pool $@
1470 fi
1471
1472 return 0
1473 }
1474
1475 # Return 0 if destroy successfully or the pool exists; $? otherwise
1476 # Note: In local zones, this function should return 0 silently.
1477 #
1478 # $1 - pool name
1479 # Destroy pool with the given parameters.
1480
1481 function destroy_pool #pool
1482 {
1483 typeset pool=${1%%/*}
1484 typeset mtpt
1485
1486 if [[ -z $pool ]]; then
1487 log_note "No pool name given."
1488 return 1
1489 fi
1490
1491 if is_global_zone ; then
1492 if poolexists "$pool" ; then
1493 mtpt=$(get_prop mountpoint "$pool")
1494
1495 # At times, syseventd activity can cause attempts to
1496 # destroy a pool to fail with EBUSY. We retry a few
1497 # times allowing failures before requiring the destroy
1498 # to succeed.
1499 typeset -i wait_time=10 ret=1 count=0
1500 must=""
1501 while [[ $ret -ne 0 ]]; do
1502 $must zpool destroy -f $pool
1503 ret=$?
1504 [[ $ret -eq 0 ]] && break
1505 log_note "zpool destroy failed with $ret"
1506 [[ count++ -ge 7 ]] && must=log_must
1507 sleep $wait_time
1508 done
1509
1510 [[ -d $mtpt ]] && \
1511 log_must rm -rf $mtpt
1512 else
1513 log_note "Pool does not exist. ($pool)"
1514 return 1
1515 fi
1516 fi
1517
1518 return 0
1519 }
1520
1521 #
1522 # Firstly, create a pool with 5 datasets. Then, create a single zone and
1523 # export the 5 datasets to it. In addition, we also add a ZFS filesystem
1524 # and a zvol device to the zone.
1525 #
1526 # $1 zone name
1527 # $2 zone root directory prefix
1528 # $3 zone ip
1529 #
1530 function zfs_zones_setup #zone_name zone_root zone_ip
1531 {
1532 typeset zone_name=${1:-$(hostname)-z}
1533 typeset zone_root=${2:-"/zone_root"}
1534 typeset zone_ip=${3:-"10.1.1.10"}
1535 typeset prefix_ctr=$ZONE_CTR
1536 typeset pool_name=$ZONE_POOL
1537 typeset -i cntctr=5
1538 typeset -i i=0
1539
1540 # Create pool and 5 container within it
1541 #
1542 [[ -d /$pool_name ]] && rm -rf /$pool_name
1543 log_must zpool create -f $pool_name $DISKS
1544 while ((i < cntctr)); do
1545 log_must zfs create $pool_name/$prefix_ctr$i
1546 ((i += 1))
1547 done
1548
1549 # create a zvol
1550 log_must zfs create -V 1g $pool_name/zone_zvol
1551 block_device_wait
1552
1553 #
1554 # If current system support slog, add slog device for pool
1555 #
1556 if verify_slog_support ; then
1557 typeset sdevs="/var/tmp/sdev1 /var/tmp/sdev2"
1558 log_must mkfile $MINVDEVSIZE $sdevs
1559 log_must zpool add $pool_name log mirror $sdevs
1560 fi
1561
1562 # this isn't supported just yet.
1563 # Create a filesystem. In order to add this to
1564 # the zone, it must have it's mountpoint set to 'legacy'
1565 # log_must zfs create $pool_name/zfs_filesystem
1566 # log_must zfs set mountpoint=legacy $pool_name/zfs_filesystem
1567
1568 [[ -d $zone_root ]] && \
1569 log_must rm -rf $zone_root/$zone_name
1570 [[ ! -d $zone_root ]] && \
1571 log_must mkdir -p -m 0700 $zone_root/$zone_name
1572
1573 # Create zone configure file and configure the zone
1574 #
1575 typeset zone_conf=/tmp/zone_conf.$$
1576 echo "create" > $zone_conf
1577 echo "set zonepath=$zone_root/$zone_name" >> $zone_conf
1578 echo "set autoboot=true" >> $zone_conf
1579 i=0
1580 while ((i < cntctr)); do
1581 echo "add dataset" >> $zone_conf
1582 echo "set name=$pool_name/$prefix_ctr$i" >> \
1583 $zone_conf
1584 echo "end" >> $zone_conf
1585 ((i += 1))
1586 done
1587
1588 # add our zvol to the zone
1589 echo "add device" >> $zone_conf
1590 echo "set match=/dev/zvol/dsk/$pool_name/zone_zvol" >> $zone_conf
1591 echo "end" >> $zone_conf
1592
1593 # add a corresponding zvol rdsk to the zone
1594 echo "add device" >> $zone_conf
1595 echo "set match=$ZVOL_RDEVDIR/$pool_name/zone_zvol" >> $zone_conf
1596 echo "end" >> $zone_conf
1597
1598 # once it's supported, we'll add our filesystem to the zone
1599 # echo "add fs" >> $zone_conf
1600 # echo "set type=zfs" >> $zone_conf
1601 # echo "set special=$pool_name/zfs_filesystem" >> $zone_conf
1602 # echo "set dir=/export/zfs_filesystem" >> $zone_conf
1603 # echo "end" >> $zone_conf
1604
1605 echo "verify" >> $zone_conf
1606 echo "commit" >> $zone_conf
1607 log_must zonecfg -z $zone_name -f $zone_conf
1608 log_must rm -f $zone_conf
1609
1610 # Install the zone
1611 zoneadm -z $zone_name install
1612 if (($? == 0)); then
1613 log_note "SUCCESS: zoneadm -z $zone_name install"
1614 else
1615 log_fail "FAIL: zoneadm -z $zone_name install"
1616 fi
1617
1618 # Install sysidcfg file
1619 #
1620 typeset sysidcfg=$zone_root/$zone_name/root/etc/sysidcfg
1621 echo "system_locale=C" > $sysidcfg
1622 echo "terminal=dtterm" >> $sysidcfg
1623 echo "network_interface=primary {" >> $sysidcfg
1624 echo "hostname=$zone_name" >> $sysidcfg
1625 echo "}" >> $sysidcfg
1626 echo "name_service=NONE" >> $sysidcfg
1627 echo "root_password=mo791xfZ/SFiw" >> $sysidcfg
1628 echo "security_policy=NONE" >> $sysidcfg
1629 echo "timezone=US/Eastern" >> $sysidcfg
1630
1631 # Boot this zone
1632 log_must zoneadm -z $zone_name boot
1633 }
1634
1635 #
1636 # Reexport TESTPOOL & TESTPOOL(1-4)
1637 #
1638 function reexport_pool
1639 {
1640 typeset -i cntctr=5
1641 typeset -i i=0
1642
1643 while ((i < cntctr)); do
1644 if ((i == 0)); then
1645 TESTPOOL=$ZONE_POOL/$ZONE_CTR$i
1646 if ! ismounted $TESTPOOL; then
1647 log_must zfs mount $TESTPOOL
1648 fi
1649 else
1650 eval TESTPOOL$i=$ZONE_POOL/$ZONE_CTR$i
1651 if eval ! ismounted \$TESTPOOL$i; then
1652 log_must eval zfs mount \$TESTPOOL$i
1653 fi
1654 fi
1655 ((i += 1))
1656 done
1657 }
1658
1659 #
1660 # Verify a given disk or pool state
1661 #
1662 # Return 0 is pool/disk matches expected state, 1 otherwise
1663 #
1664 function check_state # pool disk state{online,offline,degraded}
1665 {
1666 typeset pool=$1
1667 typeset disk=${2#$DEV_DSKDIR/}
1668 typeset state=$3
1669
1670 [[ -z $pool ]] || [[ -z $state ]] \
1671 && log_fail "Arguments invalid or missing"
1672
1673 if [[ -z $disk ]]; then
1674 #check pool state only
1675 zpool get -H -o value health $pool \
1676 | grep -i "$state" > /dev/null 2>&1
1677 else
1678 zpool status -v $pool | grep "$disk" \
1679 | grep -i "$state" > /dev/null 2>&1
1680 fi
1681
1682 return $?
1683 }
1684
1685 #
1686 # Cause a scan of all scsi host adapters by default
1687 #
1688 # $1 optional host number
1689 #
1690 function scan_scsi_hosts
1691 {
1692 typeset hostnum=${1}
1693
1694 if is_linux; then
1695 if [[ -z $hostnum ]]; then
1696 for host in /sys/class/scsi_host/host*; do
1697 log_must eval "echo '- - -' > $host/scan"
1698 done
1699 else
1700 log_must eval \
1701 "echo /sys/class/scsi_host/host$hostnum/scan" \
1702 > /dev/null
1703 log_must eval \
1704 "echo '- - -' > /sys/class/scsi_host/host$hostnum/scan"
1705 fi
1706 fi
1707 }
1708 #
1709 # Wait for newly created block devices to have their minors created.
1710 #
1711 function block_device_wait
1712 {
1713 if is_linux; then
1714 udevadm trigger
1715 udevadm settle
1716 fi
1717 }
1718
1719 #
1720 # Online or offline a disk on the system
1721 #
1722 # First checks state of disk. Test will fail if disk is not properly onlined
1723 # or offlined. Online is a full rescan of SCSI disks by echoing to every
1724 # host entry.
1725 #
1726 function on_off_disk # disk state{online,offline} host
1727 {
1728 typeset disk=$1
1729 typeset state=$2
1730 typeset host=$3
1731
1732 [[ -z $disk ]] || [[ -z $state ]] && \
1733 log_fail "Arguments invalid or missing"
1734
1735 if is_linux; then
1736 if [[ $state == "offline" ]] && ( is_mpath_device $disk ); then
1737 dm_name="$(readlink $DEV_DSKDIR/$disk \
1738 | nawk -F / '{print $2}')"
1739 slave="$(ls /sys/block/${dm_name}/slaves \
1740 | nawk '{print $1}')"
1741 while [[ -n $slave ]]; do
1742 #check if disk is online
1743 lsscsi | egrep $slave > /dev/null
1744 if (($? == 0)); then
1745 slave_dir="/sys/block/${dm_name}"
1746 slave_dir+="/slaves/${slave}/device"
1747 ss="${slave_dir}/state"
1748 sd="${slave_dir}/delete"
1749 log_must eval "echo 'offline' > ${ss}"
1750 log_must eval "echo '1' > ${sd}"
1751 lsscsi | egrep $slave > /dev/null
1752 if (($? == 0)); then
1753 log_fail "Offlining" \
1754 "$disk failed"
1755 fi
1756 fi
1757 slave="$(ls /sys/block/$dm_name/slaves \
1758 2>/dev/null | nawk '{print $1}')"
1759 done
1760 elif [[ $state == "offline" ]] && ( is_real_device $disk ); then
1761 #check if disk is online
1762 lsscsi | egrep $disk > /dev/null
1763 if (($? == 0)); then
1764 dev_state="/sys/block/$disk/device/state"
1765 dev_delete="/sys/block/$disk/device/delete"
1766 log_must eval "echo 'offline' > ${dev_state}"
1767 log_must eval "echo '1' > ${dev_delete}"
1768 lsscsi | egrep $disk > /dev/null
1769 if (($? == 0)); then
1770 log_fail "Offlining $disk" \
1771 "failed"
1772 fi
1773 else
1774 log_note "$disk is already offline"
1775 fi
1776 elif [[ $state == "online" ]]; then
1777 #force a full rescan
1778 scan_scsi_hosts $host
1779 block_device_wait
1780 if is_mpath_device $disk; then
1781 dm_name="$(readlink $DEV_DSKDIR/$disk \
1782 | nawk -F / '{print $2}')"
1783 slave="$(ls /sys/block/$dm_name/slaves \
1784 | nawk '{print $1}')"
1785 lsscsi | egrep $slave > /dev/null
1786 if (($? != 0)); then
1787 log_fail "Onlining $disk failed"
1788 fi
1789 elif is_real_device $disk; then
1790 lsscsi | egrep $disk > /dev/null
1791 if (($? != 0)); then
1792 log_fail "Onlining $disk failed"
1793 fi
1794 else
1795 log_fail "$disk is not a real dev"
1796 fi
1797 else
1798 log_fail "$disk failed to $state"
1799 fi
1800 fi
1801 }
1802
1803 #
1804 # Get the mountpoint of snapshot
1805 # For the snapshot use <mp_filesystem>/.zfs/snapshot/<snap>
1806 # as its mountpoint
1807 #
1808 function snapshot_mountpoint
1809 {
1810 typeset dataset=${1:-$TESTPOOL/$TESTFS@$TESTSNAP}
1811
1812 if [[ $dataset != *@* ]]; then
1813 log_fail "Error name of snapshot '$dataset'."
1814 fi
1815
1816 typeset fs=${dataset%@*}
1817 typeset snap=${dataset#*@}
1818
1819 if [[ -z $fs || -z $snap ]]; then
1820 log_fail "Error name of snapshot '$dataset'."
1821 fi
1822
1823 echo $(get_prop mountpoint $fs)/.zfs/snapshot/$snap
1824 }
1825
1826 #
1827 # Given a pool and file system, this function will verify the file system
1828 # using the zdb internal tool. Note that the pool is exported and imported
1829 # to ensure it has consistent state.
1830 #
1831 function verify_filesys # pool filesystem dir
1832 {
1833 typeset pool="$1"
1834 typeset filesys="$2"
1835 typeset zdbout="/tmp/zdbout.$$"
1836
1837 shift
1838 shift
1839 typeset dirs=$@
1840 typeset search_path=""
1841
1842 log_note "Calling zdb to verify filesystem '$filesys'"
1843 zfs unmount -a > /dev/null 2>&1
1844 log_must zpool export $pool
1845
1846 if [[ -n $dirs ]] ; then
1847 for dir in $dirs ; do
1848 search_path="$search_path -d $dir"
1849 done
1850 fi
1851
1852 log_must zpool import $search_path $pool
1853
1854 zdb -cudi $filesys > $zdbout 2>&1
1855 if [[ $? != 0 ]]; then
1856 log_note "Output: zdb -cudi $filesys"
1857 cat $zdbout
1858 log_fail "zdb detected errors with: '$filesys'"
1859 fi
1860
1861 log_must zfs mount -a
1862 log_must rm -rf $zdbout
1863 }
1864
1865 #
1866 # Given a pool, and this function list all disks in the pool
1867 #
1868 function get_disklist # pool
1869 {
1870 typeset disklist=""
1871
1872 disklist=$(zpool iostat -v $1 | nawk '(NR >4) {print $1}' | \
1873 grep -v "\-\-\-\-\-" | \
1874 egrep -v -e "^(mirror|raidz1|raidz2|spare|log|cache)$")
1875
1876 echo $disklist
1877 }
1878
1879 #
1880 # Given a pool, and this function list all disks in the pool with their full
1881 # path (like "/dev/sda" instead of "sda").
1882 #
1883 function get_disklist_fullpath # pool
1884 {
1885 args="-P $1"
1886 get_disklist $args
1887 }
1888
1889
1890
1891 # /**
1892 # This function kills a given list of processes after a time period. We use
1893 # this in the stress tests instead of STF_TIMEOUT so that we can have processes
1894 # run for a fixed amount of time, yet still pass. Tests that hit STF_TIMEOUT
1895 # would be listed as FAIL, which we don't want : we're happy with stress tests
1896 # running for a certain amount of time, then finishing.
1897 #
1898 # @param $1 the time in seconds after which we should terminate these processes
1899 # @param $2..$n the processes we wish to terminate.
1900 # */
1901 function stress_timeout
1902 {
1903 typeset -i TIMEOUT=$1
1904 shift
1905 typeset cpids="$@"
1906
1907 log_note "Waiting for child processes($cpids). " \
1908 "It could last dozens of minutes, please be patient ..."
1909 log_must sleep $TIMEOUT
1910
1911 log_note "Killing child processes after ${TIMEOUT} stress timeout."
1912 typeset pid
1913 for pid in $cpids; do
1914 ps -p $pid > /dev/null 2>&1
1915 if (($? == 0)); then
1916 log_must kill -USR1 $pid
1917 fi
1918 done
1919 }
1920
1921 #
1922 # Verify a given hotspare disk is inuse or avail
1923 #
1924 # Return 0 is pool/disk matches expected state, 1 otherwise
1925 #
1926 function check_hotspare_state # pool disk state{inuse,avail}
1927 {
1928 typeset pool=$1
1929 typeset disk=${2#$DEV_DSKDIR/}
1930 typeset state=$3
1931
1932 cur_state=$(get_device_state $pool $disk "spares")
1933
1934 if [[ $state != ${cur_state} ]]; then
1935 return 1
1936 fi
1937 return 0
1938 }
1939
1940 #
1941 # Verify a given slog disk is inuse or avail
1942 #
1943 # Return 0 is pool/disk matches expected state, 1 otherwise
1944 #
1945 function check_slog_state # pool disk state{online,offline,unavail}
1946 {
1947 typeset pool=$1
1948 typeset disk=${2#$DEV_DSKDIR/}
1949 typeset state=$3
1950
1951 cur_state=$(get_device_state $pool $disk "logs")
1952
1953 if [[ $state != ${cur_state} ]]; then
1954 return 1
1955 fi
1956 return 0
1957 }
1958
1959 #
1960 # Verify a given vdev disk is inuse or avail
1961 #
1962 # Return 0 is pool/disk matches expected state, 1 otherwise
1963 #
1964 function check_vdev_state # pool disk state{online,offline,unavail}
1965 {
1966 typeset pool=$1
1967 typeset disk=${2#$/DEV_DSKDIR/}
1968 typeset state=$3
1969
1970 cur_state=$(get_device_state $pool $disk)
1971
1972 if [[ $state != ${cur_state} ]]; then
1973 return 1
1974 fi
1975 return 0
1976 }
1977
1978 #
1979 # Check the output of 'zpool status -v <pool>',
1980 # and to see if the content of <token> contain the <keyword> specified.
1981 #
1982 # Return 0 is contain, 1 otherwise
1983 #
1984 function check_pool_status # pool token keyword
1985 {
1986 typeset pool=$1
1987 typeset token=$2
1988 typeset keyword=$3
1989
1990 zpool status -v "$pool" 2>/dev/null | nawk -v token="$token:" '
1991 ($1==token) {print $0}' \
1992 | grep -i "$keyword" > /dev/null 2>&1
1993
1994 return $?
1995 }
1996
1997 #
1998 # These 5 following functions are instance of check_pool_status()
1999 # is_pool_resilvering - to check if the pool is resilver in progress
2000 # is_pool_resilvered - to check if the pool is resilver completed
2001 # is_pool_scrubbing - to check if the pool is scrub in progress
2002 # is_pool_scrubbed - to check if the pool is scrub completed
2003 # is_pool_scrub_stopped - to check if the pool is scrub stopped
2004 #
2005 function is_pool_resilvering #pool
2006 {
2007 check_pool_status "$1" "scan" "resilver in progress since "
2008 return $?
2009 }
2010
2011 function is_pool_resilvered #pool
2012 {
2013 check_pool_status "$1" "scan" "resilvered "
2014 return $?
2015 }
2016
2017 function is_pool_scrubbing #pool
2018 {
2019 check_pool_status "$1" "scan" "scrub in progress since "
2020 return $?
2021 }
2022
2023 function is_pool_scrubbed #pool
2024 {
2025 check_pool_status "$1" "scan" "scrub repaired"
2026 return $?
2027 }
2028
2029 function is_pool_scrub_stopped #pool
2030 {
2031 check_pool_status "$1" "scan" "scrub canceled"
2032 return $?
2033 }
2034
2035 #
2036 # Use create_pool()/destroy_pool() to clean up the information in
2037 # in the given disk to avoid slice overlapping.
2038 #
2039 function cleanup_devices #vdevs
2040 {
2041 typeset pool="foopool$$"
2042
2043 if poolexists $pool ; then
2044 destroy_pool $pool
2045 fi
2046
2047 create_pool $pool $@
2048 destroy_pool $pool
2049
2050 return 0
2051 }
2052
2053 #
2054 # Verify the rsh connectivity to each remote host in RHOSTS.
2055 #
2056 # Return 0 if remote host is accessible; otherwise 1.
2057 # $1 remote host name
2058 # $2 username
2059 #
2060 function verify_rsh_connect #rhost, username
2061 {
2062 typeset rhost=$1
2063 typeset username=$2
2064 typeset rsh_cmd="rsh -n"
2065 typeset cur_user=
2066
2067 getent hosts $rhost >/dev/null 2>&1
2068 if (($? != 0)); then
2069 log_note "$rhost cannot be found from" \
2070 "administrative database."
2071 return 1
2072 fi
2073
2074 ping $rhost 3 >/dev/null 2>&1
2075 if (($? != 0)); then
2076 log_note "$rhost is not reachable."
2077 return 1
2078 fi
2079
2080 if ((${#username} != 0)); then
2081 rsh_cmd="$rsh_cmd -l $username"
2082 cur_user="given user \"$username\""
2083 else
2084 cur_user="current user \"`logname`\""
2085 fi
2086
2087 if ! $rsh_cmd $rhost true; then
2088 log_note "rsh to $rhost is not accessible" \
2089 "with $cur_user."
2090 return 1
2091 fi
2092
2093 return 0
2094 }
2095
2096 #
2097 # Verify the remote host connection via rsh after rebooting
2098 # $1 remote host
2099 #
2100 function verify_remote
2101 {
2102 rhost=$1
2103
2104 #
2105 # The following loop waits for the remote system rebooting.
2106 # Each iteration will wait for 150 seconds. there are
2107 # total 5 iterations, so the total timeout value will
2108 # be 12.5 minutes for the system rebooting. This number
2109 # is an approxiate number.
2110 #
2111 typeset -i count=0
2112 while ! verify_rsh_connect $rhost; do
2113 sleep 150
2114 ((count = count + 1))
2115 if ((count > 5)); then
2116 return 1
2117 fi
2118 done
2119 return 0
2120 }
2121
2122 #
2123 # Replacement function for /usr/bin/rsh. This function will include
2124 # the /usr/bin/rsh and meanwhile return the execution status of the
2125 # last command.
2126 #
2127 # $1 usrname passing down to -l option of /usr/bin/rsh
2128 # $2 remote machine hostname
2129 # $3... command string
2130 #
2131
2132 function rsh_status
2133 {
2134 typeset ruser=$1
2135 typeset rhost=$2
2136 typeset -i ret=0
2137 typeset cmd_str=""
2138 typeset rsh_str=""
2139
2140 shift; shift
2141 cmd_str="$@"
2142
2143 err_file=/tmp/${rhost}.$$.err
2144 if ((${#ruser} == 0)); then
2145 rsh_str="rsh -n"
2146 else
2147 rsh_str="rsh -n -l $ruser"
2148 fi
2149
2150 $rsh_str $rhost /bin/ksh -c "'$cmd_str; \
2151 print -u 2 \"status=\$?\"'" \
2152 >/dev/null 2>$err_file
2153 ret=$?
2154 if (($ret != 0)); then
2155 cat $err_file
2156 rm -f $std_file $err_file
2157 log_fail "rsh itself failed with exit code $ret..."
2158 fi
2159
2160 ret=$(grep -v 'print -u 2' $err_file | grep 'status=' | \
2161 cut -d= -f2)
2162 (($ret != 0)) && cat $err_file >&2
2163
2164 rm -f $err_file >/dev/null 2>&1
2165 return $ret
2166 }
2167
2168 #
2169 # Get the SUNWstc-fs-zfs package installation path in a remote host
2170 # $1 remote host name
2171 #
2172 function get_remote_pkgpath
2173 {
2174 typeset rhost=$1
2175 typeset pkgpath=""
2176
2177 pkgpath=$(rsh -n $rhost "pkginfo -l SUNWstc-fs-zfs | grep BASEDIR: |\
2178 cut -d: -f2")
2179
2180 echo $pkgpath
2181 }
2182
2183 #/**
2184 # A function to find and locate free disks on a system or from given
2185 # disks as the parameter. It works by locating disks that are in use
2186 # as swap devices and dump devices, and also disks listed in /etc/vfstab
2187 #
2188 # $@ given disks to find which are free, default is all disks in
2189 # the test system
2190 #
2191 # @return a string containing the list of available disks
2192 #*/
2193 function find_disks
2194 {
2195 # Trust provided list, no attempt is made to locate unused devices.
2196 if is_linux; then
2197 echo "$@"
2198 return
2199 fi
2200
2201
2202 sfi=/tmp/swaplist.$$
2203 dmpi=/tmp/dumpdev.$$
2204 max_finddisksnum=${MAX_FINDDISKSNUM:-6}
2205
2206 swap -l > $sfi
2207 dumpadm > $dmpi 2>/dev/null
2208
2209 # write an awk script that can process the output of format
2210 # to produce a list of disks we know about. Note that we have
2211 # to escape "$2" so that the shell doesn't interpret it while
2212 # we're creating the awk script.
2213 # -------------------
2214 cat > /tmp/find_disks.awk <<EOF
2215 #!/bin/nawk -f
2216 BEGIN { FS="."; }
2217
2218 /^Specify disk/{
2219 searchdisks=0;
2220 }
2221
2222 {
2223 if (searchdisks && \$2 !~ "^$"){
2224 split(\$2,arr," ");
2225 print arr[1];
2226 }
2227 }
2228
2229 /^AVAILABLE DISK SELECTIONS:/{
2230 searchdisks=1;
2231 }
2232 EOF
2233 #---------------------
2234
2235 chmod 755 /tmp/find_disks.awk
2236 disks=${@:-$(echo "" | format -e 2>/dev/null | /tmp/find_disks.awk)}
2237 rm /tmp/find_disks.awk
2238
2239 unused=""
2240 for disk in $disks; do
2241 # Check for mounted
2242 grep "${disk}[sp]" /etc/mnttab >/dev/null
2243 (($? == 0)) && continue
2244 # Check for swap
2245 grep "${disk}[sp]" $sfi >/dev/null
2246 (($? == 0)) && continue
2247 # check for dump device
2248 grep "${disk}[sp]" $dmpi >/dev/null
2249 (($? == 0)) && continue
2250 # check to see if this disk hasn't been explicitly excluded
2251 # by a user-set environment variable
2252 echo "${ZFS_HOST_DEVICES_IGNORE}" | grep "${disk}" > /dev/null
2253 (($? == 0)) && continue
2254 unused_candidates="$unused_candidates $disk"
2255 done
2256 rm $sfi
2257 rm $dmpi
2258
2259 # now just check to see if those disks do actually exist
2260 # by looking for a device pointing to the first slice in
2261 # each case. limit the number to max_finddisksnum
2262 count=0
2263 for disk in $unused_candidates; do
2264 if [ -b $DEV_DSKDIR/${disk}s0 ]; then
2265 if [ $count -lt $max_finddisksnum ]; then
2266 unused="$unused $disk"
2267 # do not impose limit if $@ is provided
2268 [[ -z $@ ]] && ((count = count + 1))
2269 fi
2270 fi
2271 done
2272
2273 # finally, return our disk list
2274 echo $unused
2275 }
2276
2277 #
2278 # Add specified user to specified group
2279 #
2280 # $1 group name
2281 # $2 user name
2282 # $3 base of the homedir (optional)
2283 #
2284 function add_user #<group_name> <user_name> <basedir>
2285 {
2286 typeset gname=$1
2287 typeset uname=$2
2288 typeset basedir=${3:-"/var/tmp"}
2289
2290 if ((${#gname} == 0 || ${#uname} == 0)); then
2291 log_fail "group name or user name are not defined."
2292 fi
2293
2294 log_must useradd -g $gname -d $basedir/$uname -m $uname
2295 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.profile
2296 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.bash_profile
2297 echo "export PATH=\"$STF_PATH\"" >>$basedir/$uname/.login
2298
2299 # Add new users to the same group and the command line utils.
2300 # This allows them to be run out of the original users home
2301 # directory as long as it permissioned to be group readable.
2302 if is_linux; then
2303 cmd_group=$(stat --format="%G" $(which zfs))
2304 log_must usermod -a -G $cmd_group $uname
2305 fi
2306
2307 return 0
2308 }
2309
2310 #
2311 # Delete the specified user.
2312 #
2313 # $1 login name
2314 # $2 base of the homedir (optional)
2315 #
2316 function del_user #<logname> <basedir>
2317 {
2318 typeset user=$1
2319 typeset basedir=${2:-"/var/tmp"}
2320
2321 if ((${#user} == 0)); then
2322 log_fail "login name is necessary."
2323 fi
2324
2325 if id $user > /dev/null 2>&1; then
2326 log_must userdel $user
2327 fi
2328
2329 [[ -d $basedir/$user ]] && rm -fr $basedir/$user
2330
2331 return 0
2332 }
2333
2334 #
2335 # Select valid gid and create specified group.
2336 #
2337 # $1 group name
2338 #
2339 function add_group #<group_name>
2340 {
2341 typeset group=$1
2342
2343 if ((${#group} == 0)); then
2344 log_fail "group name is necessary."
2345 fi
2346
2347 # Assign 100 as the base gid, a larger value is selected for
2348 # Linux because for many distributions 1000 and under are reserved.
2349 if is_linux; then
2350 while true; do
2351 groupadd $group > /dev/null 2>&1
2352 typeset -i ret=$?
2353 case $ret in
2354 0) return 0 ;;
2355 *) return 1 ;;
2356 esac
2357 done
2358 else
2359 typeset -i gid=100
2360 while true; do
2361 groupadd -g $gid $group > /dev/null 2>&1
2362 typeset -i ret=$?
2363 case $ret in
2364 0) return 0 ;;
2365 # The gid is not unique
2366 4) ((gid += 1)) ;;
2367 *) return 1 ;;
2368 esac
2369 done
2370 fi
2371 }
2372
2373 #
2374 # Delete the specified group.
2375 #
2376 # $1 group name
2377 #
2378 function del_group #<group_name>
2379 {
2380 typeset grp=$1
2381 if ((${#grp} == 0)); then
2382 log_fail "group name is necessary."
2383 fi
2384
2385 if is_linux; then
2386 getent group $grp > /dev/null 2>&1
2387 typeset -i ret=$?
2388 case $ret in
2389 # Group does not exist.
2390 2) return 0 ;;
2391 # Name already exists as a group name
2392 0) log_must groupdel $grp ;;
2393 *) return 1 ;;
2394 esac
2395 else
2396 groupmod -n $grp $grp > /dev/null 2>&1
2397 typeset -i ret=$?
2398 case $ret in
2399 # Group does not exist.
2400 6) return 0 ;;
2401 # Name already exists as a group name
2402 9) log_must groupdel $grp ;;
2403 *) return 1 ;;
2404 esac
2405 fi
2406
2407 return 0
2408 }
2409
2410 #
2411 # This function will return true if it's safe to destroy the pool passed
2412 # as argument 1. It checks for pools based on zvols and files, and also
2413 # files contained in a pool that may have a different mountpoint.
2414 #
2415 function safe_to_destroy_pool { # $1 the pool name
2416
2417 typeset pool=""
2418 typeset DONT_DESTROY=""
2419
2420 # We check that by deleting the $1 pool, we're not
2421 # going to pull the rug out from other pools. Do this
2422 # by looking at all other pools, ensuring that they
2423 # aren't built from files or zvols contained in this pool.
2424
2425 for pool in $(zpool list -H -o name)
2426 do
2427 ALTMOUNTPOOL=""
2428
2429 # this is a list of the top-level directories in each of the
2430 # files that make up the path to the files the pool is based on
2431 FILEPOOL=$(zpool status -v $pool | grep /$1/ | \
2432 awk '{print $1}')
2433
2434 # this is a list of the zvols that make up the pool
2435 ZVOLPOOL=$(zpool status -v $pool | grep "$ZVOL_DEVDIR/$1$" \
2436 | awk '{print $1}')
2437
2438 # also want to determine if it's a file-based pool using an
2439 # alternate mountpoint...
2440 POOL_FILE_DIRS=$(zpool status -v $pool | \
2441 grep / | awk '{print $1}' | \
2442 awk -F/ '{print $2}' | grep -v "dev")
2443
2444 for pooldir in $POOL_FILE_DIRS
2445 do
2446 OUTPUT=$(zfs list -H -r -o mountpoint $1 | \
2447 grep "${pooldir}$" | awk '{print $1}')
2448
2449 ALTMOUNTPOOL="${ALTMOUNTPOOL}${OUTPUT}"
2450 done
2451
2452
2453 if [ ! -z "$ZVOLPOOL" ]
2454 then
2455 DONT_DESTROY="true"
2456 log_note "Pool $pool is built from $ZVOLPOOL on $1"
2457 fi
2458
2459 if [ ! -z "$FILEPOOL" ]
2460 then
2461 DONT_DESTROY="true"
2462 log_note "Pool $pool is built from $FILEPOOL on $1"
2463 fi
2464
2465 if [ ! -z "$ALTMOUNTPOOL" ]
2466 then
2467 DONT_DESTROY="true"
2468 log_note "Pool $pool is built from $ALTMOUNTPOOL on $1"
2469 fi
2470 done
2471
2472 if [ -z "${DONT_DESTROY}" ]
2473 then
2474 return 0
2475 else
2476 log_note "Warning: it is not safe to destroy $1!"
2477 return 1
2478 fi
2479 }
2480
2481 #
2482 # Get the available ZFS compression options
2483 # $1 option type zfs_set|zfs_compress
2484 #
2485 function get_compress_opts
2486 {
2487 typeset COMPRESS_OPTS
2488 typeset GZIP_OPTS="gzip gzip-1 gzip-2 gzip-3 gzip-4 gzip-5 \
2489 gzip-6 gzip-7 gzip-8 gzip-9"
2490
2491 if [[ $1 == "zfs_compress" ]] ; then
2492 COMPRESS_OPTS="on lzjb"
2493 elif [[ $1 == "zfs_set" ]] ; then
2494 COMPRESS_OPTS="on off lzjb"
2495 fi
2496 typeset valid_opts="$COMPRESS_OPTS"
2497 zfs get 2>&1 | grep gzip >/dev/null 2>&1
2498 if [[ $? -eq 0 ]]; then
2499 valid_opts="$valid_opts $GZIP_OPTS"
2500 fi
2501 echo "$valid_opts"
2502 }
2503
2504 #
2505 # Verify zfs operation with -p option work as expected
2506 # $1 operation, value could be create, clone or rename
2507 # $2 dataset type, value could be fs or vol
2508 # $3 dataset name
2509 # $4 new dataset name
2510 #
2511 function verify_opt_p_ops
2512 {
2513 typeset ops=$1
2514 typeset datatype=$2
2515 typeset dataset=$3
2516 typeset newdataset=$4
2517
2518 if [[ $datatype != "fs" && $datatype != "vol" ]]; then
2519 log_fail "$datatype is not supported."
2520 fi
2521
2522 # check parameters accordingly
2523 case $ops in
2524 create)
2525 newdataset=$dataset
2526 dataset=""
2527 if [[ $datatype == "vol" ]]; then
2528 ops="create -V $VOLSIZE"
2529 fi
2530 ;;
2531 clone)
2532 if [[ -z $newdataset ]]; then
2533 log_fail "newdataset should not be empty" \
2534 "when ops is $ops."
2535 fi
2536 log_must datasetexists $dataset
2537 log_must snapexists $dataset
2538 ;;
2539 rename)
2540 if [[ -z $newdataset ]]; then
2541 log_fail "newdataset should not be empty" \
2542 "when ops is $ops."
2543 fi
2544 log_must datasetexists $dataset
2545 log_mustnot snapexists $dataset
2546 ;;
2547 *)
2548 log_fail "$ops is not supported."
2549 ;;
2550 esac
2551
2552 # make sure the upper level filesystem does not exist
2553 if datasetexists ${newdataset%/*} ; then
2554 log_must zfs destroy -rRf ${newdataset%/*}
2555 fi
2556
2557 # without -p option, operation will fail
2558 log_mustnot zfs $ops $dataset $newdataset
2559 log_mustnot datasetexists $newdataset ${newdataset%/*}
2560
2561 # with -p option, operation should succeed
2562 log_must zfs $ops -p $dataset $newdataset
2563 block_device_wait
2564
2565 if ! datasetexists $newdataset ; then
2566 log_fail "-p option does not work for $ops"
2567 fi
2568
2569 # when $ops is create or clone, redo the operation still return zero
2570 if [[ $ops != "rename" ]]; then
2571 log_must zfs $ops -p $dataset $newdataset
2572 fi
2573
2574 return 0
2575 }
2576
2577 #
2578 # Get configuration of pool
2579 # $1 pool name
2580 # $2 config name
2581 #
2582 function get_config
2583 {
2584 typeset pool=$1
2585 typeset config=$2
2586 typeset alt_root
2587
2588 if ! poolexists "$pool" ; then
2589 return 1
2590 fi
2591 alt_root=$(zpool list -H $pool | awk '{print $NF}')
2592 if [[ $alt_root == "-" ]]; then
2593 value=$(zdb -C $pool | grep "$config:" | awk -F: \
2594 '{print $2}')
2595 else
2596 value=$(zdb -e $pool | grep "$config:" | awk -F: \
2597 '{print $2}')
2598 fi
2599 if [[ -n $value ]] ; then
2600 value=${value#'}
2601 value=${value%'}
2602 fi
2603 echo $value
2604
2605 return 0
2606 }
2607
2608 #
2609 # Privated function. Random select one of items from arguments.
2610 #
2611 # $1 count
2612 # $2-n string
2613 #
2614 function _random_get
2615 {
2616 typeset cnt=$1
2617 shift
2618
2619 typeset str="$@"
2620 typeset -i ind
2621 ((ind = RANDOM % cnt + 1))
2622
2623 typeset ret=$(echo "$str" | cut -f $ind -d ' ')
2624 echo $ret
2625 }
2626
2627 #
2628 # Random select one of item from arguments which include NONE string
2629 #
2630 function random_get_with_non
2631 {
2632 typeset -i cnt=$#
2633 ((cnt =+ 1))
2634
2635 _random_get "$cnt" "$@"
2636 }
2637
2638 #
2639 # Random select one of item from arguments which doesn't include NONE string
2640 #
2641 function random_get
2642 {
2643 _random_get "$#" "$@"
2644 }
2645
2646 #
2647 # Detect if the current system support slog
2648 #
2649 function verify_slog_support
2650 {
2651 typeset dir=/tmp/disk.$$
2652 typeset pool=foo.$$
2653 typeset vdev=$dir/a
2654 typeset sdev=$dir/b
2655
2656 mkdir -p $dir
2657 mkfile $MINVDEVSIZE $vdev $sdev
2658
2659 typeset -i ret=0
2660 if ! zpool create -n $pool $vdev log $sdev > /dev/null 2>&1; then
2661 ret=1
2662 fi
2663 rm -r $dir
2664
2665 return $ret
2666 }
2667
2668 #
2669 # The function will generate a dataset name with specific length
2670 # $1, the length of the name
2671 # $2, the base string to construct the name
2672 #
2673 function gen_dataset_name
2674 {
2675 typeset -i len=$1
2676 typeset basestr="$2"
2677 typeset -i baselen=${#basestr}
2678 typeset -i iter=0
2679 typeset l_name=""
2680
2681 if ((len % baselen == 0)); then
2682 ((iter = len / baselen))
2683 else
2684 ((iter = len / baselen + 1))
2685 fi
2686 while ((iter > 0)); do
2687 l_name="${l_name}$basestr"
2688
2689 ((iter -= 1))
2690 done
2691
2692 echo $l_name
2693 }
2694
2695 #
2696 # Get cksum tuple of dataset
2697 # $1 dataset name
2698 #
2699 # sample zdb output:
2700 # Dataset data/test [ZPL], ID 355, cr_txg 2413856, 31.0K, 7 objects, rootbp
2701 # DVA[0]=<0:803046400:200> DVA[1]=<0:81199000:200> [L0 DMU objset] fletcher4
2702 # lzjb LE contiguous unique double size=800L/200P birth=2413856L/2413856P
2703 # fill=7 cksum=11ce125712:643a9c18ee2:125e25238fca0:254a3f74b59744
2704 function datasetcksum
2705 {
2706 typeset cksum
2707 sync
2708 cksum=$(zdb -vvv $1 | grep "^Dataset $1 \[" | grep "cksum" \
2709 | awk -F= '{print $7}')
2710 echo $cksum
2711 }
2712
2713 #
2714 # Get cksum of file
2715 # #1 file path
2716 #
2717 function checksum
2718 {
2719 typeset cksum
2720 cksum=$(cksum $1 | awk '{print $1}')
2721 echo $cksum
2722 }
2723
2724 #
2725 # Get the given disk/slice state from the specific field of the pool
2726 #
2727 function get_device_state #pool disk field("", "spares","logs")
2728 {
2729 typeset pool=$1
2730 typeset disk=${2#$DEV_DSKDIR/}
2731 typeset field=${3:-$pool}
2732
2733 state=$(zpool status -v "$pool" 2>/dev/null | \
2734 nawk -v device=$disk -v pool=$pool -v field=$field \
2735 'BEGIN {startconfig=0; startfield=0; }
2736 /config:/ {startconfig=1}
2737 (startconfig==1) && ($1==field) {startfield=1; next;}
2738 (startfield==1) && ($1==device) {print $2; exit;}
2739 (startfield==1) &&
2740 ($1==field || $1 ~ "^spares$" || $1 ~ "^logs$") {startfield=0}')
2741 echo $state
2742 }
2743
2744
2745 #
2746 # print the given directory filesystem type
2747 #
2748 # $1 directory name
2749 #
2750 function get_fstype
2751 {
2752 typeset dir=$1
2753
2754 if [[ -z $dir ]]; then
2755 log_fail "Usage: get_fstype <directory>"
2756 fi
2757
2758 #
2759 # $ df -n /
2760 # / : ufs
2761 #
2762 df -n $dir | awk '{print $3}'
2763 }
2764
2765 #
2766 # Given a disk, label it to VTOC regardless what label was on the disk
2767 # $1 disk
2768 #
2769 function labelvtoc
2770 {
2771 typeset disk=$1
2772 if [[ -z $disk ]]; then
2773 log_fail "The disk name is unspecified."
2774 fi
2775 typeset label_file=/var/tmp/labelvtoc.$$
2776 typeset arch=$(uname -p)
2777
2778 if is_linux; then
2779 log_note "Currently unsupported by the test framework"
2780 return 1
2781 fi
2782
2783 if [[ $arch == "i386" ]]; then
2784 echo "label" > $label_file
2785 echo "0" >> $label_file
2786 echo "" >> $label_file
2787 echo "q" >> $label_file
2788 echo "q" >> $label_file
2789
2790 fdisk -B $disk >/dev/null 2>&1
2791 # wait a while for fdisk finishes
2792 sleep 60
2793 elif [[ $arch == "sparc" ]]; then
2794 echo "label" > $label_file
2795 echo "0" >> $label_file
2796 echo "" >> $label_file
2797 echo "" >> $label_file
2798 echo "" >> $label_file
2799 echo "q" >> $label_file
2800 else
2801 log_fail "unknown arch type"
2802 fi
2803
2804 format -e -s -d $disk -f $label_file
2805 typeset -i ret_val=$?
2806 rm -f $label_file
2807 #
2808 # wait the format to finish
2809 #
2810 sleep 60
2811 if ((ret_val != 0)); then
2812 log_fail "unable to label $disk as VTOC."
2813 fi
2814
2815 return 0
2816 }
2817
2818 #
2819 # check if the system was installed as zfsroot or not
2820 # return: 0 ture, otherwise false
2821 #
2822 function is_zfsroot
2823 {
2824 df -n / | grep zfs > /dev/null 2>&1
2825 return $?
2826 }
2827
2828 #
2829 # get the root filesystem name if it's zfsroot system.
2830 #
2831 # return: root filesystem name
2832 function get_rootfs
2833 {
2834 typeset rootfs=""
2835 rootfs=$(awk '{if ($2 == "/" && $3 == "zfs") print $1}' \
2836 /etc/mnttab)
2837 if [[ -z "$rootfs" ]]; then
2838 log_fail "Can not get rootfs"
2839 fi
2840 zfs list $rootfs > /dev/null 2>&1
2841 if (($? == 0)); then
2842 echo $rootfs
2843 else
2844 log_fail "This is not a zfsroot system."
2845 fi
2846 }
2847
2848 #
2849 # get the rootfs's pool name
2850 # return:
2851 # rootpool name
2852 #
2853 function get_rootpool
2854 {
2855 typeset rootfs=""
2856 typeset rootpool=""
2857 rootfs=$(awk '{if ($2 == "/" && $3 =="zfs") print $1}' \
2858 /etc/mnttab)
2859 if [[ -z "$rootfs" ]]; then
2860 log_fail "Can not get rootpool"
2861 fi
2862 zfs list $rootfs > /dev/null 2>&1
2863 if (($? == 0)); then
2864 rootpool=`echo $rootfs | awk -F\/ '{print $1}'`
2865 echo $rootpool
2866 else
2867 log_fail "This is not a zfsroot system."
2868 fi
2869 }
2870
2871 #
2872 # Check if the given device is physical device
2873 #
2874 function is_physical_device #device
2875 {
2876 typeset device=${1#$DEV_DSKDIR}
2877 device=${device#$DEV_RDSKDIR}
2878
2879 if is_linux; then
2880 [[ -b "$DEV_DSKDIR/$device" ]] && \
2881 [[ -f /sys/module/loop/parameters/max_part ]]
2882 return $?
2883 else
2884 echo $device | egrep "^c[0-F]+([td][0-F]+)+$" > /dev/null 2>&1
2885 return $?
2886 fi
2887 }
2888
2889 #
2890 # Check if the given device is a real device (ie SCSI device)
2891 #
2892 function is_real_device #disk
2893 {
2894 typeset disk=$1
2895 [[ -z $disk ]] && log_fail "No argument for disk given."
2896
2897 if is_linux; then
2898 lsblk $DEV_RDSKDIR/$disk -o TYPE 2>/dev/null | \
2899 egrep disk >/dev/null
2900 return $?
2901 fi
2902 }
2903
2904 #
2905 # Check if the given device is a loop device
2906 #
2907 function is_loop_device #disk
2908 {
2909 typeset disk=$1
2910 [[ -z $disk ]] && log_fail "No argument for disk given."
2911
2912 if is_linux; then
2913 lsblk $DEV_RDSKDIR/$disk -o TYPE 2>/dev/null | \
2914 egrep loop >/dev/null
2915 return $?
2916 fi
2917 }
2918
2919 #
2920 # Check if the given device is a multipath device and if there is a sybolic
2921 # link to a device mapper and to a disk
2922 # Currently no support for dm devices alone without multipath
2923 #
2924 function is_mpath_device #disk
2925 {
2926 typeset disk=$1
2927 [[ -z $disk ]] && log_fail "No argument for disk given."
2928
2929 if is_linux; then
2930 lsblk $DEV_MPATHDIR/$disk -o TYPE 2>/dev/null | \
2931 egrep mpath >/dev/null
2932 if (($? == 0)); then
2933 readlink $DEV_MPATHDIR/$disk > /dev/null 2>&1
2934 return $?
2935 else
2936 return $?
2937 fi
2938 fi
2939 }
2940
2941 # Set the slice prefix for disk partitioning depending
2942 # on whether the device is a real, multipath, or loop device.
2943 # Currently all disks have to be of the same type, so only
2944 # checks first disk to determine slice prefix.
2945 #
2946 function set_slice_prefix
2947 {
2948 typeset disk
2949 typeset -i i=0
2950
2951 if is_linux; then
2952 while (( i < $DISK_ARRAY_NUM )); do
2953 disk="$(echo $DISKS | nawk '{print $(i + 1)}')"
2954 if ( is_mpath_device $disk ) && [[ -z $(echo $disk | awk 'substr($1,18,1)\
2955 ~ /^[[:digit:]]+$/') ]] || ( is_real_device $disk ); then
2956 export SLICE_PREFIX=""
2957 return 0
2958 elif ( is_mpath_device $disk || is_loop_device \
2959 $disk ); then
2960 export SLICE_PREFIX="p"
2961 return 0
2962 else
2963 log_fail "$disk not supported for partitioning."
2964 fi
2965 (( i = i + 1))
2966 done
2967 fi
2968 }
2969
2970 #
2971 # Set the directory path of the listed devices in $DISK_ARRAY_NUM
2972 # Currently all disks have to be of the same type, so only
2973 # checks first disk to determine device directory
2974 # default = /dev (linux)
2975 # real disk = /dev (linux)
2976 # multipath device = /dev/mapper (linux)
2977 #
2978 function set_device_dir
2979 {
2980 typeset disk
2981 typeset -i i=0
2982
2983 if is_linux; then
2984 while (( i < $DISK_ARRAY_NUM )); do
2985 disk="$(echo $DISKS | nawk '{print $(i + 1)}')"
2986 if is_mpath_device $disk; then
2987 export DEV_DSKDIR=$DEV_MPATHDIR
2988 return 0
2989 else
2990 export DEV_DSKDIR=$DEV_RDSKDIR
2991 return 0
2992 fi
2993 (( i = i + 1))
2994 done
2995 else
2996 export DEV_DSKDIR=$DEV_RDSKDIR
2997 fi
2998 }
2999
3000 #
3001 # Get the directory path of given device
3002 #
3003 function get_device_dir #device
3004 {
3005 typeset device=$1
3006
3007 if ! $(is_physical_device $device) ; then
3008 if [[ $device != "/" ]]; then
3009 device=${device%/*}
3010 fi
3011 if [[ -b "$DEV_DSKDIR/$device" ]]; then
3012 device="$DEV_DSKDIR"
3013 fi
3014 echo $device
3015 else
3016 echo "$DEV_DSKDIR"
3017 fi
3018 }
3019
3020 #
3021 # Get persistent name for given disk
3022 #
3023 function get_persistent_disk_name #device
3024 {
3025 typeset device=$1
3026 typeset dev_id
3027
3028 if is_linux; then
3029 if is_real_device $device; then
3030 dev_id="$(udevadm info -q all -n $DEV_DSKDIR/$device \
3031 | egrep disk/by-id | nawk '{print $2; exit}' \
3032 | nawk -F / '{print $3}')"
3033 echo $dev_id
3034 elif is_mpath_device $device; then
3035 dev_id="$(udevadm info -q all -n $DEV_DSKDIR/$device \
3036 | egrep disk/by-id/dm-uuid \
3037 | nawk '{print $2; exit}' \
3038 | nawk -F / '{print $3}')"
3039 echo $dev_id
3040 else
3041 echo $device
3042 fi
3043 else
3044 echo $device
3045 fi
3046 }
3047
3048 #
3049 # Load scsi_debug module with specified parameters
3050 #
3051 function load_scsi_debug # dev_size_mb add_host num_tgts max_luns
3052 {
3053 typeset devsize=$1
3054 typeset hosts=$2
3055 typeset tgts=$3
3056 typeset luns=$4
3057
3058 [[ -z $devsize ]] || [[ -z $hosts ]] || [[ -z $tgts ]] || \
3059 [[ -z $luns ]] && log_fail "Arguments invalid or missing"
3060
3061 if is_linux; then
3062 modprobe -n scsi_debug
3063 if (($? != 0)); then
3064 log_unsupported "Platform does not have scsi_debug"
3065 "module"
3066 fi
3067 lsmod | egrep scsi_debug > /dev/null
3068 if (($? == 0)); then
3069 log_fail "scsi_debug module already installed"
3070 else
3071 log_must modprobe scsi_debug dev_size_mb=$devsize \
3072 add_host=$hosts num_tgts=$tgts max_luns=$luns
3073 block_device_wait
3074 lsscsi | egrep scsi_debug > /dev/null
3075 if (($? == 1)); then
3076 log_fail "scsi_debug module install failed"
3077 fi
3078 fi
3079 fi
3080 }
3081
3082 #
3083 # Get the package name
3084 #
3085 function get_package_name
3086 {
3087 typeset dirpath=${1:-$STC_NAME}
3088
3089 echo "SUNWstc-${dirpath}" | /usr/bin/sed -e "s/\//-/g"
3090 }
3091
3092 #
3093 # Get the word numbers from a string separated by white space
3094 #
3095 function get_word_count
3096 {
3097 echo $1 | wc -w
3098 }
3099
3100 #
3101 # To verify if the require numbers of disks is given
3102 #
3103 function verify_disk_count
3104 {
3105 typeset -i min=${2:-1}
3106
3107 typeset -i count=$(get_word_count "$1")
3108
3109 if ((count < min)); then
3110 log_untested "A minimum of $min disks is required to run." \
3111 " You specified $count disk(s)"
3112 fi
3113 }
3114
3115 function ds_is_volume
3116 {
3117 typeset type=$(get_prop type $1)
3118 [[ $type = "volume" ]] && return 0
3119 return 1
3120 }
3121
3122 function ds_is_filesystem
3123 {
3124 typeset type=$(get_prop type $1)
3125 [[ $type = "filesystem" ]] && return 0
3126 return 1
3127 }
3128
3129 function ds_is_snapshot
3130 {
3131 typeset type=$(get_prop type $1)
3132 [[ $type = "snapshot" ]] && return 0
3133 return 1
3134 }
3135
3136 #
3137 # Check if Trusted Extensions are installed and enabled
3138 #
3139 function is_te_enabled
3140 {
3141 svcs -H -o state labeld 2>/dev/null | grep "enabled"
3142 if (($? != 0)); then
3143 return 1
3144 else
3145 return 0
3146 fi
3147 }
3148
3149 # Utility function to determine if a system has multiple cpus.
3150 function is_mp
3151 {
3152 if is_linux; then
3153 (($(nproc) > 1))
3154 else
3155 (($(psrinfo | wc -l) > 1))
3156 fi
3157
3158 return $?
3159 }
3160
3161 function get_cpu_freq
3162 {
3163 if is_linux; then
3164 lscpu | awk '/CPU MHz/ { print $3 }'
3165 else
3166 psrinfo -v 0 | awk '/processor operates at/ {print $6}'
3167 fi
3168 }
3169
3170 # Run the given command as the user provided.
3171 function user_run
3172 {
3173 typeset user=$1
3174 shift
3175
3176 log_note "user:$user $@"
3177 eval su - \$user -c \"$@\" > /tmp/out 2>/tmp/err
3178 return $?
3179 }
3180
3181 #
3182 # Check if the pool contains the specified vdevs
3183 #
3184 # $1 pool
3185 # $2..n <vdev> ...
3186 #
3187 # Return 0 if the vdevs are contained in the pool, 1 if any of the specified
3188 # vdevs is not in the pool, and 2 if pool name is missing.
3189 #
3190 function vdevs_in_pool
3191 {
3192 typeset pool=$1
3193 typeset vdev
3194
3195 if [[ -z $pool ]]; then
3196 log_note "Missing pool name."
3197 return 2
3198 fi
3199
3200 shift
3201
3202 typeset tmpfile=$(mktemp)
3203 zpool list -Hv "$pool" >$tmpfile
3204 for vdev in $@; do
3205 grep -w ${vdev##*/} $tmpfile >/dev/null 2>&1
3206 [[ $? -ne 0 ]] && return 1
3207 done
3208
3209 rm -f $tmpfile
3210
3211 return 0;
3212 }
3213
3214 function get_max
3215 {
3216 typeset -l i max=$1
3217 shift
3218
3219 for i in "$@"; do
3220 max=$(echo $((max > i ? max : i)))
3221 done
3222
3223 echo $max
3224 }
3225
3226 function get_min
3227 {
3228 typeset -l i min=$1
3229 shift
3230
3231 for i in "$@"; do
3232 min=$(echo $((min < i ? min : i)))
3233 done
3234
3235 echo $min
3236 }
3237
3238 #
3239 # Synchronize all the data in pool
3240 #
3241 # $1 pool name
3242 #
3243 function sync_pool #pool
3244 {
3245 typeset pool=${1:-$TESTPOOL}
3246
3247 log_must $SYNC
3248 log_must sleep 2
3249 # Flush all the pool data.
3250 typeset -i ret
3251 zpool scrub $pool >/dev/null 2>&1
3252 ret=$?
3253 (( $ret != 0 )) && \
3254 log_fail "zpool scrub $pool failed."
3255
3256 while ! is_pool_scrubbed $pool; do
3257 if is_pool_resilvered $pool ; then
3258 log_fail "$pool should not be resilver completed."
3259 fi
3260 log_must sleep 2
3261 done
3262 }
3263
3264 #
3265 # Wait for zpool 'freeing' property drops to zero.
3266 #
3267 # $1 pool name
3268 #
3269 function wait_freeing #pool
3270 {
3271 typeset pool=${1:-$TESTPOOL}
3272 while true; do
3273 [[ "0" == "$(zpool list -Ho freeing $pool)" ]] && break
3274 log_must sleep 1
3275 done
3276 }
3277
3278 #
3279 # Check if ZED is currently running, if not start ZED.
3280 #
3281 function zed_start
3282 {
3283 if is_linux; then
3284 # ZEDLET_DIR=/var/tmp/zed
3285 if [[ ! -d $ZEDLET_DIR ]]; then
3286 log_must mkdir $ZEDLET_DIR
3287 fi
3288
3289 # Verify the ZED is not already running.
3290 pgrep -x zed > /dev/null
3291 if (($? == 0)); then
3292 log_fail "ZED already running"
3293 fi
3294
3295 # ZEDLETDIR=</etc/zfs/zed.d | ${SRCDIR}/cmd/zed/zed.d>
3296 log_must cp ${ZEDLETDIR}/all-syslog.sh $ZEDLET_DIR
3297
3298 log_note "Starting ZED"
3299 # run ZED in the background and redirect foreground logging
3300 # output to zedlog
3301 log_must eval "zed -vF -d $ZEDLET_DIR -p $ZEDLET_DIR/zed.pid" \
3302 "-s $ZEDLET_DIR/state 2>${ZEDLET_DIR}/zedlog &"
3303 fi
3304 }
3305
3306 #
3307 # Kill ZED process
3308 #
3309 function zed_stop
3310 {
3311 if is_linux; then
3312 if [[ -f ${ZEDLET_DIR}/zed.pid ]]; then
3313 zedpid=$(cat ${ZEDLET_DIR}/zed.pid)
3314 log_must kill $zedpid
3315 fi
3316 log_must rm -f ${ZEDLET_DIR}/all-syslog.sh
3317 log_must rm -f ${ZEDLET_DIR}/zed.pid
3318 log_must rm -f ${ZEDLET_DIR}/zedlog
3319 log_must rm -f ${ZEDLET_DIR}/state
3320 log_must rmdir $ZEDLET_DIR
3321 fi
3322 }