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