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