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